Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Anˆlush DiktÔwn 
me to NetworkX thc Python 
Mwus c A. MpountourÐdhc 
Tm ma Majhmatik¸n PanepisthmÐou Pˆtrac 
mboudour@upatras.gr 
22 NoembrÐou 2012 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
PÐnakac Perieqomènwn 
1 Python kai NetworkX 
2 Eisagwg  Grˆfwn 
Mh Kateujunìmenoi Grˆfoi 
Kateujunìmenoi Grˆfoi 
Grˆfoi me Bˆrh Akm¸n 
DimereÐc Grˆfoi 
Qarakthristikˆ (Attributes) Kìmbwn kai Akm¸n 
TÔpoi Grˆfwn kai Sqesiak¸n Dedomènwn 
Genn torec Prokataskeuasmènwn Grˆfwn 
3 Diktuakˆ Mètra 
BajmoÐ Kìmbwn 
Kentrikìthtec Kìmbwn 
Suntelest c Suss¸reushc kai Metabatikìthta 
Amoibaiìthta 
Apostˆseic 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
4 DiktuakoÐ DiamerismoÐ 
Sunist¸sec kai KlÐkec 
k–Pur nec (k–Cores) 
Blockmodeling 
Koinìthtec (Communities) 
5 Diktuakˆ Montèla 
Anameiximìthta (Assortativity) 
Epirro  kai Diˆqush 
DÐktua Mikr¸n Kìsmwn (Small–Worlds) 
DÐktua QwrÐc KlÐmaka (Scale–Free) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Python kai NetworkX 
H Python eÐnai mia ermhneutik  gl¸ssa, dhlad , 
ekteleÐtai qwrÐc na qreiˆzetai na perˆsei apì thn 
diadikasÐa thc sÔntaxhc. 
H Python diatÐjetai gia katèbasma (mazÐ me odhgÐec 
egkatˆstashc) gia ìla ta leitourgikˆ sust mata 
apì to http://www.python.org/download/. IdiaÐtera 
gia ta Windows sunistˆtai h ActivePython. 
To NetworkX eÐnai èna pakèto thc Python gia thn 
dhmiourgÐa kai ton qeirismì grˆfwn kai diktÔwn. 
To pakèto, h tekmhrÐwsh ki ˆlloi pìroi tou 
NetworkX eÐnai diajèsimoi sto 
http://networkx.lanl.gov/, ìpwc kai sthn apoj kh 
pakètwn thc Python PyPI. 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Mia apeujeÐac (one–click) egkatˆstash thc Python, 
tou NetworkX ki ˆllwn monˆdwn thc Python mporeÐ 
na gÐnei apì thn dianom  thc Python Enthought, 
pou dÐnetai dwreˆn gia akadhmaðk  qr sh. 
Pollèc qr simec plhroforÐec gia thn egkatˆstash 
twn Python, NetworkX ki ˆllwn aparaÐthtwn 
upologistik¸n pìrwn dÐnontai sth selÐda twn 
Resources tou maj matoc Comp 200 tou 
PanepisthmÐou tou Rice. 
'Oloi oi k¸dikec twn diafanei¸n ed¸ èqoun 
dokimasjeÐ pˆnw sthn èkdosh 2.7.3 thc Python kai 
thn èkdosh 1.7 tou NetworkX. 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Sto prooÐmio thc graf c (script) ìlwn twn 
ekteloÔmenwn ed¸ kwdÐkwn thc Python ja prèpei 
na eisˆgontai oi ex c grammèc: 
import networkx as nx 
import matplotlib . pyplot as plt 
import pylab 
Sthn antigraf  ki epikìllhsh twn entol¸n pou 
dÐnontai sth sunèqeia prèpei na proseqjeÐ h 
stoÐqish stic allagèc gramm¸n, giatÐ h Python 
eÐnai euaÐsjhth wc proc thn autìmath topojèthsh 
esoq¸n (indentation). 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Eisagwg  Grˆfwn 
Pr¸ta, dÐnontai oi entolèc gia mh kateujunìmenouc 
grˆfouc. 
Arqikˆ, dhmiourgeÐtai ènac kenìc grˆfoc: 
G = nx. Graph () 
Metˆ, eisˆgontai arqikˆ oi kìmboi, p.q., oi 5 
kìmboi '1', '2', '3', '4' kai '5' mpaÐnoun wc ex c: 
G. add_nodes_from ([1,2,3,4,5]) 
Bèbaia, antÐ gia arijmoÔc, oi kìmboi mporoÔn na 
eisˆgontai wc onìmata   lèxeic, p.q.: 
G. add_nodes_from ([ 'John ', 'Mary ']) 
G. add_node (" London ") 
G. add_nodes_from ([ 'a','b','c','d','e']) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Tèloc, eisˆgontai oi akmèc, p.q., gia touc 5 
kìmbouc 1–5 na 6 sundèseic: 
G. add_edges_from ([(1,2),(1,4),(2,3),(3,4),(3,5),(4,5 )]) 
Oi lÐstec kai to pl joc twn eisaqjèntwn kìmbwn 
kai akm¸n dÐnontai apì tic entolèc: 
G. nodes () 
G. number_of_nodes () 
G. edges () 
G. number_of_edges () 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Gia to sqediasmì tou eisaqjèntoc grˆfou, p.q, 
tou paradeÐgmatoc autoÔ tou grˆfou me 5 
kìmbouc kai 6 akmèc, ekteloÔntai oi entolèc: 
plt . figure () 
nx. draw (G) 
plt . show ( block = False ) 
H teleutaÐa entol  dèqetai diˆforec paramètrouc, 
ìpwc, p.q., gia diaforetikì tÔpo sqedÐou (layout), 
mègejoc kai qr¸ma kìmbwn kai afaÐresh twn 
onomˆtwn (ids) twn kìmbwn: 
nx. draw_spring (G, node_size =100 , node_color ='# A0CBE2 ', 
with_labels = False ) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
ParaleÐpontac parametropoi seic, o sqediasmìc 
tou grˆfou tou paradeÐgmatoc dÐnei to sq ma: 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Oi kìmboi mporoÔn na topotethjoÔn se stajerèc jèseic, 
efìson o grˆfoc eÐqe eisaqjeÐ me tic suntetagmènec 
touc, pou mporeÐ na gÐnei, p.q., sto parˆdeigma autì 
wc ex c: 
pos ={1:(0,0),2:(1,0),4:(0,1),3:(1,1),5:(0.5,2.0)} 
nx. draw (G, pos ) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Eisagwg  Kateujunìmenwn Grˆfwn 
GÐnetai wc ex c, p.q., sto parˆdeigma autì: 
G=nx. DiGraph () 
G. add_nodes_from (["A","B","C","D"]) 
G. add_edges_from ([( "A","B"), ("C","A"), ("C","B"), 
("B","D")]) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Eisagwg  Grˆfwn me Bˆrh Akm¸n 
GÐnetai, p.q., sto parˆdeigma autì me tic entolèc: 
G=nx. Graph () 
G. add_weighted_edges_from ([( 'a','b',4),( 'a','c',8), 
('a','d',5),( 'c','d',3)]) 
kai o sqediasmìc wc ex c: 
pos =nx. spring_layout (G) 
edge_labels = dict ([((u,v ,),d['weight ']) 
for u,v,d in G. edges ( data = True )]) 
nx. draw_networkx_nodes (G,pos , node_size = 700) 
nx. draw_networkx_edges (G, pos) 
nx. draw_networkx_labels (G,pos , font_size =20) 
nx. draw_networkx_edge_labels (G,pos , 
edge_labels = edge_labels , font_size =20) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
AntÐ thc parˆjeshc twn tim¸n twn bar¸n, oi akmèc mporoÔn na 
sqediasjoÔn me eÔrh anˆloga twn tim¸n twn bar¸n wc ex c: 
nx. draw_networkx_nodes (G,pos , node_size = 700) 
edgewidth =[] 
for (u,v,d) in G. edges ( data = True ): 
edgewidth . append (d['weight ']) 
nx. draw_networkx_edges (G,pos , edge_color ='b', 
width = edgewidth ) 
nx. draw_networkx_labels (G,pos , font_size =20) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Epiplèon, mporoÔn na krathjoÔn sto sqèdio mìno oi akmèc me bˆrh 
megalÔtera kˆpoiac endiˆmeshc tim c, p.q., 4: 
elarge = [(u,v) for (u,v,d) in G. edges ( data = True ) 
if d['weight '] >4] 
esmall = [(u,v) for (u,v,d) in G. edges ( data = True ) 
if d['weight '] <=4] 
nx. draw_networkx_edges (G,pos , edgelist = elarge , 
edge_color ='b',width = edgewidth ) 
nx. draw_networkx_edges (G,pos , edgelist = esmall , width =6, 
alpha =0.5, edge_color ='g',style ='dashed ') 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Eisagwg  Dimer¸n Grˆfwn 
H eisagwg  dimer¸n grˆfwn gÐnetai ìpwc, p.q., 
sto parakˆtw parˆdeigma: 
from networkx . algorithms import bipartite 
G = nx. Graph () 
G. add_nodes_from ([1,2,3,4], bipartite =0) 
G. add_nodes_from ([ 'a','b','c'], bipartite =1) 
G. add_edges_from ([(1,'a'),(1,'b'),(2,'a'),(2,'b'), 
(2,'c'),(3,'c'),(4,'b'),(4,'c')]) 
pos ={1:(0,0), 
2:(0,1), 
3:(0,2), 
4:(0,3), 
'a':(1,0.5), 
'b':(1,1.5), 
'c':(1,2.5)} 
O èlegqoc an prìkeitai perÐ dimeroÔc grˆfou 
(True)   ìqi (False) gÐnetai me thn entol : 
print bipartite . is_bipartite (G) 
True 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
O sqediasmìc tou dimeroÔc grˆfou gÐnetai wc 
ex c: 
mode1 , mode2 = bipartite . sets (G) 
nx. draw_networkx_nodes (G,pos , nodelist = list ( mode1 ), 
node_color ='b',node_size = 700) 
nx. draw_networkx_nodes (G,pos , nodelist = list ( mode2 ), 
node_color ='g',node_size = 700) 
nx. draw_networkx_edges (G, pos) 
nx. draw_networkx_labels (G,pos , font_size =20 , 
font_color ='# FFFFFF ') 
plt . axis ('off ') 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Qarakthristikˆ (Attributes) Kìmbwn kai Akm¸n 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
TÔpoi Grˆfwn kai Sqesiak¸n Dedomènwn 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Genn torec Prokataskeuasmènwn Grˆfwn 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
BajmoÐ Kìmbwn 
Pr¸ta, gia mh kateujunìmenouc grˆfouc: O grˆfoc 
G = (V, E) sto sÔnolo koruf¸n V eÐnai mh kateujunìmenoc, ìtan to 
sÔnolo twn akm¸n tou E eÐnai èna summetrikì uposÔnolo tou V  V. 
GeÐtonec kìmbwn: Gia duo korufèc i, j 2 V tou G, h j lègetai 
geÐtonac thc i ìtan (i, j) 2 E. 
G. nodes () # List of nodes 
G. number_of_nodes () # Number of nodes 
len (G) # Number of nodes 
G. order () # Number of nodes 
G. number_of_edges () # Number of of edges 
G. neighbors (1) # Neighbors of node 1 
## To get the list of neighbors of all nodes : 
for node in G. nodes (): 
print node , G. neighbors ( node ) 
PÐnakac GeitnÐashc (Adjacency Matrix): EÐnai ènac 
(summetrikìc) pÐnakac A = fAgi,j2V tˆxhc jVj  jVj tètoioc ¸ste 
A = 1, ìtan i, j geÐtonec, A = 0, diaforetikˆ. 
A = nx. to_numpy_matrix (G) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
BajmoÐ kìmbwn: Sto mh kateujunìmeno grˆfo G, o 
bajmìc miac koruf c i, pou sumbolÐzetai wc ki, 
orÐzetai san to pl joc twn geitìnwn tou i, 
dhlad , to pl joc twn sundèsewn pou 
prospÐptoun sto i. Profan¸c, isqÔei: 
ki = 
X 
j2V 
A = 
X 
i2V 
A 
ki, epiplèon, 
X 
i2V 
ki = 
X 
i,j2V 
A = 2jEj 
G. degree (1) # Degree of node 1 
len (G. neighbors (1) # Degree of node 1 
G. degree () # List of degrees of all nodes 
## To get the table of degrees of all nodes : 
for node in G. nodes (): 
print node , G. degree ( node ) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
BajmoÐ Kìmbwn Kateujunìmenwn Grˆfwn 
'Estw o kateujunìmenoc grˆfoc G = (V, E) sto sÔnolo koruf¸n V, 
ìpou t¸ra to sÔnolo twn akm¸n tou E eÐnai èna mh summetrikì 
uposÔnolo tou V  V ki o antÐstoiqoc pÐnakac geitnÐashc A = fAg 
eÐnai mh summetrikìc. 
O bajmìc eisìdou thc koruf c i tou G, pou sumbolÐzetai wc kin i , 
orÐzetai san to pl joc twn sundèsewn pou xekinoÔn apì geÐtonec 
tou i kai kateujÔnontai proc ton i, dhlad , 
kin 
i = 
X 
j2V 
A 
O bajmìc exìdou thc koruf c i tou G, pou sumbolÐzetai wc kout i , 
orÐzetai san to pl joc twn sundèsewn pou xekinoÔn apì ton i kai 
kateujÔnontai proc geÐtonec tou i, dhlad , 
kout 
i = 
X 
i2V 
A 
Profan¸c, isqÔei: 
X 
i2V 
kin 
i = 
X 
j2V 
kout 
i = 
X 
i,j2V 
A = jEj 
G. in_degree (1) #In - Degree of node 1 
G. out_degree (1) #Out - Degree of node 1 
G. in_degree () # List of in - degrees of all nodes 
G. out_degree () # List of out - degrees of all nodes 
## To get the table of in - degrees and out - degrees of all nodes : 
for node in G. nodes (): 
print node , G. in_degree ( node ), G. out_degree ( node ) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Katanomèc Bajm¸n 
Istìgramma katanom c bajm¸n (gia mh kateujunìmeno grˆfo): 
degrees = G. degree () 
values = sorted ( set ( degrees . values ())) 
hist = [ degrees . values (). count (x) for x in values ] 
fig = plt . figure () 
ax = fig . add_subplot ( 111 ) 
plt . plot ( values ,hist ,'ro -') 
#ax . set_xscale (' log ') # Logarithmic x scale 
#ax . set_yscale (' log ') # Logarithmic y scale 
plt . title ( Degree Distribution ) 
plt . xlabel ('Degree ') 
plt . ylabel ('Number of nodes ') 
Istìgramma katanom c bajm¸n (gia kateujunìmeno grˆfo): 
## in_degrees = G. in_degree () 
in_values = sorted ( set ( in_degrees . values ())) 
in_hist = [ in_degrees . values (). count (x) for x in in_values ] 
out_degrees = G. out_degree () 
out_values = sorted ( set ( out_degrees . values ())) 
out_hist = [ out_degrees . values (). count (x) for x in out_values ] 
fig = plt . figure () 
ax = fig . add_subplot ( 111 ) 
plt . plot ( in_values , in_hist ,'ro -') 
plt . plot ( out_values , out_hist ,'bv -') 
#ax . set_xscale (' log ') # Logarithmic x scale 
#ax . set_yscale (' log ') # Logarithmic y scale 
plt . titke (In - Degree  Out - Degree Distributions ) 
plt . xlabel ('Degree ') 
plt . ylabel ('Number of nodes ') 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Parˆdeigma: To dÐktuo karˆte 
G = nx. karate_club_graph () 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Kentrikìthtec Kìmbwn: 
1. Kentrikìthta BajmoÔ (Degree Centrality) 
Oi orismoÐ OLWN twn kentrikìthtwn pou ja d¸soume 
ed¸ kai sth sunèqeia aforoÔn mh kateujunìmenouc 
(aploÔc) grˆfouc. 
H kentrikìthta bajmoÔ (degree centrality) xi tou kìmbou 
i isoÔtai proc ton bajmì ki tou kìmbou autoÔ: 
xi = ki 
x8 = 5 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
2. Kentrikìthta Endiamesìthtac 
(Betweenness Centrality) 
H kentrikìthta endiamesìthtac (betweenness centrality) xi tou kìmbou 
i isoÔtai proc: 
xi = 
X 
s6=i6=t2V 
nist 
gst 
ìpou nist eÐnai to pl joc twn gewdaitik¸n diadrom¸n metaxÔ twn 
kìmbwn s kai t, pou pernoÔn apì ton kìmbo i, kai gst eÐnai to sunolikì 
pl joc twn gewdaitik¸n diadrom¸n metaxÔ twn kìmbwn s kai t. 
n23 
,23 = 2 
g3,23 = 4 
x2 = 0.1436 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
3. Kentrikìthta EggÔthtac 
(Closeness Centrality) 
H kentrikìthta eggÔthtac (closeness centrality) xi tou kìmbou i 
isoÔtai proc: 
xi = 
n P 
j2V d 
ìpou n eÐnai to pl joc twn kìmbwn tou grˆfou kai d h gewdaitik  
apìstash apì ton kìmbo i proc opoiod pote ˆllo kìmbo j. 
x0 = 0.5689 
x2 = 0.5593 
x33 = 0.55 
x31 = 0.5409 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
4. Kentrikìthta IdiodianÔsmatoc 
(Eigenvector Centrality) 
H kentrikìthta idiodianÔsmatoc (eigenvector centrality) xi tou kìmbou i 
isoÔtai proc: 
xi = 1 
1 
X 
j2V 
Axj 
ìpou A eÐnai o pÐnakac geitnÐashc (adjacency matrix) tou grˆfou kai 
xi eÐnai oi sunist¸sec tou idiadianÔsmatoc tou A, pou antistoiqoÔn 
sth megalÔterh idiotim  tou 1. 
x33 = 0.3734 
x0 = 0.3555 
x2 = 0.3172 
x32 = 0.3086 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Upologismìc Kentrikìthtwn 
Entolèc NetworkX: 
nx. degree_centrality (G) 
nx. betweenness_centrality (G) 
nx. closeness_centrality (G) 
nx. eigenvector_centrality (G) 
EktÔpwsh se pÐnaka: 
centralities = [] 
centralities . append (nx. degree_centrality (G )); 
centralities . append (nx. betweenness_centrality (G )); 
centralities . append (nx. closeness_centrality (G)); 
centralities . append (nx. eigenvector_centrality (G )); 
for node in G. nodes_iter (): 
measures = (t). join ( map ( lambda f: str(f[ node ]), 
centralities )) 
print %s %s % (node , measures ) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Parˆdeigma: To dÐktuo Karˆte 
G = nx. karate_club_graph () 
Nodes Degree C. Betweenness C. Closeness C. Eigenvector C. 
0 0.484848485 0.437635281 0.568965517 0.355490721 
1 0.272727273 0.053936688 0.485294118 0.265959605 
2 0.303030303 0.143656806 0.559322034 0.317192417 
3 0.181818182 0.011909271 0.464788732 0.211179694 
4 0.090909091 0.000631313 0.379310345 0.075968879 
5 0.121212121 0.029987374 0.38372093 0.079483113 
6 0.121212121 0.029987374 0.38372093 0.079483113 
7 0.121212121 0 0.44 0.170959892 
8 0.151515152 0.055926828 0.515625 0.227404355 
9 0.060606061 0.000847763 0.434210526 0.102674504 
10 0.090909091 0.000631313 0.379310345 0.075968879 
11 0.03030303 0 0.366666667 0.052855817 
12 0.060606061 0 0.370786517 0.084254727 
13 0.151515152 0.045863396 0.515625 0.226473112 
14 0.060606061 0 0.370786517 0.10140365 
15 0.060606061 0 0.370786517 0.10140365 
16 0.060606061 0 0.284482759 0.023635566 
17 0.060606061 0 0.375 0.092399699 
18 0.060606061 0 0.370786517 0.10140365 
19 0.090909091 0.032475048 0.5 0.147912918 
20 0.060606061 0 0.370786517 0.10140365 
21 0.060606061 0 0.375 0.092399699 
22 0.060606061 0 0.370786517 0.10140365 
23 0.151515152 0.017613636 0.392857143 0.150118912 
24 0.090909091 0.002209596 0.375 0.057052326 
25 0.090909091 0.003840488 0.375 0.059206342 
26 0.060606061 0 0.362637363 0.075579616 
27 0.121212121 0.022333454 0.458333333 0.133477386 
28 0.090909091 0.001794733 0.452054795 0.131077964 
29 0.121212121 0.002922078 0.38372093 0.134961122 
30 0.121212121 0.014411977 0.458333333 0.174758637 
31 0.181818182 0.138275613 0.540983607 0.191034394 
32 0.363636364 0.145247114 0.515625 0.308643749 
33 0.515151515 0.304074976 0.55 0.373362539 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Kentrikìthta Mikr  tim  Megˆlh tim  
Degree LÐgoi geÐtonec (sundèseic) PolloÐ geÐtonec (sundèseic) 
Betweenness Mikrìc èlegqoc ro c Megˆloc èlegqoc ro c 
Closeness Proc thn perifèreia Proc to kèntro 
Eigenvector LÐgoi   lÐgo shmantikoÐ geÐtonec PolloÐ   polÔ shmantikoÐ geÐtonec 
To dÐktuo twn stratiwtik¸n tou David Krackhardt: 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Suntelest c Suss¸reushc 
O suntelest c suss¸reushc (clustering coefficient) Ci tou kìmbou i 
orÐzetai wc: 
Ci = 
2i 
ki(ki  1) 
, 
ìpou i eÐnai to pl joc twn sundèsewn metaxÔ twn geitonik¸n 
kìmbwn tou i kai d i proc opoiod pote ˆllo kìmbo ki eÐnai to 
pl joc twn geitonik¸n kìmbwn tou i. 
nx. clustering (G) # Clustering coefficients of all nodes 
nx. clustering (G,n) # Clustering coefficient of node n 
C23 = 
2  4 
5  4 = 0.4 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Diktuak  Metabatikìthta 
O sunolikìc suntelest c suss¸reushc (global clustering coefficient) 
(ìlou) tou grˆfou G orÐzetai wc h mèsh tim  twn suntelest¸n 
suss¸reushc twn kìmbwn tou: 
C(G) = 
1 
jVj 
X 
i 
Ci 
H metabatikìthta (transitivity) tou grˆfou G orÐzetai wc to phlÐko: 
T(G) = 
pl joc trig¸nwn 
pl joc sundedemènwn triˆdwn 
nx. average_clustering (G) 
# Graph clustering coefficient 
nx. transitivity (G) 
# Graph transitivity 
C(G) = 0.16 
T(G) = 0.19 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Amoibaiìthta Sundèsewn se Kateujunìmeno Grˆfo 
Se ènan kateujunìmeno grˆfo, o suntelest c amoibaiìthtac 
sundèsewn/desm¸n (link/tie mutuality coefficient) orÐzetai wc ex c: 
M(G) = 
pl joc antapodidìmenwn sundèsewn 
pl joc ìlwn twn sundèsewn/tìxwn 
G. to_undirected ( True ). size () 
# Reciprocated edges 
G. number_of_edges () 
# Total edges 
Er(G) = 64 
E(G) = 195 
M(G) = 0.3282 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Apostˆseic 
Se ènan grˆfo G, gia kˆje duo kìmbouc i, j, h (gewdaitik ) apìstas  
touc d(i, j) orÐzetai wc to m koc thc suntomìterhc diadrom c apì to i 
sto j, efìson oi kìmboi autoÐ eÐnai sundedemènoi, en¸ d(i, j) = 1, 
diaforetikˆ (kai fusikˆ, d(i, i) = 0). (H ‘‘suntomìterh diadrom ’’ 
metaxÔ duo kìmbwn eÐnai h diadrom  pou èqei to elˆqisto m koc 
anˆmesa se ìlec tic diadromèc metaxÔ twn duo kìmbwn.) 
To mèso m koc suntomìterhc diadrom c (average shortest path length) 
orÐzetai wc ex c: 
a = 
1 
jVj(jVj  1) 
X 
i,j2V 
d(i, j) 
a = nx. average_shortest_path_length (G) 
a = 2.4082 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Mètra Diˆforwn Empeirik¸n DiktÔwn TABLE I. The general characteristics of several real networks. For each network we indicated the number of nodes, the average 
degree !k, the average path length ! and the clustering coefficient C. For a comparison we have included the average path 
length !rand and clustering coefficient Crand of a random graph with the same size and average degree. The last column 
identifies the symbols in Figs. 8 and 9. 
Network Size !k ! !rand C Crand Reference Nr. 
WWW, site level, undir. 153, 127 35.21 3.1 3.35 0.1078 0.00023 Adamic 1999 Internet, domain level 3015 - 6209 3.52 - 4.11 3.7 - 3.76 6.36 - 6.18 0.18 - 0.3 0.001 Yook et al. 2001a, 
Pastor-Satorras et al. 2001 Movie actors 225, 226 61 3.65 2.99 0.79 0.00027 Watts, Strogatz 1998 LANL coauthorship 52, 909 9.7 5.9 4.79 0.43 1.8 × 10−4 Newman 2001a,b MEDLINE coauthorship 1, 520, 251 18.1 4.6 4.91 0.066 1.1 × 10−5 Newman 2001a,b SPIRES coauthorship 56, 627 173 4.0 2.12 0.726 0.003 Newman 2001a,b,c NCSTRL coauthorship 11, 994 3.59 9.7 7.34 0.496 3 × 10−4 Newman 2001a,b Math coauthorship 70, 975 3.9 9.5 8.2 0.59 5.4 × 10−5 Barab´asi et al. 2001 Neurosci. coauthorship 209, 293 11.5 6 5.01 0.76 5.5 × 10−5 Barab´asi et al. 2001 E. coli, substrate graph 282 7.35 2.9 3.04 0.32 0.026 Wagner, Fell 2000 10 
E. coli, reaction graph 315 28.3 2.62 1.98 0.59 0.09 Wagner, Fell 2000 11 
Ythan estuary food web 134 8.7 2.43 2.26 0.22 0.06 Montoya, Sol´e 2000 12 
Silwood park food web 154 4.75 3.40 3.23 0.15 0.03 Montoya, Sol´e 2000 13 
Words, cooccurence 460.902 70.13 2.67 3.03 0.437 0.0001 Cancho, Sol´e 2001 14 
Words, synonyms 22, 311 13.48 4.5 3.84 0.7 0.0006 Yook et al. 2001 15 
Power grid 4, 941 2.67 18.7 12.4 0.08 0.005 Watts, Strogatz 1998 16 
C. Elegans 282 14 2.65 2.25 0.28 0.05 Watts, Strogatz 1998 17 
TABLE II. The scaling exponents characterizing the degree distribution of several scale-free networks, for which P(k) follows 
a power-law (2). We indicate the size of the network, its average degree !k and the cutoff  for the power-law scaling. For 
directed networks we list separately the indegree (#in) and outdegree (#out) exponents, while for the undirected networks, 
marked with a star, these values are identical. The columns lreal , lrand and lpow compare the average path length of real 
networks with power-law degree distribution and the prediction of random graph theory (17) and that of Newman, Strogatz 
and Watts (2000) (62), as discussed in Sect. V. The last column identifies the symbols in Figs. 8 and 9. 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Sunist¸sec kai KlÐkec 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Sunist¸sec kai KlÐkec 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Blockmodeling 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Koinìthtec (Communities) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Anameiximìthta (Assortativity) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
Epirro  kai Diˆqush 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
DÐktua Mikr¸n Kìsmwn (Small–Worlds) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
DÐktua QwrÐc KlÐmaka (Scale–Free) 
Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python

More Related Content

Ανάλυση Δικτύων με το NetworkX της Python: Μια προκαταρκτική (αλλά ημιτελής ως το καλοκαίρι 2014) εισαγωγή

  • 1. Anˆlush DiktÔwn me to NetworkX thc Python Mwus c A. MpountourÐdhc Tm ma Majhmatik¸n PanepisthmÐou Pˆtrac mboudour@upatras.gr 22 NoembrÐou 2012 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 2. PÐnakac Perieqomènwn 1 Python kai NetworkX 2 Eisagwg  Grˆfwn Mh Kateujunìmenoi Grˆfoi Kateujunìmenoi Grˆfoi Grˆfoi me Bˆrh Akm¸n DimereÐc Grˆfoi Qarakthristikˆ (Attributes) Kìmbwn kai Akm¸n TÔpoi Grˆfwn kai Sqesiak¸n Dedomènwn Genn torec Prokataskeuasmènwn Grˆfwn 3 Diktuakˆ Mètra BajmoÐ Kìmbwn Kentrikìthtec Kìmbwn Suntelest c Suss¸reushc kai Metabatikìthta Amoibaiìthta Apostˆseic Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 3. 4 DiktuakoÐ DiamerismoÐ Sunist¸sec kai KlÐkec k–Pur nec (k–Cores) Blockmodeling Koinìthtec (Communities) 5 Diktuakˆ Montèla Anameiximìthta (Assortativity) Epirro  kai Diˆqush DÐktua Mikr¸n Kìsmwn (Small–Worlds) DÐktua QwrÐc KlÐmaka (Scale–Free) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 4. Python kai NetworkX H Python eÐnai mia ermhneutik  gl¸ssa, dhlad , ekteleÐtai qwrÐc na qreiˆzetai na perˆsei apì thn diadikasÐa thc sÔntaxhc. H Python diatÐjetai gia katèbasma (mazÐ me odhgÐec egkatˆstashc) gia ìla ta leitourgikˆ sust mata apì to http://www.python.org/download/. IdiaÐtera gia ta Windows sunistˆtai h ActivePython. To NetworkX eÐnai èna pakèto thc Python gia thn dhmiourgÐa kai ton qeirismì grˆfwn kai diktÔwn. To pakèto, h tekmhrÐwsh ki ˆlloi pìroi tou NetworkX eÐnai diajèsimoi sto http://networkx.lanl.gov/, ìpwc kai sthn apoj kh pakètwn thc Python PyPI. Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 5. Mia apeujeÐac (one–click) egkatˆstash thc Python, tou NetworkX ki ˆllwn monˆdwn thc Python mporeÐ na gÐnei apì thn dianom  thc Python Enthought, pou dÐnetai dwreˆn gia akadhmaðk  qr sh. Pollèc qr simec plhroforÐec gia thn egkatˆstash twn Python, NetworkX ki ˆllwn aparaÐthtwn upologistik¸n pìrwn dÐnontai sth selÐda twn Resources tou maj matoc Comp 200 tou PanepisthmÐou tou Rice. 'Oloi oi k¸dikec twn diafanei¸n ed¸ èqoun dokimasjeÐ pˆnw sthn èkdosh 2.7.3 thc Python kai thn èkdosh 1.7 tou NetworkX. Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 6. Sto prooÐmio thc graf c (script) ìlwn twn ekteloÔmenwn ed¸ kwdÐkwn thc Python ja prèpei na eisˆgontai oi ex c grammèc: import networkx as nx import matplotlib . pyplot as plt import pylab Sthn antigraf  ki epikìllhsh twn entol¸n pou dÐnontai sth sunèqeia prèpei na proseqjeÐ h stoÐqish stic allagèc gramm¸n, giatÐ h Python eÐnai euaÐsjhth wc proc thn autìmath topojèthsh esoq¸n (indentation). Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 7. Eisagwg  Grˆfwn Pr¸ta, dÐnontai oi entolèc gia mh kateujunìmenouc grˆfouc. Arqikˆ, dhmiourgeÐtai ènac kenìc grˆfoc: G = nx. Graph () Metˆ, eisˆgontai arqikˆ oi kìmboi, p.q., oi 5 kìmboi '1', '2', '3', '4' kai '5' mpaÐnoun wc ex c: G. add_nodes_from ([1,2,3,4,5]) Bèbaia, antÐ gia arijmoÔc, oi kìmboi mporoÔn na eisˆgontai wc onìmata   lèxeic, p.q.: G. add_nodes_from ([ 'John ', 'Mary ']) G. add_node (" London ") G. add_nodes_from ([ 'a','b','c','d','e']) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 8. Tèloc, eisˆgontai oi akmèc, p.q., gia touc 5 kìmbouc 1–5 na 6 sundèseic: G. add_edges_from ([(1,2),(1,4),(2,3),(3,4),(3,5),(4,5 )]) Oi lÐstec kai to pl joc twn eisaqjèntwn kìmbwn kai akm¸n dÐnontai apì tic entolèc: G. nodes () G. number_of_nodes () G. edges () G. number_of_edges () Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 9. Gia to sqediasmì tou eisaqjèntoc grˆfou, p.q, tou paradeÐgmatoc autoÔ tou grˆfou me 5 kìmbouc kai 6 akmèc, ekteloÔntai oi entolèc: plt . figure () nx. draw (G) plt . show ( block = False ) H teleutaÐa entol  dèqetai diˆforec paramètrouc, ìpwc, p.q., gia diaforetikì tÔpo sqedÐou (layout), mègejoc kai qr¸ma kìmbwn kai afaÐresh twn onomˆtwn (ids) twn kìmbwn: nx. draw_spring (G, node_size =100 , node_color ='# A0CBE2 ', with_labels = False ) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 10. ParaleÐpontac parametropoi seic, o sqediasmìc tou grˆfou tou paradeÐgmatoc dÐnei to sq ma: Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 11. Oi kìmboi mporoÔn na topotethjoÔn se stajerèc jèseic, efìson o grˆfoc eÐqe eisaqjeÐ me tic suntetagmènec touc, pou mporeÐ na gÐnei, p.q., sto parˆdeigma autì wc ex c: pos ={1:(0,0),2:(1,0),4:(0,1),3:(1,1),5:(0.5,2.0)} nx. draw (G, pos ) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 12. Eisagwg  Kateujunìmenwn Grˆfwn GÐnetai wc ex c, p.q., sto parˆdeigma autì: G=nx. DiGraph () G. add_nodes_from (["A","B","C","D"]) G. add_edges_from ([( "A","B"), ("C","A"), ("C","B"), ("B","D")]) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 13. Eisagwg  Grˆfwn me Bˆrh Akm¸n GÐnetai, p.q., sto parˆdeigma autì me tic entolèc: G=nx. Graph () G. add_weighted_edges_from ([( 'a','b',4),( 'a','c',8), ('a','d',5),( 'c','d',3)]) kai o sqediasmìc wc ex c: pos =nx. spring_layout (G) edge_labels = dict ([((u,v ,),d['weight ']) for u,v,d in G. edges ( data = True )]) nx. draw_networkx_nodes (G,pos , node_size = 700) nx. draw_networkx_edges (G, pos) nx. draw_networkx_labels (G,pos , font_size =20) nx. draw_networkx_edge_labels (G,pos , edge_labels = edge_labels , font_size =20) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 14. Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 15. AntÐ thc parˆjeshc twn tim¸n twn bar¸n, oi akmèc mporoÔn na sqediasjoÔn me eÔrh anˆloga twn tim¸n twn bar¸n wc ex c: nx. draw_networkx_nodes (G,pos , node_size = 700) edgewidth =[] for (u,v,d) in G. edges ( data = True ): edgewidth . append (d['weight ']) nx. draw_networkx_edges (G,pos , edge_color ='b', width = edgewidth ) nx. draw_networkx_labels (G,pos , font_size =20) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 16. Epiplèon, mporoÔn na krathjoÔn sto sqèdio mìno oi akmèc me bˆrh megalÔtera kˆpoiac endiˆmeshc tim c, p.q., 4: elarge = [(u,v) for (u,v,d) in G. edges ( data = True ) if d['weight '] >4] esmall = [(u,v) for (u,v,d) in G. edges ( data = True ) if d['weight '] <=4] nx. draw_networkx_edges (G,pos , edgelist = elarge , edge_color ='b',width = edgewidth ) nx. draw_networkx_edges (G,pos , edgelist = esmall , width =6, alpha =0.5, edge_color ='g',style ='dashed ') Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 17. Eisagwg  Dimer¸n Grˆfwn H eisagwg  dimer¸n grˆfwn gÐnetai ìpwc, p.q., sto parakˆtw parˆdeigma: from networkx . algorithms import bipartite G = nx. Graph () G. add_nodes_from ([1,2,3,4], bipartite =0) G. add_nodes_from ([ 'a','b','c'], bipartite =1) G. add_edges_from ([(1,'a'),(1,'b'),(2,'a'),(2,'b'), (2,'c'),(3,'c'),(4,'b'),(4,'c')]) pos ={1:(0,0), 2:(0,1), 3:(0,2), 4:(0,3), 'a':(1,0.5), 'b':(1,1.5), 'c':(1,2.5)} O èlegqoc an prìkeitai perÐ dimeroÔc grˆfou (True)   ìqi (False) gÐnetai me thn entol : print bipartite . is_bipartite (G) True Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 18. O sqediasmìc tou dimeroÔc grˆfou gÐnetai wc ex c: mode1 , mode2 = bipartite . sets (G) nx. draw_networkx_nodes (G,pos , nodelist = list ( mode1 ), node_color ='b',node_size = 700) nx. draw_networkx_nodes (G,pos , nodelist = list ( mode2 ), node_color ='g',node_size = 700) nx. draw_networkx_edges (G, pos) nx. draw_networkx_labels (G,pos , font_size =20 , font_color ='# FFFFFF ') plt . axis ('off ') Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 19. Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 20. Qarakthristikˆ (Attributes) Kìmbwn kai Akm¸n Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 21. TÔpoi Grˆfwn kai Sqesiak¸n Dedomènwn Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 22. Genn torec Prokataskeuasmènwn Grˆfwn Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 23. BajmoÐ Kìmbwn Pr¸ta, gia mh kateujunìmenouc grˆfouc: O grˆfoc G = (V, E) sto sÔnolo koruf¸n V eÐnai mh kateujunìmenoc, ìtan to sÔnolo twn akm¸n tou E eÐnai èna summetrikì uposÔnolo tou V V. GeÐtonec kìmbwn: Gia duo korufèc i, j 2 V tou G, h j lègetai geÐtonac thc i ìtan (i, j) 2 E. G. nodes () # List of nodes G. number_of_nodes () # Number of nodes len (G) # Number of nodes G. order () # Number of nodes G. number_of_edges () # Number of of edges G. neighbors (1) # Neighbors of node 1 ## To get the list of neighbors of all nodes : for node in G. nodes (): print node , G. neighbors ( node ) PÐnakac GeitnÐashc (Adjacency Matrix): EÐnai ènac (summetrikìc) pÐnakac A = fAgi,j2V tˆxhc jVj jVj tètoioc ¸ste A = 1, ìtan i, j geÐtonec, A = 0, diaforetikˆ. A = nx. to_numpy_matrix (G) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 24. BajmoÐ kìmbwn: Sto mh kateujunìmeno grˆfo G, o bajmìc miac koruf c i, pou sumbolÐzetai wc ki, orÐzetai san to pl joc twn geitìnwn tou i, dhlad , to pl joc twn sundèsewn pou prospÐptoun sto i. Profan¸c, isqÔei: ki = X j2V A = X i2V A ki, epiplèon, X i2V ki = X i,j2V A = 2jEj G. degree (1) # Degree of node 1 len (G. neighbors (1) # Degree of node 1 G. degree () # List of degrees of all nodes ## To get the table of degrees of all nodes : for node in G. nodes (): print node , G. degree ( node ) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 25. BajmoÐ Kìmbwn Kateujunìmenwn Grˆfwn 'Estw o kateujunìmenoc grˆfoc G = (V, E) sto sÔnolo koruf¸n V, ìpou t¸ra to sÔnolo twn akm¸n tou E eÐnai èna mh summetrikì uposÔnolo tou V V ki o antÐstoiqoc pÐnakac geitnÐashc A = fAg eÐnai mh summetrikìc. O bajmìc eisìdou thc koruf c i tou G, pou sumbolÐzetai wc kin i , orÐzetai san to pl joc twn sundèsewn pou xekinoÔn apì geÐtonec tou i kai kateujÔnontai proc ton i, dhlad , kin i = X j2V A O bajmìc exìdou thc koruf c i tou G, pou sumbolÐzetai wc kout i , orÐzetai san to pl joc twn sundèsewn pou xekinoÔn apì ton i kai kateujÔnontai proc geÐtonec tou i, dhlad , kout i = X i2V A Profan¸c, isqÔei: X i2V kin i = X j2V kout i = X i,j2V A = jEj G. in_degree (1) #In - Degree of node 1 G. out_degree (1) #Out - Degree of node 1 G. in_degree () # List of in - degrees of all nodes G. out_degree () # List of out - degrees of all nodes ## To get the table of in - degrees and out - degrees of all nodes : for node in G. nodes (): print node , G. in_degree ( node ), G. out_degree ( node ) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 26. Katanomèc Bajm¸n Istìgramma katanom c bajm¸n (gia mh kateujunìmeno grˆfo): degrees = G. degree () values = sorted ( set ( degrees . values ())) hist = [ degrees . values (). count (x) for x in values ] fig = plt . figure () ax = fig . add_subplot ( 111 ) plt . plot ( values ,hist ,'ro -') #ax . set_xscale (' log ') # Logarithmic x scale #ax . set_yscale (' log ') # Logarithmic y scale plt . title ( Degree Distribution ) plt . xlabel ('Degree ') plt . ylabel ('Number of nodes ') Istìgramma katanom c bajm¸n (gia kateujunìmeno grˆfo): ## in_degrees = G. in_degree () in_values = sorted ( set ( in_degrees . values ())) in_hist = [ in_degrees . values (). count (x) for x in in_values ] out_degrees = G. out_degree () out_values = sorted ( set ( out_degrees . values ())) out_hist = [ out_degrees . values (). count (x) for x in out_values ] fig = plt . figure () ax = fig . add_subplot ( 111 ) plt . plot ( in_values , in_hist ,'ro -') plt . plot ( out_values , out_hist ,'bv -') #ax . set_xscale (' log ') # Logarithmic x scale #ax . set_yscale (' log ') # Logarithmic y scale plt . titke (In - Degree Out - Degree Distributions ) plt . xlabel ('Degree ') plt . ylabel ('Number of nodes ') Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 27. Parˆdeigma: To dÐktuo karˆte G = nx. karate_club_graph () Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 28. Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 29. Kentrikìthtec Kìmbwn: 1. Kentrikìthta BajmoÔ (Degree Centrality) Oi orismoÐ OLWN twn kentrikìthtwn pou ja d¸soume ed¸ kai sth sunèqeia aforoÔn mh kateujunìmenouc (aploÔc) grˆfouc. H kentrikìthta bajmoÔ (degree centrality) xi tou kìmbou i isoÔtai proc ton bajmì ki tou kìmbou autoÔ: xi = ki x8 = 5 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 30. 2. Kentrikìthta Endiamesìthtac (Betweenness Centrality) H kentrikìthta endiamesìthtac (betweenness centrality) xi tou kìmbou i isoÔtai proc: xi = X s6=i6=t2V nist gst ìpou nist eÐnai to pl joc twn gewdaitik¸n diadrom¸n metaxÔ twn kìmbwn s kai t, pou pernoÔn apì ton kìmbo i, kai gst eÐnai to sunolikì pl joc twn gewdaitik¸n diadrom¸n metaxÔ twn kìmbwn s kai t. n23 ,23 = 2 g3,23 = 4 x2 = 0.1436 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 31. 3. Kentrikìthta EggÔthtac (Closeness Centrality) H kentrikìthta eggÔthtac (closeness centrality) xi tou kìmbou i isoÔtai proc: xi = n P j2V d ìpou n eÐnai to pl joc twn kìmbwn tou grˆfou kai d h gewdaitik  apìstash apì ton kìmbo i proc opoiod pote ˆllo kìmbo j. x0 = 0.5689 x2 = 0.5593 x33 = 0.55 x31 = 0.5409 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 32. 4. Kentrikìthta IdiodianÔsmatoc (Eigenvector Centrality) H kentrikìthta idiodianÔsmatoc (eigenvector centrality) xi tou kìmbou i isoÔtai proc: xi = 1 1 X j2V Axj ìpou A eÐnai o pÐnakac geitnÐashc (adjacency matrix) tou grˆfou kai xi eÐnai oi sunist¸sec tou idiadianÔsmatoc tou A, pou antistoiqoÔn sth megalÔterh idiotim  tou 1. x33 = 0.3734 x0 = 0.3555 x2 = 0.3172 x32 = 0.3086 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 33. Upologismìc Kentrikìthtwn Entolèc NetworkX: nx. degree_centrality (G) nx. betweenness_centrality (G) nx. closeness_centrality (G) nx. eigenvector_centrality (G) EktÔpwsh se pÐnaka: centralities = [] centralities . append (nx. degree_centrality (G )); centralities . append (nx. betweenness_centrality (G )); centralities . append (nx. closeness_centrality (G)); centralities . append (nx. eigenvector_centrality (G )); for node in G. nodes_iter (): measures = (t). join ( map ( lambda f: str(f[ node ]), centralities )) print %s %s % (node , measures ) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 34. Parˆdeigma: To dÐktuo Karˆte G = nx. karate_club_graph () Nodes Degree C. Betweenness C. Closeness C. Eigenvector C. 0 0.484848485 0.437635281 0.568965517 0.355490721 1 0.272727273 0.053936688 0.485294118 0.265959605 2 0.303030303 0.143656806 0.559322034 0.317192417 3 0.181818182 0.011909271 0.464788732 0.211179694 4 0.090909091 0.000631313 0.379310345 0.075968879 5 0.121212121 0.029987374 0.38372093 0.079483113 6 0.121212121 0.029987374 0.38372093 0.079483113 7 0.121212121 0 0.44 0.170959892 8 0.151515152 0.055926828 0.515625 0.227404355 9 0.060606061 0.000847763 0.434210526 0.102674504 10 0.090909091 0.000631313 0.379310345 0.075968879 11 0.03030303 0 0.366666667 0.052855817 12 0.060606061 0 0.370786517 0.084254727 13 0.151515152 0.045863396 0.515625 0.226473112 14 0.060606061 0 0.370786517 0.10140365 15 0.060606061 0 0.370786517 0.10140365 16 0.060606061 0 0.284482759 0.023635566 17 0.060606061 0 0.375 0.092399699 18 0.060606061 0 0.370786517 0.10140365 19 0.090909091 0.032475048 0.5 0.147912918 20 0.060606061 0 0.370786517 0.10140365 21 0.060606061 0 0.375 0.092399699 22 0.060606061 0 0.370786517 0.10140365 23 0.151515152 0.017613636 0.392857143 0.150118912 24 0.090909091 0.002209596 0.375 0.057052326 25 0.090909091 0.003840488 0.375 0.059206342 26 0.060606061 0 0.362637363 0.075579616 27 0.121212121 0.022333454 0.458333333 0.133477386 28 0.090909091 0.001794733 0.452054795 0.131077964 29 0.121212121 0.002922078 0.38372093 0.134961122 30 0.121212121 0.014411977 0.458333333 0.174758637 31 0.181818182 0.138275613 0.540983607 0.191034394 32 0.363636364 0.145247114 0.515625 0.308643749 33 0.515151515 0.304074976 0.55 0.373362539 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 35. Kentrikìthta Mikr  tim  Megˆlh tim  Degree LÐgoi geÐtonec (sundèseic) PolloÐ geÐtonec (sundèseic) Betweenness Mikrìc èlegqoc ro c Megˆloc èlegqoc ro c Closeness Proc thn perifèreia Proc to kèntro Eigenvector LÐgoi   lÐgo shmantikoÐ geÐtonec PolloÐ   polÔ shmantikoÐ geÐtonec To dÐktuo twn stratiwtik¸n tou David Krackhardt: Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 36. Suntelest c Suss¸reushc O suntelest c suss¸reushc (clustering coefficient) Ci tou kìmbou i orÐzetai wc: Ci = 2i ki(ki 1) , ìpou i eÐnai to pl joc twn sundèsewn metaxÔ twn geitonik¸n kìmbwn tou i kai d i proc opoiod pote ˆllo kìmbo ki eÐnai to pl joc twn geitonik¸n kìmbwn tou i. nx. clustering (G) # Clustering coefficients of all nodes nx. clustering (G,n) # Clustering coefficient of node n C23 = 2 4 5 4 = 0.4 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 37. Diktuak  Metabatikìthta O sunolikìc suntelest c suss¸reushc (global clustering coefficient) (ìlou) tou grˆfou G orÐzetai wc h mèsh tim  twn suntelest¸n suss¸reushc twn kìmbwn tou: C(G) = 1 jVj X i Ci H metabatikìthta (transitivity) tou grˆfou G orÐzetai wc to phlÐko: T(G) = pl joc trig¸nwn pl joc sundedemènwn triˆdwn nx. average_clustering (G) # Graph clustering coefficient nx. transitivity (G) # Graph transitivity C(G) = 0.16 T(G) = 0.19 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 38. Amoibaiìthta Sundèsewn se Kateujunìmeno Grˆfo Se ènan kateujunìmeno grˆfo, o suntelest c amoibaiìthtac sundèsewn/desm¸n (link/tie mutuality coefficient) orÐzetai wc ex c: M(G) = pl joc antapodidìmenwn sundèsewn pl joc ìlwn twn sundèsewn/tìxwn G. to_undirected ( True ). size () # Reciprocated edges G. number_of_edges () # Total edges Er(G) = 64 E(G) = 195 M(G) = 0.3282 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 39. Apostˆseic Se ènan grˆfo G, gia kˆje duo kìmbouc i, j, h (gewdaitik ) apìstas  touc d(i, j) orÐzetai wc to m koc thc suntomìterhc diadrom c apì to i sto j, efìson oi kìmboi autoÐ eÐnai sundedemènoi, en¸ d(i, j) = 1, diaforetikˆ (kai fusikˆ, d(i, i) = 0). (H ‘‘suntomìterh diadrom ’’ metaxÔ duo kìmbwn eÐnai h diadrom  pou èqei to elˆqisto m koc anˆmesa se ìlec tic diadromèc metaxÔ twn duo kìmbwn.) To mèso m koc suntomìterhc diadrom c (average shortest path length) orÐzetai wc ex c: a = 1 jVj(jVj 1) X i,j2V d(i, j) a = nx. average_shortest_path_length (G) a = 2.4082 Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 40. Mètra Diˆforwn Empeirik¸n DiktÔwn TABLE I. The general characteristics of several real networks. For each network we indicated the number of nodes, the average degree !k, the average path length ! and the clustering coefficient C. For a comparison we have included the average path length !rand and clustering coefficient Crand of a random graph with the same size and average degree. The last column identifies the symbols in Figs. 8 and 9. Network Size !k ! !rand C Crand Reference Nr. WWW, site level, undir. 153, 127 35.21 3.1 3.35 0.1078 0.00023 Adamic 1999 Internet, domain level 3015 - 6209 3.52 - 4.11 3.7 - 3.76 6.36 - 6.18 0.18 - 0.3 0.001 Yook et al. 2001a, Pastor-Satorras et al. 2001 Movie actors 225, 226 61 3.65 2.99 0.79 0.00027 Watts, Strogatz 1998 LANL coauthorship 52, 909 9.7 5.9 4.79 0.43 1.8 × 10−4 Newman 2001a,b MEDLINE coauthorship 1, 520, 251 18.1 4.6 4.91 0.066 1.1 × 10−5 Newman 2001a,b SPIRES coauthorship 56, 627 173 4.0 2.12 0.726 0.003 Newman 2001a,b,c NCSTRL coauthorship 11, 994 3.59 9.7 7.34 0.496 3 × 10−4 Newman 2001a,b Math coauthorship 70, 975 3.9 9.5 8.2 0.59 5.4 × 10−5 Barab´asi et al. 2001 Neurosci. coauthorship 209, 293 11.5 6 5.01 0.76 5.5 × 10−5 Barab´asi et al. 2001 E. coli, substrate graph 282 7.35 2.9 3.04 0.32 0.026 Wagner, Fell 2000 10 E. coli, reaction graph 315 28.3 2.62 1.98 0.59 0.09 Wagner, Fell 2000 11 Ythan estuary food web 134 8.7 2.43 2.26 0.22 0.06 Montoya, Sol´e 2000 12 Silwood park food web 154 4.75 3.40 3.23 0.15 0.03 Montoya, Sol´e 2000 13 Words, cooccurence 460.902 70.13 2.67 3.03 0.437 0.0001 Cancho, Sol´e 2001 14 Words, synonyms 22, 311 13.48 4.5 3.84 0.7 0.0006 Yook et al. 2001 15 Power grid 4, 941 2.67 18.7 12.4 0.08 0.005 Watts, Strogatz 1998 16 C. Elegans 282 14 2.65 2.25 0.28 0.05 Watts, Strogatz 1998 17 TABLE II. The scaling exponents characterizing the degree distribution of several scale-free networks, for which P(k) follows a power-law (2). We indicate the size of the network, its average degree !k and the cutoff for the power-law scaling. For directed networks we list separately the indegree (#in) and outdegree (#out) exponents, while for the undirected networks, marked with a star, these values are identical. The columns lreal , lrand and lpow compare the average path length of real networks with power-law degree distribution and the prediction of random graph theory (17) and that of Newman, Strogatz and Watts (2000) (62), as discussed in Sect. V. The last column identifies the symbols in Figs. 8 and 9. Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 41. Sunist¸sec kai KlÐkec Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 42. Sunist¸sec kai KlÐkec Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 43. Blockmodeling Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 44. Koinìthtec (Communities) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 45. Anameiximìthta (Assortativity) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 46. Epirro  kai Diˆqush Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 47. DÐktua Mikr¸n Kìsmwn (Small–Worlds) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python
  • 48. DÐktua QwrÐc KlÐmaka (Scale–Free) Mwus c A. MpountourÐdhc Anˆlush DiktÔwn me to NetworkX thc Python