satyaki30

100 trials

Apr 18th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Apr 13 18:38:04 2016
  4.  
  5. @author: Dyuti
  6. """
  7. # independent cascade model
  8.  
  9. from __future__ import division
  10. import networkx as nx
  11. from collections import deque
  12. import random
  13. import operator
  14. from copy import deepcopy
  15. import numpy as np
  16.  
  17. import sys
  18.  
  19. WHITE = 0
  20. GRAY = 1
  21. BLACK = 2
  22.  
  23. #======================================================================
  24. def read_graph(filename):
  25.     G = nx.DiGraph()
  26.     with open(filename, 'r') as f:
  27.         for line in f.readlines():
  28.             u, v = map(int, line.split())
  29.             G.add_edge(u, v)
  30.             G.add_edge(v, u)
  31.     return G
  32.    
  33.    
  34. #======================================================================
  35.    
  36.    
  37. def createw(G):
  38.    
  39.     "this function creates the edge weight of all edges in the edgelist"
  40.     rank = list()
  41.     weight = list()
  42.     ex= nx.pagerank(G)
  43.     for edge in G.edges_iter():
  44.         u, v = edge
  45.         wt = ex[u] * ex[v] / sum(ex[i] for i in G.neighbors_iter(v))
  46.         rank.append((u, v, wt))
  47.     sortdic = sorted(rank, reverse=True, key=operator.itemgetter(2))
  48.     #rank_diffuse = deepcopy(sortdic)
  49.    #normalization
  50.     m = len(G.edges())
  51.     for i, thing in enumerate(sortdic):
  52.         u, v, w = thing
  53.         min = sortdic[len(sortdic)-1][2]
  54.         max = sortdic[0][2]
  55.         wt = ex[u] * ex[v] / sum(ex[i] for i in G.neighbors_iter(v))
  56.         wt1 = (sortdic[i][2]-min)/(max-min)
  57.         weight.append(wt1)
  58.         G[u][v]['weight'] = wt1
  59.  
  60.     return weight
  61.  
  62. #======================================================================
  63.  
  64. def prRank(G):
  65.     ex = nx.pagerank_numpy(G)
  66.     listpr= sorted(ex.items(), reverse = True , key=operator.itemgetter(1))
  67.     #first 10 values
  68.     listpr = [x[0] for x in listpr[:10]]
  69.     #first 100 values
  70.     #listpr = [x[0] for x in listpr[:100]]
  71.     #first 10% nodes
  72.     #n=int(len(G.nodes())*0.1)
  73.     #listpr = [x[0] for x in listpr[:n]]
  74.     return listpr
  75.  
  76. #======================================================================
  77.  
  78. def degreeRank(G):
  79.     ex=nx.degree_centrality(G)
  80.     listpr= sorted(ex.items(), reverse = True , key=operator.itemgetter(1))
  81.     #first 10 values
  82.     #listpr = [x[0] for x in listpr[:10]]
  83.     #first 100 values
  84.     #listpr = [x[0] for x in listpr[:100]]
  85.     #first 10% nodes
  86.     n=int(len(G.nodes())*0.1)
  87.     listpr = [x[0] for x in listpr[:n]]
  88.     return listpr
  89.  
  90. #======================================================================
  91.  
  92. def independent_cascade(G, seeds, weight):
  93.     '''
  94.    runs the independent cascade model on the graph G where edge weights represent the respective
  95.    propagation probabilities
  96.  
  97.    seeds is a set of seed nodes
  98.    returns the fraction of spread
  99.    '''
  100.     color = {}
  101.     for node in G.nodes_iter():
  102.         if node in seeds:
  103.             color[node] = GRAY
  104.         else:
  105.             color[node] = WHITE
  106.  
  107.     Q = deque(seeds)
  108.    
  109.     spread_count = len(seeds)
  110.     while len(Q) != 0:
  111.         u = Q.popleft()
  112.         for v in G.neighbors_iter(u):
  113.             if color[v] == WHITE:
  114.                 # BECAUSE THE DIFFUSION HAS TO BE FAIr
  115.                 r = random.choice(weight)
  116.                 if r < G[u][v]['weight']:
  117.                     Q.append(v)
  118.                     color[v] = GRAY
  119.                     spread_count += 1
  120.         color[u] = BLACK
  121.    
  122.         #print spread_count
  123.     return spread_count / G.order()
  124.  
  125.  
  126. #======================================================================
  127.  
  128. def main():
  129.     x = []
  130.     G = read_graph('eron.txt')
  131.    
  132.     for u, v in karate.edges_iter():
  133.         G.add_edge(u, v)
  134.         G.add_edge(v, u)
  135.  
  136.  
  137.     mean = createw(G)    
  138.  
  139.     for i in xrange(100):
  140.         listcSum = prRank(G)
  141.         x.append(independent_cascade(G, listcSum,mean))
  142.        
  143.     print x
  144.     print 'mean',np.mean(x)
  145.     print 'std',np.std(x)
  146.        
  147.  
  148.  
  149. if __name__== '__main__':
  150.     main()
Add Comment
Please, Sign In to add comment