コード例 #1
0
        /// <summary>
        /// Augment current Path to residual Graph
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="residualGraph"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        private W AugmentResidualGraph(WeightedDiGraph <T, W> graph,
                                       WeightedDiGraph <T, W> residualGraph, List <T> path)
        {
            var min = operators.MaxWeight;

            for (int i = 0; i < path.Count - 1; i++)
            {
                var vertex_1 = residualGraph.FindVertex(path[i]);
                var vertex_2 = residualGraph.FindVertex(path[i + 1]);

                var edgeValue = vertex_1.OutEdges[vertex_2];

                if (min.CompareTo(edgeValue) > 0)
                {
                    min = edgeValue;
                }
            }

            //augment path
            for (int i = 0; i < path.Count - 1; i++)
            {
                var vertex_1 = residualGraph.FindVertex(path[i]);
                var vertex_2 = residualGraph.FindVertex(path[i + 1]);

                //substract from forward paths
                vertex_1.OutEdges[vertex_2] = operators.SubstractWeights(vertex_1.OutEdges[vertex_2], min);

                //add for backward paths
                vertex_2.OutEdges[vertex_1] = operators.AddWeights(vertex_2.OutEdges[vertex_1], min);
            }

            return(min);
        }
コード例 #2
0
        /// <summary>
        /// Get shortest distance to target
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        public ShortestPathResult <T, W> GetShortestPath(WeightedDiGraph <T, W> graph, T source, T destination)
        {
            //regular argument checks
            if (graph == null || graph.FindVertex(source) == null ||
                graph.FindVertex(destination) == null)
            {
                throw new ArgumentException();
            }

            var progress  = new Dictionary <T, W>();
            var parentMap = new Dictionary <T, T>();

            foreach (var vertex in graph.Vertices)
            {
                parentMap.Add(vertex.Key, default(T));
                progress.Add(vertex.Key, operators.MaxValue);
            }

            progress[source] = operators.DefaultValue;

            var iterations = graph.Vertices.Count - 1;
            var updated    = true;

            while (iterations > 0 && updated)
            {
                updated = false;

                foreach (var vertex in graph.Vertices)
                {
                    //skip not discovered nodes
                    if (progress[vertex.Key].Equals(operators.MaxValue))
                    {
                        continue;
                    }

                    foreach (var edge in vertex.Value.OutEdges)
                    {
                        var currentDistance = progress[edge.Key.Value];
                        var newDistance     = operators.Sum(progress[vertex.Key],
                                                            vertex.Value.OutEdges[edge.Key]);

                        if (newDistance.CompareTo(currentDistance) < 0)
                        {
                            updated = true;
                            progress[edge.Key.Value]  = newDistance;
                            parentMap[edge.Key.Value] = vertex.Key;
                        }
                    }
                }

                iterations--;

                if (iterations < 0)
                {
                    throw new Exception("Negative cycle exists in this graph.");
                }
            }

            return(tracePath(graph, parentMap, source, destination));
        }
コード例 #3
0
        /// <summary>
        /// Search path to target using the heuristic.
        /// </summary>
        public ShortestPathResult <T, W> FindShortestPath(WeightedDiGraph <T, W> graph, T source, T destination)
        {
            //regular argument checks
            if (graph?.FindVertex(source) == null || graph.FindVertex(destination) == null)
            {
                throw new ArgumentException();
            }

            //track progress for distance to each Vertex from source
            var progress = new Dictionary <T, W>();

            //trace our current path by mapping current vertex to its Parent
            var parentMap = new Dictionary <T, T>();

            //min heap to pick next closest vertex
            var minHeap = new FibonacciHeap <AStarWrap <T, W> >();
            //keep references of heap Node for decrement key operation
            var heapMapping = new Dictionary <T, AStarWrap <T, W> >();

            //add vertices to min heap and progress map
            foreach (var vertex in graph.Vertices)
            {
                //init parent
                parentMap.Add(vertex.Key, default(T));

                //init to max value
                progress.Add(vertex.Key, operators.MaxValue);

                if (vertex.Key.Equals(source))
                {
                    continue;
                }
            }

            //start from source vertex as current
            var current = new AStarWrap <T, W>(heuristic, destination)
            {
                Distance = operators.DefaultValue,
                Vertex   = source
            };

            //insert neighbour in heap
            minHeap.Insert(current);
            heapMapping[source] = current;

            //until heap is empty
            while (minHeap.Count > 0)
            {
                //next min vertex to visit
                current = minHeap.Extract();
                heapMapping.Remove(current.Vertex);

                //no path exists, so return max value
                if (current.Distance.Equals(operators.MaxValue))
                {
                    return(new ShortestPathResult <T, W>(null, operators.MaxValue));
                }

                //visit neighbours of current
                foreach (var neighbour in graph.Vertices[current.Vertex].OutEdges.Where(x => !x.Key.Value.Equals(source)))
                {
                    //new distance to neighbour
                    var newDistance = operators.Sum(current.Distance,
                                                    graph.Vertices[current.Vertex].OutEdges[neighbour.Key]);

                    //current distance to neighbour
                    var existingDistance = progress[neighbour.Key.Value];

                    //update distance if new is better
                    if (newDistance.CompareTo(existingDistance) < 0)
                    {
                        progress[neighbour.Key.Value] = newDistance;

                        if (heapMapping.ContainsKey(neighbour.Key.Value))
                        {
                            //decrement distance to neighbour in heap
                            var decremented = new AStarWrap <T, W>(heuristic, destination)
                            {
                                Distance = newDistance, Vertex = neighbour.Key.Value
                            };
                            minHeap.UpdateKey(heapMapping[neighbour.Key.Value], decremented);
                            heapMapping[neighbour.Key.Value] = decremented;
                        }
                        else
                        {
                            //insert neighbour in heap
                            var discovered = new AStarWrap <T, W>(heuristic, destination)
                            {
                                Distance = newDistance, Vertex = neighbour.Key.Value
                            };
                            minHeap.Insert(discovered);
                            heapMapping[neighbour.Key.Value] = discovered;
                        }

                        //trace parent
                        parentMap[neighbour.Key.Value] = current.Vertex;
                    }
                }
            }

            return(tracePath(graph, parentMap, source, destination));
        }
コード例 #4
0
        /// <summary>
        /// Get shortest distance to target
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        public ShortestPathResult <T, W> GetShortestPath(WeightedDiGraph <T, W> graph, T source, T destination)
        {
            //regular argument checks
            if (graph == null || graph.FindVertex(source) == null ||
                graph.FindVertex(destination) == null)
            {
                throw new ArgumentException();
            }

            //track progress for distance to each Vertex from source
            var progress = new Dictionary <T, W>();

            //trace our current path by mapping current vertex to its Parent
            var parentMap = new Dictionary <T, T>();

            //min heap to pick next closest vertex
            var minHeap = new FibornacciMinHeap <MinHeapWrap <T, W> >();
            //keep references of heap Node for decrement key operation
            var heapMapping = new Dictionary <T, FibornacciHeapNode <MinHeapWrap <T, W> > >();

            //add vertices to min heap and progress map
            foreach (var vertex in graph.Vertices)
            {
                //init parent
                parentMap.Add(vertex.Key, default(T));

                //init to max value
                progress.Add(vertex.Key, operators.MaxValue);

                if (vertex.Key.Equals(source))
                {
                    continue;
                }

                //construct heap for all vertices with Max Distance as default
                var wrap = new MinHeapWrap <T, W>()
                {
                    Distance = operators.MaxValue,
                    Target   = vertex.Key
                };

                var heapNode = minHeap.Insert(wrap);
                heapMapping.Add(vertex.Key, heapNode);
            }

            //start from source vertex as current
            var sourceVertex = graph.Vertices[source];
            var current      = new MinHeapWrap <T, W>()
            {
                Distance = operators.DefaultValue,
                Target   = source
            };

            //until heap is empty
            while (minHeap.Count > 0)
            {
                //no path exists, so return max value
                if (current.Distance.Equals(operators.MaxValue))
                {
                    return(new ShortestPathResult <T, W>(null, operators.MaxValue));
                }

                //visit neighbours of current
                foreach (var neighbour in graph.Vertices[current.Target].OutEdges)
                {
                    //new distance to neighbour
                    var newDistance = operators.Sum(current.Distance,
                                                    graph.Vertices[current.Target].OutEdges[neighbour.Key]);

                    //current distance to neighbour
                    var existingDistance = progress[neighbour.Key.Value];

                    //update distance if new is better
                    if (newDistance.CompareTo(existingDistance) < 0)
                    {
                        progress[neighbour.Key.Value] = newDistance;
                        heapMapping[neighbour.Key.Value].Value.Distance = newDistance;

                        //decrement distance to neighbour in heap
                        minHeap.DecrementKey(heapMapping[neighbour.Key.Value]);

                        //trace parent
                        parentMap[neighbour.Key.Value] = current.Target;
                    }
                }

                //next min vertex to visit
                current = minHeap.ExtractMin();
            }

            return(tracePath(graph, parentMap, source, destination));
        }
コード例 #5
0
ファイル: Dijikstra.cs プロジェクト: Greibus/Dashboard
        public ShortestPathResult <T, W> GetShortestPath(WeightedDiGraph <T, W> graph, T source, T destination)
        {
            if (graph == null || graph.FindVertex(source) == null ||
                graph.FindVertex(destination) == null)
            {
                throw new ArgumentException();
            }

            var progress = new Dictionary <T, W>();

            var parentMap = new Dictionary <T, T>();

            var minHeap     = new FibornacciMinHeap <MinHeapWrap <T, W> >();
            var heapMapping = new Dictionary <T, FibornacciHeapNode <MinHeapWrap <T, W> > >();

            foreach (var vertex in graph.Vertices)
            {
                parentMap.Add(vertex.Key, default(T));

                progress.Add(vertex.Key, operators.MaxValue);

                if (vertex.Key.Equals(source))
                {
                    continue;
                }

                var wrap = new MinHeapWrap <T, W>()
                {
                    Distance = operators.MaxValue,
                    Target   = vertex.Key
                };

                var heapNode = minHeap.Insert(wrap);
                heapMapping.Add(vertex.Key, heapNode);
            }

            var sourceVertex = graph.Vertices[source];
            var current      = new MinHeapWrap <T, W>()
            {
                Distance = operators.DefaultValue,
                Target   = source
            };

            while (minHeap.Count > 0)
            {
                if (current.Distance.Equals(operators.MaxValue))
                {
                    return(new ShortestPathResult <T, W>(null, operators.MaxValue));
                }

                foreach (var neighbour in graph.Vertices[current.Target].OutEdges)
                {
                    var newDistance = operators.Sum(current.Distance,
                                                    graph.Vertices[current.Target].OutEdges[neighbour.Key]);

                    var existingDistance = progress[neighbour.Key.Value];

                    if (newDistance.CompareTo(existingDistance) < 0)
                    {
                        progress[neighbour.Key.Value] = newDistance;
                        heapMapping[neighbour.Key.Value].Value.Distance = newDistance;

                        minHeap.DecrementKey(heapMapping[neighbour.Key.Value]);

                        parentMap[neighbour.Key.Value] = current.Target;
                    }
                }

                current = minHeap.ExtractMin();
            }

            return(tracePath(graph, parentMap, source, destination));
        }