/// <summary> /// Creates the path string to return according Predecessor list. /// </summary> /// <param name="dataList">List that contains all predecessors of a Node.</param> /// <param name="startNode">Initial node of a path.</param> /// <param name="endNode">End node of a path.</param> /// <returns>String that contains the path.</returns> private static string BuildPathCharge(List <PredecessorData> dataList, char startNode, char endNode) { PredecessorData data = dataList.Find(d => d.Node.Equals(endNode)); string result = ""; while (data != null) { if (result.Length > 0) { result = Convert.ToString(data.Node) + "-" + result; } else { result = Convert.ToString(data.Node); } data = dataList.Find(d => d.Node.Equals(data.Predecessor)); if (data != null && data.Node.Equals(startNode)) { result = Convert.ToString(data.Node) + "-" + result; break; } else if (data == null) { result = Convert.ToString(startNode) + "-" + result; } } return(result); }
/// <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> /// Adds information about a node and your predecessor in node's predecessors list. /// </summary> /// <param name="dataList">List that contains all predecessors of a Node.</param> /// <param name="node">Graph node.</param> /// <param name="pred">Predecessor of a node.</param> /// <returns>List with predecessors of a node, updated with information provided.</returns> internal static List <PredecessorData> AddToList(List <PredecessorData> dataList, char node, char pred) { PredecessorData data = dataList.Find(p => p.Node.Equals(node)); if (data == null) { dataList.Add(new PredecessorData { Node = node, Predecessor = pred }); } else { data.Predecessor = pred; } return(dataList); }
/// <summary> /// Computes the Shorthest Path between two nodes. It creates a Minimum spanning tree of a Graph. /// </summary> private void FindShortestPath() { char minEdgeCharge; bool addPredecessor; while (edge.Count > 0) { addPredecessor = false; // Gets the node that has the minimum weight minEdgeCharge = FindMinimumWeightNodeFromEdge(); edge.Remove(minEdgeCharge); // Adds the node to spanning tree tree.Add(minEdgeCharge); // Finds and iterate on the edges of a node that has the minimum weight at the moment Node minNode = graph.Nodes.Find(n => n.Label.Equals(minEdgeCharge)); foreach (Node node in graph.Nodes) { if ((minNode.edges.Find(n => n.NodeLabel.Equals(node.Label)) != null) && IsMinimumWeight(minNode, node)) { predecessor = PredecessorData.AddToList(predecessor, node.Label, minNode.Label); SetWeight(minNode, node); if (!startNode.Equals(node.Label)) { edge.Add(node.Label); } else { tree.Add(node.Label); } addPredecessor = true; } } if (!addPredecessor) { tree.Remove(minEdgeCharge); } } }