#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct{
int vexnum,arcnum;
char vexs[MAX_VERTEX_NUM];
AdjMatrix arc;
}MGraph;
typedef struct{
int *base;
int top;
int stacksize;
}SqStack;
typedef struct{
int *Qbase;
int front,rear;
}SqQueue;
int visited[MAX_VERTEX_NUM];
int LocateAdj(MGraph G, char v){
int i;
for(i = 0; i < G.vexnum; i ++){
if(G.vexs[i] == v)
return i;
}
return -1;
}
void CreateAdjMatrix(MGraph &G){
int i,j,k;
char v1,v2;
printf("please input the vexnum and arcnum:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
getchar();
printf("please input the vexs:\n");
for(i = 0; i < G.vexnum; i ++)
scanf("%c",&G.vexs[i]);
for(i = 0; i < G.vexnum; i ++)
for(j = 0; j < G.vexnum; j ++)
G.arc[i][j] = 0;
printf("please inputb the arc:\n");
for(k = 0; k < G.arcnum; k ++){
getchar();
scanf("%c%c",&v1,&v2);
i = LocateAdj(G,v1); j = LocateAdj(G,v2);
G.arc[i][j] = G.arc[j][i] = 1;
}
}
int FirstAdj(MGraph G, int i){
int j;
for(j = 0; j < G.vexnum; j ++){
if(G.arc[i][j])
return j;
}
return -1;
}
int NextAdj(MGraph G, int i, int j){
for(j = j + 1; j < G.vexnum; j ++){
if(G.arc[i][j])
return j;
}
return -1;
}
void DFS(MGraph G, int i){
int j;
visited[i] = 1;
printf("%3c",G.vexs[i]);
for(j = FirstAdj(G,i); j >= 0; j = NextAdj(G,i,j)){
if(!visited[j])
DFS(G,j);
}
}
void DFSTraverse1(MGraph G){
int i;
for(i = 0; i < G.vexnum; i ++)
visited[i] = 0;
for(i = 0; i < G.vexnum; i ++){
if(!visited[i])
DFS(G,i);
}
}
void InitStack(SqStack &S){
S.base = (int *)malloc(sizeof(int) * MAX_VERTEX_NUM);
if(!S.base)
return ;
S.top = 0;
S.stacksize = MAX_VERTEX_NUM;
}
void Push(SqStack &S, char ch){
if(S.top >= S.stacksize){
S.base = (int *)realloc(S.base,(S.stacksize + MAX_VERTEX_NUM) * sizeof(int));
if(!S.base)
return ;
S.stacksize += MAX_VERTEX_NUM;
}
S.base[S.top ++] = ch;
}
int StackEmpty(SqStack S){
if(!S.top)
return 1;
return 0;
}
void Pop(SqStack &S, int &i){
if(!S.top)
return ;
i = S.base[-- S.top];
}
void GetTop(SqStack S, int &i){
i = S.base[S.top - 1];
}
void DFS2(MGraph G, int i){
int j,k;
SqStack S;
InitStack(S);
Push(S,i);
printf("%3c",G.vexs[i]);
visited[0] = 1;
while(!StackEmpty(S)){
GetTop(S,i);
for(j = 0; j < G.vexnum; j ++){
if(G.arc[i][j] && !visited[j]){
Push(S,j);
printf("%3c",G.vexs[j]);
visited[j] = 1;
i = j;
j = 0;
}
}
if(!StackEmpty(S)){
Pop(S,k);
}
}
}
void DFSTraverse2(MGraph G){
int i;
for(i = 0; i < G.vexnum; i ++)
visited[i] = 0;
for(i = 0; i < G.vexnum; i ++){
if(!visited[i])
DFS2(G,i);
}
}
void InitQueue(SqQueue &Q){
Q.Qbase = (int *)malloc(sizeof(int) * MAX_VERTEX_NUM);
if(!Q.Qbase)
return ;
Q.rear = Q.front = 0;
}
void EnQueue(SqQueue &Q, int i){
Q.Qbase[Q.rear ++] = i;
}
void DeQueue(SqQueue &Q, int &i){
i = Q.Qbase[Q.front ++];
}
int QueueEmpty(SqQueue Q){
if(Q.front == Q.rear)
return 1;
return 0;
}
void BFS(MGraph G, int i){
int k,j;
SqQueue Q;
InitQueue(Q);
printf("%3c",G.vexs[i]);
visited[i] = 1;
EnQueue(Q,i);
while(!QueueEmpty(Q)){
DeQueue(Q,k);
for(j = FirstAdj(G,k); j >= 0; j = NextAdj(G,k,j)){
if(!visited[j]){
visited[j] = 1;
printf("%3c",G.vexs[j]);
EnQueue(Q,j);
}
}
}
}
void BFSTraverse(MGraph G){
int i;
for(i = 0; i < G.vexnum; i ++)
visited[i] = 0;
for(i = 0; i < G.vexnum; i ++){
if(!visited[i])
BFS(G,i);
}
}
int main(){
MGraph G;
CreateAdjMatrix(G);
DFSTraverse1(G);
putchar('\n');
DFSTraverse2(G);
putchar('\n');
BFSTraverse(G);
return 0;
}
邻接矩阵的深度优先遍历(递归以及非递归),广度优先遍历
最新推荐文章于 2024-09-07 18:57:34 发布