/
traceplot copy.py
129 lines (112 loc) · 2.77 KB
/
traceplot copy.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
import networkx as nx
import random
import numpy as np
from random import choice, random
import collections
import matplotlib.pyplot as plt
def CalculateCMu(X,G,C):
sum=0
div=0
for i in range(0,len(X)):
sum=sum+(float(C[X[i]])/G.degree(X[i]))
div=div+(float(1)/G.degree(X[i]))
return float(sum)/div
def runSRW(G,C):
T=[]
for i in range(0,10):
X=[]
startNode = G.nodes()[0]
currentNode = startNode
X.append(startNode)
run=True
while (run is True):
nextNode=choice(G.neighbors(currentNode))
currentNode=nextNode
X.append(currentNode)
if C[currentNode]> 4:
run=False
T.append(len(X))
return np.mean(T)
def makeChoice(neighbors):
communityArray=[]
for neighbor in neighbors:
communityArray.append(C[neighbor])
cnt = collections.Counter()
for word in communityArray:
cnt[word] += 1
chosen=choice(list(cnt))
chosenArray=[]
for neighbor in neighbors:
if C[neighbor] is chosen:
chosenArray.append(neighbor)
chosenNode=choice(chosenArray)
return chosenNode
def PurposePro(fromNode,toNode,C,G):
toNodeCom=C[toNode]
neighbors=G.neighbors(fromNode)
communityArray=[]
for neighbor in neighbors:
communityArray.append(C[neighbor])
cnt = collections.Counter()
for word in communityArray:
cnt[word] += 1
firstProp=float(1)/len(list(cnt))
secondProp=float(1)/cnt[toNodeCom]
finalProp=firstProp*secondProp
return finalProp
def runRW(G,t,C):
X=[]
startNode = G.nodes()[0]
currentNode = startNode
X.append(startNode)
# # walk start
for j in range(1,t):
nextNode=makeChoice(G.neighbors(currentNode))
PurposePro(currentNode,nextNode,C,G)
a = float(G.degree(nextNode)*PurposePro(nextNode,currentNode,C,G))/ float(G.degree(currentNode)*PurposePro(currentNode,nextNode,C,G))
r = np.random.uniform(0, 1)
if r<a:
currentNode=nextNode
X.append(currentNode)
return X
def addEdges(G,start1,end1,start2,end2,number):
for i in range(0,number):
a=choice(range(start1, end1))
b=choice(range(start2, end2))
G.add_edge(a,b)
print [a,b]
return G
if __name__ == "__main__":
Nvalue = []
ResultN =[]
ResultS =[]
for n in range(1,11):
Nvalue.append(5*50*n)
C=[None]*(5*50*n)
for i in range(0,50*n):
C[i]=1
for i in range(50*n,100*n):
C[i]=2
for i in range(100*n,150*n):
C[i]=3
for i in range(150*n,200*n):
C[i]=4
for i in range(200*n,250*n):
C[i]=5
G = nx.connected_caveman_graph(5,50*n)
G.remove_edge(0,5*50*n-1)
G=addEdges(G,1,50*n-1,50*n+1,100*n-1,3)
G=addEdges(G,50*n+1,100*n-1,100*n+1,150*n-1,3)
G=addEdges(G,100*n+1,150*n-1,150*n+1,200*n-1,3)
G=addEdges(G,150*n+1,200*n-1,200*n+1,250*n-1,3)
TS=runSRW(G,C)
plt.plot(S,'b')
print 'RW run to'
S2=[]
X2=runRW(G,40000,C)
for key in X2:
S2.append(C[key])
plt.plot(S2,'r')
plt.ylabel('C(x)')
plt.xlabel('blue line: SRW, red line: new RW')
plt.show()