private List <Vertice> GetPath(List <Edge> path, Vertice destinationNode) { var nodes = new List <Vertice>() { destinationNode }; var previousNode = this.GetStartNode(path, destinationNode); while (previousNode != null) { nodes.Add(previousNode); previousNode = this.GetStartNode(path, previousNode); } nodes.Reverse(); return(nodes); }
private List <Edge> FindMinimalDistances(Vertice node, List <Edge> path, HashSet <Vertice> visitedNodes, HashSet <Vertice> notVisitedNodes) { var neighbors = this.GetUnvisitedNodeNeighbors(node, visitedNodes); var edges = new List <Edge>(); foreach (var target in neighbors) { var distanceBetween = this.DirectedGraph.GetDistanceBetween(node, target); var shortestDistance = this.GetShortestDistance(path, target); if (shortestDistance > shortestDistance + distanceBetween) { notVisitedNodes.Add(target); edges.Add(this.DirectedGraph.GetEdge(node, target)); } } return(edges); }
private Vertice GetMinimumDistance(List <Edge> path, HashSet <Vertice> vertexes) { Vertice minimum = null; foreach (var vertex in vertexes) { if (minimum == null) { minimum = vertex; } else { if (this.GetShortestDistance(path, vertex) < this.GetShortestDistance(path, minimum)) { minimum = vertex; } } } return(minimum); }
/// <summary> /// Gets the weight of the minimum distance from one node to anoter /// </summary> /// <param name="startNode">The start node.</param> /// <param name="lastNode">The last node.</param> /// <returns>minimum weight</returns> public int GetLengthOfMinimumDistanceTo(Vertice startNode, Vertice lastNode) { List <Vertice> possibleRoutes = this.DirectedGraph.GetNodeNeighbors(startNode); List <Vertice> pathTo = null; foreach (var route in possibleRoutes) { pathTo = new List <Vertice>() { startNode }; var path = this.FindShortestPath(route); pathTo.AddRange(this.GetPath(path, lastNode)); if (pathTo.Last() == lastNode) { break; } } return(this.CalculateShortestPath(pathTo.ToArray())); }
/// <summary> /// Gets the number of round trips by maximum stops. /// </summary> /// <param name="startNode">The start node.</param> /// <param name="maxStops">The maximum stops.</param> /// <returns>number of possible round trips</returns> public int GetNumberOfRoundTripsByMaxStops(Vertice startNode, int maxStops) { return(this.GetNumberOfNodesTo(startNode, startNode, maxStops)); }
private Vertice GetStartNode(List <Edge> path, Vertice destination) { var destinationNode = path.FirstOrDefault(p => p.DestinationNode == destination); return(destinationNode != null ? destinationNode.StartNode : null); }
private int GetShortestDistance(List <Edge> path, Vertice destination) { var edge = path.FirstOrDefault(p => p.DestinationNode == destination); return(edge != null ? edge.Weight : int.MaxValue); }
public bool RouteExists(Vertice source, Vertice destination) { return(this.Edges.Exists(p => p.StartNode == source && p.DestinationNode == destination)); }
public Edge GetEdge(Vertice source, Vertice destination) { return(this.Edges.FirstOrDefault(p => p.StartNode == source && p.DestinationNode == destination)); }