void Graph::dijkstra(Vertex *src) { comparisons = 0;//tallies number of comparisons made src->d = 0; src->p = NULL; PriorityQueue<Vertex*> pq; for(int i = 0;i<vertList.size();++i){//Fill priority queue if(vertList[i]->getID() != src->getID()){ vertList[i]->d = INT_MAX; vertList[i]->p = NULL; } pq.insertItem(vertList[i]->d, vertList[i], vertList[i]->getID()); } while(!pq.isEmpty()){//Start sifting through the vertices Vertex* u = pq.minElement(); pq.removeMin(); int sz = u->outList.size(); for(int i =0; i < sz;++i){ int alt = u->d + u->outList[i]->getWeight(); Vertex* evalVert = u->outList[i]->geteVertP(); comparisons++; if(u->outList[i]->geteVertP()->d == INT_MAX){//relax function Locator repkey = pq.getLoc(evalVert->getID()); pq.replaceKey(repkey,alt); evalVert->setD(alt); evalVert->setP(u); } else if(alt < u->outList[i]->geteVertP()->d){//relax function Locator repkey = pq.getLoc(evalVert->getID()); pq.replaceKey(repkey,alt); evalVert->setD(alt); evalVert->setP(u); } } } comparisons += pq.getComps();//grab comparisons from priority queue return; }
int main() { PriorityQueue<string> pq; PQVector<string> pqv; string elem; int what = 1; cout << "1. Priority Queue with Binary Heap\n2. Priority Queue with Vector\nChoic: "; cin >> what; if(what ==1){ string again, right, left; left = 'y'; right = 'Y'; again = 'Y'; elem = "abc"; pq.insertItem(3, elem); elem = "2ab"; pq.insertItem(4, elem); elem = "bbc"; pq.insertItem(1, elem); elem = "cnn"; pq.insertItem(5, elem); elem = "ddn"; pq.insertItem(6, elem); elem = "eda"; pq.insertItem(7, elem); elem = "ggg"; pq.insertItem(9, elem); elem = "kkk"; pq.insertItem(10, elem); elem = "lll"; pq.insertItem(11, elem); elem = "mmm"; pq.insertItem(12, elem); // elem = "qqq"; pq.insertItem(16, elem); cout << "\nAlready inserted heaps\n"; cout << "===============\n"; pq.printPQ(); cout << "===============\n"; // cout << "---------Before Removing Min\n"; // cout << "minElement: " << pq.minElement() << endl; // cout << "minKey: " << pq.minKey() << endl; // cout << "Min element removed." << endl; // pq.remove(pq.min()); // cout << "--------After removing min\n"; // cout << "minElement: " << pq.minElement() << endl; // cout << "minKey: " << pq.minKey() << endl; // pq.decreaseKey(pq.min(),pq.minKey()); // cout << "--------After decreasing loc 3\n"; // pq.decreaseKey(3,6); // cout << "================\n"; // pq.decreaseKey(pq.minKey(), pq.min()); while (again.compare(right) == 0 || again.compare(left) == 0) { char choi; cout << "What would you like to do? (Press corresponding number and hit enter)"<<endl; cout << "1. Insert items into a priority queue."<<endl; cout << "2. Print out priority queue."<<endl; cout << "3. Remove an item with a specific location."<<endl; cout << "4. Find item with minimum key."<<endl; cout << "5. Decrease Key."<<endl; cout << "6. Build Priority Queue." << endl; cout << "7. Exit."<<endl; cout << "Choie: "; cin >> choi; if(choi == '1') { int key; char element[255]; cout << "Key: "; cin >> key; cout << "Elem: "; cin >> element; pq.insertItem(key, element); } else if(choi == '2')