comparison env/lib/python3.9/site-packages/networkx/algorithms/community/tests/test_kernighan_lin.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for the :mod:`networkx.algorithms.community.kernighan_lin`
2 module.
3
4 """
5 import pytest
6
7 import networkx as nx
8 from networkx.algorithms.community import kernighan_lin_bisection
9 from itertools import permutations
10
11
12 def assert_partition_equal(x, y):
13 assert set(map(frozenset, x)) == set(map(frozenset, y))
14
15
16 def test_partition():
17 G = nx.barbell_graph(3, 0)
18 C = kernighan_lin_bisection(G)
19 assert_partition_equal(C, [{0, 1, 2}, {3, 4, 5}])
20
21
22 def test_partition_argument():
23 G = nx.barbell_graph(3, 0)
24 partition = [{0, 1, 2}, {3, 4, 5}]
25 C = kernighan_lin_bisection(G, partition)
26 assert_partition_equal(C, partition)
27
28
29 def test_seed_argument():
30 G = nx.barbell_graph(3, 0)
31 C = kernighan_lin_bisection(G, seed=1)
32 assert_partition_equal(C, [{0, 1, 2}, {3, 4, 5}])
33
34
35 def test_non_disjoint_partition():
36 with pytest.raises(nx.NetworkXError):
37 G = nx.barbell_graph(3, 0)
38 partition = ({0, 1, 2}, {2, 3, 4, 5})
39 kernighan_lin_bisection(G, partition)
40
41
42 def test_too_many_blocks():
43 with pytest.raises(nx.NetworkXError):
44 G = nx.barbell_graph(3, 0)
45 partition = ({0, 1}, {2}, {3, 4, 5})
46 kernighan_lin_bisection(G, partition)
47
48
49 def test_multigraph():
50 G = nx.cycle_graph(4)
51 M = nx.MultiGraph(G.edges())
52 M.add_edges_from(G.edges())
53 M.remove_edge(1, 2)
54 for labels in permutations(range(4)):
55 mapping = dict(zip(M, labels))
56 A, B = kernighan_lin_bisection(nx.relabel_nodes(M, mapping), seed=0)
57 assert_partition_equal(
58 [A, B], [{mapping[0], mapping[1]}, {mapping[2], mapping[3]}]
59 )