Ejemplo n.º 1
0
        public void Swap(ref MinHeapObject a, ref MinHeapObject b)
        {
            int tempPrio = a.priority;
            int tempVertix = a.vertix;

            a.priority = b.priority;
            a.vertix = b.vertix;

            b.priority = tempPrio;
            b.vertix = tempVertix;
        }
Ejemplo n.º 2
0
 public bool isMember(MinHeapObject m)
 {
     for (int i = 1; i < _index; i++)
     {
         if (m.vertix == _collection[i].vertix)
         {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 3
0
        public void Insert(MinHeapObject a)
        {
            _collection[_index] = a;

            HeapifyUp(_index);
            _index++;
        }
Ejemplo n.º 4
0
        public MinHeapObject ExtractMinimum()
        {
            MinHeapObject minimum = new MinHeapObject(_collection[1].priority, _collection[1].vertix);

            _collection[1].vertix = _collection[_index - 1].vertix;
            _collection[1].priority = _collection[--_index].priority;
            _collection[_index] = new MinHeapObject();
            Heapify();
            return minimum;
        }
Ejemplo n.º 5
0
        public void decreasekey(MinHeapObject m)
        {
            //Console.WriteLine("m.vertix : {0} , m.priority : {1}", m.vertix, m.priority);
            for (int i = 1; i < _index; i++)
            {
                if (m.vertix == _collection[i].vertix)
                {
                    //Console.WriteLine("vertix found");
                  //  Console.ReadLine();
                    _collection[i].priority = m.priority;
                    //Console.WriteLine("vertix found , new priority : " + _collection[i].priority + " index : " + i);

                    HeapifyUp(i);
                }
            }
        }
Ejemplo n.º 6
0
Archivo: UCA.cs Proyecto: fahadm/EVHDM
        public List<Vector2> UniformCostSearch(int from, int to)
        {
            List<MinHeapObject> ListOfHObjects = new List<MinHeapObject>();
            List<int> Previous = new List<int>(Nodes.Count);
            MinHeap Heap = new MinHeap();
            MinHeapObject Root = new MinHeapObject(0, from);
            for (int i = 0; i < Nodes.Count(); i++)
            {
                Previous.Add(-1);
                if (i != from)
                {

                    // Add infinity and node to the  Heap
                    MinHeapObject CurrentObject = new MinHeapObject(Int32.MaxValue, i);

                    Heap.Insert(CurrentObject);
                    ListOfHObjects.Add(CurrentObject);
                }
                else
                {
                    //Insert the root to the Heap with distance =0
                    Heap.Insert(Root);
                    ListOfHObjects.Add(Root);
                }
            }

            while (Heap.Count > 0)
            {
                MinHeapObject Current = Heap.ExtractMinimum();

                if (Current.priority == int.MaxValue) // if priority is infinity then break
                {
                    break;
                }

                for (int i = 0; i < Nodes[Current.vertix].AdjacencyList.Count; i++)
                {
                    int index = Nodes[Current.vertix].AdjacencyList[i];
                    if (Heap.isMember(new MinHeapObject(-1, index)))
                    {

                        int AltDistance = Current.priority + (weights[Current.vertix, index]);

                        if (AltDistance < ListOfHObjects[index].priority)
                        {
                            ListOfHObjects[index].priority = AltDistance;

                            Previous[index] = Current.vertix;
                            //    Console.WriteLine("Updated New parent of " + index + " is " + Current.vertix);
                            Heap.decreasekey(ListOfHObjects[index]);

                        }
                    }
                }

            }
            var RetList = new List<int>();
            var VecList = new List<Vector2>();
            int u = to;
            Console.WriteLine("Path Start\n ");
            while (u >= 0 && Previous[u] != -1 && u != from)
            {

                //Console.WriteLine(u);
                VecList.Add(Nodes[u].Location);
                RetList.Add(u);
                u = Previous[u];
                Console.WriteLine(u);

            }
            if (u != -1)
                VecList.Add(Nodes[u].Location);
            Console.WriteLine("Path End\n");
            //  RetList.Reverse();

            InitializeCriticalPoints(RetList, from, to);

            //         Environment.Exit(0);

            return VecList;
        }