c - 如何确定特定节点是否找到所有其他节点?

标签 c search graph-algorithm depth-first-search

所以我试图确定是否可以找到一个可用于查找某个图中的所有其他节点的特定节点。我在 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);
        }
    }
}

最佳答案

您的代码有问题:

这里你在if子句中定义了另一个x,它与你在main开头定义的完全不同。

if (visited[j]==0){ // has not been visited
    int x = 0; // boolean int variable set to 0
}

而且,您还忘记在每次 DFS 之前重置 visited

关于c - 如何确定特定节点是否找到所有其他节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33482212/

相关文章:

客户端 - 使用 servbyport() 函数搜索所有服务

algorithm - 图中最大权重的循环

algorithm - 同时考虑节点和边的图寻路算法

sql - PostgreSQL 11.2 按单个表中的公共(public)属性对对象进行分组

c - C中的输出重定向

c - 避免在 Linux (GCC) 上使用 getch() 按 Enter "No-echo"

c - 在一个程序中混合使用 SCHED_FIFO 和 SCHED_RR?

database - 不从搜索查询中检索任何数据

algorithm - 如何计算二分搜索复杂度

java - 搜索列表中与 id 有关的最后一个值