def __init__(self, G): # G is Digraph O(V+E) wc finder = DirectedCycle(G) if not finder.hasCycle(): dfs = DepthFirstOrder(G) self._order = dfs.reversePost() # topological order self._rank = [] # rank[v] = position of vertex v in topological order i = 0 for v in self. order: self._rank[v] = i i += 1
def Topological(EdgeWeightedDigraph G): # EdgeWeightedDigraph """Determines if digraph G has a topological order and, if so, finds topological order.""" EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G) if not finder.hasCycle(): dfs = DepthFirstOrder(G) order = dfs.reversePost()