Dud*_*ude 2 algorithm depth-first-search data-structures
谁能解释使用邻接矩阵进行深度优先搜索的算法?我知道使用递归的深度优先搜索的算法,我尝试使用Adjacency矩阵实现它,但它不是很成功.
到目前为止我所拥有的是什么
dfs(G,i){
mark i as visited;
for(traverse through the edges of i vertex){
if(vertex of edge is unseen){
DFS(G,newVerted)
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
Run Code Online (Sandbox Code Playgroud)
n顶点no是哪里,G是图形,G[i][j]表示顶点i连接到顶点j