Advertisement
Guest User

PageRank Power Iteration

a guest
Jan 22nd, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. #!-*- coding: utf-8 -*-
  2. '''
  3. @author : Hasan Mahmud Tuhin
  4.          19CS-JD14M
  5. @Description:
  6.             This code finds page rank of a graph using power iteration method.
  7. '''
  8.  
  9. class PageRank:
  10.    
  11.     def __init__(self,n):
  12.         self.epsilone = 0.000000000001
  13.         self.Node = n
  14.         self.matrix = [ [0] * self.Node ] * self.Node;
  15.         print(self.matrix)
  16.         self.rank  =[]
  17.         self.old_rank = [0] * self.Node
  18.         for i in range(0,self.Node):
  19.             self.rank.append( 1 / self.Node)
  20.             #self.old_rank.append(1/self.Node)
  21.    
  22.     def iteration(self):
  23.         try:
  24.             product = []
  25.             for i in range(0,self.Node):
  26.                 self.old_rank[i] = self.rank[i]
  27.                 product.append(0)
  28.            
  29.             #- begain multiplication
  30.             for i in range(0,self.Node):
  31.                 for j in range(0,self.Node):
  32.                     product[i] += self.matrix[i][j] * self.rank[j]
  33.            
  34.             #-- copy rank matrix
  35.             for i in range(0,self.Node):
  36.                 self.rank[0] = product[0]
  37.         except Exception as e:
  38.             print("Error on calculation power iteration .{}".format(e))
  39.    
  40.     def stop(self):
  41.         try:
  42.             for i in range(0,self.Node):
  43.                 if abs(self.rank[i] - self.old_rank[i]) <  self.epsilone:
  44.                     continue
  45.                 else:
  46.                     return False
  47.             return True
  48.         except:
  49.             pass
  50.            
  51.     def input_values(self):
  52.         try:
  53.             print("Enter values of Matrix");
  54.             for i in range(0,self.Node):
  55.                 #self.matrix[i].append([])
  56.                 for j in range(0,self.Node):
  57.                     print("Enter value for matrix[{}][{}] : ".format(i,j),end='');
  58.                     self.matrix[i][j] = float( input() )
  59.         except Exception as e:
  60.             print("Error in input. {}".format(e))
  61.    
  62.        
  63.     def show_rank(self):
  64.         try:
  65.             print("\nPage Rank is :\n")
  66.             for i in range(0,self.Node):
  67.                 print("[{}]".format(self.rank[i]))
  68.         except:
  69.             pass
  70.    
  71.     def show_old(self):
  72.         print("old Rank matrix is: ")
  73.         for i in range(0,self.Node):
  74.             print("[{}]".format(self.old_rank[i]))
  75.            
  76.            
  77. if __name__ == "__main__":
  78.     print("Enter Number of Nodes")
  79.     N = int(input())
  80.    
  81.     pg = PageRank(N)
  82.    
  83.     pg.input_values()
  84.     #-- do iteration 50 times, if no difference between two
  85.     #--- iteration then break
  86.     for i in range(0,50):
  87.         pg.iteration()
  88.         print("{} Iteration Done".format(i));
  89.         if pg.stop():
  90.             print("Stop, Because Two interation are same")
  91.             break
  92.         pg.show_rank()
  93.         #pg.show_old()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement