示例#1
0
        public static List <Edge <T> > GetMSTUsingPrims <T>(this Graph <T> g)
        {
            BinaryMinHeap <Vertex <T> > minHeap = new BinaryMinHeap <Vertex <T> >();
            //Set all nodes in hash map to infinity

            Dictionary <Vertex <T>, Edge <T> > vertexToEdgeMap = new Dictionary <Vertex <T>, Edge <T> >();

            //Final result
            var result = new List <Edge <T> >();

            //insert all vertices with infinite value initially.
            foreach (var v in g.AllVertex.Values)
            {
                minHeap.AddNode(Int32.MaxValue, v);
            }

            //Start from Random Vertex and decrease min heap to 0
            Vertex <T> startVertex = g.AllVertex.FirstOrDefault().Value;

            minHeap.Decrease(startVertex, 0);

            //iterate till heap + map has elements in it
            while (!minHeap.IsEmpty())
            {
                //Extract the min vertex from heap
                var minVertex = minHeap.extractMin().Data;

                //get the corresponding edge for this vertex if present and add it to final result.
                //This edge wont be present for first vertex.
                Edge <T> spanningTreeEdge = vertexToEdgeMap.ContainsKey(minVertex) ? vertexToEdgeMap[minVertex] : null;

                if (spanningTreeEdge != null)
                {
                    result.Add(spanningTreeEdge);
                }

                //Iterate through all the edges for the current minVertex
                foreach (var edge in minVertex.GetAdjEdges())
                {
                    Vertex <T> otherVertex = GetVertexForEdge(minVertex, edge);

                    //Check if the other vertex already exists in the map and weight attached is greater than weight of the edge. If yes, replace
                    if (minHeap.ContainsData(otherVertex) && minHeap.GetWeight(otherVertex) > edge.Weight)
                    {
                        minHeap.Decrease(otherVertex, edge.Weight);
                        if (vertexToEdgeMap.ContainsKey(otherVertex))
                        {
                            vertexToEdgeMap[otherVertex] = edge;
                        }
                        else
                        {
                            vertexToEdgeMap.Add(otherVertex, edge);
                        }
                    }
                }
            }

            return(result);
        }
示例#2
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);
        }