Beispiel #1
0
        public static Dictionary <Vertex <T>, Int32> DjkstrasAlgo <T>(this Graph <T> g, Vertex <T> source, Dictionary <Vertex <T>, Vertex <T> > pathMap)
        {
            BinaryMinHeap <Vertex <T> > minHeap = new BinaryMinHeap <Vertex <T> >();

            Dictionary <Vertex <T>, Int32> distanceMap = new Dictionary <Vertex <T>, int>();

            //Dictionary<Vertex<T>, Vertex<T>> pathMap = new Dictionary<Vertex<T>, Vertex<T>>();

            //Set all weights to infinity in minHeap
            foreach (var v in g.AllVertex.Values)
            {
                minHeap.AddNode(Int32.MaxValue, v);
            }

            //Decrease the weight of source to 0
            minHeap.Decrease(source, 0);


            pathMap.Add(source, null);

            while (!minHeap.IsEmpty())
            {
                //Extract the min
                int weight        = minHeap.MinNode().Weight;
                var currentVertex = minHeap.extractMin().Data;
                distanceMap.AddOrUpdateDictionary(currentVertex, weight);

                foreach (var edge in currentVertex.GetAdjEdges())
                {
                    var otherVertex = GetVertexForEdge(currentVertex, edge);
                    if (minHeap.ContainsData(otherVertex) && minHeap.GetWeight(otherVertex) > (edge.Weight + weight))
                    {
                        minHeap.Decrease(otherVertex, (edge.Weight + weight));
                        pathMap.AddOrUpdateDictionary(otherVertex, currentVertex);
                    }
                }
            }

            return(distanceMap);
        }