Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Topological_Sorting()
- {
- var L = new HashSet<Nodo>();
- Action<Nodo> Visit = null;
- Visit = (n) =>
- {
- // if n has a temporary mark then stop (not a DAG)
- if (n.color == Nodo.Color.Gray)
- throw new Exception("not a DAG!");
- // if n is not marked(i.e.has not been visited yet) then
- if (n.color == Nodo.Color.White)
- {
- // mark n temporarily
- n.color = Nodo.Color.Gray;
- // for each node m with an edge from n to m do visit(m)
- foreach (var m in Adj[n])
- Visit(m);
- // mark n permanently
- n.color = Nodo.Color.Black;
- // add n to head of L
- L.Add(n);
- }
- };
- foreach (var v in V)
- if (v.color == Nodo.Color.White)
- Visit(v);
- Console.Write("Topological sort: ");
- foreach (var x in L)
- Console.Write(x.N + " ");
- Console.WriteLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement