-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomwalkR.py
99 lines (64 loc) · 2.43 KB
/
RandomwalkR.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
import networkx as nx
import pdb as pd
import numpy as np
#Importing the edgelist
fh=open('karate.edgelist','rb')
g=nx.read_edgelist(fh)
print g
fh.close()
#Building the adjacency matrix
'''a=nx.adjacency_matrix(g)'''
a=np.array([[1,1,1,0,0],[1,1,1,0,0],[1,1,1,0,0],[0,0,0,1,1],[0,0,0,1,1]])
# this function is for random walk with Resistance and passing the a numpy array to it
def rws(a, epsilon=None, beta=None, stop_value=None):
nodes = a.shape[0]
if not stop_value:
stop_value = 1.0 / np.mean(np.sum(a, axis=1))
if not beta:
beta = 1.0 / np.mean(np.sum(a, axis=1)) / nodes
if not epsilon:
epsilon = 1.0 / np.sum(np.sum(a, axis=1)) / np.mean(np.sum(a, axis=1))
m,n = a.shape
# a=a.toarray()
d=np.eye(m)
aa = np.maximum(a,d)
bb = np.sum(aa, axis=1)
bb = 1.0 / bb
cc = np.outer(np.ones((m,1)),bb) * aa
cc = cc.T
cc[ np.isnan(cc) ] = 0
current = np.eye(m)
newcurrent = current.copy()
active = np.ones((m,1))
step = 0
improve_count = 0
active_nodes = 0
improve = np.ones((n,1))
while np.sum(active) > 0.0:
print(step, np.sum(active), improve_count)
active_nodes = np.sum(active)
current = newcurrent
curr_act = np.dot(active, np.ones((1,m)))*current
curr_stop=(np.dot(active,np.ones((1,m)))-1)*current
sel = (((np.dot(current,cc)>0).astype(np.int)-(current>0).astype(np.int))==1).astype(np.int)
temp = np.dot(curr_act,cc)-(sel*( np.dot((curr_act>0).astype(np.int),aa) )*beta)
temp = temp*(temp>0).astype(np.int)
newcurrent = temp
newcurrent=temp;
newcurrent=newcurrent*(newcurrent>(beta*cc)).astype(np.int)
newcurrent=newcurrent-np.dot((current>0).astype(int),aa)*epsilon
newcurrent=newcurrent*(newcurrent>0).astype(np.int)
newcurrent=newcurrent-curr_stop;
newcurrent=np.outer((1/np.sum(newcurrent.T, axis=0)).T,np.ones((1,nodes)))*newcurrent #normalize
improve=np.sum(np.abs(current-newcurrent), axis=1)
active=np.reshape( (improve>stop_value).astype(np.int), active.shape )
if active_nodes == np.sum(active):
improve_count=improve_count+1
else:
improve_count=0
step=step+1
if improve_count>1:
break
return current
# This returns the resistance matrix
resistancematrix=rws(a)