-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
55 lines (45 loc) · 1.09 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import gv
from pygraph.classes.graph import graph
from pygraph.readwrite.dot import write
from pygraph.algorithms.accessibility import connected_components
def h1(x):
return 5*(x + 5) % 11;
def h2(x):
return 7*(x + 3) % 19;
gr = graph()
U = []
W = []
E = []
for i in range(30):
x = 'u' + str(h1(i))
y = 'w' + str(h2(i))
U.append(x)
W.append(y)
E.append((x,y))
gr.add_nodes(list(set(U)))
gr.add_nodes(list(set(W)))
for edge in E:
gr.add_edge(edge)
inv_map = {}
for k, v in connected_components(gr).iteritems():
inv_map[v] = inv_map.get(v, [])
inv_map[v].append(k)
nodemapping = {}
newmap = {}
for k in inv_map.keys():
C = min(map(lambda x : x[1:], inv_map[k]))
for v in inv_map[k]:
nodemapping[v] = v + '__' + str(C)
newgraph = graph()
newgraph.add_nodes(map(lambda x : nodemapping[x], gr.nodes()))
for edge in E:
Q = (nodemapping[edge[0]], nodemapping[edge[1]])
newgraph.add_edge(Q)
#for edge in E:
# print E
# newgraph.add_edge((nodemapping[edge[0]], nodemapping[edge[1]]))
dot = write(newgraph)
gvv = gv.readstring(dot)
gv.layout(gvv, 'dot')
gv.render(gvv, 'png', 'x.png')