#! /usr/bin/env python3 # def pagerank_test ( A ): import numpy as np print ( '' ) print ( 'pagerank_test():' ) n = A.shape[0] # # Print the digraph. # print ( '' ) print ( ' The digraph adjacency matrix A.' ) print ( A ) input ( ' Press RETURN' ) # # Plot the digraph. # print ( '' ) print ( ' Plot the digraph A.' ) digraph_plot ( A ) input ( ' Press RETURN' ) # # Define the transition matrix T1: # print ( '' ) print ( ' Define transition matrix T:' ) T = transition_matrix ( A ) input ( ' Press RETURN' ) # # 100 people start at node 0. T * x moves them. Take STEPS steps. # print ( '' ) print ( ' Watch 100 people move around digraph, x <- T * x:' ) travel ( T ) input ( ' Press RETURN' ) # # Eigenvalues of T. # print ( '' ) print ( ' Eigenvalues of T?' ) eigen = np.linalg.eigvals ( T ) print ( np.round ( eigen, 2 ) ) input ( ' Press RETURN' ) return def digraph1 ( ): import numpy as np A1 = np.array ( [ \ [ 0, 1, 0, 0 ], \ [ 0, 0, 1, 0 ], \ [ 0, 0, 0, 1 ], \ [ 1, 0, 0, 0 ] ] ) return A1 def digraph2 ( ): import numpy as np A2 = np.array ( [ \ [ 0, 1, 1, 0 ], \ [ 0, 0, 1, 0 ], \ [ 0, 0, 0, 1 ], \ [ 1, 0, 0, 0 ] ] ) return A2 def digraph3 ( ): import numpy as np A3 = np.array ( [ \ [ 0, 1, 0, 0, 0, 1, 0 ], \ [ 0, 0, 1, 0, 0, 0, 0 ], \ [ 1, 0, 0, 1, 0, 0, 0 ], \ [ 1, 0, 0, 0, 0, 0, 0 ], \ [ 0, 0, 1, 0, 0, 0, 0 ], \ [ 0, 0, 0, 0, 0, 0, 0 ], \ [ 0, 0, 0, 0, 0, 0, 0 ] ] ) return A3 def digraph_plot ( A ): #*****************************************************************************80 # ## digraph_plot plots a digraph. # from graphviz import Digraph import numpy as np n = A.shape[0] names = np.zeros ( n, dtype = str ) for i in range ( 0, n ): names[i] = chr ( i+65 ) dot = Digraph ( format = 'png' ) for i in range ( 0, n ): dot.node ( names[i] ) for i in range ( 0, n ): for j in range ( 0, n ): if ( A[i,j] ): dot.edge ( names[i], names[j] ) dot.render ( 'digraph.dot', view = True ) return def transition_matrix ( A ): #*****************************************************************************80 # ## transition_matrix() computes a transition matrix. # global google global option import numpy as np n = A.shape[0] s = np.sum ( A, axis = 1 ) T = np.zeros ( [ n, n ], dtype = float ) for i in range ( 0, n ): if ( s[i] != 0.0 ): T[i,:] = A[i,:] / s[i] else: if ( option == 0 ): pass elif ( option == 1 ): T[i,i] = 1.0 else: T[i,:] = 1.0 / n if ( google ): p = 0.15 T = ( 1.0 - p ) * T + ( p / n ) * np.ones ( [ n, n ] ) T = np.transpose ( T ) print ( np.round ( T, 2 ) ) return T def travel ( T ): import numpy as np global steps n = T.shape[0] for i in range ( 0, steps ): if ( i == 0 ): x = np.zeros ( n ) x[0] = 100.0 else: x = np.dot ( T, x ) print ( i, np.round ( x, 2 ) ) # # Script to run examples. # global google global option global steps if ( True ): google = False option = 2 steps = 10 print ( '' ) print ( ' A1: Simple 4 node loop' ) print ( ' Verify that the crowd walks around in a bunch.' ) input ( ' Press RETURN...' ) A1 = digraph1 ( ) pagerank_test ( A1 ) if ( True ): google = False option = 2 steps = 30 print ( '' ) print ( ' A2: 4 node loop with 1 branch' ) print ( ' The initial crowd spreads out.' ) A2 = digraph2 ( ) pagerank_test ( A2 ) if ( True ): google = False option = 0 steps = 15 print ( '' ) print ( ' A3: 7 nodes, with a source, a sink, and an island' ) print ( ' option 0: "sinks" drain.' ) print ( ' People who go to room F disappear.' ) A3 = digraph3 ( ) pagerank_test ( A3 ) if ( True ): google = False option = 1 steps = 15 print ( '' ) print ( ' A3: 7 nodes, with a source, a sink, and an island' ) print ( ' option 1: "sinks" stagnate.' ) print ( ' People who go to room F can never leave.' ) A3 = digraph3 ( ) pagerank_test ( A3 ) if ( True ): google = False option = 2 steps = 15 print ( '' ) print ( ' A3: 7 nodes, with a source, a sink, and an island' ) print ( ' option 2: "sinks" evaporate.' ) print ( ' People who go to room F next visit any random room.' ) A3 = digraph3 ( ) pagerank_test ( A3 ) if ( True ): google = True option = 2 steps = 15 print ( '' ) print ( ' A3: 7 nodes, with a source, a sink, and an island' ) print ( ' Use Google smoothing option.' ) print ( ' Small probability that anyone can pop over to anywhere.' ) A3 = digraph3 ( ) pagerank_test ( A3 ) print ( '' ) print ( 'pagerank_test():' ) print ( ' Normal end of execution.' )\