Skip to content

Instantly share code, notes, and snippets.

@DanielOaks
Created March 10, 2015 03:12
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 DanielOaks/2bfa6c3189a5747ecb7e to your computer and use it in GitHub Desktop.
Save DanielOaks/2bfa6c3189a5747ecb7e to your computer and use it in GitHub Desktop.
Just an example nodelist function for networkx graphs. Works great, though it got factored out of my function
def nodelist(network):
"""Returns a drawable nodelist (for the concentric circle-based drawing functions of networkx)."""
# first off, find most largestly connected (hub) server for the center
center = nx.center(network)
center = center[0] # returns a list of centers, and we don't want that
# and create the layers off that
added_nodes = [center,]
shells = [(center,),] # holds recursive shells
any_added = True
while any_added:
any_added = False
new_shell = []
for node in shells[-1]:
new_neighbors = nx.all_neighbors(network, node)
for n in new_neighbors:
if n not in added_nodes:
new_shell.append(n)
added_nodes.append(n)
any_added = True
if len(new_shell) > 0:
new_shell = (new_shell) # make a tuple
shells.append(new_shell)
return shells
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment