class DepthFirstOrder: def __init__(self, G): """ Args: G: Digraph """ self.marked = [False for _ in range(G.V)] # 所有顶点的前序排序 self.pre = Queue() # 所有顶点的后序排列 self.post = Queue() # 所有顶点的逆后序排列 self.reverse_post = Stack() for v in range(G.V): if not self.marked[v]: self.dfs(G, v) def dfs(self, G, v): """ Args: G: Digraph v: int """ self.pre.enqueue(v) self.marked[v] = True for w in G.adj[v]: if not self.marked[w]: self.dfs(G, w) self.post.enqueue(v) self.reverse_post.push(v)
def path_to(self, v: int) -> Stack: if not self.has_path_to(v): return path = Stack() e = self.edge_to[v] while e: path.push(e) e = self.edge_to[e.from_()] return path
def __init__(self, G): """ Args: G: Digraph """ self.marked = [False for _ in range(G.V)] # 所有顶点的前序排序 self.pre = Queue() # 所有顶点的后序排列 self.post = Queue() # 所有顶点的逆后序排列 self.reverse_post = Stack() for v in range(G.V): if not self.marked[v]: self.dfs(G, v)
def path_to(self, v): """ Args: v: int Return: Iterable<DirectedEdge> """ if not self.has_path_to(v): return path = Stack() e = self.edge_to[v] while e: path.push(e) e = self.edge_to[e.from_()] return path
class DirectedCycle: def __init__(self, G): """ Args: G: Digraph """ self.marked = [False for _ in range(G.V)] self.edge_to = [x for x in range(G.V)] # 有向环中的所有顶点, 如果存在的话 self.cycle = None # 递归调用的栈上的所有顶点 self.on_stack = [False for _ in range(G.V)] for v in range(G.V): if not self.marked[v]: self.dfs(G, v) def dfs(self, G, v): """ Args: G: Digraph v: int """ self.on_stack[v] = True self.marked[v] = True for w in G.adj[v]: if self.has_cycle(): return elif not self.marked[w]: self.edge_to[w] = v self.dfs(G, w) elif self.on_stack[w]: self.cycle = Stack() x = v while x != w: self.cycle.push(x) x = self.edge_to[x] self.cycle.push(w) self.cycle.push(v) self.on_stack[v] = False def has_cycle(self): """ Return: bool """ return self.cycle != None
def dfs(self, G, v): """ Args: G: Digraph v: int """ self.on_stack[v] = True self.marked[v] = True for w in G.adj[v]: if self.has_cycle(): return elif not self.marked[w]: self.edge_to[w] = v self.dfs(G, w) elif self.on_stack[w]: self.cycle = Stack() x = v while x != w: self.cycle.push(x) x = self.edge_to[x] self.cycle.push(w) self.cycle.push(v) self.on_stack[v] = False
def path_to(self, v): """ Args: v: int Return: Iterable<int> """ if not self.has_path_to(v): return path = Stack() x = v while x != self.s: path.push(x) x = self.edge_to(x) path.push(self.s) return path