所以我试图确定是否可以找到一个特定的节点,可以用来查找某个图中的所有其他节点。我在C中做了一些伪代码。在使用深度优先搜索时,我无法确定如何检查访问节点。如何确定特定节点是否可以找到所有其他节点?
#include<stdio.h>
int n;
int Graph[n][n];
int visited[n];
int main (int argc, char *argv[]){
int i;
int j;
int x = 1;
for (i=0; i < n; i++){ // run DFS on all variables to determine if any one node implies all others
DFS(i) // starting node/variable
for (j=0; j<n; j++){
if (visited[j]==0){ // has not been visited
x = 0; // boolean int variable set to 0
}
}
if (x!=0){ // if all variables are visited, x will be equal to 1
printf("This variable implies all others");
return i;
}
}
printf("No variables imply all others");
return -1;
}
int DFS(int i)
{
int j;
visited[i]=1;
for(j=0;j<n;j++){
if(!visited[j]&&G[i][j]==1){
DFS(j);
}
}
}
哦哇我刚刚意识到那个错误,我怎么会去重置访问?我是否必须使用for循环并将所有值都设置为0或者有更简单的方法可以做到这一点? – fatalwanderer
@fatalwanderer使用for循环很简单 –