Skip to content

Instantly share code, notes, and snippets.

@zemlanin
Created November 15, 2012 21:27
Show Gist options
  • Save zemlanin/4081395 to your computer and use it in GitHub Desktop.
Save zemlanin/4081395 to your computer and use it in GitHub Desktop.
Graph visualization
def visualize(node, edges, done = [], depth = ()):
# Edge = namedtuple('Edge', 'source, dest')
paths = [e for e in edges if getattr(e, 'source') == node]
for edge in paths:
for d in depth:
if not d:
print '│',
else:
print ' ',
# is current path last in source's paths
is_last = paths.index(edge) == len(paths)-1
if is_last:
symbol = '└ '
else:
symbol = '├ '
print symbol+str(edge.dest),
if edge.dest in done:
print '*' # next destination's path has been already printed
else:
print
done.append(edge.dest)
if len(depth) < 15: # limit for better readability
visualize(edge.dest, edges, done, depth + (is_last,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment