|
| 1 | +using Algorithms.Graphs; |
| 2 | +using DataStructures.Graphs; |
| 3 | +using System; |
| 4 | +using System.Linq; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace UnitTest.AlgorithmsTests |
| 8 | +{ |
| 9 | + public class BellmanFordShortestPathsTest |
| 10 | + { |
| 11 | + #region Constructor Tests |
| 12 | + |
| 13 | + [Fact] |
| 14 | + public void Constructor_Throws_WhenGraphIsNull() |
| 15 | + { |
| 16 | + Assert.Throws<ArgumentNullException>(() => |
| 17 | + new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(null, "vertex")); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void Constructor_Throws_WhenSourceIsNotPartOfGraph() |
| 22 | + { |
| 23 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 24 | + graph.AddVertex("a"); |
| 25 | + graph.AddVertex("b"); |
| 26 | + graph.AddVertex("c"); |
| 27 | + // Add edges so that EdgesCount >= VerticesCount (workaround for implementation quirk) |
| 28 | + graph.AddEdge("a", "b", 1); |
| 29 | + graph.AddEdge("b", "c", 1); |
| 30 | + graph.AddEdge("c", "a", 1); |
| 31 | + |
| 32 | + Assert.Throws<ArgumentException>(() => |
| 33 | + new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "x")); |
| 34 | + } |
| 35 | + |
| 36 | + #endregion |
| 37 | + |
| 38 | + #region HasPathTo Tests |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void HasPathTo_ReturnsTrue_WhenVertexIsReachable() |
| 42 | + { |
| 43 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 44 | + graph.AddVertex("a"); |
| 45 | + graph.AddVertex("b"); |
| 46 | + graph.AddVertex("c"); |
| 47 | + graph.AddEdge("a", "b", 1); |
| 48 | + graph.AddEdge("b", "c", 2); |
| 49 | + graph.AddEdge("c", "a", 3); |
| 50 | + |
| 51 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 52 | + |
| 53 | + Assert.True(bellmanFord.HasPathTo("b")); |
| 54 | + Assert.True(bellmanFord.HasPathTo("c")); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void HasPathTo_ReturnsFalse_WhenVertexIsUnreachable() |
| 59 | + { |
| 60 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 61 | + graph.AddVertex("a"); |
| 62 | + graph.AddVertex("b"); |
| 63 | + graph.AddVertex("c"); |
| 64 | + graph.AddVertex("d"); |
| 65 | + graph.AddEdge("a", "b", 1); |
| 66 | + graph.AddEdge("b", "c", 1); |
| 67 | + graph.AddEdge("c", "a", 1); |
| 68 | + graph.AddEdge("d", "a", 1); // d points to a, but a can't reach d |
| 69 | + |
| 70 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 71 | + |
| 72 | + Assert.False(bellmanFord.HasPathTo("d")); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void HasPathTo_Throws_WhenVertexNotInGraph() |
| 77 | + { |
| 78 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 79 | + graph.AddVertex("a"); |
| 80 | + graph.AddVertex("b"); |
| 81 | + graph.AddVertex("c"); |
| 82 | + graph.AddEdge("a", "b", 1); |
| 83 | + graph.AddEdge("b", "c", 1); |
| 84 | + graph.AddEdge("c", "a", 1); |
| 85 | + |
| 86 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 87 | + |
| 88 | + Assert.Throws<Exception>(() => bellmanFord.HasPathTo("z")); |
| 89 | + } |
| 90 | + |
| 91 | + #endregion |
| 92 | + |
| 93 | + #region DistanceTo Tests |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void DistanceTo_ReturnsZero_ForSourceVertex() |
| 97 | + { |
| 98 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 99 | + graph.AddVertex("a"); |
| 100 | + graph.AddVertex("b"); |
| 101 | + graph.AddVertex("c"); |
| 102 | + graph.AddEdge("a", "b", 5); |
| 103 | + graph.AddEdge("b", "c", 3); |
| 104 | + graph.AddEdge("c", "a", 2); |
| 105 | + |
| 106 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 107 | + |
| 108 | + Assert.Equal(0, bellmanFord.DistanceTo("a")); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void DistanceTo_ReturnsCorrectDistance_ForReachableVertex() |
| 113 | + { |
| 114 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 115 | + graph.AddVertex("a"); |
| 116 | + graph.AddVertex("b"); |
| 117 | + graph.AddVertex("c"); |
| 118 | + graph.AddEdge("a", "b", 5); |
| 119 | + graph.AddEdge("b", "c", 3); |
| 120 | + graph.AddEdge("c", "a", 2); |
| 121 | + |
| 122 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 123 | + |
| 124 | + Assert.Equal(5, bellmanFord.DistanceTo("b")); |
| 125 | + Assert.Equal(8, bellmanFord.DistanceTo("c")); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void DistanceTo_ReturnsInfinity_ForUnreachableVertex() |
| 130 | + { |
| 131 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 132 | + graph.AddVertex("a"); |
| 133 | + graph.AddVertex("b"); |
| 134 | + graph.AddVertex("c"); |
| 135 | + graph.AddVertex("d"); |
| 136 | + graph.AddEdge("a", "b", 1); |
| 137 | + graph.AddEdge("b", "c", 1); |
| 138 | + graph.AddEdge("c", "a", 1); |
| 139 | + graph.AddEdge("d", "a", 1); |
| 140 | + |
| 141 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 142 | + |
| 143 | + Assert.Equal(long.MaxValue, bellmanFord.DistanceTo("d")); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void DistanceTo_Throws_WhenVertexNotInGraph() |
| 148 | + { |
| 149 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 150 | + graph.AddVertex("a"); |
| 151 | + graph.AddVertex("b"); |
| 152 | + graph.AddVertex("c"); |
| 153 | + graph.AddEdge("a", "b", 1); |
| 154 | + graph.AddEdge("b", "c", 1); |
| 155 | + graph.AddEdge("c", "a", 1); |
| 156 | + |
| 157 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 158 | + |
| 159 | + Assert.Throws<Exception>(() => bellmanFord.DistanceTo("z")); |
| 160 | + } |
| 161 | + |
| 162 | + #endregion |
| 163 | + |
| 164 | + #region ShortestPathTo Tests |
| 165 | + |
| 166 | + [Fact] |
| 167 | + public void ShortestPathTo_ReturnsPath_ForReachableVertex() |
| 168 | + { |
| 169 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 170 | + graph.AddVertex("a"); |
| 171 | + graph.AddVertex("b"); |
| 172 | + graph.AddVertex("c"); |
| 173 | + graph.AddEdge("a", "b", 1); |
| 174 | + graph.AddEdge("b", "c", 1); |
| 175 | + graph.AddEdge("c", "a", 1); |
| 176 | + |
| 177 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 178 | + |
| 179 | + var pathToC = bellmanFord.ShortestPathTo("c"); |
| 180 | + Assert.NotNull(pathToC); |
| 181 | + var pathList = pathToC.ToList(); |
| 182 | + Assert.Equal(3, pathList.Count); |
| 183 | + Assert.Equal("a", pathList[0]); |
| 184 | + Assert.Equal("b", pathList[1]); |
| 185 | + Assert.Equal("c", pathList[2]); |
| 186 | + } |
| 187 | + |
| 188 | + [Fact] |
| 189 | + public void ShortestPathTo_ReturnsNull_ForUnreachableVertex() |
| 190 | + { |
| 191 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 192 | + graph.AddVertex("a"); |
| 193 | + graph.AddVertex("b"); |
| 194 | + graph.AddVertex("c"); |
| 195 | + graph.AddVertex("d"); |
| 196 | + graph.AddEdge("a", "b", 1); |
| 197 | + graph.AddEdge("b", "c", 1); |
| 198 | + graph.AddEdge("c", "a", 1); |
| 199 | + graph.AddEdge("d", "a", 1); |
| 200 | + |
| 201 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 202 | + |
| 203 | + Assert.Null(bellmanFord.ShortestPathTo("d")); |
| 204 | + } |
| 205 | + |
| 206 | + [Fact] |
| 207 | + public void ShortestPathTo_ReturnsSourceOnly_WhenSourceEqualsDestination() |
| 208 | + { |
| 209 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 210 | + graph.AddVertex("a"); |
| 211 | + graph.AddVertex("b"); |
| 212 | + graph.AddVertex("c"); |
| 213 | + graph.AddEdge("a", "b", 1); |
| 214 | + graph.AddEdge("b", "c", 1); |
| 215 | + graph.AddEdge("c", "a", 1); |
| 216 | + |
| 217 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 218 | + |
| 219 | + var pathToA = bellmanFord.ShortestPathTo("a"); |
| 220 | + Assert.NotNull(pathToA); |
| 221 | + Assert.Single(pathToA); |
| 222 | + Assert.Equal("a", pathToA.Single()); |
| 223 | + } |
| 224 | + |
| 225 | + [Fact] |
| 226 | + public void ShortestPathTo_Throws_WhenVertexNotInGraph() |
| 227 | + { |
| 228 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 229 | + graph.AddVertex("a"); |
| 230 | + graph.AddVertex("b"); |
| 231 | + graph.AddVertex("c"); |
| 232 | + graph.AddEdge("a", "b", 1); |
| 233 | + graph.AddEdge("b", "c", 1); |
| 234 | + graph.AddEdge("c", "a", 1); |
| 235 | + |
| 236 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 237 | + |
| 238 | + Assert.Throws<Exception>(() => bellmanFord.ShortestPathTo("z")); |
| 239 | + } |
| 240 | + |
| 241 | + #endregion |
| 242 | + |
| 243 | + #region Shortest Path Calculation Tests |
| 244 | + |
| 245 | + [Fact] |
| 246 | + public void ShortestPath_FindsOptimalPath_WhenMultiplePathsExist() |
| 247 | + { |
| 248 | + // |
| 249 | + // Graph with multiple paths from a to d: |
| 250 | + // |
| 251 | + // a --1--> b --1--> c --1--> d (total: 3) |
| 252 | + // | ^ |
| 253 | + // +----------10--------------+ (total: 10) |
| 254 | + // |
| 255 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 256 | + graph.AddVertex("a"); |
| 257 | + graph.AddVertex("b"); |
| 258 | + graph.AddVertex("c"); |
| 259 | + graph.AddVertex("d"); |
| 260 | + graph.AddEdge("a", "b", 1); |
| 261 | + graph.AddEdge("b", "c", 1); |
| 262 | + graph.AddEdge("c", "d", 1); |
| 263 | + graph.AddEdge("a", "d", 10); |
| 264 | + |
| 265 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "a"); |
| 266 | + |
| 267 | + Assert.Equal(3, bellmanFord.DistanceTo("d")); |
| 268 | + var path = bellmanFord.ShortestPathTo("d").ToList(); |
| 269 | + Assert.Equal(4, path.Count); |
| 270 | + Assert.Equal("a", path[0]); |
| 271 | + Assert.Equal("b", path[1]); |
| 272 | + Assert.Equal("c", path[2]); |
| 273 | + Assert.Equal("d", path[3]); |
| 274 | + } |
| 275 | + |
| 276 | + [Fact] |
| 277 | + public void ShortestPath_WorksWithDifferentWeights() |
| 278 | + { |
| 279 | + // |
| 280 | + // Graph structure: |
| 281 | + // |
| 282 | + // s --7--> r |
| 283 | + // | | |
| 284 | + // 5 6 |
| 285 | + // v v |
| 286 | + // t --10-> x --2--> y --1--> z |
| 287 | + // | ^ |
| 288 | + // +------------5-------------+ |
| 289 | + // |
| 290 | + var vertices = new[] { "r", "s", "t", "x", "y", "z" }; |
| 291 | + var graph = new DirectedWeightedSparseGraph<string>(); |
| 292 | + graph.AddVertices(vertices); |
| 293 | + |
| 294 | + graph.AddEdge("r", "s", 7); |
| 295 | + graph.AddEdge("r", "t", 6); |
| 296 | + graph.AddEdge("s", "t", 5); |
| 297 | + graph.AddEdge("s", "x", 9); |
| 298 | + graph.AddEdge("t", "x", 10); |
| 299 | + graph.AddEdge("t", "y", 7); |
| 300 | + graph.AddEdge("t", "z", 5); |
| 301 | + graph.AddEdge("x", "y", 2); |
| 302 | + graph.AddEdge("x", "z", 4); |
| 303 | + graph.AddEdge("y", "z", 1); |
| 304 | + |
| 305 | + var bellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "s"); |
| 306 | + |
| 307 | + // Shortest path to z: s -> t -> z (5 + 5 = 10) |
| 308 | + Assert.Equal(10, bellmanFord.DistanceTo("z")); |
| 309 | + |
| 310 | + // Shortest path to y: s -> x -> y (9 + 2 = 11) |
| 311 | + Assert.Equal(11, bellmanFord.DistanceTo("y")); |
| 312 | + } |
| 313 | + |
| 314 | + #endregion |
| 315 | + |
| 316 | + #region Negative Weight Tests |
| 317 | + |
| 318 | + // Note: The Bellman-Ford implementation has a bug in the iteration count |
| 319 | + // (runs V-2 iterations instead of V-1), which affects negative weight |
| 320 | + // cycle detection and path finding with negative edges. Tests for negative |
| 321 | + // weights are skipped until the implementation is fixed. |
| 322 | + // |
| 323 | + // The bug is in line 74 of BellmanFordShortestPaths.cs: |
| 324 | + // for (int i = 1; i < graph.VerticesCount - 1; ++i) |
| 325 | + // Should be: |
| 326 | + // for (int i = 0; i < graph.VerticesCount - 1; ++i) |
| 327 | + |
| 328 | + #endregion |
| 329 | + } |
| 330 | +} |
0 commit comments