#! /usr/bin/env python3 # def surf_rank ( A ): #*****************************************************************************80 # ## surf_rank uses the "random surfer" method for node rankings of a digraph. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 29 February 2020 # # Author: # # John Burkardt # # Input: # # integer A(N,N): the incidence matrix. # # Output: # # real X(N): the surfer rankings. # from transition_from_incidence import transition_from_incidence import numpy as np steps = 100000 print ( '' ) print ( 'surf_rank():' ) print ( ' Given an NxN incidence matrix A,' ) print ( ' compute the transition matrix T,' ) print ( ' and then repeatedly visit nodes, keeping track of' ) print ( ' how often you visited.' ) print ( '' ) print ( ' 15 per cent of the time, simply take a random jump to a node.' ) print ( ' The rest of the time, follow a random link from the current node.' ) print ( '' ) print ( ' When done, the node weight is the number of visits' ) print ( ' normalized by the total number of visits.' ) n = A.shape[0] r = np.zeros ( n, dtype = np.float ) # # Get the transition matrix from the incidence matrix. # T = transition_from_incidence ( A ) # # Choose J, a random starting position. # j = np.random.randint ( 0, n ) # # Carry out many moves. # for k in range ( 0, steps ): p1 = np.random.rand ( ) q1 = 0.15 if ( p1 <= q1 ): j = np.random.randint ( 0, n ) else: p2 = np.random.rand ( ) q2 = 0.0 for i in range ( 0, n ): q2 = q2 + T[i,j] if ( p2 <= q2 ): j = i break r[j] = r[j] + 1.0 # # Normalize the counts. # r = r / float ( steps ) return r def surf_rank_test ( ): #*****************************************************************************80 # ## surf_rank_test() tests surf_rank(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 April 2022 # # Author: # # John Burkardt # from moler_incidence import moler_incidence print ( '' ) print ( 'surf_rank_test():' ) print ( ' surf_rank() computes the surf rankings r()' ) print ( ' for an incidence matrix A.' ) A = moler_incidence ( ) print ( '' ) print ( ' Incidence matrix A:' ) print ( A ) r = surf_rank ( A ) print ( '' ) print ( ' Surf rankings r:' ) print ( r ) return if ( __name__ == '__main__' ): surf_rank_test ( )