Search code examples
rmatrixvectordata-sciencepagerank

PageRank in R. Issue with vectors and how to iterate through adjacency matrix


I have a 500x500 adjacency matrix of 1 and 0, and I need to calculate pagerank for each page. I have a code here, where R is the matrix and T=0.15 is a constant:

 n = ncol(R)
 B = matrix(1/n, n, n) # the teleportation matrix
 A = 0.85 * R + 0.15 * B
 ranks = eigen(A)$vectors[1] # my PageRanks
 print(ranks)
[1] -0.5317519+0i

I don't have much experience with R, but I assume that the given output is a general pagerank, and I need a pagerank for each page.

Is there a way to construct a table of pageranks with relation to the matrix? I didn't find anything related to my particular case in the web.


Solution

  • Few points:

    (1) You need to convert the binary adjacency matrix (R in your case) to a column-stochastic transition matrix to start with (representing probability of transitions between the pages).

    (2) A needs to remain as column stochastic as well, then only the dominant eigenvector corresponding to the eigenvalue 1 will be the page rank vector.

    (3) To find the first eigenvector of the matrix A, you need use eigen(A)$vectors[,1]

    Example with a small 5x5 adjacency matrix R:

    set.seed(12345)
    R = matrix(sample(0:1, 25, replace=TRUE), nrow=5) # random binary adjacency matrix
    R = t(t(R) / rowSums(t(R))) # convert the adjacency matrix R to a column-stochastic transition matrix
    n = ncol(R)
    B = matrix(1/n, n, n) # the teleportation matrix
    A = 0.85 * R + 0.15 * B
    A <- t(t(A) / rowSums(t(A))) # make A column-stochastic
    ranks = eigen(A)$vectors[,1] # my PageRanks
    print(ranks)
    # [1] 0.05564937 0.05564937 0.95364105 0.14304616 0.25280990
    print(ranks / sum(ranks)) # normalized ranks
    [1] 0.03809524 0.03809524 0.65282295 0.09792344 0.17306313