Monday, May 17, 2010

Two different ways to to topological sorting over a graph

The way to do topological sort is

1.Using DFS.

a) Do the DFS on the given graph and store the DFS tree.
b) Now on the DFS tree do postorder traversal and output the children.
c) Reverse the above output to get the Topological sort of the graph.

2. Removing the indegree zero nodes.

a) Start by printing the node which has indegree zero.
b) Now remove the node which is visited and also remove the edges from or to that node from other edges.
c) repeat the process.

Indegree is the number of edges that comeinto to the node.

No comments:

Post a Comment