Пример #1
0
        public void RemoveTestTwoChildrenNotRootRightmostIsNotLeft()
        {
            SGTree <int> sgt = new SGTree <int>(0.75);

            SGTNode <int> x = new SGTNode <int>(10);
            SGTNode <int> a = new SGTNode <int>(0);
            SGTNode <int> b = new SGTNode <int>(1);
            SGTNode <int> c = new SGTNode <int>(2);
            SGTNode <int> d = new SGTNode <int>(3);
            SGTNode <int> e = new SGTNode <int>(4);

            sgt.insertFirst(x);
            sgt.insertAfter(x, a);
            sgt.insertAfter(a, e);
            sgt.insertBefore(a, b);
            sgt.insertAfter(b, c);
            sgt.insertBefore(c, d);

            sgt.Remove(a);

            bool b1 = sgt.Query(c, d);
            bool b2 = sgt.Query(c, b);

            Assert.IsTrue(b1 && b2);
        }
Пример #2
0
        public void RemoveTestNoChildrenNotRoot()
        {
            SGTree <int> sgt = new SGTree <int>(0.75);

            SGTNode <int> a = new SGTNode <int>(0);
            SGTNode <int> b = new SGTNode <int>(1);
            SGTNode <int> c = new SGTNode <int>(2);

            sgt.insertFirst(a);
            sgt.insertBefore(a, b);
            sgt.insertBefore(b, c);

            sgt.Remove(c);

            bool b1 = sgt.Query(a, b);

            Assert.IsTrue(b1);
        }
Пример #3
0
        public void RemoveTestLeftChildIsRoot()
        {
            SGTree<int> sgt = new SGTree<int>(0.75);

            SGTNode<int> a = new SGTNode<int>(0);
            SGTNode<int> b = new SGTNode<int>(1);
            SGTNode<int> c = new SGTNode<int>(2);
            SGTNode<int> d = new SGTNode<int>(3);

            sgt.insertFirst(a);
            sgt.insertBefore(a, b);
            sgt.insertBefore(b, c);
            sgt.insertBefore(c, d);

            sgt.Remove(a);

            bool b1 = sgt.Query(c, d);
            bool b2 = sgt.Query(b, c);
            bool b3 = sgt.Query(b, d);

            Assert.IsTrue(b1 && b2 && b3);
        }
Пример #4
0
        public void insertBeforeTest()
        {
            // a < root
            // b < a
            // a < c
            // d < a && d < b

            SGTree<int> sgt = new SGTree<int>(0.75);
            SGTNode<int> root = sgt.insertFirst(1);
            SGTNode<int> a = sgt.insertBefore(root, 2);
            SGTNode<int> b = sgt.insertBefore(a, 3);
            SGTNode<int> c = sgt.insertBefore(root, 4);
            SGTNode<int> d = sgt.insertBefore(b, 5);

            bool aGTroot = sgt.Query(root, a);
            bool bGTa = sgt.Query(a, b);
            bool aGTc = sgt.Query(c, a);
            bool dGTa = sgt.Query(a, d);
            bool dGTb = sgt.Query(b, d);

            Assert.IsTrue(aGTroot && bGTa && aGTc && dGTa && dGTb);
        }
Пример #5
0
        public void insertBeforeTest()
        {
            // a < root
            // b < a
            // a < c
            // d < a && d < b

            SGTree <int>  sgt  = new SGTree <int>(0.75);
            SGTNode <int> root = sgt.insertFirst(1);
            SGTNode <int> a    = sgt.insertBefore(root, 2);
            SGTNode <int> b    = sgt.insertBefore(a, 3);
            SGTNode <int> c    = sgt.insertBefore(root, 4);
            SGTNode <int> d    = sgt.insertBefore(b, 5);

            bool aGTroot = sgt.Query(root, a);
            bool bGTa    = sgt.Query(a, b);
            bool aGTc    = sgt.Query(c, a);
            bool dGTa    = sgt.Query(a, d);
            bool dGTb    = sgt.Query(b, d);

            Assert.IsTrue(aGTroot && bGTa && aGTc && dGTa && dGTb);
        }
Пример #6
0
        public void RemoveTestTwoChildrenNotRootRighstmostIsLeft()
        {
            SGTree <int> sgt = new SGTree <int>(0.75);

            SGTNode <int> a = new SGTNode <int>(0);
            SGTNode <int> b = new SGTNode <int>(1);
            SGTNode <int> c = new SGTNode <int>(2);
            SGTNode <int> d = new SGTNode <int>(3);

            sgt.insertFirst(a);
            sgt.insertBefore(a, b);
            sgt.insertBefore(b, c);
            sgt.insertAfter(b, d);

            sgt.Remove(b);

            bool b1 = sgt.Query(a, c);
            bool b2 = sgt.Query(a, d);
            bool b3 = sgt.Query(d, c);

            Assert.IsTrue(b1 && b2 && b3);
        }
Пример #7
0
        public void RemoveTestLeftChildIsRoot()
        {
            SGTree <int> sgt = new SGTree <int>(0.75);

            SGTNode <int> a = new SGTNode <int>(0);
            SGTNode <int> b = new SGTNode <int>(1);
            SGTNode <int> c = new SGTNode <int>(2);
            SGTNode <int> d = new SGTNode <int>(3);

            sgt.insertFirst(a);
            sgt.insertBefore(a, b);
            sgt.insertBefore(b, c);
            sgt.insertBefore(c, d);

            sgt.Remove(a);

            bool b1 = sgt.Query(c, d);
            bool b2 = sgt.Query(b, c);
            bool b3 = sgt.Query(b, d);

            Assert.IsTrue(b1 && b2 && b3);
        }
Пример #8
0
        public void RemoveTestTwoChildrenIsRootRightmostIsLeft()
        {
            SGTree <int> sgt = new SGTree <int>(0.75);

            SGTNode <int> b = new SGTNode <int>(1);
            SGTNode <int> c = new SGTNode <int>(2);
            SGTNode <int> d = new SGTNode <int>(3);

            sgt.insertFirst(b);
            sgt.insertBefore(b, c);
            sgt.insertAfter(b, d);

            sgt.Remove(b);

            bool b1 = sgt.Query(d, c);

            Assert.IsTrue(b1);
        }
Пример #9
0
        public bool SoftThresholdSearch(int iv, int iw)
        {
            var v = _nodes[iv];
            var w = _nodes[iw];

            // Article states double linked list for F,B and normal list for FL BL
            var f = new List <SGTNode <HKMSTNodeFinal> >(1);
            var b = new List <SGTNode <HKMSTNodeFinal> >(1);

            f.Add(w);
            w.Value.InF = true;
            b.Add(v);
            v.Value.InB = true;

            w.Value.OutEnum = w.Value.Outgoing.GetEnumerator();
            w.Value.OutEnum.MoveNext();
            v.Value.InEnum = v.Value.Incoming.GetEnumerator();
            v.Value.InEnum.MoveNext();

            SGTNode <HKMSTNodeFinal> s = v;

            // To implement soft-threshold search, we maintain FA , FP, BA , and BP as doublylinked lists
            var fa = new LinkedList <SGTNode <HKMSTNodeFinal> >();
            var fp = new LinkedList <SGTNode <HKMSTNodeFinal> >();
            var ba = new LinkedList <SGTNode <HKMSTNodeFinal> >();
            var bp = new LinkedList <SGTNode <HKMSTNodeFinal> >();

            if (w.Value.OutEnum.Current != null)
            {
                fa.AddFirst(w);
            }

            if (v.Value.InEnum.Current != null)
            {
                ba.AddFirst(v);
            }

            while (fa.Count > 0 && ba.Count > 0)
            {
                var u = fa.First; // pointer to linkedlistnode for faster remove
                var z = ba.First;

                if (_nodeOrder.Query(z.Value, u.Value) || u.Value == z.Value)
                {
                    // SEARCH_STEP(u,z)
                    var x = u.Value.Value.OutEnum.Current;
                    var y = z.Value.Value.InEnum.Current;
                    u.Value.Value.OutEnum.MoveNext();
                    z.Value.Value.InEnum.MoveNext();

                    if (u.Value.Value.OutEnum.Current == null)
                    {
                        fa.Remove(u);
                    }
                    if (z.Value.Value.InEnum.Current == null)
                    {
                        ba.Remove(z);
                    }

                    if (x.Value.InB)
                    {
                        f.ForEach(item => item.Value.InF = false);
                        b.ForEach(item => item.Value.InB = false);
                        return(false); // Pair(uz.from, x.Current);
                    }
                    else if (y.Value.InF)
                    {
                        f.ForEach(item => item.Value.InF = false);
                        b.ForEach(item => item.Value.InB = false);
                        return(false); // Pair(y.Current, uz.to);
                    }

                    if (!x.Value.InF)
                    {
                        f.Add(x);
                        x.Value.InF     = true;
                        x.Value.OutEnum = x.Value.Outgoing.GetEnumerator();
                        x.Value.OutEnum.MoveNext();
                        if (x.Value.OutEnum.Current != null)
                        {
                            fa.AddFirst(x);
                        }
                    }
                    if (!y.Value.InB)
                    {
                        b.Add(y);
                        y.Value.InB    = true;
                        y.Value.InEnum = y.Value.Incoming.GetEnumerator();
                        y.Value.InEnum.MoveNext();
                        if (y.Value.InEnum.Current != null)
                        {
                            ba.AddFirst(y);
                        }
                    }
                    // END STEP
                }
                else
                {
                    if (_nodeOrder.Query(u.Value, s))
                    {
                        fa.Remove(u);
                        fp.AddFirst(u);
                    }
                    if (_nodeOrder.Query(s, z.Value))
                    {
                        ba.Remove(z);
                        bp.AddFirst(z);
                    }
                }

                if (fa.Count == 0)
                {
                    bp.Clear();
                    ba.Remove(s);
                    if (fp.Count > 0)
                    {
                        //choose s from Fp (median stuff)
                        s = PickS(fp);

                        fa.Clear();
                        var curr = fp.First;
                        do
                        {
                            var next = curr.Next;
                            if (curr.Value.Label <= s.Label)
                            {
                                fp.Remove(curr);
                                fa.AddFirst(curr.Value);
                            }
                            curr = next;
                        } while (curr != null);
                    }
                }
                if (ba.Count == 0)
                {
                    fp.Clear();
                    fa.Remove(s);
                    if (bp.Count > 0)
                    {
                        //choose s from Bp (median stuff)
                        s = PickS(bp);

                        ba.Clear();
                        var curr = bp.First;
                        do
                        {
                            var next = curr.Next;
                            if (curr.Value.Label >= s.Label)
                            {
                                bp.Remove(curr);
                                ba.AddFirst(curr.Value);
                            }
                            curr = next;
                        } while (curr != null);
                    }
                }
            }
            // reorder
            // let t = min({v}∪{x ∈ F|out(x) = null} and reorder the vertices in F< and B> as discussed previously.

            var vAndf = f.FindAll(item => item.Value.OutEnum.Current != null);

            vAndf.Add(v);
            var t = vAndf.Min();
            // Let F< = { x ∈ F | x < t} and
            var fb = f.FindAll(item => item.Label < t.Label);
            // B > = { y ∈ B | y > t}.
            var bf = b.FindAll(item => item.Label > t.Label);

            if (t == v)
            {
                // move all vertices in fb just after t ... bf is empty
                foreach (var node in fb)
                {
                    _nodeOrder.Remove(node);
                }

                if (fb.Count > 1)
                {
                    fb = TopoSort(fb);
                }

                if (fb.Count > 0)
                {
                    var prev = _nodeOrder.insertAfter(t, fb[0]);
                    for (int i = 1; i < fb.Count; i++)
                    {
                        prev = _nodeOrder.insertAfter(prev, fb[i]);
                    }
                }
            }
            if (t.Label < v.Label)
            {
                // move all vertices in fb just before t and all vertices in bf just before all vertices in fb

                // This is required as the articles states
                if (bf.Count > 1)
                {
                    bf = TopoSort(bf);
                }
                if (fb.Count > 1)
                {
                    fb = TopoSort(fb);
                }

                foreach (var node in bf)
                {
                    _nodeOrder.Remove(node);
                }
                foreach (var node in fb)
                {
                    _nodeOrder.Remove(node);
                }

                bf.AddRange(fb);

                if (bf.Count > 0)
                {
                    var prev = _nodeOrder.insertBefore(t, bf[bf.Count - 1]);
                    if (bf.Count > 1)
                    {
                        for (int i = bf.Count - 2; i >= 0; i--)
                        {
                            prev = _nodeOrder.insertBefore(prev, bf[i]);
                        }
                    }
                }
            }


            // reset bools
            f.ForEach(item => item.Value.InF = false);
            b.ForEach(item => item.Value.InB = false);

            // all done add to Outgoing and Incoming
            _nodes[iv].Value.Outgoing.Add(_nodes[iw]);
            _nodes[iw].Value.Incoming.Add(_nodes[iv]);
            return(true);
        }
Пример #10
0
        private bool VertexGuidedSearch(int iv, int iw)
        {
            var v = _nodes[iv];
            var w = _nodes[iw];

            f.Clear();
            b.Clear();

            f.Add(w);
            w.Value.InF = true;
            b.Add(v);
            v.Value.InB = true;

            w.Value.OutEnum = w.Value.Outgoing.GetEnumerator();
            w.Value.OutEnum.MoveNext();
            v.Value.InEnum = v.Value.Incoming.GetEnumerator();
            v.Value.InEnum.MoveNext();

            var fl = new C5.IntervalHeap <SGTNode <HKMSTNode> >();
            var bl = new C5.IntervalHeap <SGTNode <HKMSTNode> >();

            if (w.Value.OutEnum.Current != null)
            {
                fl.Add(w);
            }
            if (v.Value.InEnum.Current != null)
            {
                bl.Add(v);
            }

            // For ease of notation, we adopt the convention that the
            // minimum of an empty set is bigger than any other value and the maximum of an empty
            // set is smaller than any other value.
            SGTNode <HKMSTNode> u = null;
            SGTNode <HKMSTNode> z = null;

            if (fl.Count > 0)
            {
                u = fl.FindMin();
            }
            if (bl.Count > 0)
            {
                z = bl.FindMax();
            }

            while (fl.Count > 0 && bl.Count > 0 && (u == z || _nodeOrder.Query(z, u)))
            {
                // SEARCH-STEP(vertex u, vertex z)
                var x = u.Value.OutEnum.Current;
                var y = z.Value.InEnum.Current;
                u.Value.OutEnum.MoveNext();
                z.Value.InEnum.MoveNext();

                if (u.Value.OutEnum.Current == null)
                {
                    fl.DeleteMin();
                }
                if (z.Value.InEnum.Current == null)
                {
                    bl.DeleteMax();
                }

                if (x.Value.InB)
                {
                    f.ForEach(item => item.Value.InF = false);
                    b.ForEach(item => item.Value.InB = false);
                    return(false); // Pair(uz.from, x.Current);
                }
                else if (y.Value.InF)
                {
                    f.ForEach(item => item.Value.InF = false);
                    b.ForEach(item => item.Value.InB = false);
                    return(false); // Pair(y.Current, uz.to);
                }

                if (!x.Value.InF)
                {
                    f.Add(x);
                    x.Value.InF     = true;
                    x.Value.OutEnum = x.Value.Outgoing.GetEnumerator();
                    x.Value.OutEnum.MoveNext();
                    if (x.Value.OutEnum.Current != null)
                    {
                        fl.Add(x);
                    }
                }
                if (!y.Value.InB)
                {
                    b.Add(y);
                    y.Value.InB    = true;
                    y.Value.InEnum = y.Value.Incoming.GetEnumerator();
                    y.Value.InEnum.MoveNext();
                    if (y.Value.InEnum.Current != null)
                    {
                        bl.Add(y);
                    }
                }

                // End of SEARCH-STEP(vertex u, vertex z)
                if (fl.Count > 0)
                {
                    u = fl.FindMin();
                }
                if (bl.Count > 0)
                {
                    z = bl.FindMax();
                }
            }


            // let t = min({v}∪{x ∈ F|out(x) = null} and reorder the vertices in F< and B> as discussed previously.
            var vAndf = f.FindAll(item => item.Value.OutEnum.Current != null);

            vAndf.Add(v);
            var t = vAndf.Min();
            // Let F< = { x ∈ F | x < t} and
            var fb = f.FindAll(item => item.Label < t.Label);
            // B > = { y ∈ B | y > t}.
            var bf = b.FindAll(item => item.Label > t.Label);

            if (t == v)
            {
                // move all vertices in fb just after t ... bf is empty
                foreach (var node in fb)
                {
                    _nodeOrder.Remove(node);
                }

                if (fb.Count > 1)
                {
                    fb = TopoSort(fb);
                }

                if (fb.Count > 0)
                {
                    var prev = _nodeOrder.insertAfter(t, fb[0]);
                    for (int i = 1; i < fb.Count; i++)
                    {
                        prev = _nodeOrder.insertAfter(prev, fb[i]);
                    }
                }
            }
            if (t.Label < v.Label)
            {
                // move all vertices in fb just before t and all vertices in bf just before all vertices in fb

                // This is required as the articles states
                if (bf.Count > 1)
                {
                    bf = TopoSort(bf);
                }
                if (fb.Count > 1)
                {
                    fb = TopoSort(fb);
                }

                foreach (var node in bf)
                {
                    _nodeOrder.Remove(node);
                }
                foreach (var node in fb)
                {
                    _nodeOrder.Remove(node);
                }

                foreach (var item in fb)
                {
                    bf.Add(item);
                }

                if (bf.Count > 0)
                {
                    var prev = _nodeOrder.insertBefore(t, bf[bf.Count - 1]);
                    if (bf.Count > 1)
                    {
                        for (int i = bf.Count - 2; i >= 0; i--)
                        {
                            prev = _nodeOrder.insertBefore(prev, bf[i]);
                        }
                    }
                }
            }

            // reset bools
            f.ForEach(item => item.Value.InF = false);
            b.ForEach(item => item.Value.InB = false);

            // all done add to Outgoing and Incoming
            _nodes[iv].Value.Outgoing.Add(_nodes[iw]);
            _nodes[iw].Value.Incoming.Add(_nodes[iv]);

            return(true);
        }
Пример #11
0
        public void RemoveTestTwoChildrenNotRootRightmostIsNotLeft()
        {
            SGTree<int> sgt = new SGTree<int>(0.75);

            SGTNode<int> x = new SGTNode<int>(10);
            SGTNode<int> a = new SGTNode<int>(0);
            SGTNode<int> b = new SGTNode<int>(1);
            SGTNode<int> c = new SGTNode<int>(2);
            SGTNode<int> d = new SGTNode<int>(3);
            SGTNode<int> e = new SGTNode<int>(4);

            sgt.insertFirst(x);
            sgt.insertAfter(x, a);
            sgt.insertAfter(a, e);
            sgt.insertBefore(a, b);
            sgt.insertAfter(b, c);
            sgt.insertBefore(c, d);

            sgt.Remove(a);

            bool b1 = sgt.Query(c, d);
            bool b2 = sgt.Query(c, b);

            Assert.IsTrue(b1 && b2);
        }
Пример #12
0
        public void RemoveTestTwoChildrenNotRootRighstmostIsLeft()
        {
            SGTree<int> sgt = new SGTree<int>(0.75);

            SGTNode<int> a = new SGTNode<int>(0);
            SGTNode<int> b = new SGTNode<int>(1);
            SGTNode<int> c = new SGTNode<int>(2);
            SGTNode<int> d = new SGTNode<int>(3);

            sgt.insertFirst(a);
            sgt.insertBefore(a, b);
            sgt.insertBefore(b, c);
            sgt.insertAfter(b, d);

            sgt.Remove(b);

            bool b1 = sgt.Query(a, c);
            bool b2 = sgt.Query(a, d);
            bool b3 = sgt.Query(d, c);

            Assert.IsTrue(b1 && b2 && b3);
        }
Пример #13
0
        public void RemoveTestTwoChildrenIsRootRightmostIsLeft()
        {
            SGTree<int> sgt = new SGTree<int>(0.75);

            SGTNode<int> b = new SGTNode<int>(1);
            SGTNode<int> c = new SGTNode<int>(2);
            SGTNode<int> d = new SGTNode<int>(3);

            sgt.insertFirst(b);
            sgt.insertBefore(b, c);
            sgt.insertAfter(b, d);

            sgt.Remove(b);

            bool b1 = sgt.Query(d, c);

            Assert.IsTrue(b1);
        }
Пример #14
0
        public void RemoveTestNoChildrenNotRoot()
        {
            SGTree<int> sgt = new SGTree<int>(0.75);

            SGTNode<int> a = new SGTNode<int>(0);
            SGTNode<int> b = new SGTNode<int>(1);
            SGTNode<int> c = new SGTNode<int>(2);

            sgt.insertFirst(a);
            sgt.insertBefore(a, b);
            sgt.insertBefore(b, c);

            sgt.Remove(c);

            bool b1 = sgt.Query(a, b);

            Assert.IsTrue(b1);
        }