Skip to content

Instantly share code, notes, and snippets.

@jac18281828
Last active May 23, 2020 22:40
Show Gist options
  • Select an option

  • Save jac18281828/bb2007596f8799795e3fc51a1827eeb2 to your computer and use it in GitHub Desktop.

Select an option

Save jac18281828/bb2007596f8799795e3fc51a1827eeb2 to your computer and use it in GitHub Desktop.
Toy example for networkx python graph package
import networkx as nx
# pip install networkx
# directed graph example using networkx as a graph engine
G = nx.DiGraph()
roots = set()
roots.add('cl')
G.add_edge('cl', 'dl')
G.add_edge('dl', 'ip')
G.add_edge('cl', 'ip')
G.add_edge('dl', 'mp')
G.add_edge('cl', 'mp')
G.add_edge('mp', 'hs')
for r in roots:
print "root %s" % r
spacer = {r: 0}
for prereq, target in nx.dfs_edges(G, r):
spacer[target] = spacer[prereq] + 2
print '{spacer}+-{t}'.format(
spacer=' ' * spacer[prereq],
t=target)
print ''
for np in G:
for pred in G.predecessors(np):
print "pred %s" % pred
print "next %s" % np
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment