Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. def eulerPath(graph):
  2.     # counting the number of vertices with odd degree
  3.     odd = [ x for x in graph.keys() if len(graph[x])&1 ]
  4.     odd.append( graph.keys()[0] )
  5.  
  6.     if len(odd)>3:
  7.         return None
  8.    
  9.     stack = [ odd[0] ]
  10.     path = []
  11.    
  12.     # main algorithm
  13.     while stack:
  14.         v = stack[-1]
  15.         if graph[v]:
  16.             u = graph[v][0]
  17.             stack.append(u)
  18.             # deleting edge u-v
  19.             del graph[u][ graph[u].index(v) ]
  20.             del graph[v][0]
  21.         else:
  22.             path.append( stack.pop() )
  23.    
  24.     return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement