Algorithm

list<GraphVert*> * TopologicalSort( Graph G )
{
  for( unsigned int v=0; v < G.size(); v++ ) {

    G[v]->color = white;

    // make all edges G[u,v] = white; G[start,v] = white;
  }

  list<GraphVert*> * visited = new list<GraphVert*>();

  for( unsigned int v=0; v < G.size(); v++ ) {

    // G[start,v] = black;

    if( G[v]->color == white )
      Visit( v, G, visited );
  }
      
  return visited;
}

void Visit( int u, Graph G, list<GraphVert*> * visited )
{
  G[u]->color = gray;

  for( list<int>::iterator iter = G[u]->adj->begin();
       iter != G[u]->adj->end();
       iter++ )
  {
    int v = *iter;

    // G[u,v] = black;

    if( G[v]->color == white )
       Visit( v, G, visited );
  }
  
  visited->push_front( G[u] );
  G[u]->color = black;
}


next up previous
Next: Observations Up: TOPOLOGICAL SORT Previous: Topological Sort in Words