图的深度和广度优先遍历 (c++)
实验题目(共5题, 第1题)
标题: | 图的深度和广度优先遍历 | |
时 限: | 2000 ms | |
内存限制: | 5000 K | |
总时限: | 6000 ms | |
描述: | 以邻接矩阵给出一张以整数编号为顶点的图,其中0表示不相连,1表示相连。按深度和广度优先进行遍历,输出全部结果。要求,遍历时优先较小的顶点。如,若顶点0与顶点2,顶点3,顶点4相连,则优先遍历顶点2. | |
输入: |
顶点个数
邻接矩阵 |
|
输出: |
DFS
深度遍历输出
WFS
广度遍历输出 |
|
输入样例: |
0 1 1
1 0 1
1 1 0 |
|
输出样例: |
DFS
0 1 2
1 0 2
2 0 1
WFS
0 1 2
1 0 2
2 0 1 |
|
提示: | 顶点整数从0 开始 | |
来源: | 教材 |
View Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #define MAXVNUM 20
4 #define MAXQSIZE 9 // 最大队列长度(对于循环队列,最大队列长度要减1)
5
6 typedef int QElemType;
7 int visited[MAXVNUM];
8 typedef struct MGraph
9 {
10 int vex[MAXVNUM];
11 int arcs[MAXVNUM][MAXVNUM];
12 int vexnum, arcnum;
13 } MGraph;
14
15 struct SqQueue
16 {
17 QElemType *base; // 初始化的动态分配存储空间
18 int front; // 头指针,若队列不空,指向队列头元素
19 int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
20 };
21
22 int QueueEmpty(SqQueue Q);
23 void EnQueue(SqQueue &Q,QElemType e);
24 void DeQueue(SqQueue &Q,QElemType &e);
25 void CreatMGraph(MGraph &G);
26 void DFSTraverse(MGraph G);
27 void DFS(MGraph G,int v);
28 void BFSTraverse(MGraph G);
29 int FirstAdjVex(MGraph G,int v);
30 int NextAdjVex(MGraph G,int v,int w);
31
32 int main()
33 {
34 MGraph G;
35 CreatMGraph(G);
36 printf("DFS\n");
37 DFSTraverse(G);
38 printf("WFS\n");
39 BFSTraverse(G);
40 return 0;
41 }
42 void InitQueue(SqQueue &Q)
43 {
44 // 构造一个空队列Q
45 Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
46 if(!Q.base) // 存储分配失败
47 exit(0);
48 Q.front=Q.rear=0;
49 }
50
51 int QueueEmpty(SqQueue Q)
52 {
53 // 若队列Q为空队列,则返回TRUE,否则返回FALSE
54 if(Q.front==Q.rear) // 队列空的标志
55 return 1;
56 else
57 return 0;
58 }
59
60 void EnQueue(SqQueue &Q,QElemType e)
61 {
62 // 插入元素e为Q的新的队尾元素
63 if((Q.rear+1)%MAXQSIZE==Q.front) // 队列满
64 exit(0);
65 Q.base[Q.rear]=e;
66 Q.rear=(Q.rear+1)%MAXQSIZE;
67 }
68
69 void DeQueue(SqQueue &Q,QElemType &e)
70 {
71 // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
72 if(Q.front==Q.rear) // 队列空
73 exit(0);
74 e=Q.base[Q.front];
75 Q.front=(Q.front+1)%MAXQSIZE;
76 }
77
78 void CreatMGraph(MGraph &G)
79 {
80 int i;
81 int j;
82 scanf("%d", &G.vexnum);
83 for ( i=0; i<G.vexnum; i++)
84 G.vex[i] = i;
85 for ( i=0; i<G.vexnum; i++)
86 for ( j=0; j<G.vexnum; j++)
87 {
88 scanf("%d", &G.arcs[i][j]);
89 }
90 }
91
92 void DFSTraverse(MGraph G)
93 {
94 int i;
95 int k;
96 for(k=0; k<G.vexnum; k++)
97 {
98 for(i=0; i<G.vexnum; i++)
99 visited[i]=0;
100 for(i=k; i<G.vexnum; i++)
101 {
102 if(!visited[i]) DFS(G,i);
103 }
104 printf("\n");
105 }
106 }
107
108 void DFS(MGraph G,int i)
109 {
110 int w;
111 visited[i]=1;
112 printf("%d ",G.vex[i]);
113 for(w=FirstAdjVex(G,i); w>=0; w=NextAdjVex(G,i,w))
114 {
115 if(!visited[w]) DFS(G,w);
116 }
117 }
118
119 void BFSTraverse(MGraph G)
120 {
121 int i;
122 int w;
123 int k;
124 SqQueue Q;
125 InitQueue(Q);
126 for(k=0; k<G.vexnum; k++)
127 {
128 for(i=0; i<G.vexnum; i++)
129 visited[i]=0;
130 for(i=k; i<G.vexnum; i++)
131 {
132 if(!visited[i])
133 {
134 visited[i]=1;
135 printf("%d ",G.vex[i]);
136 EnQueue(Q,i);
137 while(!QueueEmpty(Q))
138 {
139 DeQueue(Q,i);
140 for(w=FirstAdjVex(G,i); w>=0; w=NextAdjVex(G,i,w))
141 {
142 if(!visited[w])
143 {
144 visited[w]=1;
145 printf("%d ",G.vex[w]);
146 EnQueue(Q,w);
147 }
148 }
149 }
150 }
151 }
152 printf("\n");
153 }
154 }
155
156 // 返回v的第一个邻接顶点的序号。若顶点在G中没有邻接顶点,则返回-1
157 int FirstAdjVex(MGraph G,int v)
158 {
159 int i;
160 for(i=0; i<G.vexnum; i++)
161 if(G.arcs[v][i]!=0)
162 return i;
163 return -1;
164 }
165
166 // 返回下一个邻接顶点的序号
167 int NextAdjVex(MGraph G,int v,int w)
168 {
169 int i;
170 for(i=w+1; i<G.vexnum; i++)
171 if(G.arcs[v][i]!=0)
172 return i;
173 return -1;
174 }