public static System.Collections.Generic.IEnumerable <T> Sort <T, E> (IGraph <T, E> graph, IEnumerable <T> source) where E : IEdge <T> { Dictionary <T, bool> Visited = new Dictionary <T, bool>(); StackQueue <T> Stack = new StackQueue <T>(); foreach (T v in graph.Vertices) { Visited[v] = false; } foreach (T v in source) { Stack.Push(v); } while (Stack.Count != 0) { T u = Stack.Pop(); Visited[u] = true; yield return(u); foreach (T v in graph.ReversePredecessors(u)) { if (!Visited[v] && !Stack.Contains(v)) { Stack.Push(v); } } } }
public System.Collections.Generic.IEnumerator <T> GetEnumerator() { foreach (T v in graph.Vertices) { Visited[v] = false; } foreach (T v in Source) { Stack.Push(v); } while (Stack.Count != 0) { T u = Stack.Pop(); if (Visited[u]) { yield return(u); } else { Visited[u] = true; Stack.Push(u); foreach (T v in graph.ReverseSuccessors(u)) { if (!Visited[v] && !Stack.Contains(v)) { Stack.Push(v); } } } } }