#! /usr/bin/env python3 # def google_from_incidence ( A ): #*****************************************************************************80 # ## google_from_incidence() converts an incidence matrix to a Google transition matrix. # # Discussion: # # If the input incidence matrix has a node I with no connectivity, # (A(I,1:N) = 0) then we artificially set T(1:N,I)=1/N, so that # the transition matrix property of having unit column sums is preserved. # # For nodes with connectivity, we assume that 85 percent of the time # we will take a link at random, and 15 percent of the time, we will # jump to an arbitrary link. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 April 2022 # # Author: # # John Burkardt # # Input: # # integer A(N,N): the incidence matrix. # # Output: # # real G(N,N): the Google matrix. # import numpy as np n = A.shape[0] p = 0.15 G = np.zeros ( [ n, n ] ) s = sum ( A, 2 ); for i in range ( 0, n ): s = np.sum ( A[i,:] ) if ( s == 0.0 ): G[i,:] = 1.0 / float ( n ) else: G[i,:] = ( 1.0 - p ) * A[i,:] / s + p / float ( n ) G = np.transpose ( G ) return G def google_from_incidence_test ( ): #*****************************************************************************80 # ## google_from_incidence_test() tests google_from_incidence(). # # 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 ( 'google_from_incidence_test():' ) print ( ' google_from_incidence() computes the Google matrix G' ) print ( ' from an incidence matrix A.' ) A = moler_incidence ( ) print ( '' ) print ( ' Incidence matrix A:' ) print ( A ) G = google_from_incidence ( A ) print ( '' ) print ( ' Google matrix G(A):' ) print ( G ) return if ( __name__ == '__main__' ): google_from_incidence_test ( )