Created
April 11, 2014 18:18
-
-
Save robinfang/10489498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
import networkx as nx | |
from independent_cascade import * | |
from collections import defaultdict | |
import random | |
def countDegree(input): | |
result = defaultdict(int) | |
for i in input: | |
result[i] = result[i]+1 | |
f = open("degree.csv","w") | |
for k, v in result.items(): | |
f.write("%s,%s\n" % (k,v)) | |
f.close() | |
if __name__ == "__main__": | |
all = [] | |
for i in range(0,5): | |
# g = nx.barabasi_albert_graph(5000,2) | |
g = nx.watts_strogatz_graph(5000,10,0.3) | |
# nx.write_gml(g,"ba.gml") | |
nx.write_gml(g,"ws.gml") | |
for e in g.edges(): | |
g[e[0]][e[1]]['act_prob'] = 0.6 | |
degree = list(g.degree().values()) | |
degree.sort() | |
# countDegree(degree) | |
seeds = [] | |
seeds.append(random.randint(0,999)) | |
seeds.append(random.randint(0,999)) | |
result = independent_cascade(g,seeds,5000) | |
nodes_of_level = [] | |
for j in result: | |
nodes_of_level.append(len(j)) | |
all.append(nodes_of_level) | |
sum = defaultdict(int) | |
for i in all: | |
for j in i: | |
idx = i.index(j) | |
sum[idx] = sum[idx]+j | |
f = open("levelnodes2.csv","w") | |
for k,v in sum.items(): | |
f.write("%s,%s\n" % (k,v/len(all))) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment