Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def eulerPath(graph):
- # counting the number of vertices with odd degree
- odd = [ x for x in graph.keys() if len(graph[x])&1 ]
- 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
- del graph[u][ graph[u].index(v) ]
- del graph[v][0]
- else:
- path.append( stack.pop() )
- return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement