HEX
Server: Apache
System: Linux vps-cdc32557.vps.ovh.ca 5.15.0-156-generic #166-Ubuntu SMP Sat Aug 9 00:02:46 UTC 2025 x86_64
User: hanode (1017)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //lib/python3/dist-packages/networkx/algorithms/link_analysis/tests/test_hits.py
#!/usr/bin/env python
import pytest


import networkx
from networkx.testing import almost_equal

# Example from
# A. Langville and C. Meyer, "A survey of eigenvector methods of web
# information retrieval."  http://citeseer.ist.psu.edu/713792.html


class TestHITS:

    @classmethod
    def setup_class(cls):

        G = networkx.DiGraph()

        edges = [(1, 3), (1, 5),
                 (2, 1),
                 (3, 5),
                 (5, 4), (5, 3),
                 (6, 5)]

        G.add_edges_from(edges, weight=1)
        cls.G = G
        cls.G.a = dict(zip(sorted(G), [0.000000, 0.000000, 0.366025,
                                        0.133975, 0.500000, 0.000000]))
        cls.G.h = dict(zip(sorted(G), [0.366025, 0.000000, 0.211325,
                                        0.000000, 0.211325, 0.211325]))

    def test_hits(self):
        G = self.G
        h, a = networkx.hits(G, tol=1.e-08)
        for n in G:
            assert almost_equal(h[n], G.h[n], places=4)
        for n in G:
            assert almost_equal(a[n], G.a[n], places=4)

    def test_hits_nstart(self):
        G = self.G
        nstart = dict([(i, 1. / 2) for i in G])
        h, a = networkx.hits(G, nstart=nstart)

    def test_hits_numpy(self):
        numpy = pytest.importorskip('numpy')
        G = self.G
        h, a = networkx.hits_numpy(G)
        for n in G:
            assert almost_equal(h[n], G.h[n], places=4)
        for n in G:
            assert almost_equal(a[n], G.a[n], places=4)

    def test_hits_scipy(self):
        sp = pytest.importorskip('scipy')
        G = self.G
        h, a = networkx.hits_scipy(G, tol=1.e-08)
        for n in G:
            assert almost_equal(h[n], G.h[n], places=4)
        for n in G:
            assert almost_equal(a[n], G.a[n], places=4)

    def test_empty(self):
        numpy = pytest.importorskip('numpy')
        G = networkx.Graph()
        assert networkx.hits(G) == ({}, {})
        assert networkx.hits_numpy(G) == ({}, {})
        assert networkx.authority_matrix(G).shape == (0, 0)
        assert networkx.hub_matrix(G).shape == (0, 0)

    def test_empty_scipy(self):
        scipy = pytest.importorskip('scipy')
        G = networkx.Graph()
        assert networkx.hits_scipy(G) == ({}, {})

    def test_hits_not_convergent(self):
        with pytest.raises(networkx.PowerIterationFailedConvergence):
            G = self.G
            networkx.hits(G, max_iter=0)