Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        /// <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()));
        }
Ejemplo n.º 5
0
 /// <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));
 }
Ejemplo n.º 6
0
        private Vertice GetStartNode(List <Edge> path, Vertice destination)
        {
            var destinationNode = path.FirstOrDefault(p => p.DestinationNode == destination);

            return(destinationNode != null ? destinationNode.StartNode : null);
        }
Ejemplo n.º 7
0
        private int GetShortestDistance(List <Edge> path, Vertice destination)
        {
            var edge = path.FirstOrDefault(p => p.DestinationNode == destination);

            return(edge != null ? edge.Weight : int.MaxValue);
        }
Ejemplo n.º 8
0
 public bool RouteExists(Vertice source, Vertice destination)
 {
     return(this.Edges.Exists(p => p.StartNode == source && p.DestinationNode == destination));
 }
Ejemplo n.º 9
0
 public Edge GetEdge(Vertice source, Vertice destination)
 {
     return(this.Edges.FirstOrDefault(p => p.StartNode == source && p.DestinationNode == destination));
 }