def top(graph,k,centrality,confidence): if centrality == "b": b = betweenness(graph) elif centrality == "e": b = eigenvector(graph,float(confidence)) top = [] top_values = [] for i in b.keys(): added = 0 for j in range(min(len(top),int(k))): if b[top[j]] < b[i]: top.insert(j,i) added = 1 break if added == 0: top.append(i) if len(top) > int(k): top.pop() for i in range(len(top)): top_values.append(b[top[i]]) return top, top_values
def top(graph, k, centrality, confidence): if centrality == "b": b = betweenness(graph) elif centrality == "e": b = eigenvector(graph, float(confidence)) top = [] top_values = [] for i in b.keys(): added = 0 for j in range(min(len(top), int(k))): if b[top[j]] < b[i]: top.insert(j, i) added = 1 break if added == 0: top.append(i) if len(top) > int(k): top.pop() for i in range(len(top)): top_values.append(b[top[i]]) return top, top_values
print '# Page Rank execution...' pagerank, iterations, err = pr(g, alpha=1.0e-5, eps=1.0e-3) print '#', iterations, ' iterations. Error:', err top_pr = [a for a, b in topk(pagerank, seed)] # In[6]: print '# Eigenvector Centrality...', cscores, diffsum = ec(g) top_eigenc = [a for a, b in topk(cscores, seed)] print '# Done' # In[7]: print '# Betweennes centrality...', bet = betweenness(g) top_bet = [a for a, b in topk(bet, seed)] print '# Done' sys.stdout.flush() # ## Execute Independent Cascade Model # In[8]: seed = 100 max_pr_ad = independent_cascade(g, top_pr[:seed], 'Page Rank') max_eigenc_ad = independent_cascade(g, top_eigenc[:seed], 'Eigenvector') max_bet_ad = independent_cascade(g, top_bet[:seed], 'Betweenness') pr_max_seed = seed eigenc_max_seed = seed
print '#',iterations, ' iterations. Error:', err top_pr = [a for a,b in topk(pagerank, nodes)] # In[9]: print '# Eigenvector Centrality...', cscores, diffsum = ec(g) top_eigenc = [a for a, b in topk(cscores, nodes)] print '# Done' # In[10]: print '# Betweennes centrality...', bet = betweenness(g) top_bet = [a for a, b in topk(bet, nodes)] print '# Done' sys.stdout.flush() # ## Execute Independent Cascade Model seed = [i for i in xrange(200, nodes - 100, 200)] print '# Page Rank\tEigenvector\tBetweenness' i = 0 while True: max_pr_ad = independent_cascade(g, top_pr[:seed[i]], 'Page Rank') max_eigenc_ad = independent_cascade(g, top_eigenc[:seed[i]], 'Eigenvector') max_bet_ad = independent_cascade(g, top_bet[:seed[i]], 'Betweenness')
#!/usr/bin/python import sys from lesson3 import readGraph, count2Paths, diameter from lesson4 import betweenness, cb_max, topk simple_graph = {} simple_graph[0] = {1} simple_graph[1] = {0, 2} simple_graph[2] = {1, 3} simple_graph[3] = {2, 4} simple_graph[4] = {3} cb = betweenness(simple_graph, True) # graph = readGraph(sys.argv[1]) # cb = betweenness(graph) k = 10 print 'Top ', k, '=>', topk(cb, k)
#!/usr/bin/python import sys from lesson1 import randomGraph from lesson4 import betweenness from lesson5 import eigenvector_centrality # nodes = int(sys.argv[1]) #number of nodes # sep = float(sys.argv[2]) #experiments for p = sep, 2sep, 3sep, ..., 1 # graph = randomGraph(nodes,sep) simple_graph = {} simple_graph[0] = {1} simple_graph[1] = {0, 2} simple_graph[2] = {1, 3} simple_graph[3] = {2, 4} simple_graph[4] = {3} print betweenness(simple_graph) print eigenvector_centrality(simple_graph)