Esempio n. 1
0
        public void GenerateEuclideanOurNeoKruskalMST(int bucket)
        {
            if (VertexList.Count == 0)
            {
                return;
            }

            DateTime start = DateTime.Now;
            NeoDisjointSet <Vertex> disjointSet = new NeoDisjointSet <Vertex>(VertexList);

            EdgeSolution.Clear();
            C5.IPriorityQueue <Edge>[] heap2 = new C5.IntervalHeap <Edge> [bucket];
            C5.IPriorityQueue <Edge>   heap  = new C5.IntervalHeap <Edge>(new EdgeComparer());
            for (int i = 0; i < heap2.Length; i++)
            {
                heap2[i] = new C5.IntervalHeap <Edge>(new EdgeComparer());
            }


            //this.VisitedVertex = VertexList.Count;
            for (int i = 0; i < VertexList.Count - 1; i++)
            {
                for (int j = i + 1; j < VertexList.Count; j++)
                {
                    Edge e = new Edge(VertexList[i], VertexList[j]);
                    heap.Add(e);
                }
            }

            double max = heap.FindMax().Length;
            double min = heap.FindMin().Length;

            while (heap.Count > 0)
            {
                Edge s = heap.DeleteMin();
                heap2[(int)Math.Floor((((s.Length - min) / (max - min)) * (bucket - 1)))].Add(s);
            }

            for (int i = 0; i < bucket && disjointSet.Count > 0; i++)
            {
                while (heap2[i].Count > 0 && disjointSet.Count > 0)
                {
                    Edge s = heap2[i].DeleteMin();
                    if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond))
                    {
                        disjointSet.Union(s.VertexFirst, s.VertexSecond);
                        EdgeSolution.Add(s);
                    }
                }
            }

            Debug.WriteLine((DateTime.Now - start).TotalMilliseconds + " ms");
            //this.VisitedVertex = VertexList.Count;
        }
        J2KImage GetHighestPriorityImage()
        {
            J2KImage image = null;

            lock (m_syncRoot)
            {
                if (m_priorityQueue.Count > 0)
                {
                    try { image = m_priorityQueue.FindMax(); }
                    catch (Exception) { }
                }
            }
            return(image);
        }
Esempio n. 3
0
        private J2KImage GetHighestPriorityImage()
        {
            J2KImage image = null;

            lock (m_priorityQueue)
            {
                if (!m_priorityQueue.IsEmpty)
                {
                    try { image = m_priorityQueue.FindMax(); }
                    catch (Exception) { }
                }
            }

            return(image);
        }
Esempio n. 4
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;
        }
Esempio n. 5
0
        public void GenerateEuclideanOurNeoKruskalMST(int bucket)
        {
            if (VertexList.Count == 0)
                return;

            DateTime start = DateTime.Now;
            NeoDisjointSet<Vertex> disjointSet = new NeoDisjointSet<Vertex>(VertexList);
            EdgeSolution.Clear();
            C5.IPriorityQueue<Edge>[] heap2 = new C5.IntervalHeap<Edge>[bucket];
            C5.IPriorityQueue<Edge> heap = new C5.IntervalHeap<Edge>(new EdgeComparer());
            for (int i = 0; i < heap2.Length; i++)
            {
                heap2[i] = new C5.IntervalHeap<Edge>(new EdgeComparer());
            }

            //this.VisitedVertex = VertexList.Count;
            for (int i = 0; i < VertexList.Count - 1; i++)
                for (int j = i + 1; j < VertexList.Count; j++)
                {
                    Edge e = new Edge(VertexList[i], VertexList[j]);
                    heap.Add(e);
                }

            double max = heap.FindMax().Length;
            double min = heap.FindMin().Length;

            while (heap.Count > 0)
            {
                Edge s = heap.DeleteMin();
                heap2[(int)Math.Floor((((s.Length - min) / (max - min)) * (bucket - 1)))].Add(s);
            }

            for (int i = 0; i < bucket && disjointSet.Count > 0; i++)
            {
                while (heap2[i].Count > 0 && disjointSet.Count > 0)
                {
                    Edge s = heap2[i].DeleteMin();
                    if (!disjointSet.IsSameSet(s.VertexFirst, s.VertexSecond))
                    {

                        disjointSet.Union(s.VertexFirst, s.VertexSecond);
                        EdgeSolution.Add(s);
                    }
                }
            }

            Debug.WriteLine((DateTime.Now - start).TotalMilliseconds + " ms");
            //this.VisitedVertex = VertexList.Count;
        }
Esempio n. 6
0
 public KeyValuePair <TKey, TValue> PeekMax() => _heap.FindMax();
Esempio n. 7
0
        /// <summary>
        /// Returns the maximum item
        /// </summary>
        /// <returns></returns>
        public T FindMax()
        {
            IndexedItem item = _priQueue.FindMax();

            return(item.Value);
        }
Esempio n. 8
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);
        }