-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.cpp
77 lines (62 loc) · 1.34 KB
/
DFS.cpp
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
// AdjMGraph.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int (*edge)[VERTEXNUM] = (int (*)[VERTEXNUM])malloc(sizeof(int) * (VERTEXNUM * VERTEXNUM));
int *vertexStatusArr = (int*)malloc(sizeof(int) * VERTEXNUM);
for(int i = 0; i != VERTEXNUM; i++)
{
for(int j = 0; j != VERTEXNUM; j++)
edge[i][j] = 0;
}
for(int i = 0; i != VERTEXNUM; i++)
vertexStatusArr[i] = 0;
createGraph(edge,0,3);
createGraph(edge,0,4);
createGraph(edge,3,1);
createGraph(edge,3,2);
createGraph(edge,4,1);
printf("after init\n");
displayGraph(edge);
DFT(edge,vertexStatusArr);
free(edge);
printf("\n");
system("pause");
return 0;
}
void createGraph(int (*edge)[VERTEXNUM], int start, int end)
{
edge[start][end] = 1;
}
void displayGraph(int (*edge)[VERTEXNUM])
{
for(int i = 0; i < VERTEXNUM; i++)
{
for(int j = 0; j < VERTEXNUM; j++)
{
printf("%d ",edge[i][j]);
}
printf("\n");
}
}
void DFT(int (*edge)[VERTEXNUM], int *vertexStatusArr)
{
printf("开始DFS\n");
for(int i = 0; i < VERTEXNUM; i++)
{
DFTcore(edge,i,vertexStatusArr);
}
}
void DFTcore(int (*edge)[VERTEXNUM],int i, int *vertexStatusArr)
{
if(vertexStatusArr[i] == 1)
return;
printf("%d ",i);
vertexStatusArr[i] = 1;
for(int j = 0; j != VERTEXNUM; j++)
{
if(edge[i][j] == 1)
DFTcore(edge,j,vertexStatusArr);
}
}