C中强连接的组件

时间:2014-04-21 17:20:51

标签: c tarjans-algorithm

我正在使用Tarjan算法实现强连接组件。我将输入作为节点和边的链表。但是,gcc编译器每次都会在递归函数中给出分段错误(在while循环中我检查顶点的相邻节点)。

知道这段代码中有什么问题吗?

void strongconnect(int Vertex)
{
 struct sc_node * Ver;
 Ver = search_node(Vertex);
 Ver->sc_index = ind; //accessing the index information of node
 Ver->sc_lowlink = ind; // accessing the link information of node
 //Ver->visited = 1;
 ind++;
 int w;
 push(Vertex);

 struct sc_node * to_link, *to_link1;
 int to_lowlink,to_index;
 int flowlink;
 int min;
 int from_index;

 edge_trav = edge_head;    

 while(edge_trav != NULL)   //accessing linked list of edges
 {
   if(edge_trav->from_vertex == Vertex)
   {
     to_link = search_node(edge_trav->to_vertex);
     to_lowlink = to_link->sc_lowlink;
     to_index = to_link->sc_index;
     to_link1 = search_node(Vertex);
     flowlink = to_link1->sc_lowlink;
     from_index =  to_link1->sc_index;

     if(to_index == 0)
     {   Vertex = to_link->sc_data;
          printf("INSIDE RECURSION");
          strongconnect(Vertex);   // recursive loop
          min = minimum(flowlink,to_lowlink);
          to_link1->sc_lowlink = min;
     }
     else 
     {
       min =  minimum(flowlink, from_index);
       to_link1->sc_lowlink = min;
     }  }
  edge_trav = edge_trav->next;
  }
  Ver = search_node(Vertex);
  if(Ver->sc_lowlink == Ver->sc_index)
  {
   do
   {
    w = pop();
    printf("%d\t",w);
   }while(w != Vertex);
  }
}

1 个答案:

答案 0 :(得分:0)

我建议您使用gcc -s编译程序,然后使用valgrind运行程序。这是非常有用的内存泄漏检查程序。有了它,你可以自己弄清楚你是否正在编程访问非法的记忆位置。正如@Dogbert指出的那样,它看起来你正在取消引用NULL指针。