Skip to content

Instantly share code, notes, and snippets.

@luca
Created October 15, 2014 07:04
Show Gist options
  • Save luca/0a76eb5cf65a562cee0e to your computer and use it in GitHub Desktop.
Save luca/0a76eb5cf65a562cee0e to your computer and use it in GitHub Desktop.
Calculating centrality with networkx
import networkx as nx
# this is a directed graph
dg=nx.DiGraph()
# For all the nodes ...
# this adds a node to the graph
# - node could be any hashable object
# - attr_dict is an (optional) dictionary with attributes for the node
dg.add_node(node, attr_dict)
# For all edges ...
# this adds an edge from source to target
# - source, target could be any hashable object
# - attr_dict is an (optional) dictionary with attributes for the edge
dg.add_edge(source, target, attr_dict)
# This returns a dictionary { node => centrality value }
centrality_dict = nx.betweenness_centrality(dg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment