Exemplo n.º 1
0
void testPriorityQueue() {
    cout << "PriorityQueue => ";
    PriorityQueue* q = new PriorityQueue();
    CharString* cc = new CharString("test@#$%^*()_+1234567890",24);
    q->insert(0,cc);
    CharString* qq = (CharString*)q->removeMin();
    if(qq == cc) {
        cout << "Pass" << endl;
    } else {
        cout << "Fail" << "(" << qq->get() << ")" << endl;
    }
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
// DESC: Dijkstra's algorithm to find shortest distance from source to destination on a graph.
// INPUT: Graph to expand on, Vertex to represent as the source.
// OUTPUT: Linked list of the path
LinkedListT* DijkstraDistances(Graph* graph, Vertex* source) {
    // form a cloud to every point.
    // Store this cloud in the PQueue.
    LinkedListT* verticies = graph->verticies();
    PriorityQueue* pq = new PriorityQueue();

    // loop through verticies
    for(int i=0; i<verticies->size(); i++) {
        Vertex* vv = (Vertex*)verticies->get(i);
        if(vv == source) {
            vv->distance = 0; // set vertex to zero if it is the source vertex.
        } else {
            vv->distance = 999999999; // set verticies to infinity to represent an unreachable location.
        }

        // insert into queue.
        pq->insert(vv->distance,vv);
    }

    // empty out the set
    while(!pq->empty()) {
        // remove the minimum distance from the set. (usually source vertex on start)
        Vertex* v = (Vertex*)pq->removeMin();
        LinkedListT* vt = v->incidentEdges;

        // loop through incident Edges
        for(int i=0; i<vt->size(); i++) {
            Edge* e = (Edge*)vt->get(i);
            Vertex* v2 = e->opposite(v);
            int r = v->distance + e->data;

            // if the total range is less than v2's distance, then set it.
            if(r < v2->distance) {
                v2->distance = r;
                pq->replaceKey(pq->top(),(void*)v2,r);
            }
        }
    }
}
Exemplo n.º 4
0
int main() 
{  	
		int comps=0;	//BH comparisons
		string txt;
		cout << "Enter name of txt file without extension: " << endl;
		cin >> txt;
		string str(txt + ".txt");
		Graph graph(str);
		graph.PrintGraph();
		
		int sVert, dVert;
		cout << "Enter the source vertex: " << endl;
		cin >> sVert;
		cout << "Enter the destination vertex: " << endl;
		cin >> dVert;
		cout << " " << endl;
		
		
		// for storing distances and extracting the minimum;  equivalent to Q and d[] on the slides
		PriorityQueue<int> pq;
		
		int size = graph.getSize();		//graph size
		int kArray[size];
		for(int i = 0; i < size; i++)	 {kArray[i] = INT_MAX;}	//key/weight
		int xArray[size];
		for(int i = 1; i <= size; i++) {xArray[i-1] = i;}		//element
		
		pq.createPriorityQueue(kArray, xArray, xArray, size);
		// for storing the parent (previous node) on the path; equivalent to pi[] on the slides
		int* shortestPathParent = new int[size + 1];
		for(int i = 0; i < size; i++) 	{shortestPathParent[i] = 0;}
		
		
		// Dijkstra's algorithm
		Locator sl = pq.getLocator(sVert);	//locator keeps track of element's memory address
		pq.replaceKey(sl,0);
		if(sVert > size || dVert > size || sVert < 0 || dVert < 0) throw "No such vertex exists";
		vector<Vertex*> del; // removed vertices
		vector<Vertex*> vertList = graph.getVertices();
		
		while( !pq.isEmpty()) {
			Vertex* u = vertList[pq.minElement() - 1];
			Locator locu = pq.getLocator(u->getID());
			int ukey = pq.getKey(locu);
			pq.removeMin();
			del.push_back(u);
			vector<Edge*> edges = u->getOutEdges();
			for( int i = 0; i < edges.size(); i++ ) {
				Vertex* v = edges[i] -> geteVertP();
				int w = edges[i] -> getWeight();
				Locator locv = pq.getLocator(v->getID());
				if(comps++, pq.getKey(locv) > (ukey + w)) 			//if (d[b] > d[a] + w(a,b))
				{
					pq.replaceKey(locv, ukey + w);		  			//d[b] = d[a] + w(a,b)
					shortestPathParent[v->getID()] = u->getID();	//P[b] = a
				}
			}
		}
	
		Vertex* d = graph.getVertex(dVert);
		Vertex* s = graph.getVertex(sVert);
		vector<int>vers, wt;
		vector<Edge*> e = vertList[dVert-1]->getInEdges();
		int c = 0;
		vers.push_back(dVert);
		if (sVert == dVert)
		{
			vers.push_back(dVert);
			wt.push_back(0);
		}
		else
		{
			while (c != sVert)
			{
				//compare edges
				int minw = e[0]->getWeight();
				int minv = e[0]->getsVertP()->getID();

				for (int i = 0; i < e.size(); i++)
				{
					if(e[i]->getWeight() < minw)
					{
						minw = e[i]->getWeight();
						minv = e[i]->getsVertP()->getID();
					}
				}
				wt.push_back(minw);
				vers.push_back(minv);
				c = vertList[minv-1]->getID();
				e = vertList[minv-1]->getInEdges();
			}
		reverse(wt.begin(),wt.end());
		reverse(vers.begin(),vers.end());
		}
		for (int i = 0; i < vers.size()-1; i++)
		{
			cout << vers[i] << "--[" << wt[i] << "]-->";
		}
			cout << dVert << endl;
			
		int dist = 0;
		for (int i = 0; i < wt.size(); i++)
		{
			dist = dist + wt[i];
		}
		cout << " " << endl;
		cout << "Total weight of the shortest path from " << sVert << " to " << dVert << " = " << dist << endl;
		cout << "Total number of comparisons needed to decrease key values: " << comps;
		
		return 0;
	}