aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_divisive.py
blob: 6331503f97eaabee965a7f7f302b30e88601687e (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pytest

import networkx as nx


def test_edge_betweenness_partition():
    G = nx.barbell_graph(3, 0)
    C = nx.community.edge_betweenness_partition(G, 2)
    answer = [{0, 1, 2}, {3, 4, 5}]
    assert len(C) == len(answer)
    for s in answer:
        assert s in C

    G = nx.barbell_graph(3, 1)
    C = nx.community.edge_betweenness_partition(G, 3)
    answer = [{0, 1, 2}, {4, 5, 6}, {3}]
    assert len(C) == len(answer)
    for s in answer:
        assert s in C

    C = nx.community.edge_betweenness_partition(G, 7)
    answer = [{n} for n in G]
    assert len(C) == len(answer)
    for s in answer:
        assert s in C

    C = nx.community.edge_betweenness_partition(G, 1)
    assert C == [set(G)]

    C = nx.community.edge_betweenness_partition(G, 1, weight="weight")
    assert C == [set(G)]

    with pytest.raises(nx.NetworkXError):
        nx.community.edge_betweenness_partition(G, 0)

    with pytest.raises(nx.NetworkXError):
        nx.community.edge_betweenness_partition(G, -1)

    with pytest.raises(nx.NetworkXError):
        nx.community.edge_betweenness_partition(G, 10)


def test_edge_current_flow_betweenness_partition():
    pytest.importorskip("scipy")

    G = nx.barbell_graph(3, 0)
    C = nx.community.edge_current_flow_betweenness_partition(G, 2)
    answer = [{0, 1, 2}, {3, 4, 5}]
    assert len(C) == len(answer)
    for s in answer:
        assert s in C

    G = nx.barbell_graph(3, 1)
    C = nx.community.edge_current_flow_betweenness_partition(G, 2)
    answers = [[{0, 1, 2, 3}, {4, 5, 6}], [{0, 1, 2}, {3, 4, 5, 6}]]
    assert len(C) == len(answers[0])
    assert any(all(s in answer for s in C) for answer in answers)

    C = nx.community.edge_current_flow_betweenness_partition(G, 3)
    answer = [{0, 1, 2}, {4, 5, 6}, {3}]
    assert len(C) == len(answer)
    for s in answer:
        assert s in C

    C = nx.community.edge_current_flow_betweenness_partition(G, 4)
    answers = [[{1, 2}, {4, 5, 6}, {3}, {0}], [{0, 1, 2}, {5, 6}, {3}, {4}]]
    assert len(C) == len(answers[0])
    assert any(all(s in answer for s in C) for answer in answers)

    C = nx.community.edge_current_flow_betweenness_partition(G, 5)
    answer = [{1, 2}, {5, 6}, {3}, {0}, {4}]
    assert len(C) == len(answer)
    for s in answer:
        assert s in C

    C = nx.community.edge_current_flow_betweenness_partition(G, 6)
    answers = [[{2}, {5, 6}, {3}, {0}, {4}, {1}], [{1, 2}, {6}, {3}, {0}, {4}, {5}]]
    assert len(C) == len(answers[0])
    assert any(all(s in answer for s in C) for answer in answers)

    C = nx.community.edge_current_flow_betweenness_partition(G, 7)
    answer = [{n} for n in G]
    assert len(C) == len(answer)
    for s in answer:
        assert s in C

    C = nx.community.edge_current_flow_betweenness_partition(G, 1)
    assert C == [set(G)]

    C = nx.community.edge_current_flow_betweenness_partition(G, 1, weight="weight")
    assert C == [set(G)]

    with pytest.raises(nx.NetworkXError):
        nx.community.edge_current_flow_betweenness_partition(G, 0)

    with pytest.raises(nx.NetworkXError):
        nx.community.edge_current_flow_betweenness_partition(G, -1)

    with pytest.raises(nx.NetworkXError):
        nx.community.edge_current_flow_betweenness_partition(G, 10)

    N = 10
    G = nx.empty_graph(N)
    for i in range(2, N - 1):
        C = nx.community.edge_current_flow_betweenness_partition(G, i)
        assert C == [{n} for n in G]