Exemplo n.º 1
0
Arquivo: main.cpp Projeto: hutamaki/cg
    bool isWorthTrying(Node<Plot>* node, PriorityQueue& pqueue, std::unordered_map<Plot*, int>& closeList)
    {
        auto found(pqueue.find(node));
        if (found != pqueue.end())
        {
            if ((*found)->f < node->f) return false;
            else
            {
                Node<Plot>* n(*found);
                pqueue.erase(found);
                delete n;
            }
        }

        auto cl_found(closeList.find(node->node));
        if (cl_found != closeList.end())
        {
            if (cl_found->second < node->f) return false;
            else closeList.erase(cl_found);
        }
        return true;
    }
Exemplo n.º 2
0
void prim(Graph graph, char startNode){
	PriorityQueue priorityQueue;
	Node node;
	node.key = INT_MAX;
	node.pi = '\0';

	// for (auto it = graph.getVertices().begin(); it != graph.getVertices().end(); it++){
		// node.name = *it;
		// priorityQueue.push(node);
	// }

	string tempString = "";
	for (auto it = graph.getVertices().begin(); it != graph.getVertices().end(); it++)
		tempString += *it;

	cout << tempString << "!!!" << endl;
	for (int i = 0; i < tempString.length(); i++){
		node.name = tempString.c_str()[i];
		priorityQueue.push(node);
	}
	// Node node;
	// node.key = INT_MAX;
	// node.pi = '\0';

	// node.name = 'i';
	// priorityQueue.push(node);
	// node.name = 'a';
	// priorityQueue.push(node);
	// node.name = 'b';
	// priorityQueue.push(node);
	// node.name = 'c';
	// priorityQueue.push(node);
	// node.name = 'd';
	// priorityQueue.push(node);
	// node.name = 'e';
	// priorityQueue.push(node);
	// node.name = 'f';
	// priorityQueue.push(node);
	// node.name = 'g';
	// priorityQueue.push(node);
	// node.name = 'h';
	// priorityQueue.push(node);
	

	cout << "finished inserting vertices" << endl;
	priorityQueue.decreaseKey(startNode, 0, '\0');
	while (!priorityQueue.empty()){
		Node node = priorityQueue.extractMin();
		cout << "extracted: " << node.name << ": " << node.key << ", " << node.pi << endl;
		if (node.name != node.pi && node.pi != '\0')
			cout << "(" << node.name << ", " << node.pi << ") " << endl;
		for (auto it = graph.getEdges().begin(); it != graph.getEdges().end(); it++){
			cout << "checking " << it->first << endl;
			if (it->first.c_str()[0] == node.name || it->first.c_str()[1] == node.name){// Adjacency
				char adjacency = (it->first.c_str()[0] == node.name)?it->first.c_str()[1]:it->first.c_str()[0];
				cout << "going for adj" << adjacency << endl;
				if (priorityQueue.find(adjacency) && priorityQueue.getNode(adjacency).key > graph.getWeight(node.name, adjacency)){
					cout << "decreasingKey: " << adjacency << ", w: " << graph.getWeight(node.name, adjacency) << ", " << node.name << endl;
					priorityQueue.decreaseKey(adjacency, graph.getWeight(node.name, adjacency), node.name);
				}
			}
		}
	}
	cout << endl;
}