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);
        }
Ejemplo n.º 2
0
        public void TestBinaryMinHeapContains(BinaryMinHeap <char> sut)
        {
            Assert.True(sut.Contains('a'));
            Assert.True(sut.Contains('b'));
            Assert.True(sut.Contains('c'));
            Assert.True(sut.Contains('d'));
            Assert.True(sut.Contains('e'));
            Assert.True(sut.Contains('f'));
            Assert.True(sut.Contains('g'));

            Assert.False(sut.Contains('1'));
            Assert.False(sut.Contains('x'));
            Assert.False(sut.Contains('3'));
            Assert.False(sut.Contains('y'));
            Assert.False(sut.Contains('z'));
        }
Ejemplo n.º 3
0
 public void Should_return_false_on_empty_contains()
 {
     Assert.IsFalse(_emptyIntMinHeap.Contains(32));
 }