-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFT.c
81 lines (72 loc) · 1.72 KB
/
DFT.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <malloc.h>
#define VERTEXNUM 5
void createGraph(int(*edge)[VERTEXNUM], int start, int end);
void displayGraph(int(*edge)[VERTEXNUM]);
void DFT(int(*edge)[VERTEXNUM], int* vertexStatusArr);
void DFTcore(int(*edge)[VERTEXNUM], int i, int* vertexStatusArr);
int main(void){
//动态创建存放边的二维数组
int(*edge)[VERTEXNUM] = (int(*)[VERTEXNUM])malloc(sizeof(int)*VERTEXNUM*VERTEXNUM);
int i, j;
for (i = 0; i<VERTEXNUM; i++){
for (j = 0; j<VERTEXNUM; j++){
edge[i][j] = 0;
}
}
//存放顶点的遍历状态,0:未遍历,1:已遍历
int* vertexStatusArr = (int*)malloc(sizeof(int)*VERTEXNUM);
for (i = 0; i<VERTEXNUM; i++){
vertexStatusArr[i] = 0;
}
printf("after init:\n");
displayGraph(edge);
//创建图
createGraph(edge, 0, 3);
createGraph(edge, 0, 4);
createGraph(edge, 3, 1);
createGraph(edge, 3, 2);
createGraph(edge, 4, 1);
printf("after create:\n");
displayGraph(edge);
//深度优先遍历
DFT(edge, vertexStatusArr);
free(edge);
return 0;
}
//创建图
void createGraph(int(*edge)[VERTEXNUM], int start, int end){
edge[start][end] = 1;
}
//打印存储的图
void displayGraph(int(*edge)[VERTEXNUM]){
int i, j;
for (i = 0; i<VERTEXNUM; i++){
for (j = 0; j<VERTEXNUM; j++){
printf("%d ", edge[i][j]);
}
printf("\n");
}
}
//深度优先遍历
void DFT(int(*edge)[VERTEXNUM], int* vertexStatusArr){
printf("start BFT graph:\n");
int i;
for (i = 0; i<VERTEXNUM; i++){
DFTcore(edge, i, vertexStatusArr);
}
printf("\n");
}
void DFTcore(int(*edge)[VERTEXNUM], int i, int* vertexStatusArr){
if (vertexStatusArr[i] == 1){
return;
}
printf("%d ", i);
vertexStatusArr[i] = 1;
int j;
for (j = 0; j<VERTEXNUM; j++){
if (edge[i][j] == 1){
DFTcore(edge, j, vertexStatusArr);
}
}
}