self._res.add(v) child += 1 if v == parent and child > 1: self._res.add(v) # v w是环上的一条边 # 肯定不是环 # 此时可能需要更新下v的low值 elif w != parent: self._low[v] = min(self._low[v], self._low[w]) @property def result(self): return list(self._res) if __name__ == '__main__': filename = 'play_with_graph_algorithms/chapter08/g.txt' g = Graph(filename) find_cut_points = FindCutPoints(g) print(find_cut_points.result) filename = 'play_with_graph_algorithms/chapter08/g2.txt' g = Graph(filename) find_cut_points = FindCutPoints(g) print(find_cut_points.result) filename = 'play_with_graph_algorithms/chapter08/tree.txt' g = Graph(filename) find_cut_points = FindCutPoints(g) print(find_cut_points.result)
res = [] if self._end == -1: return res curr = self._end while curr != 0: res.append(curr) curr = self._pre[curr] res.append(0) return res[::-1] if __name__ == '__main__': filename = 'play_with_graph_algorithms/chapter09/g.txt' graph = Graph(filename) hamilton_loop = HamiltonLoop(graph) print(hamilton_loop.result()) filename = 'play_with_graph_algorithms/chapter09/g2.txt' graph = Graph(filename) hamilton_loop = HamiltonLoop(graph) print(hamilton_loop.result()) filename = 'play_with_graph_algorithms/chapter09/g.txt' graph = Graph(filename) hamilton_loop_v2 = HamiltonLoopV2(graph) print(hamilton_loop_v2.result()) filename = 'play_with_graph_algorithms/chapter09/g2.txt' graph = Graph(filename)