c - 如何使用c找到DFS中的组件数量?

标签 c

好吧,我遍历了 DFS,但现在我找不到组件。有什么办法可以解决吗?

for (i=1;i<=n;i++){
    for (j=1;j<=n;j++){
        printf("Enter the number of array position a[%d][%d] = ",i,j);
        scanf("%d",&ar[i][j]);
    }
}
DFS(v);
printf("\n");

for (i=1;i<=n;i++){
    if(reach[i]==1){
        count++;
    }
}
printf("Number of Components: %d",count);
    return 0;
}

最佳答案

这是该任务的算法:

Components = 0; 

For every vertex index i:
   if marks[i] == 0 then
      ++Components
      DFS(i)

DFS(v):
   marks[v] = Components
   for all vertices j adjacent to v:
      if marks[j] == 0:
         DFS(j)

Components存储组件的数量,marks[n]表示顶点n所属的组件编号。

关于c - 如何使用c找到DFS中的组件数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57220210/

相关文章:

更正字符串数组 C 的 free()

c - 报告显示使用 Eclipse CDT 的 gprof 为 "no time accumulated"

c - C 缓冲区中的指令仅作为 sudo 执行

c++ - 将 -rpath 和 $ORIGIN 与基于 libtool 的项目一起使用?

c - 逐字读取文件并输出空格

c++ - 将两个 8 位数组组合成一个 USHORT (16 位),无循环

c++ - 如何编译一个简单的Arduino程序?

c - 为什么在 C 中使用 char* arr[1] ?

c - 在哪些系统上 sleep() 不是 pthread 取消点?

C 堆栈实现