Пример #1
0
 public int CompareTo(ValueIndexPair other)
 {
     if (Val < other.Val)
         return -1;
     if (Val > other.Val)
         return 1;
     return 0;
 }
Пример #2
0
 public BinaryHeap(int defCap)
 {
     _defaultCapacity = defCap;
     _theArray = new ValueIndexPair[_defaultCapacity + 1];
     // _theArray[0] serves as dummy Parent for root (who is at 1)
     // "largest" is guaranteed to be larger than all keys in_ heap
     _theArray[0] = new ValueIndexPair(double.PositiveInfinity, -1, -1);
     _currentSize = 0;
 }
Пример #3
0
 public void Add(ValueIndexPair e)
 {
     // bubble up:
     int where = _currentSize + 1; // new last place
     while (e.CompareTo(_theArray[Parent(where)]) > 0)
     {
         _theArray[where] = _theArray[Parent(where)];
         where = Parent(where);
     }
     _theArray[where] = e;
     _currentSize++;
 }
Пример #4
0
        // returns pairs of indeces and -1,-1 if < K pairs
        public int[,] GetKBestPairs(ParseForestItem[] items1, ParseForestItem[] items2)
        {
            // in_ this case K = items1.Length

            var beenPushed = new bool[K,K];

            var result = new int[K,2];
            for (int i = 0; i < K; i++)
            {
                result[i, 0] = -1;
                result[i, 1] = -1;
            }

            if (items1 == null || items2 == null || items1[0] == null || items2[0] == null)
                return result;

            var heap = new BinaryHeap(K + 1);
            int n = 0;
            var vip = new ValueIndexPair(items1[0].Prob + items2[0].Prob, 0, 0);

            heap.Add(vip);
            beenPushed[0, 0] = true;

            while (n < K)
            {
                vip = heap.RemoveMax();

                if (vip.Val == double.NegativeInfinity)
                    break;

                result[n, 0] = vip.I1;
                result[n, 1] = vip.I2;

                n++;
                if (n >= K)
                    break;

                if (!beenPushed[vip.I1 + 1, vip.I2])
                {
                    heap.Add(new ValueIndexPair(items1[vip.I1 + 1].Prob + items2[vip.I2].Prob, vip.I1 + 1, vip.I2));
                    beenPushed[vip.I1 + 1, vip.I2] = true;
                }
                if (!beenPushed[vip.I1, vip.I2 + 1])
                {
                    heap.Add(new ValueIndexPair(items1[vip.I1].Prob + items2[vip.I2 + 1].Prob, vip.I1, vip.I2 + 1));
                    beenPushed[vip.I1, vip.I2 + 1] = true;
                }
            }

            return result;
        }