Esempio n. 1
0
long long Graph::prim(const string& s){
	vmap::const_iterator itr = vertexMap.find(s);
	if(itr == vertexMap.end())
		cerr<<"Source vertex not found"<<endl;

	clearAll();
	int n = vertexMap.size() - 1;
	Vertex *p = itr->second;
	vector<Vertex*> Q;
//	priority_queue<Vertex*, vector<Vertex*>, Compare> Q;
	p->dist = 0;
	for(vmap::const_iterator it = vertexMap.begin(); it != vertexMap.end(); it++)
	Q.push_back(it->second);
	long long cost = 0;

	while(!Q.empty()){

		make_heap(Q.begin(), Q.end(), Compare());
		Vertex *u = Q.front();
		u->done = true;
		pop_heap(Q.begin(), Q.end(), Compare());
		Q.pop_back();
		for(vector<Edge>::const_iterator it = u->adj.begin(); it != u->adj.end(); it++){
			Vertex *v = it->dest;
			if(!v->done && v->dist > it->cost){
				v->dist = it->cost;
				v->prev = u;
//				cout<<"cost of "<<v->name<<" is updated to "<<v->dist<<endl;
			}
		}

	}

	for(vmap::const_iterator it = vertexMap.begin(); it != vertexMap.end(); it++){
	 	cost+=it->second->dist;
	}

	return cost;
}
Esempio n. 2
0
void graph::addvertex(const string &name)
{
	vmap::iterator itr=work.begin();
	itr=work.find(name);
	if(itr==work.end())
	{
		vertex *v;
		v= new vertex(name);
		work[name]=v;
		return;
	}
	cout<<"\nVertex already exists!";
}
Esempio n. 3
0
map<mmddyyyy, UI> construct_hits_map(const vmap &r)
{
	map<mmddyyyy, UI> k;

	// construct map of dates and number of visits in each date
	for(vmap::const_iterator i = r.begin(); i!=r.end(); i++)
	{
		for(dvec::const_iterator j = i->second.begin(); j!=i->second.end(); j++)
		{
			if(k.find(*j)==k.end())
				k[*j] = 1;
			else
				k[*j]++;
		}
	}

	// erase unwanted elements
	mmddyyyy td = k.rbegin()->first;
	td.yyyy--; // this serves as the upper bound for erasure
	k.erase(k.begin(), k.upper_bound(td));

	return k;
}
Esempio n. 4
0
void Graph::clearAll(){

	for(vmap::iterator it = vertexMap.begin(); it != vertexMap.end(); it++)
		(*it).second->reset();
}