本文整理汇总了C++中PriorityQueue::enqueue方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::enqueue方法的具体用法?C++ PriorityQueue::enqueue怎么用?C++ PriorityQueue::enqueue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::enqueue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: buildTree
Node* buildTree(Vector <int> & weightOfChars, char & delimiter){
PriorityQueue<Node*> weightsOfNodes; // declare a PriorityQueue for simple sorting weights of nodes
bool flag = 1; // set flag to prevent overwriting a delimiter
for(int i = 0; i < weightOfChars.size(); ++i){
if(weightOfChars[i] > 0){
Node* node = new Node((i - 128), weightOfChars[i]);
weightsOfNodes.enqueue(node, weightOfChars[i]);
}
else if((flag && (i < 128)) || (flag && (i > 159))){ // looking for a character that is not a control code and has a number of occurences which is equal zero
delimiter = i;
flag = 0;
}
}
// if file is empty the pointer of root of the tree is a NULL
if(weightsOfNodes.isEmpty()){
return NULL;
}
while (weightsOfNodes.size() > 1) {
Node* lNode = weightsOfNodes.dequeue();
Node* rNode = weightsOfNodes.dequeue();
Node* newNode = new Node(lNode, rNode);
weightsOfNodes.enqueue(newNode, newNode->weight);
}
Node* root = weightsOfNodes.dequeue();
return root;
}
示例2: pathBuilderToStart
vector<Node *> dijkstrasAlgorithm(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
vector<Vertex*> path;
for (Vertex* node : graph.getNodeSet()){ //initiate all nodes with an inf. cost
node->cost = INFINITY;
}
PriorityQueue<Vertex*> pQueue;
start->cost = 0;
pQueue.enqueue(start, start->cost); //start of search
while (!pQueue.isEmpty()){
Vertex* v = pQueue.dequeue();
v->visited = true;
v->setColor(GREEN);
if (v == end){
return pathBuilderToStart(start, v); //same as the one bfs uses
}else{
for(Arc* edge : graph.getEdgeSet(v)){ //Go through each edge from the Vertex v, counting the cost for each newly visited vertex
Vertex* next = edge->finish;
if (!next->visited){
double cost = v->cost + edge->cost;
if (cost < next->cost){ //found a lesser cost, what dijkstra is all about
next->setColor(YELLOW);
next->cost = cost;
next->previous = v;
pQueue.enqueue(next, cost);
//pQueue.changePriority(next, cost); Only do this if next already is in the queue, but this should not ever occur?
}
}
}
}
}
return path;
}
示例3: main
int main() {
PriorityQueue<int> *testQueue = new PriorityQueue<int>(3);
// push a few random int's
testQueue->enqueue(4);
testQueue->enqueue(7);
testQueue->enqueue(1);
// queue shouldn't be empty; check this
if(testQueue->isEmpty()) {
cout << "The queue is empty!" << endl;
}
// pop items (more items than were pushed)
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
// should be empty now; check this
if(testQueue->isEmpty()) {
cout << "The queue is empty!" << endl;
}
return 0;
}
示例4: aStar
/*
* Method: aStar
* Parameters: BasicGraph graph by reference
* Vertex pointer variable for path start
* Vertex pointer variable for path end
* - - - - - - - - - - - - - - - - - - - - - - - - - -
* Returns a Vector of Vertex pointer variables for which
* each index corresponds to a step in the path between
* the path's start and end points, if any exists.
* This function uses A*'s algorithm, which evaluates
* the estimated total cost of all neighbors' paths to the end
* from the starting vertex before continuing. It differs from Dijkstra's
* in its prioritization of location based on their estimated total cost
* or distance from the starting point, as opposed to the current cost
* up to that point. Because A* also orients subsequent vertices
* to their earlier parent, this function uses a helper function to
* rewind the shortest route from end to start, if applicable,
* returning an empty vector if not.
*/
Vector<Vertex*> aStar(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
Vector<Vertex*> path;
PriorityQueue<Vertex*> pqueue;
pqueue.enqueue(start, heuristicFunction(start, end));
while(!pqueue.isEmpty()) {
Vertex* current = pqueue.dequeue();
visitVertex(current);
if(BasicGraph::compare(current, end) == 0) break;
for(Edge* edge : current->edges) {
double cost = current->cost + edge->cost;
if(!edge->finish->visited &&
edge->finish->getColor() != YELLOW) {
enqueueVertex(edge->finish, current, cost);
pqueue.enqueue(edge->finish, cost +
heuristicFunction(edge->finish, end));
}
if(edge->finish->getColor() == YELLOW &&
cost < edge->finish->cost) {
enqueueVertex(edge->finish, current, cost);
pqueue.changePriority(edge->finish, cost +
heuristicFunction(edge->finish, end));
}
}
}
determinePath(start, end, path);
return path;
}
示例5: main
int main()
{
PriorityQueue<TaskData> taskPQ; // Priority queue of tasks
TaskData task; // Task
int simLength, // Length of simulation (minutes)
minute, // Current minute
numPtyLevels = 2, // Number of priority levels
numArrivals = 0, // Number of new tasks arriving
j; // Loop counter
// Seed the random number generator
srand((unsigned int)time(NULL));
// cout << endl << "Enter the number of priority levels : ";
// cin >> numPtyLevels;
cout << "Enter the length of time to run the simulator : ";
cin >> simLength;
time_t timer;
for (minute = 0; minute < simLength; minute++)
{
// Dequeue the first task in the queue (if any).
if (taskPQ.isEmpty() == false)
{
task=taskPQ.dequeue();
cout << "Priority " << task.priority << " task arrived at " << task.arrived << " & completed at " << minute << endl;
}
numArrivals = rand() % 4;
switch (numArrivals)
{
case 2:
task.arrived = minute;
task.priority = rand() % numPtyLevels;
//task.priority = rand() % numPtyLevels - (task.arrived / simLength);
taskPQ.enqueue(task);
case 1:
task.arrived = minute;
task.priority = rand() % numPtyLevels;
taskPQ.enqueue(task);
default:
break;
}
}
return 0;
}
示例6: updateVertex
void Theta_Star::updateVertex(PriorityQueue &fringe, Node current, Node &succ, Node *vertex)
{
if(current.parent != NULL && lineOfSight(*current.parent, succ))
{
// Path 2
double g_current = g_val[current.parent->x+current.parent->y*(blocked->getCols()+1)];
double cost = sqrt( (succ.x-current.parent->x) * (succ.x-current.parent->x)
+ (succ.y-current.parent->y) * (succ.y-current.parent->y) );
if(cost + g_current < g_val[succ.x+succ.y*(blocked->getCols()+1)])
{
g_val[succ.x+succ.y*(blocked->getCols()+1)] = cost + g_current;
succ.h = h(succ.x,succ.y);
succ.f = g_val[succ.x+succ.y*(blocked->getCols()+1)]+succ.h;
f_val[succ.x+succ.y*(blocked->getCols()+1)] = succ.f;
succ.parent = vertex->parent;
// we didn't do this before either -> remove from fringe
fringe.enqueue(succ);
}
}
else
{
double g_current = g_val[current.x+current.y*(blocked->getCols()+1)];
double cost = sqrt( (succ.x-current.x) * (succ.x-current.x)
+ (succ.y-current.y) * (succ.y-current.y) );
if(g_current+cost < g_val[succ.x+succ.y*(blocked->getCols()+1)])
{
g_val[succ.x+succ.y*(blocked->getCols()+1)] = g_current+cost;
succ.h = h(succ.x,succ.y);
succ.f = g_val[succ.x+succ.y*(blocked->getCols()+1)]+succ.h;
succ.parent = vertex;
//cout << succ.f << endl;
if(succ.f < f_val[succ.x+succ.y*(blocked->getCols()+1)])
{
fringe.enqueue(succ);
//cout << "enqueued " << succ.x << " " << succ.y << endl;
f_val[succ.x+succ.y*(blocked->getCols()+1)] = succ.f;
}
}
}
}
示例7: if
Vector<Vertex*> dijkstrasAlgorithm(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
PriorityQueue<Vertex*> pqueue;
Vector<Vertex*> path;
pqueue.enqueue(start, 0);
start->cost = 0.0;
start->setColor(YELLOW);
while(!pqueue.isEmpty())
{
Vertex* v = pqueue.dequeue();
v->setColor(GREEN);
if (v == end)
break;
for (auto edge : v->edges)
{
Vertex* next_v = edge->finish;
if (next_v->getColor() == 0)
{
next_v->setColor(YELLOW);
double next_cost = v->cost + edge->cost;
next_v->previous = v;
next_v->cost = next_cost;
pqueue.enqueue(next_v, next_cost);
}
else if (next_v->getColor() == YELLOW)
{
double next_cost = v->cost + edge->cost;
if (next_cost < next_v->cost)
{
next_v->cost = next_cost;
next_v->previous = v;
pqueue.changePriority(next_v, next_v->cost);
}
}
}
}
if (end->getColor() == GREEN)
{
Vertex* path_v = end;
while (path_v->previous)
{
path.insert(0, path_v);
path_v = path_v->previous;
}
path.insert(0, path_v);
}
return path;
}
示例8: aStar
/*
* A* search. Will explore from 'end' until it finds 'start' or can determine that no valid path exists.
* It explores in reverse to make it easier to build path array.
*/
vector<Node *> aStar(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
vector<Vertex*> path;
PriorityQueue<Vertex*> openQueue;
//Setup starting state
end->cost = 0;
end->visited = true;
openQueue.enqueue(end, end->cost);
Vertex* current = nullptr;
//While there are still reachable nodes to explore
while(!openQueue.isEmpty()){
//Set current node to the next node in the queue
current = openQueue.dequeue();
current->setColor(GREEN);
//If current node is target, build path and return
if(current == start){
rewind(end, start, path);
return path;
}
//Add any unvisited neighbor to queue, adjust cost for already visited neighbor nodes
for(Vertex* neighbor : graph.getNeighbors(current)){
if(!neighbor->visited){
neighbor->previous = current;
neighbor->visited = true;
neighbor->cost = current->cost + graph.getArc(current, neighbor)->cost;
openQueue.enqueue(neighbor, neighbor->cost + neighbor->heuristic(start));
neighbor->setColor(YELLOW);
}else{
double newCost = current->cost + graph.getArc(current, neighbor)->cost;
if(neighbor->cost > newCost){
neighbor->previous = current;
neighbor->cost = newCost;
openQueue.changePriority(neighbor, neighbor->cost + neighbor->heuristic(start));
neighbor->setColor(YELLOW);
}
}
}
}
return path;
}
示例9: find_path
void find_path(NavGridLocation from, NavGridLocation to)
{
int grid_size = gs.grid.get_width() * gs.grid.get_height();
int from_id = hash_location(from);
std::vector<AiNode> nodes (grid_size);
for (int i = 0; i < nodes.size(); i++) {
AiNode& node = nodes[i];
node.id = i;
node.visited = false;
node.prev_node = nullptr;
node.true_score = std::numeric_limits<float>::max();
node.guess_score = std::numeric_limits<float>::max();
node.pq_node = nullptr;
}
nodes[from_id].true_score = 0.f;
nodes[from_id].guess_score = heuristic_cost(unhash_location(from_id), to);
PriorityQueue<AiNode*> pq;
nodes[from_id].pq_node = pq.enqueue(&nodes[from_id], -nodes[from_id].guess_score);
update_prev(nodes);
while (!pq.is_empty()) {
AiNode* current = pq.get_front();
pq.dequeue();
current->pq_node = nullptr;
current->visited = true;
if (current->id == hash_location(to)) {
break;
}
for (const NavGridLocation& adj : gs.grid.get_adjacent(unhash_location(current->id))) {
AiNode* adj_node = &nodes[hash_location(adj)];
if (adj_node->visited)
continue;
float new_score = current->true_score + 1;
if (new_score >= adj_node->true_score)
continue;
adj_node->prev_node = current;
adj_node->true_score = new_score;
adj_node->guess_score = new_score + heuristic_cost(unhash_location(adj_node->id), to);
if (adj_node->pq_node) {
pq.update(adj_node->pq_node, -adj_node->guess_score);
} else {
adj_node->pq_node = pq.enqueue(adj_node, -adj_node->guess_score);
}
}
}
update_prev(nodes);
update_path(nodes, to);
}
示例10: main
int main()
{
PriorityQueue<int> *temp = new PriorityQueue<int>();
temp->enqueue(1, 3);
temp->enqueue(2, 9);
temp->enqueue(3, 2);
cout << temp->dequeue() << endl;
cout << temp->dequeue() << endl;
cout << temp->dequeue() << endl;
cout << temp->dequeue() << endl;
delete temp;
return 0;
}
示例11: main
int main(){
PriorityQueue<int> pq;
std::cout << pq.size() << std::endl;
pq.enqueue(1);
pq.present();
pq.enqueue(5);
pq.present();
pq.enqueue(9);
pq.present();
std::cout << pq.size() << " - " << pq.peek() << std::endl;
pq.enqueue(7);
pq.present();
pq.enqueue(3);
pq.present();
pq.enqueue(0);
pq.enqueue(10);
pq.present();
std::cout << pq.size() << " - " << pq.peek() << std::endl;
pq.dequeue();
pq.present();
pq.dequeue();
pq.present();
std::cout << pq.size() << " - " << pq.peek() << std::endl;
return 0;
}
示例12: buildEncodingTree
/* Function: buildEncodingTree
* Usage: Node* tree = buildEncodingTree(frequency);
* --------------------------------------------------------
* Given a map from extended characters to frequencies,
* constructs a Huffman encoding tree from those frequencies
* and returns a pointer to the root.
*
* This function can assume that there is always at least one
* entry in the map, since the PSEUDO_EOF character will always
* be present.
*/
Node* buildEncodingTree(Map<ext_char, int>& frequencies) {
PriorityQueue<Node*> NodePQ;
foreach(ext_char ch in frequencies){
Node* newNode = new Node;
newNode->zero=NULL;
newNode->one=NULL;
newNode->weight=frequencies[ch];
newNode->character=ch;
NodePQ.enqueue(newNode, newNode->weight);
}
示例13: main
int main()
{
PriorityQueue PQ;
string command;
cout << "추가 : en, 삭제 : de, 종료 : quit" << endl;
while (true)
{
cin >> command;
if (command.compare("en") == 0)
{
int data;
cin >> data;
PQ.enqueue(data);
}
else if (command.compare("de") == 0)
示例14: union_set
Set<Edge*> kruskal(BasicGraph& graph) {
Set<Vertex*> vertexs = graph.getVertexSet();
Set<Edge*> edges = graph.getEdgeSet();
map<Vertex*, int> vet_index;
PriorityQueue<Edge*> pqueue;
for (Edge* edge : edges)
pqueue.enqueue(edge, edge->cost);
int N = vertexs.size();
DisjointSet union_set(N);
int i = 0;
for (Vertex* vert : vertexs)
{
vet_index[vert] = i;
++i;
}
Set<Edge*> mst;
while (union_set.count() > 1)
{
Edge* edge = pqueue.dequeue();
int p = vet_index[edge->start];
int q = vet_index[edge->finish];
if (!union_set.connected(p, q))
{
union_set.connect(p, q);
mst.add(edge);
}
}
return mst;
}
示例15: main
int main()
{
//to be able to do fair benchmarks we better use a constant seed
srand(42);
PriorityQueue<int> pq;
for (int i = 1; i < 11; i++)
{
pq.enqueue(i);
}
pq.enqueue(1);
pq.enqueue(99);
pq.enqueue(2);
pq.enqueue(10);
pq.enqueue(3);
pq.enqueue(7);
while(!pq.empty())
{
cout << pq.front() << endl;
pq.dequeue();
}
return 0;
}