/// <summary> /// Computes the shortest path between two nodes in a graph. /// </summary> /// <returns>Object <code>GraphRouteAlgorithmReturn</code> with the information about the path found. <code>null</code> otherwise.</returns> public GraphRouteAlgorithmReturn ComputeShortestRoute() { FindShortestPath(); PredecessorData pred = predecessor.Find(p => p.Node.Equals(endNode)); if (pred != null) { WeightData data = weight.Find(c => c.Node.Equals(pred.Node)); if (data.Weight != int.MaxValue) { GraphRouteAlgorithmReturn path = new GraphRouteAlgorithmReturn(); path.Value = data.Weight; path.Path = BuildPathCharge(predecessor, startNode, endNode); return(path); } else { return(null); } } else { return(null); } }
/// <summary> /// Computes all routes between two nodes in a graph. /// </summary> /// <returns>List of <code>GraphRouteAlgorithmReturn</code> with the informations about the paths found. Empty list otherwise.</returns> public List <GraphRouteAlgorithmReturn> ComputeAllRoutes() { ComputeRoutes(); List <GraphRouteAlgorithmReturn> result = new List <GraphRouteAlgorithmReturn>(); foreach (string route in routes) { GraphRouteAlgorithmReturn path = new GraphRouteAlgorithmReturn(); path.Value = route.Split('-').Length - 1; path.Path = route; result.Add(path); } return(result); }
public void ComputeShortestRouteTest() { // arrange string graphInfo = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7"; // act GraphModel graph = GraphBusiness.BuildGraph(graphInfo); // assert GraphRouteAlgorithmReturn result = GraphBusiness.ComputeShortestRoute(graph, 'A', 'C'); Assert.AreEqual(9, result.Value); Assert.AreEqual("A-B-C", result.Path); result = GraphBusiness.ComputeShortestRoute(graph, 'B', 'B'); Assert.AreEqual(9, result.Value); Assert.AreEqual("B-C-E-B", result.Path); }
/// <summary> /// Computes all routes between two nodes in a graph. /// <param name="maxStopNumber">If <code>true</code> indicates that the algorithm will search for a route with max number of stops, /// otherwise, algorithm will search for a route with exactly number of stops</param> /// </summary> /// <returns>List of <code>GraphRouteAlgorithmReturn</code> with the informations about the paths found. Empty list otherwise.</returns> private List <GraphRouteAlgorithmReturn> ComputeAllRoutes(bool isMaxStopNumber) { ComputeRoutes(true); List <GraphRouteAlgorithmReturn> result = new List <GraphRouteAlgorithmReturn>(); foreach (string route in routes) { GraphRouteAlgorithmReturn path = new GraphRouteAlgorithmReturn(); path.Value = route.Split('-').Length - 1; // number of stops in path path.Path = route; if (isMaxStopNumber || (!isMaxStopNumber && path.Value == stopNumber)) { result.Add(path); } } return(result); }