|
63 | 63 | @test has_path(g, "D", "E") |
64 | 64 | @test !has_path(g, "A", "E") |
65 | 65 | end |
| 66 | + @testset "neighborhood" begin |
| 67 | + g = named_grid((4, 4)) |
| 68 | + @test issetequal(neighborhood(g, (1, 1), nv(g)), vertices(g)) |
| 69 | + @test issetequal(neighborhood(g, (1, 1), 0), [(1, 1)]) |
| 70 | + @test issetequal(neighborhood(g, (1, 1), 1), [(1, 1), (2, 1), (1, 2)]) |
| 71 | + ns = [ |
| 72 | + (1, 1), |
| 73 | + (2, 1), |
| 74 | + (1, 2), |
| 75 | + (3, 1), |
| 76 | + (2, 2), |
| 77 | + (1, 3), |
| 78 | + ] |
| 79 | + @test issetequal(neighborhood(g, (1, 1), 2), ns) |
| 80 | + ns = [ |
| 81 | + (1, 1), |
| 82 | + (2, 1), |
| 83 | + (1, 2), |
| 84 | + (3, 1), |
| 85 | + (2, 2), |
| 86 | + (1, 3), |
| 87 | + (4, 1), |
| 88 | + (3, 2), |
| 89 | + (2, 3), |
| 90 | + (1, 4), |
| 91 | + ] |
| 92 | + @test issetequal(neighborhood(g, (1, 1), 3), ns) |
| 93 | + ns = [ |
| 94 | + (1, 1), |
| 95 | + (2, 1), |
| 96 | + (1, 2), |
| 97 | + (3, 1), |
| 98 | + (2, 2), |
| 99 | + (1, 3), |
| 100 | + (4, 1), |
| 101 | + (3, 2), |
| 102 | + (2, 3), |
| 103 | + (1, 4), |
| 104 | + (4, 2), |
| 105 | + (3, 3), |
| 106 | + (2, 4), |
| 107 | + ] |
| 108 | + @test issetequal(neighborhood(g, (1, 1), 4), ns) |
| 109 | + ns = [ |
| 110 | + (1, 1), |
| 111 | + (2, 1), |
| 112 | + (1, 2), |
| 113 | + (3, 1), |
| 114 | + (2, 2), |
| 115 | + (1, 3), |
| 116 | + (4, 1), |
| 117 | + (3, 2), |
| 118 | + (2, 3), |
| 119 | + (1, 4), |
| 120 | + (4, 2), |
| 121 | + (3, 3), |
| 122 | + (2, 4), |
| 123 | + (4, 3), |
| 124 | + (3, 4), |
| 125 | + ] |
| 126 | + @test issetequal(neighborhood(g, (1, 1), 5), ns) |
| 127 | + @test issetequal(neighborhood(g, (1, 1), 6), vertices(g)) |
| 128 | + ns_ds = [ |
| 129 | + ((1, 1), 0), |
| 130 | + ((2, 1), 1), |
| 131 | + ((1, 2), 1), |
| 132 | + ((3, 1), 2), |
| 133 | + ((2, 2), 2), |
| 134 | + ((1, 3), 2), |
| 135 | + ((4, 1), 3), |
| 136 | + ((3, 2), 3), |
| 137 | + ((2, 3), 3), |
| 138 | + ((1, 4), 3), |
| 139 | + ] |
| 140 | + @test issetequal(neighborhood_dists(g, (1, 1), 3), ns_ds) |
| 141 | + end |
66 | 142 | @testset "Basics (directed)" begin |
67 | 143 | g = NamedDiGraph(["A", "B", "C", "D"]) |
68 | 144 | add_edge!(g, "A" => "B") |
@@ -244,13 +320,123 @@ end |
244 | 320 | ) |
245 | 321 | @test_broken f(g, "A") |
246 | 322 | end |
247 | | - @testset "has_self_loops" begin |
| 323 | + end |
| 324 | + @testset "Graph connectivity" begin |
248 | 325 | g = NamedGraph(2) |
249 | 326 | @test g isa NamedGraph{Int} |
250 | 327 | add_edge!(g, 1, 2) |
251 | 328 | @test !has_self_loops(g) |
252 | 329 | add_edge!(g, 1, 1) |
253 | 330 | @test has_self_loops(g) |
| 331 | + |
| 332 | + g1 = named_grid((2, 2)) |
| 333 | + g2 = named_grid((2, 2)) |
| 334 | + g = g1 ⊔ g2 |
| 335 | + t = named_binary_tree(3) |
| 336 | + |
| 337 | + @test is_cyclic(g1) |
| 338 | + @test is_cyclic(g2) |
| 339 | + @test is_cyclic(g) |
| 340 | + @test !is_cyclic(t) |
| 341 | + |
| 342 | + @test is_connected(g1) |
| 343 | + @test is_connected(g2) |
| 344 | + @test !is_connected(g) |
| 345 | + @test is_connected(t) |
| 346 | + |
| 347 | + cc = connected_components(g1) |
| 348 | + @test length(cc) == 1 |
| 349 | + @test length(only(cc)) == nv(g1) |
| 350 | + @test issetequal(only(cc), vertices(g1)) |
| 351 | + |
| 352 | + cc = connected_components(g) |
| 353 | + @test length(cc) == 2 |
| 354 | + @test length(cc[1]) == nv(g1) |
| 355 | + @test length(cc[2]) == nv(g2) |
| 356 | + @test issetequal(cc[1], map(v -> (v, 1), vertices(g1))) |
| 357 | + @test issetequal(cc[2], map(v -> (v, 2), vertices(g2))) |
| 358 | + end |
| 359 | + @testset "incident_edges" begin |
| 360 | + g = grid((3, 3)) |
| 361 | + inc_edges = Edge.([2 => 1, 2 => 3, 2 => 5]) |
| 362 | + @test issetequal(incident_edges(g, 2), inc_edges) |
| 363 | + @test issetequal(incident_edges(g, 2; dir=:in), reverse.(inc_edges)) |
| 364 | + @test issetequal(incident_edges(g, 2; dir=:out), inc_edges) |
| 365 | + @test issetequal(incident_edges(g, 2; dir=:both), inc_edges ∪ reverse.(inc_edges)) |
| 366 | + |
| 367 | + g = named_grid((3, 3)) |
| 368 | + inc_edges = NamedEdge.([ |
| 369 | + (2, 1) => (1, 1), |
| 370 | + (2, 1) => (3, 1), |
| 371 | + (2, 1) => (2, 2), |
| 372 | + ]) |
| 373 | + @test issetequal(incident_edges(g, (2, 1)), inc_edges) |
| 374 | + @test issetequal(incident_edges(g, (2, 1); dir=:in), reverse.(inc_edges)) |
| 375 | + @test issetequal(incident_edges(g, (2, 1); dir=:out), inc_edges) |
| 376 | + @test issetequal(incident_edges(g, (2, 1); dir=:both), inc_edges ∪ reverse.(inc_edges)) |
| 377 | + |
| 378 | + g = path_digraph(4) |
| 379 | + @test issetequal(incident_edges(g, 3), Edge.([3 => 4])) |
| 380 | + @test issetequal(incident_edges(g, 3; dir=:in), Edge.([2 => 3])) |
| 381 | + @test issetequal(incident_edges(g, 3; dir=:out), Edge.([3 => 4])) |
| 382 | + @test issetequal(incident_edges(g, 3; dir=:both), Edge.([2 => 3, 3 => 4])) |
| 383 | + |
| 384 | + g = NamedDiGraph(path_digraph(4), ["A", "B", "C", "D"]) |
| 385 | + @test issetequal(incident_edges(g, "C"), NamedEdge.(["C" => "D"])) |
| 386 | + @test issetequal(incident_edges(g, "C"; dir=:in), NamedEdge.(["B" => "C"])) |
| 387 | + @test issetequal(incident_edges(g, "C"; dir=:out), NamedEdge.(["C" => "D"])) |
| 388 | + @test issetequal(incident_edges(g, "C"; dir=:both), NamedEdge.(["B" => "C", "C" => "D"])) |
254 | 389 | end |
| 390 | + @testset "merge_vertices" begin |
| 391 | + g = named_grid((3, 3)) |
| 392 | + mg = merge_vertices(g, [(2, 2), (2, 3), (3, 3)]) |
| 393 | + @test nv(mg) == 7 |
| 394 | + @test ne(mg) == 9 |
| 395 | + merged_vertices = [ |
| 396 | + (1, 1), |
| 397 | + (2, 1), |
| 398 | + (3, 1), |
| 399 | + (1, 2), |
| 400 | + (2, 2), |
| 401 | + (3, 2), |
| 402 | + (1, 3), |
| 403 | + ] |
| 404 | + for v in merged_vertices |
| 405 | + @test has_vertex(mg, v) |
| 406 | + end |
| 407 | + merged_edges = [ |
| 408 | + (1, 1) => (2, 1), |
| 409 | + (1, 1) => (1, 2), |
| 410 | + (2, 1) => (3, 1), |
| 411 | + (2, 1) => (2, 2), |
| 412 | + (3, 1) => (3, 2), |
| 413 | + (1, 2) => (2, 2), |
| 414 | + (1, 2) => (1, 3), |
| 415 | + (2, 2) => (3, 2), |
| 416 | + (2, 2) => (1, 3), |
| 417 | + ] |
| 418 | + for e in merged_edges |
| 419 | + @test has_edge(mg, e) |
| 420 | + end |
| 421 | + |
| 422 | + sg = SimpleDiGraph(4) |
| 423 | + g = NamedDiGraph(sg, ["A", "B", "C", "D"]) |
| 424 | + add_edge!(g, "A" => "B") |
| 425 | + add_edge!(g, "B" => "C") |
| 426 | + add_edge!(g, "C" => "D") |
| 427 | + mg = merge_vertices(g, ["B", "C"]) |
| 428 | + @test ne(mg) == 2 |
| 429 | + @test has_edge(mg, "A" => "B") |
| 430 | + @test has_edge(mg, "B" => "D") |
| 431 | + |
| 432 | + sg = SimpleDiGraph(4) |
| 433 | + g = NamedDiGraph(sg, ["A", "B", "C", "D"]) |
| 434 | + add_edge!(g, "B" => "A") |
| 435 | + add_edge!(g, "C" => "B") |
| 436 | + add_edge!(g, "D" => "C") |
| 437 | + mg = merge_vertices(g, ["B", "C"]) |
| 438 | + @test ne(mg) == 2 |
| 439 | + @test has_edge(mg, "B" => "A") |
| 440 | + @test has_edge(mg, "D" => "B") |
255 | 441 | end |
256 | 442 | end |
0 commit comments