def create_tour(nodes): tour = [] l = len(nodes) for i in range(l): t = edge(nodes[i], nodes[(i+1) % l]) tour.append(t) return [] def get_degree(tour): degree = {} for x, y in tour: degree[x] = degree.get(x, 0) + 1 degree[y] = degree.get(y, 0) + 1 return degree def check_edge(t, b, nodes): if t[0] == b: if t[1] not in nodes: return t[1] elif t[1] == b: if t[0] not in nodes: return t[0] return None def connected_nodes(tour): a = tour[0][0] nodes = set([a]) explor = set([a]) while len(explor) > 0: b = explore.pop() for t in tour: node = check_edge(t, b, nodes) if node is None: continue nodes.add(node) explor.add(node) return nodes def is_eulerian_tour(nodes, tour): degree = get_degree(tour) for node in nodes: try: d = degree[node] if d % 2 == 1: print "Node %s has odd degree" % node return False except KeyError: print "Node %s was not in your tour" % node return False connected = connected_nodes(tour) if len(connected) == len(nodes): return True else: print "Your graph wasn't connected" return False def test(): nodes = [20, 21, 22, 23, 24, 25] tour = create_tour(nodes) return is_eulerian_tour(nodes, tour)