File size: 10,378 Bytes
f911107 |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
import pytest
import networkx as nx
from networkx.generators import line
from networkx.utils import edges_equal
class TestGeneratorLine:
def test_star(self):
G = nx.star_graph(5)
L = nx.line_graph(G)
assert nx.is_isomorphic(L, nx.complete_graph(5))
def test_path(self):
G = nx.path_graph(5)
L = nx.line_graph(G)
assert nx.is_isomorphic(L, nx.path_graph(4))
def test_cycle(self):
G = nx.cycle_graph(5)
L = nx.line_graph(G)
assert nx.is_isomorphic(L, G)
def test_digraph1(self):
G = nx.DiGraph([(0, 1), (0, 2), (0, 3)])
L = nx.line_graph(G)
# no edge graph, but with nodes
assert L.adj == {(0, 1): {}, (0, 2): {}, (0, 3): {}}
def test_multigraph1(self):
G = nx.MultiGraph([(0, 1), (0, 1), (1, 0), (0, 2), (2, 0), (0, 3)])
L = nx.line_graph(G)
# no edge graph, but with nodes
assert edges_equal(
L.edges(),
[
((0, 3, 0), (0, 1, 0)),
((0, 3, 0), (0, 2, 0)),
((0, 3, 0), (0, 2, 1)),
((0, 3, 0), (0, 1, 1)),
((0, 3, 0), (0, 1, 2)),
((0, 1, 0), (0, 1, 1)),
((0, 1, 0), (0, 2, 0)),
((0, 1, 0), (0, 1, 2)),
((0, 1, 0), (0, 2, 1)),
((0, 1, 1), (0, 1, 2)),
((0, 1, 1), (0, 2, 0)),
((0, 1, 1), (0, 2, 1)),
((0, 1, 2), (0, 2, 0)),
((0, 1, 2), (0, 2, 1)),
((0, 2, 0), (0, 2, 1)),
],
)
def test_multigraph2(self):
G = nx.MultiGraph([(1, 2), (2, 1)])
L = nx.line_graph(G)
assert edges_equal(L.edges(), [((1, 2, 0), (1, 2, 1))])
def test_multidigraph1(self):
G = nx.MultiDiGraph([(1, 2), (2, 1)])
L = nx.line_graph(G)
assert edges_equal(L.edges(), [((1, 2, 0), (2, 1, 0)), ((2, 1, 0), (1, 2, 0))])
def test_multidigraph2(self):
G = nx.MultiDiGraph([(0, 1), (0, 1), (0, 1), (1, 2)])
L = nx.line_graph(G)
assert edges_equal(
L.edges(),
[((0, 1, 0), (1, 2, 0)), ((0, 1, 1), (1, 2, 0)), ((0, 1, 2), (1, 2, 0))],
)
def test_digraph2(self):
G = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
L = nx.line_graph(G)
assert edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))])
def test_create1(self):
G = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
L = nx.line_graph(G, create_using=nx.Graph())
assert edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))])
def test_create2(self):
G = nx.Graph([(0, 1), (1, 2), (2, 3)])
L = nx.line_graph(G, create_using=nx.DiGraph())
assert edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))])
class TestGeneratorInverseLine:
def test_example(self):
G = nx.Graph()
G_edges = [
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[2, 3],
[2, 5],
[2, 6],
[2, 7],
[3, 4],
[3, 5],
[6, 7],
[6, 8],
[7, 8],
]
G.add_edges_from(G_edges)
H = nx.inverse_line_graph(G)
solution = nx.Graph()
solution_edges = [
("a", "b"),
("a", "c"),
("a", "d"),
("a", "e"),
("c", "d"),
("e", "f"),
("e", "g"),
("f", "g"),
]
solution.add_edges_from(solution_edges)
assert nx.is_isomorphic(H, solution)
def test_example_2(self):
G = nx.Graph()
G_edges = [[1, 2], [1, 3], [2, 3], [3, 4], [3, 5], [4, 5]]
G.add_edges_from(G_edges)
H = nx.inverse_line_graph(G)
solution = nx.Graph()
solution_edges = [("a", "c"), ("b", "c"), ("c", "d"), ("d", "e"), ("d", "f")]
solution.add_edges_from(solution_edges)
assert nx.is_isomorphic(H, solution)
def test_pair(self):
G = nx.path_graph(2)
H = nx.inverse_line_graph(G)
solution = nx.path_graph(3)
assert nx.is_isomorphic(H, solution)
def test_line(self):
G = nx.path_graph(5)
solution = nx.path_graph(6)
H = nx.inverse_line_graph(G)
assert nx.is_isomorphic(H, solution)
def test_triangle_graph(self):
G = nx.complete_graph(3)
H = nx.inverse_line_graph(G)
alternative_solution = nx.Graph()
alternative_solution.add_edges_from([[0, 1], [0, 2], [0, 3]])
# there are two alternative inverse line graphs for this case
# so long as we get one of them the test should pass
assert nx.is_isomorphic(H, G) or nx.is_isomorphic(H, alternative_solution)
def test_cycle(self):
G = nx.cycle_graph(5)
H = nx.inverse_line_graph(G)
assert nx.is_isomorphic(H, G)
def test_empty(self):
G = nx.Graph()
H = nx.inverse_line_graph(G)
assert nx.is_isomorphic(H, nx.complete_graph(1))
def test_K1(self):
G = nx.complete_graph(1)
H = nx.inverse_line_graph(G)
solution = nx.path_graph(2)
assert nx.is_isomorphic(H, solution)
def test_edgeless_graph(self):
G = nx.empty_graph(5)
with pytest.raises(nx.NetworkXError, match="edgeless graph"):
nx.inverse_line_graph(G)
def test_selfloops_error(self):
G = nx.cycle_graph(4)
G.add_edge(0, 0)
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
def test_non_line_graphs(self):
# Tests several known non-line graphs for impossibility
# Adapted from L.W.Beineke, "Characterizations of derived graphs"
# claw graph
claw = nx.star_graph(3)
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, claw)
# wheel graph with 6 nodes
wheel = nx.wheel_graph(6)
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, wheel)
# K5 with one edge remove
K5m = nx.complete_graph(5)
K5m.remove_edge(0, 1)
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, K5m)
# graph without any odd triangles (contains claw as induced subgraph)
G = nx.compose(nx.path_graph(2), nx.complete_bipartite_graph(2, 3))
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
## Variations on a diamond graph
# Diamond + 2 edges (+ "roof")
G = nx.diamond_graph()
G.add_edges_from([(4, 0), (5, 3)])
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
G.add_edge(4, 5)
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
# Diamond + 2 connected edges
G = nx.diamond_graph()
G.add_edges_from([(4, 0), (4, 3)])
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
# Diamond + K3 + one edge (+ 2*K3)
G = nx.diamond_graph()
G.add_edges_from([(4, 0), (4, 1), (4, 2), (5, 3)])
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
G.add_edges_from([(5, 1), (5, 2)])
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
# 4 triangles
G = nx.diamond_graph()
G.add_edges_from([(4, 0), (4, 1), (5, 2), (5, 3)])
pytest.raises(nx.NetworkXError, nx.inverse_line_graph, G)
def test_wrong_graph_type(self):
G = nx.DiGraph()
G_edges = [[0, 1], [0, 2], [0, 3]]
G.add_edges_from(G_edges)
pytest.raises(nx.NetworkXNotImplemented, nx.inverse_line_graph, G)
G = nx.MultiGraph()
G_edges = [[0, 1], [0, 2], [0, 3]]
G.add_edges_from(G_edges)
pytest.raises(nx.NetworkXNotImplemented, nx.inverse_line_graph, G)
def test_line_inverse_line_complete(self):
G = nx.complete_graph(10)
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
def test_line_inverse_line_path(self):
G = nx.path_graph(10)
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
def test_line_inverse_line_hypercube(self):
G = nx.hypercube_graph(5)
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
def test_line_inverse_line_cycle(self):
G = nx.cycle_graph(10)
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
def test_line_inverse_line_star(self):
G = nx.star_graph(20)
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
def test_line_inverse_line_multipartite(self):
G = nx.complete_multipartite_graph(3, 4, 5)
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
def test_line_inverse_line_dgm(self):
G = nx.dorogovtsev_goltsev_mendes_graph(4)
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
def test_line_different_node_types(self):
G = nx.path_graph([1, 2, 3, "a", "b", "c"])
H = nx.line_graph(G)
J = nx.inverse_line_graph(H)
assert nx.is_isomorphic(G, J)
class TestGeneratorPrivateFunctions:
def test_triangles_error(self):
G = nx.diamond_graph()
pytest.raises(nx.NetworkXError, line._triangles, G, (4, 0))
pytest.raises(nx.NetworkXError, line._triangles, G, (0, 3))
def test_odd_triangles_error(self):
G = nx.diamond_graph()
pytest.raises(nx.NetworkXError, line._odd_triangle, G, (0, 1, 4))
pytest.raises(nx.NetworkXError, line._odd_triangle, G, (0, 1, 3))
def test_select_starting_cell_error(self):
G = nx.diamond_graph()
pytest.raises(nx.NetworkXError, line._select_starting_cell, G, (4, 0))
pytest.raises(nx.NetworkXError, line._select_starting_cell, G, (0, 3))
def test_diamond_graph(self):
G = nx.diamond_graph()
for edge in G.edges:
cell = line._select_starting_cell(G, starting_edge=edge)
# Starting cell should always be one of the two triangles
assert len(cell) == 3
assert all(v in G[u] for u in cell for v in cell if u != v)
|