.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/graph_theory/ranking/plot_01_pagerank.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_graph_theory_ranking_plot_01_pagerank.py: PageRank: ranking pages by a random surfer ================================================ Ranks the vertices of a small web of links by the stationary distribution of a random surfer who follows links with probability 0.85 and otherwise jumps to a random page, and shows the convergence of the power iteration. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.graph_theory import Graph, pagerank .. GENERATED FROM PYTHON SOURCE LINES 18-20 A small web ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-31 .. code-block:: Python links = {0: [1, 2], 1: [2], 2: [0], 3: [0, 2], 4: [0, 3, 5], 5: [4], 6: [2]} g = Graph(7, directed=True) for u, targets in links.items(): for v in targets: g.add_edge(u, v) result = pagerank(g) for page in np.argsort(result.scores)[::-1]: print(f"page {page}: {result.scores[page]:.4f} (in-links from {[u for u in links if page in links[u]]})") print(f"converged in {result.iterations} iterations") .. rst-class:: sphx-glr-script-out .. code-block:: none page 0: 0.3433 (in-links from [2, 3, 4]) page 2: 0.3432 (in-links from [0, 1, 3, 6]) page 1: 0.1674 (in-links from [0]) page 4: 0.0522 (in-links from [5]) page 5: 0.0362 (in-links from [4]) page 3: 0.0362 (in-links from [4]) page 6: 0.0214 (in-links from []) converged in 55 iterations .. GENERATED FROM PYTHON SOURCE LINES 32-34 Convergence at the rate of the damping factor ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-43 .. code-block:: Python exact = result.scores fig, ax = plt.subplots() for d in (0.5, 0.85, 0.95): errors = [np.abs(pagerank(g, damping=d, max_iter=k, tol=0).scores - pagerank(g, damping=d).scores).sum() for k in range(1, 60)] ax.semilogy(range(1, 60), errors, label=f"damping {d}") ax.set_xlabel("iteration") ax.set_ylabel("L1 error") ax.legend() .. image-sg:: /api/gallery/graph_theory/ranking/images/sphx_glr_plot_01_pagerank_001.png :alt: plot 01 pagerank :srcset: /api/gallery/graph_theory/ranking/images/sphx_glr_plot_01_pagerank_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.106 seconds) .. _sphx_glr_download_api_gallery_graph_theory_ranking_plot_01_pagerank.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_pagerank.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_pagerank.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_pagerank.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_