gpt4 book ai didi

c - DFS 函数 - 这段代码有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 04:58:03 25 4
gpt4 key购买 nike

你能告诉我这段代码有什么问题吗?它不适用于

1 2
1 3
1 4
2 5
2 6
2 7
2 8
3 8
3 9
4 10

1 -> 4 -> 10 并停止

DFS函数

void Is_Connected(graphType* g, int v){ //function to define the graph is connected or not  
int i=0;
g_node* w;
top = NULL;
g -> visited[v] = TRUE;
push(v);
printf("%d", v);
while (top != NULL) {
w = g -> adjList[v];
while (w) {
if (!g -> visited[w -> vertex]) {
push(w -> vertex);
g -> visited[w -> vertex] = TRUE;
printf("->%d", w->vertex);
v = w -> vertex;
w = g -> adjList[v];
}
else {
w = w -> link;
}
}
i++;
v = pop();
}

if (i == (g -> x)-1){ //number of vertices = number of vertetices pass through
puts("\nIs_Connected() result : yes");
}

else{
puts("\nIs_Connected() result : no");
}
}

最佳答案

top = NULL;    ==> initialised to NULL
g -> visited[v] = TRUE;
push(v);
printf("%d", v);
while (top != NULL) ===> top is NULL always ( previous assignment is NULL).
Loop is not entered at all ! ?

关于c - DFS 函数 - 这段代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2910462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com