Created
March 10, 2015 03:12
-
-
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
This file contains 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
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