Project: PageRank Algorithm · PageRank Algorithm

Lesson 2

Nikolai Chukhin · Alexander S. Kulikov

This algorithm is relatively straightforward to implement in Python.

import numpy as np
import networkx as nx

edges = ['01', '02', '04', '10', '12', '13', '14', '20', '23', '24', '31', '34', '43']
graph = nx.DiGraph([(int(e[0]), int(e[1])) for e in edges])
n = graph.number_of_nodes()
d = 0.85

A = nx.stochastic_graph(graph)
T = nx.to_numpy_array(A, nodelist=sorted(graph.nodes())).T
U = T * d + (1 - d) / n
r = [1 / n] * n

print(*np.linalg.matrix_power(U, 100) @ r)

0.1008074  0.19880144 0.1008074  0.32997492 0.26960884