|
| 1 | +package path |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "math" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hmdsefi/gograph" |
| 9 | +) |
| 10 | + |
| 11 | +type weightedEdge struct { |
| 12 | + from, to string |
| 13 | + weight float64 |
| 14 | +} |
| 15 | + |
| 16 | +// directedWeighted returns a directed, weighted graph with the given edges. |
| 17 | +// The vertices are added in the order they first appear. |
| 18 | +func directedWeighted(edges ...weightedEdge) gograph.Graph[string] { |
| 19 | + g := gograph.New[string](gograph.Directed(), gograph.Weighted()) |
| 20 | + for _, e := range edges { |
| 21 | + _, _ = g.AddEdge(gograph.NewVertex(e.from), gograph.NewVertex(e.to), gograph.WithEdgeWeight(e.weight)) |
| 22 | + } |
| 23 | + return g |
| 24 | +} |
| 25 | + |
| 26 | +// distanceFunc returns the distance from the start vertex to v, and false |
| 27 | +// when the function reports v as unreachable. |
| 28 | +type distanceFunc func(v string) (float64, bool) |
| 29 | + |
| 30 | +// shortestPaths runs every single-source shortest-path function on the |
| 31 | +// same graph. Each one reports an unreachable vertex in its own way. |
| 32 | +var shortestPaths = []struct { |
| 33 | + name string |
| 34 | + run func(g gograph.Graph[string], start string) (distanceFunc, error) |
| 35 | +}{ |
| 36 | + { |
| 37 | + name: "FloydWarshall", |
| 38 | + run: func(g gograph.Graph[string], start string) (distanceFunc, error) { |
| 39 | + dist, err := FloydWarshall(g) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return func(v string) (float64, bool) { |
| 44 | + return dist[start][v], !math.IsInf(dist[start][v], 1) |
| 45 | + }, nil |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "BellmanFord", |
| 50 | + run: func(g gograph.Graph[string], start string) (distanceFunc, error) { |
| 51 | + dist, err := BellmanFord(g, start) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + return func(v string) (float64, bool) { |
| 56 | + return dist[v], !math.IsInf(dist[v], 1) |
| 57 | + }, nil |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Dijkstra", |
| 62 | + run: func(g gograph.Graph[string], start string) (distanceFunc, error) { |
| 63 | + dist := Dijkstra(g, start) |
| 64 | + return func(v string) (float64, bool) { |
| 65 | + return dist[v], dist[v] != math.MaxFloat64 |
| 66 | + }, nil |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "DijkstraSimple", |
| 71 | + run: func(g gograph.Graph[string], start string) (distanceFunc, error) { |
| 72 | + dist := DijkstraSimple(g, start) |
| 73 | + return func(v string) (float64, bool) { |
| 74 | + return dist[v], dist[v] != math.MaxFloat64 |
| 75 | + }, nil |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "DijkstraMultiSource", |
| 80 | + run: func(g gograph.Graph[string], start string) (distanceFunc, error) { |
| 81 | + nearest, err := DijkstraMultiSource(g, start) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + return nearest.Distance, nil |
| 86 | + }, |
| 87 | + }, |
| 88 | +} |
| 89 | + |
| 90 | +func TestShortestPaths_NaNWeight(t *testing.T) { |
| 91 | + // the NaN edge to b is skipped, so b is reached through c, and d, |
| 92 | + // which only a NaN edge leads to, is unreachable |
| 93 | + g := directedWeighted( |
| 94 | + weightedEdge{"a", "b", math.NaN()}, |
| 95 | + weightedEdge{"a", "c", 1}, |
| 96 | + weightedEdge{"c", "b", 1}, |
| 97 | + weightedEdge{"a", "d", math.NaN()}, |
| 98 | + ) |
| 99 | + |
| 100 | + for _, tt := range shortestPaths { |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + distance, err := tt.run(g, "a") |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("unexpected error: %v", err) |
| 105 | + } |
| 106 | + if d, ok := distance("b"); !ok || d != 2 { |
| 107 | + t.Errorf("b: expected distance 2, got %v (reachable %v)", d, ok) |
| 108 | + } |
| 109 | + if d, ok := distance("d"); ok { |
| 110 | + t.Errorf("d: expected it to be unreachable, got distance %v", d) |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestShortestPaths_PositiveInfinityWeight(t *testing.T) { |
| 117 | + // a path through a +Inf edge is as long as no path |
| 118 | + g := directedWeighted( |
| 119 | + weightedEdge{"a", "b", math.Inf(1)}, |
| 120 | + weightedEdge{"a", "c", 1}, |
| 121 | + weightedEdge{"b", "d", 1}, |
| 122 | + ) |
| 123 | + |
| 124 | + for _, tt := range shortestPaths { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + distance, err := tt.run(g, "a") |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("unexpected error: %v", err) |
| 129 | + } |
| 130 | + if d, ok := distance("c"); !ok || d != 1 { |
| 131 | + t.Errorf("c: expected distance 1, got %v (reachable %v)", d, ok) |
| 132 | + } |
| 133 | + for _, v := range []string{"b", "d"} { |
| 134 | + if d, ok := distance(v); ok { |
| 135 | + t.Errorf("%s: expected it to be unreachable, got distance %v", v, d) |
| 136 | + } |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestShortestPaths_NegativeInfinityDistance(t *testing.T) { |
| 143 | + tests := []struct { |
| 144 | + name string |
| 145 | + edges []weightedEdge |
| 146 | + }{ |
| 147 | + { |
| 148 | + name: "negative infinite weight in a cycle", |
| 149 | + edges: []weightedEdge{{"a", "b", math.Inf(-1)}, {"b", "a", 1}}, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "negative infinite weight", |
| 153 | + edges: []weightedEdge{{"a", "b", math.Inf(-1)}, {"b", "c", 1}}, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "negative cycle that sums below the float64 range", |
| 157 | + edges: []weightedEdge{{"a", "b", -1e308}, {"b", "a", -1e308}, {"c", "c", 0}}, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "path that sums below the float64 range", |
| 161 | + edges: []weightedEdge{{"a", "b", -1e308}, {"b", "c", -1e308}}, |
| 162 | + }, |
| 163 | + } |
| 164 | + |
| 165 | + for _, tt := range tests { |
| 166 | + t.Run(tt.name, func(t *testing.T) { |
| 167 | + g := directedWeighted(tt.edges...) |
| 168 | + if dist, err := BellmanFord(g, "a"); !errors.Is(err, ErrNegativeWeightCycle) { |
| 169 | + t.Errorf("BellmanFord: expected ErrNegativeWeightCycle, got %v and %v", dist, err) |
| 170 | + } |
| 171 | + if dist, err := FloydWarshall(g); !errors.Is(err, ErrNegativeWeightCycle) { |
| 172 | + t.Errorf("FloydWarshall: expected ErrNegativeWeightCycle, got %v and %v", dist, err) |
| 173 | + } |
| 174 | + if _, err := DijkstraMultiSource(g, "a"); !errors.Is(err, ErrNegativeWeight) { |
| 175 | + t.Errorf("DijkstraMultiSource: expected ErrNegativeWeight, got %v", err) |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func TestKCenter_NonFiniteWeights(t *testing.T) { |
| 182 | + tests := []struct { |
| 183 | + name string |
| 184 | + edges []weightedEdge |
| 185 | + radius float64 |
| 186 | + }{ |
| 187 | + { |
| 188 | + // b is reached through c, past the NaN edge |
| 189 | + name: "NaN weight", |
| 190 | + edges: []weightedEdge{{"a", "b", math.NaN()}, {"a", "c", 1}, {"c", "b", 1}}, |
| 191 | + radius: 2, |
| 192 | + }, |
| 193 | + { |
| 194 | + // b is only behind the +Inf edge, so a can't cover it |
| 195 | + name: "positive infinite weight", |
| 196 | + edges: []weightedEdge{{"a", "b", math.Inf(1)}, {"a", "c", 1}}, |
| 197 | + radius: math.Inf(1), |
| 198 | + }, |
| 199 | + } |
| 200 | + |
| 201 | + for _, tt := range tests { |
| 202 | + t.Run(tt.name, func(t *testing.T) { |
| 203 | + centers, radius, err := KCenter(directedWeighted(tt.edges...), 1, "a") |
| 204 | + if err != nil { |
| 205 | + t.Fatalf("unexpected error: %v", err) |
| 206 | + } |
| 207 | + if len(centers) != 1 || centers[0].Label() != "a" { |
| 208 | + t.Fatalf("expected the fixed center a, got %v", centers) |
| 209 | + } |
| 210 | + if radius != tt.radius { |
| 211 | + t.Fatalf("expected radius %v, got %v", tt.radius, radius) |
| 212 | + } |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
0 commit comments