예제 #1
0
        public TravelPath findCheapestPath(int startLocation, int goal)
        {
            var res = _graph.findChepestPaths(startLocation, goal);

            if (res.Count() == 1)
            {
                return(res.First());
            }
            else
            {
                return(null);
            }
        }
예제 #2
0
        public IEnumerable <TravelPath> findCheapestFirePaths(int startLocation)
        {
            //reduction: want to find chepest path that include  edge with fire
            // insert node "inside" the fire edges and calculate the distance to it
            //in order to get the right the path insert "opposite" edge inside the fire edge
            //with cost infinity.
            //edges between real node and virtual node will be with original cost
            TravelGraph           reductionGraph     = createWorldWithClearWays();
            List <int>            fireNodes          = new List <int>();
            int                   generatePlace      = reductionGraph.Vertices.Max();
            Dictionary <int, int> virtualToRealPlace = new Dictionary <int, int>();

            foreach (var edge in _fireWays)
            {
                var newNodeTarget = ++generatePlace;
                reductionGraph.AddVertex(newNodeTarget);

                var newNodeSource = ++generatePlace;
                reductionGraph.AddVertex(newNodeSource);

                reductionGraph.AddEdge(new TravelEdge(edge.Source, newNodeTarget), _graph.GetCost(edge));
                reductionGraph.AddEdge(new TravelEdge(newNodeSource, edge.Target), _graph.GetCost(edge));

                //opposite edge
                virtualToRealPlace.Add(newNodeTarget, edge.Target);
                virtualToRealPlace.Add(newNodeSource, edge.Source);
                reductionGraph.AddEdge(new TravelEdge(newNodeSource, newNodeTarget), double.MaxValue);

                fireNodes.Add(newNodeSource);
                fireNodes.Add(newNodeTarget);
            }

            var paths = reductionGraph.findChepestPaths(startLocation, fireNodes.ToArray()).ToArray();

            foreach (var path in paths)
            {
                foreach (var virt in virtualToRealPlace.Keys)
                {
                    path.Replace(virt, virtualToRealPlace[virt]);
                }
            }
            return(paths);
        }