-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path007.GraphDFS.cpp
More file actions
39 lines (37 loc) · 816 Bytes
/
007.GraphDFS.cpp
File metadata and controls
39 lines (37 loc) · 816 Bytes
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
#include "graph.h"
//深度优先搜索,辅助递归函数
void GraphDFS_helper(Graph *graph,int u,int &time)
{
graph->G[u].d = ++time;
graph->G[u].color = GRAY;
LNode *lnptr = graph->G[u].next;
while(lnptr)
{
int v = lnptr->n;
if(graph->G[v].color == WHITE)
{
graph->G[v].pi = u;
GraphDFS_helper(graph,v,time);
}
lnptr = lnptr->next;
}
graph->G[u].color = BLACK;
graph->G[u].f = ++time;
}
//深度优先搜索
void GraphDFS(Graph *graph)
{
int i,n=graph->nodeNum,time=0;
//init args
for(i=0;i<n;++i)
{
graph->G[i].color = WHITE;
graph->G[i].pi = NIL;
}
//DFS
for(i=0;i<n;++i)
{
if(graph->G[i].color == WHITE)
GraphDFS_helper(graph,i,time);
}
}