示例#1
0
        public DijResult GetDirResult(Node startNode, Node endNode, Graph graph)
        {
            var  result        = new DijResult();
            var  routes        = result.Routes = new List <Route>();
            Node tempStartNode = null;
            Node tempEndNode   = endNode;

            while (tempStartNode != startNode)
            {
                //var row = this[endNode]
                tempStartNode = this[tempEndNode].PreviousVertex;
                var route = graph.GetLink(tempStartNode, tempEndNode);
                routes.Add(new Route(from: tempStartNode, to: tempEndNode, distance: route.Distance));
                tempEndNode = tempStartNode;
            }
            routes.Reverse();
            return(result);
        }
示例#2
0
        public DijResult Dij(string from, string to)
        {
            var         startNode      = this[from];
            var         endNode        = this[to];
            List <Node> visitedNodes   = new List <Node>();
            List <Node> unvisitedNodes = new List <Node>(this.Nodes);
            var         shortestDistancesFromStartNode = new ShortestPathsTable();

            unvisitedNodes.ForEach((unvisitedNode) =>
            {
                if (unvisitedNode == startNode)
                {
                    shortestDistancesFromStartNode.Add(unvisitedNode, new ShortestPathsTableRow(0, startNode));
                }
                else
                {
                    shortestDistancesFromStartNode.Add(unvisitedNode, new ShortestPathsTableRow(double.MaxValue, null));
                }
            });
            var visitingNode = startNode;
            var routes       = RoutesContainGivenNode(startNode);

            routes.ForEach((route) =>
            {
                var otherNode   = route.OtherNode(visitingNode);
                var tableRow    = shortestDistancesFromStartNode[otherNode];
                var oldDistance = tableRow.ShortestDistance;
                var newDistance = route.Distance;
                if (newDistance < oldDistance)
                {
                    tableRow.ShortestDistance = newDistance;
                    tableRow.PreviousVertex   = visitingNode;
                }
            });
            unvisitedNodes.Remove(visitingNode);
            visitedNodes.Add(visitingNode);

            while (unvisitedNodes.Count != 0)
            {
                var  min = double.MaxValue;
                Node closestNodeUnvisited = null;
                unvisitedNodes.ForEach((node) =>
                {
                    var current = shortestDistancesFromStartNode[node].ShortestDistance;
                    if (current < min)
                    {
                        min = current;
                        closestNodeUnvisited = node;
                    }
                });
                visitingNode = closestNodeUnvisited;
                var routesInLoop = RoutesContainGivenNode(visitingNode, visitedNodes);
                routesInLoop.ForEach((route) =>
                {
                    var otherNode   = route.OtherNode(visitingNode);
                    var tableRow    = shortestDistancesFromStartNode[otherNode];
                    var oldDistance = tableRow.ShortestDistance;
                    var newDistance = route.Distance + shortestDistancesFromStartNode[visitingNode].ShortestDistance;
                    if (newDistance < oldDistance)
                    {
                        tableRow.ShortestDistance = newDistance;
                        tableRow.PreviousVertex   = visitingNode;
                    }
                    ;
                });
                unvisitedNodes.Remove(visitingNode);
                visitedNodes.Add(visitingNode);
                ;
            }
            var dijResult       = new DijResult();
            var dijResultRoutes = dijResult.Routes;
            var routeQuene      = new Queue <Route>();
            var result          = shortestDistancesFromStartNode.GetDirResult(startNode: startNode, endNode: endNode, graph: this);

            result.Graph = this;
            return(result);
        }