Betweenness Centrality with networkx

카테고리 없음 2014. 7. 4. 11:33 Posted by mathboy

>>> g.edges()

[(1, 2), (1, 3)]

>>> for i in range(1,4):

...     for j in range(1,4):

...         print([p for p in nx.all_shortest_paths(g,i,j) if len(p)>2])

... 

[]

[]

[]

[]

[]

[[2, 1, 3]]

[]

[[3, 1, 2]]

[]

>>> nx.betweenness_centrality(g)

{1: 1.0, 2: 0.0, 3: 0.0}



g = nx.Graph()

g.add_edge(1,2)

g.add_edge(1,3)

g.add_edge(2,3)

g.add_edge(1,4)

end = 5

for i in range(1,end):

    for j in range(1,end):

        #print 'from', i, 'to', j

        print([p for p in nx.all_shortest_paths(g,i,j) if len(p)>2])

        #print nx.dijkstra_path(g,i,j)

        #print '=============='

print nx.betweenness_centrality(g)

plt.cla()

nx.draw(g)


[]

[]

[]

[]

[]

[]

[]

[[2, 1, 4]]

[]

[]

[]

[[3, 1, 4]]

[]

[[4, 1, 2]]

[[4, 1, 3]]

[]

>>> print nx.betweenness_centrality(g)

{1: 0.6666666666666666, 2: 0.0, 3: 0.0, 4: 0.0}

>>> plt.cla()

>>> nx.draw(g)

>>> nx.betweenness_centrality(g, normalized=None)

{1: 2.0, 2: 0.0, 3: 0.0, 4: 0.0}

>>> 2/3

0.6666666666666666