Topological Sort

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
Artikali
Learning poster
Posts: 68
Joined: Wed Sep 21, 2005 5:27 pm

Topological Sort

Post by Artikali »

I used standart Topological sort algorithm, but if node==5000 it says stack overflow, any one know how to do it without recursion.
Thanks
ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon »

ya, it can be done using indegree. indegree indicates number of edges coming into u from any other vertices.

1. output and remove any node u, elements of V, such that indegree = 0.
2. decrease all indegree[v] by one, if (u,v) is an edge.
3. jump to 1 untill all node has been removed.

Note that, you will always find at least one node of indegree 0 if there exists no cycle in the graph.
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
Post Reply

Return to “Algorithms”