/
K-Walk-Main.py
201 lines (167 loc) · 5.24 KB
/
K-Walk-Main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import networkx as nx
import matplotlib.pyplot as plt
import random
import AtoB
from mayavi import mlab
import numpy
def Kwalk(G,node,length,numOfRandomWalks):
"""
Syntax : Kwalk(G,node,length,numOfRandomWalks)
where :
G is Graph
Node denotes 'from Node' to start a random walk
length denotes k_walk_length
numOfRandomWalks denotes the number of randomwalks from a node
Flagger is a Global Dictionary containing the information about the flags associated with each node.
"""
global Flagger
while numOfRandomWalks > 0 :
temp_node = node
temp_length = length
while temp_length > 0 :
neighbors = G.neighbors(temp_node)
choice = random.choice(neighbors)
temp_length = temp_length - 1
temp_node = choice
try :
Flagger[choice] +=1
except KeyError :
Flagger[choice] = 1
numOfRandomWalks-=1
def Correctness_Of_DominatingSet(G,length,NumOfRandomWalk,DominatingSet):
for node in G.nodes():
probability = Probality_Of_Hiting_A_Dominating_Set(G,node,length,NumOfRandomWalk,DominatingSet)
if ( probability < 0.4 ) :
return False
return True
def Probality_Of_Hiting_A_Dominating_Set(G,node,length,numOfRandomWalks,DominatingSet):
NoOfHits = 0
temp_numOfRandomWalks = numOfRandomWalks
while numOfRandomWalks > 0 :
temp_node = node
if node in DominatingSet :
NoOfHits +=1
else :
temp_length = length
while temp_length > 0 :
neighbors = G.neighbors(temp_node)
choice = random.choice(neighbors)
temp_length = temp_length - 1
temp_node = choice
if choice in DominatingSet :
NoOfHits +=1
break
numOfRandomWalks-=1
numOfRandomWalks = temp_numOfRandomWalks
probability = float(NoOfHits)/numOfRandomWalks
return probability
def Plot_Graph(G,highlight_nodes):
"""
Syntax : Plot_Graph(G,highlight_nodes)
where :
G is Graph.
highlight_nodes is set of nodes which needs to be highlighted.
"""
pos=nx.spring_layout(G) # positions for all nodes
nx.draw_networkx_nodes(G,pos,
nodelist=G.nodes(), #[i[1] for i in Flag_Node_Pair[lenght_set:]],
node_color='r',
node_size=50,
alpha=0.8)
nx.draw_networkx_nodes(G,pos,
nodelist=highlight_nodes, #[i[1] for i in Flag_Node_Pair[:lenght_set]],
node_color='b',
node_size=50,
alpha=0.8)
labels={}
for i in G.nodes():
labels[i] = str(i)
nx.draw_networkx_labels(G,pos,labels,font_size=8)
nx.draw_networkx_edges(G,pos,width=0.5,alpha=0.5)
plt.show()
#m*n denotes the grid size
m=10
n=10
#numOfRandomWalks denotes the number of randomwalks from a node
numOfRandomWalks = 200000
#Flagger is a dictionary denotes the no of flags for each node in the Graph
Flagger = {}
#k_walk_length denotes the lenght of the walk you want to take from each node.
k_walk_length = 20
#Length of the Dominating Set
lenght_set = 4
#Dominating Set
DominatingSet = []
#Grid Network
G=nx.grid_2d_graph(m,n)
#From each node take a random walk of specified k_walk_length,numOfRandomWalks
for node in G.nodes():
#Function call Kwalk(G,node,k_walk_length,numOfRandomWalks)
Kwalk(G,node,k_walk_length,numOfRandomWalks)
# Flag_Node_Pair = [ ( Number of Flags,Node ) , ( , ) ..............]
Flag_Node_Pair = Flagger.items()
total=sum(Flagger.values())
s =[]
for i in range(m):
x = []
for j in range(n):
x.append(Flagger[(i,j)]*600/float(total))
s.append(x)
m = numpy.array([i for i in s ])
mlab.barchart(m)
mlab.show()
"""
for i in range(len(Flag_Node_Pair)):
Flag_Node_Pair[i] = (Flag_Node_Pair[i][1],Flag_Node_Pair[i][0])
#Sorting the Flag_Node_Pair, to find the nodes which have most frequently visited !
Flag_Node_Pair.sort()
Flag_Node_Pair.reverse()
for length_set in range(1,len(Flag_Node_Pair)):
DominatingSet = [i[1] for i in Flag_Node_Pair[:length_set]]
if Correctness_Of_DominatingSet(G,k_walk_length,numOfRandomWalks,DominatingSet) :
break
Plot_Graph(G,DominatingSet)
x = input("tell me something : ")
#-------------------------------------------------------------------------------------------------------------------------------------
#NodePairs in G
Nodes = G.nodes()
NodePairs = []
length_set = 10
for i in range(len(Nodes)) :
for j in range(i+1,len(Nodes)):
NodePairs.append((Nodes[i],Nodes[j]))
#Flagger in intersection centrality
Flagger = {}
length = len(NodePairs)
print "hello"
count = 0
for i in NodePairs:
count+=1
pathA = []
pathB = []
print "Completed", float(count)/length
hit = AtoB.findHit(G,i[0],i[1], pathA, pathB)
try :
Flagger[hit] +=1
except KeyError :
Flagger[hit] = 0
# Flag_Node_Pair = [ ( Number of Flags,Node ) , ( , ) ..............]
Flag_Node_Pair = Flagger.items()
for i in range(len(Flag_Node_Pair)):
Flag_Node_Pair[i] = (Flag_Node_Pair[i][1],Flag_Node_Pair[i][0])
Flag_Node_Pair.sort()
Flag_Node_Pair.reverse()
#highlight_nodes = [i[1] for i in Flag_Node_Pair[:length_set]]
#Plot_Graph(G,highlight_nodes)
total=sum(Flagger.values())
s =[]
for i in range(m):
x = []
for j in range(n):
x.append(Flagger[(i,j)]/float(20))
s.append(x)
m = numpy.array([i for i in s ])
mlab.barchart(m)
mlab.show()
#-----------------------------------------------------------------------------------------------------------------------------------
"""