|
| 1 | +"""Unit tests for the :mod:`networkx.algorithms.approximation.kcutsets` module.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import networkx as nx |
| 5 | +from networkx.algorithms.approximation import minimum_multiway_cut, minimum_k_cut |
| 6 | +from networkx import minimum_cut_value |
| 7 | + |
| 8 | + |
| 9 | +class TestMinMultiwayCut: |
| 10 | + """Unit tests for the approximate Minimum Multiway Cut function |
| 11 | + :func:`~networkx.algorithms.approximation.kcutsets.minimum_multiway_cut`. |
| 12 | + """ |
| 13 | + |
| 14 | + def test_null_graph(self): |
| 15 | + """Test empty graph.""" |
| 16 | + G = nx.null_graph() |
| 17 | + with pytest.raises( |
| 18 | + nx.NetworkXError, match="Expected non-empty NetworkX graph!" |
| 19 | + ): |
| 20 | + minimum_multiway_cut(G, G.nodes()) |
| 21 | + |
| 22 | + def test_undirected_non_connected(self): |
| 23 | + """Test an undirected disconnected graph.""" |
| 24 | + G = nx.path_graph(10) |
| 25 | + G.remove_edge(3, 4) |
| 26 | + with pytest.raises(nx.NetworkXError, match="Graph not connected."): |
| 27 | + minimum_multiway_cut(G, G.nodes()) |
| 28 | + |
| 29 | + def test_invalid_terminals(self): |
| 30 | + """Test empty terminals.""" |
| 31 | + |
| 32 | + G = nx.path_graph(10) |
| 33 | + with pytest.raises( |
| 34 | + nx.NetworkXError, match="At least two terminals should be provided." |
| 35 | + ): |
| 36 | + minimum_multiway_cut(G, []) |
| 37 | + |
| 38 | + def test_path_graph_unweighted(self): |
| 39 | + """Test min multiway cut for a path graph.""" |
| 40 | + G = nx.path_graph(2) |
| 41 | + cut_value, cutset = minimum_multiway_cut(G, [0, 1]) |
| 42 | + assert cut_value == 1 |
| 43 | + G.remove_edges_from(cutset) |
| 44 | + assert len(list(nx.connected_components(G))) == 2 |
| 45 | + |
| 46 | + def test_path_graph_weighted(self): |
| 47 | + """Test min multiway cut for a path graph with weights.""" |
| 48 | + G = nx.Graph() |
| 49 | + G.add_weighted_edges_from( |
| 50 | + [(0, 1, 10), (1, 2, 10), (2, 3, 5)], weight="capacity" |
| 51 | + ) |
| 52 | + cut_value, cutset = minimum_multiway_cut(G, [0, 3], weight="capacity") |
| 53 | + assert cut_value == 5 |
| 54 | + |
| 55 | + def test_complete_graph(self): |
| 56 | + """Test min multiway cut for a complete graph.""" |
| 57 | + G = nx.complete_graph(5) |
| 58 | + cut_value, cutset = minimum_multiway_cut(G, G.nodes()) |
| 59 | + assert cut_value == 10 |
| 60 | + # remove the edges |
| 61 | + G.remove_edges_from(cutset) |
| 62 | + assert set(G.edges()) == set() |
| 63 | + |
| 64 | + @pytest.mark.parametrize( |
| 65 | + "graph_class", |
| 66 | + [ |
| 67 | + nx.krackhardt_kite_graph, |
| 68 | + nx.icosahedral_graph, |
| 69 | + nx.petersen_graph, |
| 70 | + nx.pappus_graph, |
| 71 | + nx.truncated_cube_graph, |
| 72 | + nx.tutte_graph, |
| 73 | + ], |
| 74 | + ) |
| 75 | + def test_compare_min_cut(self, graph_class): |
| 76 | + """Compare minimum_cut_value and minimum_multiway_cut considering 2 nodes. |
| 77 | +
|
| 78 | + For two nodes the minimum_cut_value(G, s, t) should be equivalent to |
| 79 | + minimum_multiway_cut(G, {s,t}). |
| 80 | + """ |
| 81 | + G = graph_class() |
| 82 | + nx.set_edge_attributes(G, values=10, name="weight") |
| 83 | + s, t = min(G), max(G) |
| 84 | + cut_value, cutset = minimum_multiway_cut(G, {s, t}, weight="weight") |
| 85 | + assert cut_value == minimum_cut_value(G, s, t, capacity="weight") |
| 86 | + |
| 87 | + |
| 88 | +class TestMinkCut: |
| 89 | + """Unit tests for the approximate Minimum k-Cut function |
| 90 | + :func:`~networkx.algorithms.approximation.kcutsets.minimum_k_cut`. |
| 91 | + """ |
| 92 | + |
| 93 | + def test_null_graph(self): |
| 94 | + """Test empty graph.""" |
| 95 | + G = nx.null_graph() |
| 96 | + with pytest.raises( |
| 97 | + nx.NetworkXError, match="Expected non-empty NetworkX graph!" |
| 98 | + ): |
| 99 | + minimum_k_cut(G, 3) |
| 100 | + |
| 101 | + def test_undirected_non_connected(self): |
| 102 | + """Test an undirected disconnected graph.""" |
| 103 | + G = nx.path_graph(10) |
| 104 | + G.remove_edge(3, 4) |
| 105 | + with pytest.raises(nx.NetworkXError, match="Graph not connected."): |
| 106 | + minimum_k_cut(G, 3) |
| 107 | + |
| 108 | + def test_invalid_k(self): |
| 109 | + """Test empty terminals.""" |
| 110 | + G = nx.path_graph(10) |
| 111 | + with pytest.raises(nx.NetworkXError, match="k should be within 1 and 10"): |
| 112 | + minimum_k_cut(G, 0) |
| 113 | + with pytest.raises(nx.NetworkXError, match="k should be within 1 and 10"): |
| 114 | + minimum_k_cut(G, 11) |
| 115 | + |
| 116 | + def test_path_graph_unweighted(self): |
| 117 | + """Test min k-cut for a path graph.""" |
| 118 | + G = nx.path_graph(2) |
| 119 | + cut_value, cutset = minimum_k_cut(G, 2) |
| 120 | + assert cut_value == 1 |
| 121 | + G.remove_edges_from(cutset) |
| 122 | + assert len(list(nx.connected_components(G))) == 2 |
| 123 | + |
| 124 | + def test_path_graph_weighted_k2(self): |
| 125 | + """Test min k-cut for a path graph with weights.""" |
| 126 | + G = nx.Graph() |
| 127 | + G.add_weighted_edges_from( |
| 128 | + [(0, 1, 10), (1, 2, 10), (2, 3, 5)], weight="capacity" |
| 129 | + ) |
| 130 | + cut_value, cutset = minimum_k_cut(G, 2, weight="capacity") |
| 131 | + assert cut_value == 5 |
| 132 | + G.remove_edges_from(cutset) |
| 133 | + assert len(list(nx.connected_components(G))) == 2 |
| 134 | + |
| 135 | + def test_path_graph_weighted_k3(self): |
| 136 | + """Test min k-cut for a path graph with weights.""" |
| 137 | + G = nx.Graph() |
| 138 | + G.add_weighted_edges_from( |
| 139 | + [(0, 1, 10), (1, 2, 10), (2, 3, 5)], weight="capacity" |
| 140 | + ) |
| 141 | + cut_value, cutset = minimum_k_cut(G, 3, weight="capacity") |
| 142 | + assert cut_value == 15 |
| 143 | + G.remove_edges_from(cutset) |
| 144 | + assert len(list(nx.connected_components(G))) == 3 |
| 145 | + |
| 146 | + def test_complete_graph_k2(self): |
| 147 | + """Test min k-cut for a complete graph for k=2.""" |
| 148 | + G = nx.complete_graph(5) |
| 149 | + cut_value, cutset = minimum_k_cut(G, 2) |
| 150 | + # it should contain all the edges incident to a node |
| 151 | + assert cut_value == 4 |
| 152 | + # remove the edges |
| 153 | + G.remove_edges_from(cutset) |
| 154 | + assert len(list(nx.connected_components(G))) == 2 |
| 155 | + |
| 156 | + def test_complete_graph_all(self): |
| 157 | + """Test min k-cut for a complete graph.""" |
| 158 | + G = nx.complete_graph(5) |
| 159 | + cut_value, cutset = minimum_k_cut(G, 5) |
| 160 | + assert cut_value == 10 |
| 161 | + # remove the edges |
| 162 | + G.remove_edges_from(cutset) |
| 163 | + assert set(G.edges()) == set() |
| 164 | + |
| 165 | + def test_complete_graph_weighted(self): |
| 166 | + """Test min k-cut for a weighted complete graph.""" |
| 167 | + G = nx.complete_graph(5) |
| 168 | + nx.set_edge_attributes(G, values=10, name="weight") |
| 169 | + cut_value, cutset = minimum_k_cut(G, 5, weight="weight") |
| 170 | + assert cut_value == 100 |
| 171 | + # remove the edges |
| 172 | + G.remove_edges_from(cutset) |
| 173 | + assert set(G.edges()) == set() |
| 174 | + |
| 175 | + @pytest.mark.parametrize( |
| 176 | + "graph_class", |
| 177 | + [ |
| 178 | + nx.krackhardt_kite_graph, |
| 179 | + nx.icosahedral_graph, |
| 180 | + nx.petersen_graph, |
| 181 | + nx.pappus_graph, |
| 182 | + nx.truncated_cube_graph, |
| 183 | + nx.tutte_graph, |
| 184 | + ], |
| 185 | + ) |
| 186 | + @pytest.mark.parametrize("k", list(range(1, 11))) |
| 187 | + def test_compare_min_cut(self, graph_class, k): |
| 188 | + """Test multiple graph types and k.""" |
| 189 | + G = graph_class() |
| 190 | + cut_value, cutset = minimum_k_cut(G, k) |
| 191 | + G.remove_edges_from(cutset) |
| 192 | + assert len(list(nx.connected_components(G))) >= k |
0 commit comments