void topsort( graph G )
{
QUEUE Q;
unsigned int counter;
vertex v, w;
/*1*/ Q = create_queue( NUM_VERTEX ); make_null( Q ); counter = 0;
/*2*/ for each vertex v
/*3*/ if( indegree[v] = 0 )
/*4*/ enqueue( v, Q );
/*5*/ while( !is_empty( Q ) )
{
/*6*/ v = dequeue( Q );
/*7*/ top_num[v] = ++counter; /* assign next number */
/*8*/ for each w adjacent to v
/*9*/ if( --indegree[w] = 0 )
/*10*/ enqueue( w, Q );
}
/*11*/ if( counter != NUM_VERTEX )
/*12*/ error("Graph has a cycle");
/*13*/ dispose_queue( Q ); /* free the memory */
}
No comments:
Post a Comment