I am trying to implement a recursive depth first traversal on a weighted digraph(, but it seems as if my output is always off. As in, I am getting extra visits to nodes. This is what I am currently working with:
void Dfs( int u, vector<bool> visited, vector < char > label, vector < vector < int > > adj)
{
visited[u] = true;
cout << label[u];
for ( int i = 0; i < (signed)visited.size(); i++)
{
if (visited[i] != true && adj[u][i] != 0)
{
cout << "->";
Dfs( i, visited, label, adj);
}
}
}
如果贴上标签(A = 0, 等......),所访问的是一种病媒,不论是否曾访问过某一指数上的垂直,并且是紧急矩阵。
Saye 我有一张图表和正确的深度第一次搜索是:A->D->B->E,我最后获得的是A->D->B->C->E->C->B->E。 如果它能够帮助,例如,应急矩阵就象:
| A B C D E
--|---------------
A | - - - 6 -
B | - - 8 3 2
C | - 8 - 7 -
D | 6 3 7 - -
E | - 2 - - -