Simple topological sort pseudocode



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 */
}
 
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment