I tried the Page Rank function on a normal matrix with no Null Node. The ith row shows the node and the jth column shows the transition. So Matrix[i,j] denotes the transition from the ith row to the jth column
Transition Matrix
library(igraph)
#-----B is the matrix----#
g2<-graph_from_adjacency_matrix(B, mode = "directed" , weighted = TRUE)
plot(g2)
B1<-page.rank(g2, damping = 1)$vector
C1<-as.data.frame(B1)
This gave me the result(no damping):
The PageRank comes out to be (3/9, 2/9, 2/9, 2/9)
Now, I applied the same to another matrix with the Null node:
New Matrix with 3rd Row being the Null node
What I should get is a row vector of 0,0,0,0 but what I get on using the function is:
The PageRank comes out to be (0.2, 0.2666666,0.2666666,0.2666666)
What am I doing wrong?
As far as I understand PageRank it is not defined when there are nodes with an out-degree of zero (as you have here). According to this answer to a related question: How does pageranking algorithm deal with webpage without outbound links? this is commonly dealt with by connecting the offending node to all of the others (including itself) with equal probability.
I tried this out with your example
B1 <- structure(c(0, 0.5, 0.25, 0, 0.333333333333333, 0, 0.25, 0.5,
0.333333333333333, 0, 0.25, 0.5, 0.333333333333333, 0.5, 0.25,
0), .Dim = c(4L, 4L))
g1 <- graph_from_adjacency_matrix(B1, mode = "directed", weighted = TRUE)
page_rank(g1, damping = 1)$vector
and this gives
[1] 0.2000000 0.2666667 0.2666667 0.2666667
which is the same as you have.
[Updated from comments]
Under the hood igraph is using prpack so this must be accounting for nodes with zero out degree. If you want to flag up the problem before you run the page_rank function you could just check for:
any(degree(g1, mode = "out") == 0)
so actually get the zero vector that you want and preserve node names it could be something like:
outdeg <- degree(g1, mode = "out")
if (any(outdeg==0)) {
B2 <- 0*outdeg
} else {
B2 <- page_rank(g1, damping = 1)
}
or even smaller
B2 <- any(degree(g1, mode = "out") == 0) * page_rank(g1, damping = 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With