好吧,我遍历了 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/