Skip to content

Instantly share code, notes, and snippets.

@jamesharr
Created May 28, 2020 17:44
Show Gist options
  • Save jamesharr/92bc1ffc61595d625fd5ab6860c755c9 to your computer and use it in GitHub Desktop.
Save jamesharr/92bc1ffc61595d625fd5ab6860c755c9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import networkx as nx
import community
G = nx.Graph()
G.add_node('node_a')
G.add_node('node_b')
G.add_node('node_c')
G.add_node('node_d')
G.add_edges_from([
('node_a', 'node_b', {"weight":10}),
('node_a', 'node_c', {"weight":100}),
('node_a', 'node_d', {"weight":10}),
('node_b', 'node_c', {"weight":10}),
('node_b', 'node_d', {"weight":100}),
('node_c', 'node_d', {"weight":10}),
])
p = community.best_partition(G, weight='weight')
print(p)
@jamesharr
Copy link
Author

Output:

{'node_a': 0, 'node_b': 1, 'node_c': 0, 'node_d': 1}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment