示例#1
0
        private static void TestBinaryMinHeap()
        {
            BinaryMinHeap <String> heap = new BinaryMinHeap <String>();

            heap.AddNode(3, "Tushar");
            heap.AddNode(4, "Ani");
            heap.AddNode(8, "Vijay");
            heap.AddNode(10, "Pramila");
            heap.AddNode(5, "Roy");
            heap.AddNode(6, "NTF");
            heap.PrintHeap();

            heap.Decrease("Pramila", 1);


            heap.Decrease("Vijay", 1);
            heap.Decrease("Ani", 11);
            heap.Decrease("NTF", 4);

            heap.PrintHeap();

            var node = heap.extractMin();

            while (node != null)
            {
                Console.WriteLine("Min Node extracted is :" + node.Data + " " + node.Weight);
                heap.PrintHeap();

                node = heap.extractMin();
            }
        }
示例#2
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);
        }
        public void TestBinaryMinHeapDecrease()
        {
            var sut = new BinaryMinHeap <char>();
            var a   = new BinaryMinHeap <char> .Node {
                Id = 'a', Weight = -1
            };
            var b = new BinaryMinHeap <char> .Node {
                Id = 'b', Weight = 2
            };
            var c = new BinaryMinHeap <char> .Node {
                Id = 'c', Weight = 6
            };
            var d = new BinaryMinHeap <char> .Node {
                Id = 'd', Weight = 4
            };
            var e = new BinaryMinHeap <char> .Node {
                Id = 'e', Weight = 5
            };
            var f = new BinaryMinHeap <char> .Node {
                Id = 'f', Weight = 7
            };
            var g = new BinaryMinHeap <char> .Node {
                Id = 'g', Weight = 8
            };

            sut.Add(a);
            sut.Add(b);
            sut.Add(c);
            sut.Add(d);
            sut.Add(e);
            sut.Add(f);
            sut.Add(g);

            f.Weight = -2;
            sut.Decrease(f);

            g.Weight = -3;
            sut.Decrease(g);

            var expected = new[] { 'g', 'f', 'a', 'b', 'd', 'e', 'c' };

            var actual = expected.Select(_ => sut.ExtractMinimum());

            //foreach (BinaryMinHeap<char>.Node node in actual)
            //{
            //    _output.WriteLine(node.ToString());
            //}
            Assert.True(expected.SequenceEqual(actual.Select(_ => _.Id)));
        }
        private IEnumerable <Edge> GetMinimumSpanningTreeEdgesBad(Dictionary <char, List <Edge> > g)
        {
            var h   = new BinaryMinHeap <char>();
            var vte = new Dictionary <char, Edge>();
            // Fill Heap
            var isFirst = true;

            foreach (char key in g.Keys)
            {
                if (isFirst)
                {
                    h.Add(new BinaryMinHeap <char> .Node {
                        Id = key, Weight = 0
                    });
                    isFirst = false;
                }
                else
                {
                    h.Add(new BinaryMinHeap <char> .Node {
                        Id = key, Weight = int.MaxValue
                    });
                }
            }

            var result = new List <Edge>();

            while (h.HasItem())
            {
                var v = h.ExtractMinimum();
                vte.TryGetValue(v.Id, out Edge ste);
                if (ste != null)
                {
                    result.Add(ste);
                }

                foreach (Edge e in g[v.Id])
                {
                    char adj = e.V2;
                    if (!h.Contains(adj))
                    {
                        continue;
                    }

                    var node = h.GetNode(adj);
                    if (node.Weight > e.Weight)
                    {
                        node.Weight = e.Weight;
                        h.Decrease(node);

                        if (!vte.ContainsKey(node.Id))
                        {
                            vte.Add(node.Id, e);
                        }
                        vte[node.Id] = e;
                    }
                }
            }

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