Project: PageRank Algorithm · PageRank Algorithm

Lesson 6

Nikolai Chukhin · Alexander S. Kulikov

In practice, one may also use the built-in implementation of PageRank provided by the \(\texttt{networkx}\) library. An example of using this function is shown below:

from networkx import DiGraph, pagerank

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

personalization = {0: 0.5, 1: 0.5, 2: 0, 3: 0}
pr = pagerank(graph, alpha=0.85, personalization=personalization)

pagerank_values = [pr[node] for node in sorted(graph.nodes())]
print(*pagerank_values)

0.15176189025701853 0.23680596808451773 0.09332033182355251 0.2795439514933751 0.23856785834153624

Note that the \(\texttt{pagerank}\) function supports a variety of parameters. We have already discussed the parameter \(\texttt{alpha}\), which corresponds to the damping factor \(d\), and the parameter \(\texttt{personalization}\), which enables the computation of a personalized PageRank vector. In addition, the parameters \(\texttt{max\_{iter}}\) and \(\texttt{tol}\) allow one to control the maximum number of iterations and the convergence tolerance, respectively, thereby regulating the accuracy of the computation. The parameter \(\texttt{nstart}\) specifies the initial probability distribution, \(\texttt{weight}\) determines how edge weights are incorporated into the transition probabilities, and \(\texttt{dangling}\) defines the treatment of dangling nodes (sink vertices), specifying how their probability mass is redistributed among the other vertices of the graph according to a prescribed distribution.