SHARE
TWEET

Untitled

a guest Nov 14th, 2017 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def random_walk(adj_matrix, steps=100, baised=False, alpha=1.0):
  2.  
  3.     #D_I, D_J, D_K ----- D OF EVERYTHING-------- (DONE)
  4.  
  5.     d_term=[]
  6.    
  7.     for i in range(len(adj_matrix)):
  8.        
  9.         current=adj_matrix[i].count(1)
  10.  
  11.         d_term.append(current)
  12.  
  13.     d_sum=sum(d_term)
  14.  
  15.     #-----------Create C_I TERM FOR BAISED----------
  16.     c_i_term=[]
  17.     c_i_term_list=[]
  18.  
  19.     for i in range(len(adj_matrix)):
  20.  
  21.         for j in range(len(adj_matrix)):
  22.  
  23.             current_term= adj_matrix[i][j]*(adj_matrix[j].count(1)**alpha)
  24.  
  25.             c_i_term.append(current_term)
  26.  
  27.         #adding the sum of c_i to the list
  28.         c_i_term_list.append(sum(c_i_term))
  29.  
  30.         #clear list
  31.         c_i_term[:]=[]
  32.  
  33.     #---------------DENOMENATOR for T_i_J ----------
  34.     current_term=[]
  35.     current_term_list=[]
  36.     denomenator_list=[]
  37.  
  38.     for i in range(len(adj_matrix)):
  39.  
  40.         for k in range(len(adj_matrix)):
  41.  
  42.             current_term=adj_matrix[i][k]*(adj_matrix[k].count(1)**alpha)
  43.  
  44.             current_term_list.append(current_term)
  45.  
  46.         denomenator_list.append(sum(current_term_list))
  47.  
  48.  
  49.     #--------DENOMINATOR for P_I (BAISED)---------
  50.     denomenator_term=[]
  51.  
  52.     for k in range(len(adj_matrix)-1):
  53.  
  54.         #denomenator for p_i
  55.         d= c_i_term_list[k]*(adj_matrix[k].count(1)**alpha)
  56.  
  57.         denomenator_term.append(d)
  58.  
  59.  
  60.     #------CREATING P_I LISTS -----------#
  61.  
  62.     if baised==False:
  63.  
  64.         # make p_i_list
  65.         p_i_list=[]
  66.  
  67.         for i in range(len(adj_matrix)):
  68.  
  69.             p_i= adj_matrix[i].count(1)/d_sum
  70.  
  71.             p_i_list.append(p_i)
  72.    
  73.     else:
  74.  
  75.         #creating list of p_i terms
  76.         p_i_list=[]
  77.  
  78.         for i in range(len(adj_matrix)):
  79.  
  80.             p_i= (c_i_term_list[i]*(adj_matrix[i].count(1**alpha))/sum(denomenator_term))
  81.            
  82.             p_i_list.append(p_i)
  83.  
  84.     #CREATING TRANSITION MATRIX ( DONE - BAISED==FALSE)
  85.     trans_matrix = [ [0 for i in range(len(adj_matrix))] for j in range(len(adj_matrix)) ]
  86.  
  87.    
  88.     for i in range(len(adj_matrix)):
  89.        
  90.         for j in range(len(adj_matrix)):
  91.  
  92.             #if baised=true
  93.             if baised==False:
  94.  
  95.                 adj_matrix[i][j] = adj_matrix[i][j]/adj_matrix[i].count(1)
  96.  
  97.             else:
  98.  
  99.                 adj_matrix[i][j] = (adj_matrix[i][j]*adj_matrix[j].count(1))/denomenator_list[i]
  100.  
  101.  
  102.     #setting up walk list with starting term
  103.    
  104.     current_vertex=choose_vertex(p_i_list)
  105.  
  106.     walk=[current_vertex]
  107.  
  108.     #adding to walk by iterating through the transition matrix
  109.     for i in range(0, steps):
  110.  
  111.         new_vertex=choose_vertex(adj_matrix[current_vertex])
  112.  
  113.         walk.append(new_vertex)
  114.  
  115.         current_vertex=new_vertex
  116.  
  117.  
  118.     print walk
  119.  
  120.  
  121. random_walk(get_graph_from_series(), steps=200, baised=False, alpha=1)
  122. print''
  123.  
  124. print''
  125. random_walk(get_graph_from_series(), steps=200, baised=True, alpha=5.0)
RAW Paste Data
Top