深度优先搜索(Depth-First-Search,DFS)
程序员文章站
2022-05-22 21:00:15
...
//深度优先搜索(Depth-First-Search,DFS)
//类似于先序遍历,采用递归算法实现
bool visited[MAX_VERTEX_NUM]
void DFSTraverse(Graph G)
{
for(i = 0;i<G.vexnum;++i)
{
visited[i] = false;
}
for(i = 0;i < G.vexnum ;++i)
{
if(visited[i] == false)
DFS(G,i);
}
}
void DFS(Graph G,int i)
{
visit(i);
visited[i] = true;
for(w = FirstNieghbor(G,i); i>=0; w =NextNeighbor(G,i,w))
{
if(!visited[w])
DFS(G,w);
}
}