def eulerPath(graph): # counting the number of vertices with odd degree odd = [ x for x in graph.keys() if len(graph[x])&1 ] print odd odd.append( graph.keys()[0] ) if len(odd)>3: return None stack = [ odd[0] ] path = [] # main algorithm while stack: v = stack[-1] if graph[v]: u = graph[v][0] stack.append(u) # deleting edge u-v #print graph[u][ graph[u].index(v) ] #print graph[u].index(v) del graph[u][ graph[u].index(v) ] del graph[v][0] else: path.append( stack.pop() ) return path stack_ = eulerPath({ 1:[2,3], 2:[1,3,4,5], 3:[1,2,4,5], 4:[2,3,5], 5:[2,3,4] }) print stack_