return False def result(self): 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 = '../g.txt' graph = Graph(filename) hamilton_loop = HamiltonLoop(graph) print(hamilton_loop.result()) filename = '../g2.txt' graph = Graph(filename) hamilton_loop = HamiltonLoop(graph) print(hamilton_loop.result())
def result(self): res = [] if not self.has_euler_loop: return res stack = [] currv = 0 stack.append(1) newg = copy(self._G) while len(stack) > 0: if newg.degree(currv) > 0: stack.append(currv) w = list(newg.adj(currv))[0] newg.remove_edge(currv, w) currv = w else: res.append(currv) currv = stack.pop() return res if __name__ == '__main__': filename = '../g.txt' g = Graph(filename) eluer_loop = EulerLoop(g) print(eluer_loop.result()) filename = '../g2.txt' g = Graph(filename) eluer_loop = EulerLoop(g) print(eluer_loop.result())