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
|
import pytest
import networkx as nx
def test_greedy_source_expansion_karate_club():
G = nx.karate_club_graph()
community = nx.community.greedy_source_expansion(G, source=16)
expected = {0, 4, 5, 6, 10, 16}
assert community == expected
def test_greedy_source_expansion_cutoff():
G = nx.karate_club_graph()
community = nx.community.greedy_source_expansion(G, source=16, cutoff=3)
assert community == {5, 6, 16}
def test_greedy_source_expansion_invalid_method():
G = nx.karate_club_graph()
with pytest.raises(ValueError):
nx.community.greedy_source_expansion(G, source=16, cutoff=3, method="invalid")
def test_greedy_source_expansion_connected_component():
G_edges = [(0, 2), (0, 1), (1, 0), (2, 1), (2, 0), (3, 4), (4, 3)]
G = nx.Graph(G_edges)
expected = {0, 1, 2}
community = nx.community.greedy_source_expansion(G, source=0)
assert community == expected
def test_greedy_source_expansion_directed_graph():
G_edges = [
(0, 2),
(0, 1),
(1, 0),
(2, 1),
(2, 0),
(3, 4),
(4, 3),
(4, 5),
(5, 3),
(5, 6),
(0, 6),
]
G = nx.DiGraph(G_edges)
expected = {0, 1, 2, 6}
community = nx.community.greedy_source_expansion(G, source=0)
assert community == expected
def test_greedy_source_expansion_multigraph():
G = nx.MultiGraph(nx.karate_club_graph())
G.add_edge(0, 1)
G.add_edge(0, 9)
expected = {0, 4, 5, 6, 10, 16}
community = nx.community.greedy_source_expansion(G, source=16)
assert community == expected
def test_greedy_source_expansion_empty_graph():
G = nx.Graph()
G.add_nodes_from(range(5))
expected = {0}
assert nx.community.greedy_source_expansion(G, source=0) == expected
|