aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/networkx/algorithms/tests/test_walks.py
blob: 7a6b323932988e1b9513118162df62e9613ee65b (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Unit tests for the :mod:`networkx.algorithms.walks` module."""

import pytest

import networkx as nx

pytest.importorskip("numpy")
pytest.importorskip("scipy")


def test_directed():
    G = nx.DiGraph([(0, 1), (1, 2), (2, 0)])
    num_walks = nx.number_of_walks(G, 3)
    expected = {0: {0: 1, 1: 0, 2: 0}, 1: {0: 0, 1: 1, 2: 0}, 2: {0: 0, 1: 0, 2: 1}}
    assert num_walks == expected


def test_undirected():
    G = nx.cycle_graph(3)
    num_walks = nx.number_of_walks(G, 3)
    expected = {0: {0: 2, 1: 3, 2: 3}, 1: {0: 3, 1: 2, 2: 3}, 2: {0: 3, 1: 3, 2: 2}}
    assert num_walks == expected


def test_non_integer_nodes():
    G = nx.DiGraph([("A", "B"), ("B", "C"), ("C", "A")])
    num_walks = nx.number_of_walks(G, 2)
    expected = {
        "A": {"A": 0, "B": 0, "C": 1},
        "B": {"A": 1, "B": 0, "C": 0},
        "C": {"A": 0, "B": 1, "C": 0},
    }
    assert num_walks == expected


def test_zero_length():
    G = nx.cycle_graph(3)
    num_walks = nx.number_of_walks(G, 0)
    expected = {0: {0: 1, 1: 0, 2: 0}, 1: {0: 0, 1: 1, 2: 0}, 2: {0: 0, 1: 0, 2: 1}}
    assert num_walks == expected


def test_negative_length_exception():
    G = nx.cycle_graph(3)
    with pytest.raises(ValueError):
        nx.number_of_walks(G, -1)


def test_hidden_weight_attr():
    G = nx.cycle_graph(3)
    G.add_edge(1, 2, weight=5)
    num_walks = nx.number_of_walks(G, 3)
    expected = {0: {0: 2, 1: 3, 2: 3}, 1: {0: 3, 1: 2, 2: 3}, 2: {0: 3, 1: 3, 2: 2}}
    assert num_walks == expected