连通图的 DFS

问题描述

我正在实现图形的 DFS。这是我的代码

// sv is the starting node for traversal
// edges is the adjacency matrix 
// n is the number of vertices
// visited is boolean vector to keep track of the visited nodes
void printdfs(vector<vector <int>> edges,int n,int sv,vector<bool> visited){
    
    cout<<sv<<"\t";
    visited[sv] = true;
    for(int i=0;i<n;i++){
        if(i == sv){
            continue;
        }
        if(edges[sv][i] == 1){
            if(visited[i] == true){
                continue; 
            }                       
            printdfs(edges,n,i,visited);
        }       
    }
}

Graph-image

我什至对代码进行了几次空运行,但我无法确定出了什么问题,我没有得到预期的输出,并且它一次又一次地打印已经访问过的节点。有人可以指出错误吗。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)