开发者

DFS function - what is wrong with this code?

开发者 https://www.devze.com 2023-01-01 14:50 出处:网络
Can you tell me what is the wrong with this code? It is not working with 1 2 1 3 1 4 2 5 2 6 2 7 2 8 3 8 3 9 4 10

Can you tell me what is the wrong with this code? It is not working with

1 2

1 3

1 4

2 5

2 6

2 7

2 8

3 8

3 9

4 10

1 -> 4 -> 10 and stop

DFS function

开发者_如何学Go
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 ! ?
0

精彩评论

暂无评论...
验证码 换一张
取 消