Example #1
0
 // put an element of type pair in priority queue
 public void Insert(Node_info value)
 {
     EnsureHeapCapacity();
     heap.Add(value);
     heapify_up(size);
     size++;
 }
Example #2
0
 private void heapify_down(int idx)
 {
     while (get_left_child_index(idx) < size)
     {
         int left_child_idx = get_left_child_index(idx);
         int min_idx        = idx;
         if (heap[min_idx].CompareTo(heap[left_child_idx]) == 1)
         {
             min_idx = left_child_idx;
         }
         int right_child_idx = get_right_child_index(idx);
         if (right_child_idx < size && heap[min_idx].CompareTo(heap[right_child_idx]) == 1)
         {
             min_idx = right_child_idx;
         }
         if (min_idx != idx)
         {
             Node_info temp = heap[idx];
             heap[idx]     = heap[min_idx];
             heap[min_idx] = temp;
         }
         else
         {
             return;
         }
         idx = min_idx;
     }
 }
Example #3
0
 // compares 2 pairs return 0 if both are equal 1 if PHANTOM object is greater -1 if parameter object is greater
 public int CompareTo(Node_info P)
 {
     if (Math.Abs(item3 - P.item3) < double.Epsilon)
     {
         return(0);
     }
     else if (item3 - P.item3 < 0)
     {
         return(-1);
     }
     return(1);
 }
Example #4
0
        public static void shortest_path(int x, int y)
        {
            int width  = ImageOperations.GetWidth(MainForm.ImageMatrix);
            int height = ImageOperations.GetHeight(MainForm.ImageMatrix);

            Console.WriteLine(x + " " + y);
            int starting_node = generate_id(x, y, ImageOperations.GetWidth(MainForm.ImageMatrix));

            int[]    parent1 = new int[width * height + 5];
            bool[]   vis     = new bool[width * height + 5];
            double[] values  = new double[width * height + 5];
            for (int i = 0; i < width * height + 2; ++i)
            {
                values[i] = double.MaxValue;
            }
            PriorityQueue q = new PriorityQueue();
            Node_info     n = new Node_info(1258, -1, 0);

            q.Insert(n);
            while (!q.is_empty())
            {
                n = q.poll();
                double val = Convert.ToDouble(n.get_item3());
                int    cur = Convert.ToInt32(n.get_item1());
                int    par = Convert.ToInt32(n.get_item2());
                if (vis[cur])
                {
                    continue;
                }
                vis[cur]     = true;
                values[cur]  = val;
                parent1[cur] = par;
                for (int i = 0; i < adjlist[cur].Count; i++)
                {
                    double weight = adjlist[cur][i].Item2;
                    weight += val;
                    Node_info n1 = new Node_info(adjlist[cur][i].Item1, cur, weight);
                    q.Insert(n1);
                }
            }
            List <int> Par = new List <int>();
            int        nn  = 4334;

            while (parent1[nn] != -1)
            {
                Console.WriteLine(parent1[nn]);
                Par.Add(parent1[nn]);
                nn = parent1[nn];
            }
            WriteFilePath(Par, Par.Count, width, 1258, 4334);
            WriteFileOutput(adjlist, height);
        }
Example #5
0
        private void heapify_up(int idx)
        {
            int parent = get_parent_index(idx);

            while (idx > 0 && heap[idx].CompareTo(heap[parent]) == -1)
            {
                Node_info temp = heap[idx];
                heap[idx]    = heap[parent];
                heap[parent] = temp;
                idx          = parent;
                parent       = get_parent_index(idx);
            }
        }
Example #6
0
        // return and remove the minimum pair in Priority queue
        public Node_info poll()
        {
            if (size == 0)
            {
                throw new IndexOutOfRangeException();
            }

            Node_info temp = heap[0];

            heap[0]        = heap[size - 1];
            heap[size - 1] = temp;
            heap.RemoveAt(size - 1);
            size--;
            heapify_down(0);
            return(temp);
        }