Ejemplo n.º 1
0
    private void Swap(int i, int j)
    {
        HeapElem tmp = _heap[i];

        _heap[i] = _heap[j];
        _heap[j] = tmp;
    }
Ejemplo n.º 2
0
    private void Grow()
    {
        int newCapacity = _heap.Length * GrowFactor;
        var newHeap     = new HeapElem[newCapacity];

        Array.Copy(_heap, newHeap, _heap.Length);
        _heap = newHeap;
    }
Ejemplo n.º 3
0
        private void Dijkstry(string start, string end)
        {
            HeapElem smallestPathFromCity = null, p, t;
            string   vertexFromHeap, key;
            int      oldValNeighbour, lengthToCurrCity, sum;

            while (heap.Count > 0)
            {
                smallestPathFromCity = heap.First();
                //heap = RemoveHeap(heap);
                heap.Remove(smallestPathFromCity);
                vertexFromHeap = smallestPathFromCity.vertex;
                if (visited[vertexFromHeap] == true)
                {
                    continue;
                }
                lengthToCurrCity        = smallestPathFromCity.length;
                visited[vertexFromHeap] = true;

                foreach (KeyValuePair <string, int> x in array[vertexFromHeap].incList)
                {
                    if (!table.ContainsKey(x.Key))
                    {
                        table.Add(x.Key, new TableElement());
                        visited.Add(x.Key, false);
                    }
                    key             = x.Key;
                    oldValNeighbour = table[key].length;
                    sum             = lengthToCurrCity + x.Value;
                    if (sum < oldValNeighbour)
                    {
                        table[key].length     = sum;
                        table[key].prevVertex = vertexFromHeap;

                        if (visited[key] == false)
                        {
                            t = new HeapElem(oldValNeighbour, key);
                            p = new HeapElem(sum, key);
                            if (heap.TryGetValue(t, out t))
                            {
                                heap.Remove(t);
                                heap.Add(p);
                            }
                            else
                            {
                                heap.Add(p);
                            }
                        }
                    }
                }
                if (String.Compare(end, vertexFromHeap) == 0)
                {
                    break;
                }
            }
        }
Ejemplo n.º 4
0
 static HeapElem SearchHeapElemNext(HeapElem he, ref List <HeapElem> lshe)
 {
     foreach (var v in lshe)
     {
         if (v.start > he.end)
         {
             return(v);
         }
     }
     return(lshe[0]);
 }
Ejemplo n.º 5
0
    public int Pop()
    {
        if (_count == 0)
        {
            throw new InvalidOperationException("Heap is empty");
        }
        HeapElem ret = _heap[0];

        _count--;
        Swap(_count, 0); // swap last element into first slot
        HeapifyDown(0);
        return(ret.value);
    }
Ejemplo n.º 6
0
    public void Add(int value, float score)
    {
        _seenVals.Add(value);
        var elem = new HeapElem();

        elem.value = value;
        elem.score = score;
        if (_count == _heap.Length)
        {
            Grow();
        }

        _heap[_count++] = elem;
        HeapifyUp(_count - 1);
    }
Ejemplo n.º 7
0
            public int CompareTo(object obj)
            {
                if (obj == null)
                {
                    return(1);
                }

                HeapElem otherTemperature = obj as HeapElem;

                if (otherTemperature != null)
                {
                    return((int)this.GetMergeNorm() - (int)otherTemperature.GetMergeNorm());
                }
                else
                {
                    throw new ArgumentException("Object is not a HeapElement");
                }
            }
Ejemplo n.º 8
0
        static void MergeTwoHeapElem(HeapElem h1, HeapElem h2)
        {
            List <float> keyEl = new List <float>(dictCol.Keys);
            // We have 2 in the list
            HeapStatElem hse = new HeapStatElem();
            HeapElem     he  = new HeapElem();

            he.spread = h1.spread + h2.spread;
            he.start  = h1.start;
            he.end    = h2.end;

            hse.range_high_key = h2.hsElem.range_high_key;
            hse.equal_rows     = h2.hsElem.equal_rows;

            hse.distint_range_rows = h1.hsElem.distint_range_rows + 1 + h2.hsElem.distint_range_rows;
            hse.range_rows         = h1.hsElem.range_rows + h1.hsElem.equal_rows + h2.hsElem.range_rows;
            hse.average_range_rows = hse.range_rows / hse.distint_range_rows;

            he.hsElem = hse;

            List <float> lisError = new List <float>();

            // Now that we will go from the start to the end
            // indices, so that we can find out the error values
            for (int j = he.start; j <= he.end; j++)
            {
                float estimate = hse.average_range_rows;
                float actual   = dictCol[keyEl[j]];

                float err = (estimate + 1) / (actual + 1);
                if (err < 1)
                {
                    err = 1 / err;
                }

                lisError.Add(err);
            }

            lisError.Sort();
            he.errorList = lisError;
            // Now we will need to generate the sorted
            // list and find out the new metrics

            float prod1 = 0, sum1 = 0;
            int   k = 0;

            for (int j = he.start; j <= he.end; j++, k++)
            {
                float val = (k + 1) * lisError[k];
                prod1 += val;
                sum1  += lisError[k];
            }


            float sumn  = k * (k + 1) / 2;
            float sumns = k * (k + 1) * (2 * k + 1) / 6;

            float betanom = k * prod1 - sumn * sum1;
            float betaden = k * (sumns) - sumn * sumn;

            float betam = betanom / betaden;

            float alphanom = sum1 * sumns - prod1 * sumn;
            float alpha    = alphanom / betaden;

            h1.mergeend       = he.end;
            h1.mergestart     = he.start;
            h1.mergehsElemM   = hse;
            h1.mergeerrorList = lisError;
            h1.mergeintercept = betam;
            h1.mergeslope     = alpha;
            h1.mergespread    = he.spread;
        }
Ejemplo n.º 9
0
        // This is the one that creates various histogram elements
        // from the values we already store and keep
        static void CreateHistogramFromAlgorithm(List <float> colVal, List <StatStep> histLs, int num)
        {
            //we  have the histogram
            int          countHistInit = dictCol.Count();
            List <float> keyEl         = new List <float>(dictCol.Keys);


            List <HeapStatElem> lhse      = new List <HeapStatElem>();
            List <float>        errorList = new List <float>();


            // Next will be the priority heap implementation
            // So if there are modulo values, they will be picked i nthe end
            // in a separate process
            for (int i = 0; i < countHistInit / num - 1; i++)
            {
                HeapStatElem hs = new HeapStatElem();

                hs.range_high_key = keyEl[i * num + num - 1];

                // For each histogram element , add their values in the q-error metric, which is a temporary thing

                for (int j = 0; j < num - 1; j++)
                {
                    hs.range_rows += dictCol[keyEl[i * num + j]];
                }
                hs.equal_rows         = dictCol[keyEl[i * num + (num - 1)]];
                hs.distint_range_rows = num - 1;
                hs.average_range_rows = hs.range_rows / hs.distint_range_rows;
                lhse.Add(hs);
            }

            HeapStatElem hsElem = new HeapStatElem();

            hsElem.equal_rows     = dictCol[keyEl[countHistInit - 1]];
            hsElem.range_high_key = keyEl[countHistInit - 1];
            int tot = 0;
            int d   = 0;

            for (int i = ((countHistInit / num) - 1) * num; i < countHistInit - 1; i++)
            {
                if (i < countHistInit - 1)
                {
                    tot += dictCol[keyEl[i]];
                    d++;
                }
            }

            hsElem.range_rows         = tot;
            hsElem.average_range_rows = tot / d;
            hsElem.distint_range_rows = d;

            // Final bucket if there are some values left, we will work on it
            lhse.Add(hsElem);

            // We have the hist Elem.
            // Now use that to find the  -q list and fill the q-error for each bucket.

            // Now have the q-errors

            List <float>    qeList     = new List <float>();
            List <HeapElem> heElemList = new List <HeapElem>();

            maxalpha = float.MinValue;
            maxbeta  = float.MinValue;


            foreach (var v in keyEl)
            {
                float estimate = CalculateEMQEsitmateFromHist(ref lhse, v);
                float actual   = dictCol[v];

                //  now we have the actual and estimate
                float qe = (estimate + 1) / (actual + 1);
                if (qe < 1)
                {
                    qe = (float)(1) / qe;
                }

                qeList.Add(qe);
            }

            for (int i = 0; i < countHistInit / num; i++)
            {
                HeapElem he = new HeapElem();
                he.start     = i * num;
                he.end       = (i + 1) * (num) - 1;
                he.hsElem    = lhse[i];
                he.spread    = (int)lhse[i].range_rows + (int)lhse[i].equal_rows;
                he.errorList = new List <float>();

                float prod1 = 0, sum2 = 0, nls = 0, ls = 0;
                for (int j = 0; j < num; j++)
                {
                    he.errorList.Add(qeList[i * num + j]);
                }
                he.errorList.Sort();
                for (int j = 0; j < num; j++)

                {
                    float val = (j + 1) * he.errorList[j];
                    prod1 += val;

                    sum2 += he.errorList[j];
                }


                float sumn    = (num) * (num + 1) / 2;
                float sumns   = (num) * (num + 1) * (2 * num + 1) / 6;
                float betanom = num * prod1 - (sumn) * sum2;
                float betaden = num * sumns - (sumn) * (sumn);
                float beta    = betanom / betaden;

                float alphanom = prod1 * sumns - sum2 * sumn;
                float alphaden = betaden;
                float alpha    = alphanom / alphaden;

                he.intercept = alpha;
                he.slope     = beta;

                if (alpha > maxalpha)
                {
                    maxalpha = alpha;
                }

                if (beta > maxbeta)
                {
                    maxbeta = beta;
                }

                // We will get the merged norm, that we will need
                he.mergenorm = he.GetMergeNorm();
                heElemList.Add(he);
                // At the end of the three, we will need to find the beta and alpha
            }

            HeapElem heElem = new HeapElem();
            int      c      = 0;

            heElem.start = (countHistInit / num) * num;
            heElem.end   = countHistInit - 1;

            heElem.hsElem = lhse[countHistInit / num - 1];

            float prod1o = 0, sum2o = 0, nlso = 0, lso = 0;

            heElem.errorList = new List <float>();

            // Now for the last bucket
            for (int i = (countHistInit / num) * num; i < countHistInit; i++)
            {
                c++;
                heElem.errorList.Add(qeList[i]);
            }

            heElem.errorList.Sort();


            for (int i = (countHistInit / num) * num, k = 0; i < countHistInit; i++, k++)
            {
                float val = (k + 1) * heElem.errorList[k];
                prod1o += val;

                sum2o += heElem.errorList[k];
            }

            heElem.spread = c;

            float sumno    = c * (c + 1) / 2;
            float sumnso   = c * (c + 1) * (2 * c + 1) / 6;
            float betanomo = c * prod1o - sumno * sum2o;
            float betadeno = c * sumnso - sumno * sumno;

            float betao = betanomo / betadeno;

            float alphanomo = sum2o * sumnso - prod1o * sumno;
            float alphao    = alphanomo / betadeno;

            if (alphao > maxalpha)
            {
                maxalpha = alphao;
            }
            if (betao > maxbeta)
            {
                maxbeta = betao;
            }

            heElem.intercept = alphao;
            heElem.slope     = betao;
            heElem.mergenorm = heElem.GetMergeNorm();
            heElemList.Add(heElem);

            // The list if ready, now, we need to build the Heap where the top element with the
            // We will iterate over this list and now create the their mergestories.

            int heapElemCount = heElemList.Count;

            for (int i = 0; i < heapElemCount - 1; i++)
            {
                HeapElem h1 = heElemList[i];
                HeapElem h2 = heElemList[i + 1];


                MergeTwoHeapElem(h1, h2);
                // We have for nodes, their merged values.
            }
            // Heap Elem List has all the heaps with their mergeability information defined.

            Heap.PriorityQueue <HeapElem, Tuple <float, float> > heapForWork = new Heap.PriorityQueue <HeapElem, Tuple <float, float> >();
            for (int i = 0; i < heElemList.Count - 1; i++)
            {
                heapForWork.Enqueue(heElemList[i], new Tuple <float, float>(heElemList[i].mergeslope, heElemList[i].mergeintercept));
            }

            // At this point the heap is ready. Now pop and keep merging till the end.
            while (heapForWork.Count > numSteps)
            {
                HeapElem he = heapForWork.Dequeue().Key;

                // We got the he elem, now the hard work of the merged one to be inserted.

                HeapElem     heNew = new HeapElem();
                HeapStatElem hs    = new HeapStatElem();
                heNew.hsElem    = he.mergehsElemM;
                heNew.intercept = he.mergeintercept;
                heNew.slope     = he.mergeslope;
                heNew.intercept = he.mergeintercept;
                heNew.errorList = he.mergeerrorList;
                heNew.spread    = he.spread;
                heNew.end       = he.mergeend;
                heNew.start     = he.mergestart;


                int k = 0;
                for (int i = 0; i < heElemList.Count; i++)
                {
                    if (heElemList[i].start > he.end)
                    {
                        k             = i;
                        heElemList[i] = heNew;
                        break;
                    }
                }

                if (k < heElemList.Count - 2)
                {
                    // Now to merge it with the next element.
                    MergeTwoHeapElem(heNew, heElemList[k + 2]);
                    // We iwll keep removing it after every change.
                    heElemList.RemoveAt(k + 1);
                }



                foreach (var v in heapForWork)
                {
                    if (v.Key.end + 1 == he.start)
                    {
                        MergeTwoHeapElem(v.Key, heNew);
                        break;
                    }
                }

                // So both the sides are merged and we can have them in the heap inserted back again.
            }

            // At this point, we will have merged a few cases here and here

            // Now build the list

            foreach (var v in heElemList)
            {
                // Should have the lmited he element list
                StatStep ss = new StatStep();
                ss.average_range_rows = v.hsElem.average_range_rows;
                ss.distint_range_rows = v.hsElem.distint_range_rows;
                ss.equal_rows         = v.hsElem.equal_rows;
                ss.range_high_key     = v.hsElem.range_high_key;
                ss.range_rows         = v.hsElem.range_rows;
                histLs.Add(ss);
            }

            return;
        }
Ejemplo n.º 10
0
        public string VirtRoad(string start, string startNewRoad, string endNewRoad, int roadLength)
        {
            if (prevCityPath != start || table == null)
            {
                prevCityPath = start;
                heap         = new SortedSet <HeapElem>(new ByLength());
                visited      = new Dictionary <string, bool>();
                visited.Add(start, false);
                heap.Add(new HeapElem(0, start));
                table = new Dictionary <string, TableElement>();
                table.Add(start, new TableElement());
                table[start].length     = 0;
                table[start].prevVertex = start;
                Dijkstry(start, startNewRoad);
                if (!visited.ContainsKey(endNewRoad) || !visited[endNewRoad])
                {
                    Dijkstry(start, endNewRoad);
                }
            }
            else
            {
                if (!visited.ContainsKey(startNewRoad) || !visited[startNewRoad])
                {
                    Dijkstry(start, startNewRoad);
                }
                if (!visited.ContainsKey(endNewRoad) || !visited[endNewRoad])
                {
                    Dijkstry(start, endNewRoad);
                }
            }
            SortedSet <HeapElem> heapNR = new SortedSet <HeapElem>(new ByLength());
            //[x,1] -is on heap
            Dictionary <string, bool[]> isVisitedHeap = new Dictionary <string, bool[]>();
            int      cityShortestPath = 0;
            int      oldValue = table[endNewRoad].length;
            int      newValue = roadLength + table[startNewRoad].length;
            bool     onHeap, isVisited;
            HeapElem t, p;

            if (oldValue > newValue)
            {
                cityShortestPath++;
                isVisitedHeap.Add(endNewRoad, new bool[2]);
                heapNR.Add(new HeapElem(newValue, endNewRoad));
            }
            else if (table[startNewRoad].length > roadLength + table[endNewRoad].length)
            {
                isVisitedHeap.Add(startNewRoad, new bool[2]);
                cityShortestPath++;
                heapNR.Add(new HeapElem(roadLength + table[endNewRoad].length, startNewRoad));
            }
            else
            {
                return("0");
            }

            HeapElem el = null;

            while (heapNR.Count > 0 && cityShortestPath < 100)
            {
                el = heapNR.First();
                heapNR.Remove(el);
                isVisitedHeap[el.vertex][1] = false;
                isVisitedHeap[el.vertex][0] = true;
                foreach (KeyValuePair <string, int> x in array[el.vertex].incList)
                {
                    if (!isVisitedHeap.ContainsKey(x.Key))
                    {
                        isVisitedHeap.Add(x.Key, new bool[2]);
                    }
                    if (!visited[x.Key])
                    {
                        Dijkstry(start, x.Key);
                    }
                    oldValue = table[x.Key].length;
                    newValue = el.length + x.Value;
                    if (oldValue > newValue)
                    {
                        onHeap    = isVisitedHeap[x.Key][1];
                        isVisited = isVisitedHeap[x.Key][0];
                        if (onHeap)
                        {
                            heapNR.Remove(new HeapElem(oldValue, x.Key));
                            heapNR.Add(new HeapElem(newValue, x.Key));
                        }
                        else if (!isVisited)
                        {
                            cityShortestPath++;
                            heapNR.Add(new HeapElem(newValue, x.Key));
                            isVisitedHeap[x.Key][1] = true;
                        }
                    }
                }
            }
            return((cityShortestPath >= 100) ? "100+" : cityShortestPath.ToString());
        }