Skip to content

Instantly share code, notes, and snippets.

@renoth
Created September 20, 2021 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoth/393affcb1c7e003f8c5d69172cf6c2b8 to your computer and use it in GitHub Desktop.
Save renoth/393affcb1c7e003f8c5d69172cf6c2b8 to your computer and use it in GitHub Desktop.
import networkx as nx
def getLeaves():
return [v for v, d in g.out_degree if d == 0]
g = nx.DiGraph()
g.add_node("root")
g.add_node("mod-a")
g.add_node("mod-b")
g.add_node("lib-a")
g.add_node("lib-b")
g.add_node("lib-c")
g.add_node("base")
g.add_edge("root", "mod-a")
g.add_edge("root", "mod-b")
g.add_edge("mod-a", "lib-a")
g.add_edge("mod-a", "lib-b")
g.add_edge("mod-b", "lib-a")
g.add_edge("mod-b", "lib-c")
g.add_edge("lib-a", "base")
g.add_edge("lib-b", "base")
leaves = getLeaves()
while len(leaves) != 0:
print(leaves)
print(g.nodes)
for leaf in leaves:
print("remove " + leaf)
g.remove_node(leaf)
leaves = getLeaves()
print()
print(leaves)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment