# PageRank.R (Mauro Brunato, april 2007) # # Computing PageRank score # Uncomment a line ending by "q()" to stop the program at that point # and show the corresponding quantity. ################################## # # GRAPH DEFINITION # The graph matrix # graph <- rbind ( c( 0, 1, 0, 0, 0 ), c( 0, 0, 1, 0, 0 ), c( 1, 0, 0, 1, 0 ), c( 0, 1, 0, 0, 1 ), c( 0, 0, 0, 1, 0 )); # Graph size # N <- length (graph[,1]); # Uncomment to see the graph matrix # #graph;q(); ##################################### # # PAGERANK SCORE COMPUTATION # # Uncomment one of the following # the second introduces a damping factor # L <- graph; #L <- graph + .01; # Row normalization for ( i in 1:N ) L[i,] <- L[i,] / sum(L[i,]); # Uncomment the following to see the transition matrix # #L;q(); # Set initial conditions # Uncomment one of the following # rank <- matrix ( c(1, rep (0, N-1)), N ); #rank <- matrix ( rep (1, N), N ); # Uncomment to see initial ranking vector and stop # #rank;q(); # Compute the transpose matrix # tL = t(L); # Uncomment to see the spectral decomposition and halt # #eigen(tL);q(); # Apply the PageRank iterations # for ( i in 1:101 ) { rank <- tL %*% rank; rank <- rank / sum(rank); } # Print the final ranking and stop # rank;