Created
November 15, 2012 21:27
-
-
Save zemlanin/4081395 to your computer and use it in GitHub Desktop.
Graph visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
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