library(igraph)
library(expm)

#Question 1

Form the A matrix. Then, introduce decay and form the B matrix as we did in the course notes. (5 Points)

#Let the zero row equal to the equal probability row, with each page having the same probability
A = matrix(c(0,1/2,1/2,0,0,0,
             1/6,1/6,1/6,1/6,1/6,1/6,
             1/3,1/3,0,0,1/3,0,
             0,0,0,0,1/2,1/2,
             0,0,0,1/2,0,1/2,
             0,0,0,1,0,0), nrow = 6, ncol = 6)
round(A,3)
##      [,1]  [,2]  [,3] [,4] [,5] [,6]
## [1,]  0.0 0.167 0.333  0.0  0.0    0
## [2,]  0.5 0.167 0.333  0.0  0.0    0
## [3,]  0.5 0.167 0.000  0.0  0.0    0
## [4,]  0.0 0.167 0.000  0.0  0.5    1
## [5,]  0.0 0.167 0.333  0.5  0.0    0
## [6,]  0.0 0.167 0.000  0.5  0.5    0
n = nrow(A)
d <- 0.85
B <- (d * A) + ((1-d)/n)
round(B,4)
##       [,1]   [,2]   [,3]  [,4]  [,5]  [,6]
## [1,] 0.025 0.1667 0.3083 0.025 0.025 0.025
## [2,] 0.450 0.1667 0.3083 0.025 0.025 0.025
## [3,] 0.450 0.1667 0.0250 0.025 0.025 0.025
## [4,] 0.025 0.1667 0.0250 0.025 0.450 0.875
## [5,] 0.025 0.1667 0.3083 0.450 0.025 0.025
## [6,] 0.025 0.1667 0.0250 0.450 0.450 0.025

Start with a uniform rank vector r and perform power iterations on B till convergence. That is, compute the solution r = Bn × r. Attempt this for a sufficiently large n so that r actually converges. (5 Points)

r = c(1/6,1/6,1/6,1/6,1/6,1/6)

R <- data.frame(matrix(nrow=6,ncol=10))

#n goes from 10 to 100

for(i in seq(1,10))
{
    R[i] <- B%^%(i*10) %*% r
}
R
##           X1         X2         X3         X4         X5         X6         X7
## 1 0.05205661 0.05170616 0.05170475 0.05170475 0.05170475 0.05170475 0.05170475
## 2 0.07428990 0.07368173 0.07367927 0.07367926 0.07367926 0.07367926 0.07367926
## 3 0.05782138 0.05741406 0.05741242 0.05741241 0.05741241 0.05741241 0.05741241
## 4 0.34797267 0.34870083 0.34870367 0.34870369 0.34870369 0.34870369 0.34870369
## 5 0.19975859 0.19990313 0.19990381 0.19990381 0.19990381 0.19990381 0.19990381
## 6 0.26810085 0.26859408 0.26859607 0.26859608 0.26859608 0.26859608 0.26859608
##           X8         X9        X10
## 1 0.05170475 0.05170475 0.05170475
## 2 0.07367926 0.07367926 0.07367926
## 3 0.05741241 0.05741241 0.05741241
## 4 0.34870369 0.34870369 0.34870369
## 5 0.19990381 0.19990381 0.19990381
## 6 0.26859608 0.26859608 0.26859608

Compute the eigen-decomposition of B and verify that you indeed get an eigenvalue of 1 as the largest eigenvalue and that its corresponding eigenvector is the same vector that you obtained in the previous power iteration method. Further, this eigenvector has all positive entries and it sums to 1.(10 points)

eig <- eigen(B)
eig
## eigen() decomposition
## $values
## [1]  1.00000000+0i  0.57619235+0i -0.42500000+0i -0.42500000-0i -0.34991524+0i
## [6] -0.08461044+0i
## 
## $vectors
##              [,1]          [,2]                      [,3]
## [1,] 0.1044385+0i  0.2931457+0i  2.486934e-15+0.0000e+00i
## [2,] 0.1488249+0i  0.5093703+0i -8.528385e-16-6.9832e-23i
## [3,] 0.1159674+0i  0.3414619+0i -1.930646e-15-0.0000e+00i
## [4,] 0.7043472+0i -0.5890805+0i -7.071068e-01+0.0000e+00i
## [5,] 0.4037861+0i -0.1413606+0i  7.071068e-01+0.0000e+00i
## [6,] 0.5425377+0i -0.4135367+0i  0.000000e+00-1.7058e-08i
##                           [,4]           [,5]            [,6]
## [1,]  2.486934e-15-0.0000e+00i -0.06471710+0i -0.212296003+0i
## [2,] -8.528385e-16+6.9832e-23i  0.01388698+0i  0.854071294+0i
## [3,] -1.930646e-15+0.0000e+00i  0.07298180+0i -0.363638739+0i
## [4,] -7.071068e-01+0.0000e+00i -0.66058664+0i  0.018399984+0i
## [5,]  7.071068e-01-0.0000e+00i  0.73761812+0i -0.304719509+0i
## [6,]  0.000000e+00+1.7058e-08i -0.09918316+0i  0.008182973+0i
sum(eig$vectors[1,])
## [1] 0.1205711+0i

Use the graph package in R and its page.rank method to compute the Page Rank of the graph as given in A. Note that you don’t need to apply decay. The package starts with a connected graph and applies decay internally. Verify that you do get the same PageRank vector as the two approaches above. (10 points)

graph <- graph_from_adjacency_matrix(t(A),weighted = TRUE)
plot(graph)

page.rank(graph)
## $vector
## [1] 0.05170475 0.07367926 0.05741241 0.34870369 0.19990381 0.26859608
## 
## $value
## [1] 1
## 
## $options
## NULL