def walk_edges(graph, src, direction='outgoing'): from collections import deque nmap, m = graph.adjacency_matrix() nmap = dict_reverse(nmap) found = False for i in nmap: if nmap[i] == src: src = i found = True break if not found: raise "Invalid src node" seen = set() q = deque([src]) while len(q) > 0: src = q.popleft() seen.add(src) for i in len(m): if m[src,i] > 0: yield (nmap[src], nmap[i]) if i not in seen: seen.add(i) q.append(i)
def graph_string(graph): rv = [] nmap, m = graph.adjacency_matrix() nmap = dict_reverse(nmap) for i in range(len(m)): for j in range(len(m)): if m[i,j] > 0: rv.append('%s -> %s' % (nmap[i], nmap[j])) return ', '.join(rv)