3
Communities in Zachary's Karate Club graph.
5
Double-click to expand and contract communities.
8
http://vlado.fmf.uni-lj.si/pub/networks/data/Ucinet/UciData.htm
12
An information flow model for conflict and fission in small groups.
13
Journal of Anthropological Research, 33, 452-473.
16
__author__ = """Aric Hagberg (hagberg@lanl.gov)\n Katy Bold (kbold@princeton.edu"""
17
# Copyright (C) 2008 by
18
# Aric Hagberg <hagberg@lanl.gov>
19
# Dan Schult <dschult@colgate.edu>
20
# Pieter Swart <swart@lanl.gov>
21
# Distributed under the terms of the GNU Lesser General Public License
22
# http://www.gnu.org/copyleft/lesser.html
26
def karate_graph(create_using=None, **kwds):
27
from networkx.generators.classic import empty_graph
29
G=empty_graph(34,create_using=create_using,**kwds)
30
G.name="Zachary's Karate Club"
33
0 1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0
34
1 0 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0
35
1 1 0 1 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0
36
1 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
37
1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
38
1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
39
1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
40
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
41
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
42
0 0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 0 1 0 1 0 1 0 0 1 1
57
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0
58
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0
59
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
60
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1
61
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
62
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1
63
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
64
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1
65
0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0 0 0 0 1 1 1 0 1
66
0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0"""
70
for line in string.split(zacharydat,'\n'):
71
thisrow=map(int,string.split(line,' '))
72
for col in range(0,len(thisrow)):
74
G.add_edge(row,col) # col goes from 0,33
78
def contract_community(G,K,community):
79
G.add_node(community['label'],color=community['color'],size='2.0')
80
for node in community['nodes']:
81
for nbr in G.neighbors(node):
82
if nbr not in community['nodes']:
83
G.add_edge(community['label'],nbr)
87
def expand_community(G,K,community):
88
G.delete_node(community['label'])
89
for node in community['nodes']:
90
G.add_node(node,color=community['color'])
91
for node in community['nodes']:
92
for nbr in K.neighbors(node):
96
G.add_edge(node,node_map[nbr])
99
def get_communities():
100
# assign communities and some properties
106
communities['c1']['nodes']=[0, 1, 2, 3, 7, 11, 12, 13, 17, 19, 21]
107
communities['c2']['nodes']=[4, 5, 6, 10, 16]
108
communities['c3']['nodes']=[8, 9, 14, 15, 18, 20, 22, 26, 29, 30, 32, 33]
109
communities['c4']['nodes']=[23, 24, 25, 27, 28, 31]
110
communities['c1']['color']='#ff0000'
111
communities['c2']['color']='#00ff00'
112
communities['c3']['color']='#0000ff'
113
communities['c4']['color']='#afafaf'
114
communities['c1']['label']='c1'
115
communities['c2']['label']='c2'
116
communities['c3']['label']='c3'
117
communities['c4']['label']='c4'
120
for label,c in communities.items():
123
return communities,node_map
126
def vertex_callback(id):
131
contract_community(G,K,communities[node_map[n]])
132
elif n in communities:
133
expand_community(G,K,communities[n])
140
if __name__ == "__main__":
142
import networkx as nx
146
communities,node_map=get_communities()
147
for key,c in communities.items():
148
G.set_node_attr(c['nodes'],color=c['color'])
151
print "Double-click to expand and contract communities."
154
myPort = random.randint(20739,20999)
155
# Set up a callback for left double-clicks on vertices.
156
G.ubigraph.set_vertex_style_attribute(0, "callback_left_doubleclick",
157
"http://127.0.0.1:" + str(myPort) + "/vertex_callback")
159
# Now make an XMLRPC server to handle tha callbacks.
160
from SimpleXMLRPCServer import SimpleXMLRPCServer
162
server = SimpleXMLRPCServer(("localhost", myPort))
163
server.register_introspection_functions()
164
server.register_function(vertex_callback)
165
# Run the server's main loop
166
print "Listening for callbacks from ubigraph_server on port " + str(myPort)
167
server.serve_forever()