Last active
May 23, 2020 22:40
-
-
Save jac18281828/bb2007596f8799795e3fc51a1827eeb2 to your computer and use it in GitHub Desktop.
Toy example for networkx python graph package
This file contains hidden or 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
| 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