Skip to content

Instantly share code, notes, and snippets.

@nbyouri
Last active August 23, 2017 20:20
Show Gist options
  • Save nbyouri/e20c07921cc13c3e8c048a56f7e000d1 to your computer and use it in GitHub Desktop.
Save nbyouri/e20c07921cc13c3e8c048a56f7e000d1 to your computer and use it in GitHub Desktop.
Power Method for PageRank
#############################################
# PageRank algorithm using the power method #
# youri mouton 18/08/2017 #
#############################################
# Adjacency matrix
A = [ 0 2 0;
0 0 2;
1 1 0];
# Out degree matrix
D = diagm(sum(A,2)[:])
# Transition probability matrix
P = inv(D) * A
# Transition probability matrix transposition
PT = P'
# We assume each node has equal importance, so for 3 nodes,
# initialise a [1/3;1/3;1/3] vector.
# Initial vector that will iterate until convergence
V = ones(size(A,1),1)*(1/size(A,1))
# Main power method loop, 50 iterations are adequate
# since the convergence is fast.
# You could also do PT^50 * V.
# The power method effectively calculates the right eigenvector of PT, the
# transposed transition probability matrix.
for i = 1 : 50
V = PT * V
end
print(V)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment