Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. v = input("enter the vertices: ")
  2. v = [x.strip(' ') for x in v.split(',')]
  3.  
  4.  
  5.  
  6. e = input("enter the edges: ")
  7.  
  8. e = [x.strip(' ') for x in e.split(',')]
  9.  
  10. graph = list(tuple(x.split('-')) for x in e)
  11.  
  12. def eulerian_trail(graph):
  13.  
  14.  
  15. first_vertex = graph[0][0]
  16. adj = {}
  17. trail = []
  18.  
  19. for edge in graph:
  20. a,b = edge
  21. if a not in adj:
  22. adj[a] = []
  23. if b not in adj:
  24. adj[b] = []
  25. adj[a].append(b)
  26. adj[b].append(a)
  27.  
  28. for vertex in v:
  29. c = vertex
  30. if c == first_vertex:
  31. trail.append(c)
  32. trail.extend(adj[c][0])
  33. adj.pop([c][0],0)
  34. if c in trail:
  35. if adj[c][0] != trail[-1]:
  36. trail.extend(adj[c][0])
  37. adj.pop([c][0],0)
  38. else:
  39. trail.extend(adj[c][1])
  40. adj.pop([c][1],0)
  41.  
  42.  
  43.  
  44. return trail,adj
  45.  
  46. eulerian_trail(graph)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement