|
| 1 | +@testset "All simple paths" begin |
| 2 | + # single path |
| 3 | + g = path_graph(4) |
| 4 | + paths = all_simple_paths(g, 1, 4) |
| 5 | + @test Set(paths) == Set(collect(paths)) == Set([[1, 2, 3, 4]]) |
| 6 | + |
| 7 | + # printing |
| 8 | + @test sprint(show, paths) == "SimplePathIterator{SimpleGraph{Int64}}(1 → 4)" |
| 9 | + |
| 10 | + # complete graph with cutoff |
| 11 | + g = complete_graph(4) |
| 12 | + @test Set(all_simple_paths(g, 1, 4; cutoff=2)) == Set([[1, 2, 4], [1, 3, 4], [1, 4]]) |
| 13 | + |
| 14 | + # two paths |
| 15 | + g = path_graph(4) |
| 16 | + add_vertex!(g) |
| 17 | + add_edge!(g, 3, 5) |
| 18 | + paths = all_simple_paths(g, 1, [4, 5]) |
| 19 | + @test Set(paths) == Set([[1, 2, 3, 4], [1, 2, 3, 5]]) |
| 20 | + @test Set(collect(paths)) == Set([[1, 2, 3, 4], [1, 2, 3, 5]]) # check `collect` also |
| 21 | + |
| 22 | + # two paths, with one beyond a cut-off |
| 23 | + g = path_graph(4) |
| 24 | + add_vertex!(g) |
| 25 | + add_edge!(g, 3, 5) |
| 26 | + add_vertex!(g) |
| 27 | + add_edge!(g, 5, 6) |
| 28 | + paths = all_simple_paths(g, 1, [4, 6]) |
| 29 | + @test Set(paths) == Set([[1, 2, 3, 4], [1, 2, 3, 5, 6]]) |
| 30 | + paths = all_simple_paths(g, 1, [4, 6]; cutoff=3) |
| 31 | + @test Set(paths) == Set([[1, 2, 3, 4]]) |
| 32 | + |
| 33 | + # two targets in line emits two paths |
| 34 | + g = path_graph(4) |
| 35 | + add_vertex!(g) |
| 36 | + paths = all_simple_paths(g, 1, [3, 4]) |
| 37 | + @test Set(paths) == Set([[1, 2, 3], [1, 2, 3, 4]]) |
| 38 | + |
| 39 | + # two paths digraph |
| 40 | + g = SimpleDiGraph(5) |
| 41 | + add_edge!(g, 1, 2) |
| 42 | + add_edge!(g, 2, 3) |
| 43 | + add_edge!(g, 3, 4) |
| 44 | + add_edge!(g, 3, 5) |
| 45 | + paths = all_simple_paths(g, 1, [4, 5]) |
| 46 | + @test Set(paths) == Set([[1, 2, 3, 4], [1, 2, 3, 5]]) |
| 47 | + |
| 48 | + # two paths digraph with cutoff |
| 49 | + g = SimpleDiGraph(5) |
| 50 | + add_edge!(g, 1, 2) |
| 51 | + add_edge!(g, 2, 3) |
| 52 | + add_edge!(g, 3, 4) |
| 53 | + add_edge!(g, 3, 5) |
| 54 | + paths = all_simple_paths(g, 1, [4, 5]; cutoff=3) |
| 55 | + @test Set(paths) == Set([[1, 2, 3, 4], [1, 2, 3, 5]]) |
| 56 | + |
| 57 | + # digraph with a cycle |
| 58 | + g = SimpleDiGraph(4) |
| 59 | + add_edge!(g, 1, 2) |
| 60 | + add_edge!(g, 2, 3) |
| 61 | + add_edge!(g, 3, 1) |
| 62 | + add_edge!(g, 2, 4) |
| 63 | + paths = all_simple_paths(g, 1, 4) |
| 64 | + @test Set(paths) == Set([[1, 2, 4]]) |
| 65 | + |
| 66 | + # digraph with a cycle; paths with two targets share a node in the cycle |
| 67 | + g = SimpleDiGraph(4) |
| 68 | + add_edge!(g, 1, 2) |
| 69 | + add_edge!(g, 2, 3) |
| 70 | + add_edge!(g, 3, 1) |
| 71 | + add_edge!(g, 2, 4) |
| 72 | + paths = all_simple_paths(g, 1, [3, 4]) |
| 73 | + @test Set(paths) == Set([[1, 2, 3], [1, 2, 4]]) |
| 74 | + |
| 75 | + # another digraph with a cycle; check cycles are excluded, regardless of cutoff |
| 76 | + g = SimpleDiGraph(6) |
| 77 | + add_edge!(g, 1, 2) |
| 78 | + add_edge!(g, 2, 3) |
| 79 | + add_edge!(g, 3, 4) |
| 80 | + add_edge!(g, 4, 5) |
| 81 | + add_edge!(g, 5, 2) |
| 82 | + add_edge!(g, 5, 6) |
| 83 | + paths = all_simple_paths(g, 1, 6) |
| 84 | + paths′ = all_simple_paths(g, 1, 6; cutoff=typemax(Int)) |
| 85 | + @test Set(paths) == Set(paths′) == Set([[1, 2, 3, 4, 5, 6]]) |
| 86 | + |
| 87 | + # same source and target vertex |
| 88 | + g = path_graph(4) |
| 89 | + @test Set(all_simple_paths(g, 1, 1)) == Set([[1]]) |
| 90 | + @test Set(all_simple_paths(g, 3, 3)) == Set([[3]]) |
| 91 | + @test Set(all_simple_paths(g, 1, [1, 1])) == Set([[1]]) |
| 92 | + @test Set(all_simple_paths(g, 1, [1, 4])) == Set([[1], [1, 2, 3, 4]]) |
| 93 | + |
| 94 | + # cutoff prunes paths (note: maximum path length below is `nv(g) - 1`) |
| 95 | + g = complete_graph(4) |
| 96 | + paths = all_simple_paths(g, 1, 2; cutoff=1) |
| 97 | + @test Set(paths) == Set([[1, 2]]) |
| 98 | + |
| 99 | + paths = all_simple_paths(g, 1, 2; cutoff=2) |
| 100 | + @test Set(paths) == Set([[1, 2], [1, 3, 2], [1, 4, 2]]) |
| 101 | + |
| 102 | + # nontrivial graph |
| 103 | + g = SimpleDiGraph(6) |
| 104 | + add_edge!(g, 1, 2) |
| 105 | + add_edge!(g, 2, 3) |
| 106 | + add_edge!(g, 3, 4) |
| 107 | + add_edge!(g, 4, 5) |
| 108 | + |
| 109 | + add_edge!(g, 1, 6) |
| 110 | + add_edge!(g, 2, 6) |
| 111 | + add_edge!(g, 2, 4) |
| 112 | + add_edge!(g, 6, 5) |
| 113 | + add_edge!(g, 5, 3) |
| 114 | + add_edge!(g, 5, 4) |
| 115 | + |
| 116 | + paths = all_simple_paths(g, 2, [3, 4]) |
| 117 | + @test Set(paths) == Set([ |
| 118 | + [2, 3], [2, 4, 5, 3], [2, 6, 5, 3], [2, 4], [2, 3, 4], [2, 6, 5, 4], [2, 6, 5, 3, 4] |
| 119 | + ]) |
| 120 | + |
| 121 | + paths = all_simple_paths(g, 2, [3, 4]; cutoff=3) |
| 122 | + @test Set(paths) == |
| 123 | + Set([[2, 3], [2, 4, 5, 3], [2, 6, 5, 3], [2, 4], [2, 3, 4], [2, 6, 5, 4]]) |
| 124 | + |
| 125 | + paths = all_simple_paths(g, 2, [3, 4]; cutoff=2) |
| 126 | + @test Set(paths) == Set([[2, 3], [2, 4], [2, 3, 4]]) |
| 127 | +end |
0 commit comments