In [1]:
import networkx as nx
import string
import matplotlib.pyplot as plt
from prettytable import PrettyTable
%matplotlib inline
https://networkx.github.io/documentation/latest/examples/algorithms/davis_club.html
In [2]:
"""
Davis Southern Club Women
Shows how to make unipartite projections of the graph and compute the
properties of those graphs.
These data were collected by Davis et al
in the 1930s. They represent observed attendance at 14 social events
by 18 Southern women. The graph is bipartite (clubs, women).
Data from:
http://vlado.fmf.uni-lj.si/pub/networks/data/Ucinet/UciData.htm
"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
__date__ = "$Date: 2005-05-12 14:33:11 -0600 (Thu, 12 May 2005) $"
__credits__ = """"""
__revision__ = "$Revision: 998 $"
# Copyright (C) 2004 by
# Aric Hagberg <hagberg@lanl.gov>
# Dan Schult <dschult@colgate.edu>
# Pieter Swart <swart@lanl.gov>
# All rights reserved.
# BSD license.
def davis_club_graph(create_using=None, **kwds):
nwomen=14
nclubs=18
G=nx.generators.empty_graph(nwomen+nclubs,create_using=create_using,**kwds)
G.clear()
G.name="Davis Southern Club Women"
women="""\
EVELYN
LAURA
THERESA
BRENDA
CHARLOTTE
FRANCES
ELEANOR
PEARL
RUTH
VERNE
MYRNA
KATHERINE
SYLVIA
NORA
HELEN
DOROTHY
OLIVIA
FLORA"""
clubs="""\
E1
E2
E3
E4
E5
E6
E7
E8
E9
E10
E11
E12
E13
E14"""
davisdat="""\
1 1 1 1 1 1 0 1 1 0 0 0 0 0
1 1 1 0 1 1 1 1 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0 0 0 0 0
1 0 1 1 1 1 1 1 0 0 0 0 0 0
0 0 1 1 1 0 1 0 0 0 0 0 0 0
0 0 1 0 1 1 0 1 0 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 0 1 1 0 0 0 0 0
0 0 0 0 1 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 1 0 0
0 0 0 0 0 0 0 1 1 1 0 1 0 0
0 0 0 0 0 0 0 1 1 1 0 1 1 1
0 0 0 0 0 0 1 1 1 1 0 1 1 1
0 0 0 0 0 1 1 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 0 1 1 1 1 1
0 0 0 0 0 0 0 1 1 1 0 1 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0"""
# women names
w={}
n=0
for name in women.split('\n'):
w[n]=name
n+=1
# club names
c={}
n=0
for name in clubs.split('\n'):
c[n]=name
n+=1
# parse matrix
row=0
for line in davisdat.split('\n'):
thisrow=list(map(int,line.split(' ')))
for col in range(0,len(thisrow)):
if thisrow[col]==1:
G.add_edge(w[row],c[col])
row+=1
return (G,list(w.values()),list(c.values()))
def project(B,pv,result=False,**kwds):
"""
Returns a graph that is the unipartite projection of the
bipartite graph B onto the set of nodes given in list pv.
The nodes retain their names and are connected if they share a
common node in the vertex set of {B not pv}.
No attempt is made to verify that the input graph B is bipartite.
"""
if result:
G=result
else:
G=nx.Graph(**kwds)
for v in pv:
G.add_node(v)
for cv in B.neighbors(v):
G.add_edges_from([(v,u) for u in B.neighbors(cv)])
return G
In [3]:
# return graph and women and clubs lists
(G,women,clubs)=davis_club_graph()
# project bipartite graph onto women nodes
W=project(G,women)
# project bipartite graph onto club nodes
C=project(G,clubs)
In [4]:
nx.draw_networkx(G,with_labels=True,pos=nx.circular_layout(G))
In [5]:
nx.draw_networkx(W,with_labels=True,pos=nx.circular_layout(W))
In [6]:
nx.draw_networkx(C,with_labels=True, pos=nx.circular_layout(C))
In [7]:
for i in W.nodes_iter():
if (len(W.neighbors(i))) != 18:
print i + " --- " + str(len(W.neighbors(i)))
In [8]:
def createTable(graph):
deg=nx.degree(graph)
deg_cent=nx.degree_centrality(graph)
bet_cent=nx.betweenness_centrality(graph)
clo_cent=nx.closeness_centrality(graph)
eig_cent = nx.eigenvector_centrality(graph)
table=[[name,deg[name],round(deg_cent[name],4),round(bet_cent[name],4),round(clo_cent[name],4),round(eig_cent[name],4)] for name in graph.nodes()]
table=sorted(table,key=lambda x: -x[2])
t = PrettyTable(['Name','Degree','Degree Cent','Betweenness','Closeness','EigenCent'])
for i in range(0, len(table)):
t.add_row(table[i])
return t
In [9]:
print(createTable(W))
In [10]:
print(createTable(C))
In [25]:
def getneigh(W,G):
for i in W.nodes_iter():
print i + " --- " + str(len(G.neighbors(i))) + " --- " + str(G.neighbors(i))
In [26]:
getneigh(W,G)
In [27]:
getneigh(C,G)