int* vmin(int* vertices, Grafo &my_grafo){
	map <int, Aresta *> arestas = my_grafo.get_allArestas();
	int *retorno = new int[arestas.size()]; // vetor dos id das arestas

	// primeiramente, seleciona-se as arestas conforme a regra classica de conjunto dijunto de prim   

	for (int i = 0; i<arestas.size(); i++){ //O(m) m arestas
		Aresta *a = arestas[i];
		if (vertices[a->getOrigem()] != vertices[a->getDestino()]){
			retorno[a->getId()] = 1; 
		} else {
			retorno[a->getId()] = 0; 
		}
	}

	// depois verificamos a dominância entre as arestas

	for (int i = 0; i<arestas.size(); i++){ // O(m^2)
		if (retorno[i] == 1){ // se a aresta de id=i está contida no conjunto... 
			Aresta *ai = arestas[i];
			for (int j = i+1; j<arestas.size(); j++){
				if (retorno[j] == 1 && retorno[i] == 1){
					if (a_domina_b(ai, arestas[j])){
						retorno[j] = 0;
					} else if (a_domina_b(arestas[j], ai)){
						retorno[i] = 0;
					}
				}
			}
		}
	}
	return retorno;
}
ListaAdjacente::~ListaAdjacente()
{
    Aresta * p = origem;
    while(p != NULL){
        Aresta * aux = p;
        p = p->getProx();
        delete aux;
    }
}
void ListaAdjacente::removeFirst()
{
    if(origem == NULL)
        return;
    
    Aresta * p = origem;
    origem = p->getProx();
    size--;
    delete p;
}
bool ListaAdjacente::hasInList(int n)
{
    Aresta * p = origem;
    while(p != NULL){
        if(p->getId() == n)
            return true;
        p = p->getProx();
    }
    return false;
}
int ListaAdjacente::getSize(){
    Aresta * p = origem;
    int count = 0;
    
    while(p != NULL){
        count++;
        p = p->getProx();
    }
    
    return count;
}
string ListaAdjacente::printAdjacencias()
{
    stringstream ss;
    Aresta * p = origem;
    while(p != NULL){
        ss << p->getId() << " - Força: " << p->getForca() << endl;
        p = p->getProx();
    }
    ss << endl;
    
    return ss.str();
}
void Grafo::JsonArvoreMinima() {
	std::vector<Aresta *> lArestas = this->Kruskal();
	int custo = 0;
	int id1, id2;
	cout << "{\"arvoreminima\":{\"arestas\":[";
	for(int i = 0; i < (int) lArestas.size(); i++) {
		Aresta * cAresta = lArestas[i];
		if (i > 0) cout << ",";
		id1 = cAresta->GetId1();
		id2 = cAresta->GetId2();
		custo+= cAresta->GetPeso();
		cout << "(" << id1 << "," << id2 << ")";
	}
	cout << "],\"custo\":" << custo << "}}" << endl;
}
Example #8
0
void Dfs::visit(Vertice *vertice) {
    Aresta *aresta; Vertice *verticeAtual;
    vertice->setCor(Qt::gray);
    emit update(grafo);
    sleep(1);
    vertice->setTempoEntrada(tempo++);
    for(aresta = vertice->getArestas(); aresta; aresta = aresta->getProximo()) {
        verticeAtual = grafo->getVertice()[aresta->getIndiceAdj()];
        if(verticeAtual->getCor() == Qt::white) {
            verticeAtual->setPai(vertice);
            visit(verticeAtual);
        }
    }
    vertice->setTempoSaida(tempo++);
    vertice->setCor(Qt::black);
    emit update(grafo);
    sleep(1);
}
bool ListaAdjacente::addLast(int id, int forca)
{
    Aresta * p = new Aresta();
    p->setId(id);
    p->setForca(forca);
    
    if(vazia()){
        origem = p;
        fim = p;
    } else {
        if(hasInList(id)){
            return false;
        }
        Aresta * aux = new Aresta();
        aux = fim;
        aux->setProx(p);
        fim = p;
    }
    
    size++;
    return true;
}
void ListaAdjacente::removeByIndex(int pos){
    if(origem == NULL)
        return;
    
    if(origem->getId() == pos){
        removeFirst();
        return;
    }
    
    Aresta * p = origem;
    
    while(p->getProx() != NULL && p->getProx()->getId() != pos)
        p = p->getProx();
    
    if (p->getProx() == NULL)
        return;
    
    Aresta * aux = p->getProx();
    
    p->setProx(aux->getProx());
    
    --size;
    delete aux;
}
/* recebe um vetor de inteiros, onde o valor do indice i é 1 se a aresta de id=i deve ser contabilizada
retorna um vetor de aresta do grafo normal 
*/
vector <Aresta *> maximal(Grafo *g, int *arestas, Conjunto *conjIt, vector<pair<int, int> > relacao2){ 
	vector <Aresta *> retorno;
	for (int i=0; i<g->getQuantArestas(); i++){
		Aresta *aresta = (g->get_allArestas())[i];
		//if (arestas[aresta->getId()]!=1){
			if (conjIt->compare(aresta->getOrigem(), aresta->getDestino())==false){
				bool esta = false;
				for (int j=0; j<relacao2.size(); j++){
					Aresta *firstt = (g->get_allArestas())[relacao2[j].first];
					if (relacao2[j].second == aresta->getId() && conjIt->compare(firstt->getOrigem(), firstt->getDestino())==false) {
						esta = true;
						break;
					}
				}
				if (esta == false){
					retorno.push_back(aresta);
				//	return retorno;
				}
			}
		//}
	}
	
	return retorno;
}
Example #12
0
						bool operator==( Aresta &A ){
							
							return (this->custo == A.getCusto());

						}
Example #13
0
						bool operator>( Aresta &A ){
							
							return (this->custo > A.getCusto());

						}
Aresta::Aresta(const Aresta & cAresta) {
	this->id1 = cAresta.GetId1();
	this->id2 = cAresta.GetId2();
	this->peso = cAresta.GetPeso();
}
std::vector<Aresta *> Grafo::Kruskal() {
	std::vector<Aresta *> retorno;
	//so faz se nao eh direcionado
	if (!this->Direcionado()) {
		//conjunto de vertices e ranks
		std::vector<int> ranks;
		//inicializa conjunto de vertices e ranks
		Heap * nHeap;
		int tamHeapMax = 0;
		conjKruskal.clear();
		ranks.clear();
		for (int i = 0; i < tamGrafo; i++) {
			this->conjKruskal.push_back(i);
			ranks.push_back(1);
			for (int j =0; j < tamGrafo; j++)
				if (this->Conexao(i,j))
					tamHeapMax++;
		}
		nHeap = new Heap(tamHeapMax);
		nHeap->SetTipo(MINIMO);
		for (int i = 0; i < tamGrafo; i++) {
			for (int j =i; j < tamGrafo; j++) {
				if (this->Conexao(i,j)) {
					Aresta * nAresta = new Aresta(*this->GetVertice(i).GetAresta(j));
					nHeap->Push(nAresta);
				}
			}
		}
		
		//nHeap->ListaHeap();
		int id1;
		int id2;
		int id1tmp;
		int id2tmp;

		std::vector<Aresta *> lArestas;

		while(nHeap->GetTamHeap() > 0 ) {
			Aresta * cAresta = nHeap->Pop();
			id1 = cAresta->GetId1();
			id2 = cAresta->GetId2();
			//cout << "id1: " << id1 << " id2:" << id2 << endl;
			
			if ((id1tmp = this->BuscaKruskal(id1)) != (id2tmp = BuscaKruskal(id2))) {
				//cout << "conjuntos diferentes: " << "id1: " << this->BuscaKruskal(id1) << " id2: " << this->BuscaKruskal(id2) << endl;
				retorno.push_back(cAresta);
				if (ranks[id1tmp] != ranks[id2tmp]) {
					int maior = (ranks[id1tmp] > ranks[id2tmp] ? id1tmp : id2tmp);
					int menor = (ranks[id2tmp] > ranks[id1tmp] ? id1tmp : id2tmp);
					conjKruskal[menor] = maior;
					//cout << "ranks: " << " menor: " << ranks[menor] << "maior: " << ranks[maior] << endl;
				} else {
					conjKruskal[id2tmp] = id1tmp;
					ranks[id1tmp] += 1;
					//cout << "aumentou rank" << endl;
				}
			}
		}
	}
	
	return retorno;
}