Пример #1
0
        public void TestBinaryMinHeap(BinaryMinHeap <char> sut)
        {
            var expected = new[] { 'a', 'b', 'd', 'e', 'c', 'f', 'g' };
            var actual   = expected.Select(_ => sut.ExtractMinimum());

            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);
        }
Пример #3
0
        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)));
        }
Пример #4
0
        public void ThrowExceptionWhenExtractingAnEmptyBinaryMinHeap()
        {
            var sut = new BinaryMinHeap <char>();

            Assert.Throws <ArgumentOutOfRangeException>(() => sut.ExtractMinimum());
        }