Advertisement
Guest User

Untitled

a guest
Mar 31st, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. from sage.all import *
  2.  
  3. fi = file('eul10c.g6', 'r')
  4. lines = fi.readlines()
  5. fi.close()
  6.  
  7. def is_eulerian(G, trail):
  8.   edges = G.edges()
  9.   if len(trail) is not len(edges):
  10.     return False
  11.   for e in trail:
  12.     if e not in edges: return False
  13.   return True
  14.  
  15. def trail(used):
  16.   return map(lambda x: (min(x), max(x), None), used)
  17.  
  18. def algo(G, dfs):
  19.   def score(v):
  20.     return len(dfs) - dfs.index(v)
  21.  
  22.   v = dfs[0]
  23.   used = []
  24.   while True:
  25.     found = False
  26.     for z in sorted(G.neighbors(v), key=score):
  27.       e = set([v, z])
  28.       if e not in used:
  29.         found = True
  30.         v = z
  31.         used += [e]
  32.         break
  33.     if not found:
  34.       return used
  35.  
  36. for line in lines:
  37.   G = Graph(line, sparse=True)
  38.   dfs = list(G.depth_first_search(0))
  39.  
  40.   if not is_eulerian(G, trail(algo(G, dfs))):
  41.     print map(lambda x: (x[0], x[1]), G.edges())
  42.     print dfs
  43.     print trail(algo(G, dfs))
  44.     break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement