Understanding PriorityQueue: A Code Snippet Guide

  • Thread starter Thread starter TenNen
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around the use and understanding of the PriorityQueue data structure in programming, particularly in C++. Participants seek clarification on its operations, differences from simple queues, and request code examples to better grasp its implementation and functionality.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant requests a code snippet to clarify the basic uses of PriorityQueue, indicating confusion from previous readings.
  • Another participant describes the basic operations of a simple queue (FIFO), but does not distinguish it from a priority queue, leading to potential confusion.
  • A participant emphasizes the difference between a simple queue and a priority queue, suggesting that priority queues involve more complex behavior.
  • There are multiple requests for examples of PriorityQueue usage, with one participant expressing a desire for more detailed examples that could be tested on compilers.
  • One participant discusses the implementation of a binary heap as a priority queue, detailing the structure and operations such as insertion and deletion, along with a code example for heap sort.
  • Another participant acknowledges confusion over terminology, suggesting that priority queues are sometimes incorrectly described as first-in-first-out.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding PriorityQueue, with some confusion about its operations compared to simple queues. There is no consensus on the best way to explain or demonstrate its functionality, and multiple perspectives on its implementation are presented.

Contextual Notes

Some participants mention the need for practical examples and code snippets, indicating that theoretical explanations alone may not suffice for understanding. There are also references to external resources that may not be universally accessible or applicable.

Who May Find This Useful

This discussion may be useful for programmers and students looking to understand the PriorityQueue data structure, its operations, and its implementation in C++. It may also benefit those seeking clarification on the differences between simple queues and priority queues.

TenNen
Messages
97
Reaction score
0
Can you give me a code snip clearifying some basic uses of PriorityQueue ?
I read Bjarn but wasn't clear enough, could you please help me ?
Thanks in advance,
 
Computer science news on Phys.org
This is all you need to know:

FIRST IN = FIRST OUT.

There are three main operations, queue, dequeue, and peek. dequeue removes first element, queue adds item to end of list, peek retrieves first item but doesn't remove.

It would be used in cases where you have a bunch of messages or items that need to be accessed in that order. So like if you had a lineup in a store, you would queue people up, and dequeue them in order. In an operating a message loop might be used. As messages arrive they are put on the queue and the program takes one off every loop.;
 
Goalie_Ca said:
This is all you need to know:

FIRST IN = FIRST OUT.

There are three main operations, queue, dequeue, and peek. dequeue removes first element, queue adds item to end of list, peek retrieves first item but doesn't remove.

It would be used in cases where you have a bunch of messages or items that need to be accessed in that order. So like if you had a lineup in a store, you would queue people up, and dequeue them in order. In an operating a message loop might be used. As messages arrive they are put on the queue and the program takes one off every loop.;
Goalie_Ca,
I have tried to search the net but I haven't found anything like examples to test on my compilers, I didn't know about peek either, thanks for your information.
Could you please write me an example using priority_queue ? If Philips were here, he sure would be able to give more examples.
Come on Goal-Ca write it please, I would like to learn a little...true, thanks a lot in advance
 
lol, ya, i got the terms mixed up... but I'm not the only one. I've seen priority queue used to describe that priority is given to first in.

TenNen, firstly can you write a simple queue? (or a linked list?, reference or array based?) From there you simply add a priority variable to each node and change the behaviour of insertion and deletion to accommodate. You can also add other functions.

Here's the c++ STL implementation. Play around with this first, then if you get comfortable write your own (write a linked list first if you don't know how!)
http://www.sgi.com/tech/stl/priority_queue.html
 
TenNen, I'm guessing you want to know about the binary heap priority queue? To implement it you'll need to know a few things.

1. A heap is a binary tree using an array instead of pointers. Each parent should have a maximum number of children. In my example I willl show you a heap that has two children per parent.

Heap: [1,3,5,6,7,8,9] =

Binary Tree:
Code:
        1
       /  \
     3     5
   /   \    / \
  6    7  8  9

Position 0 in the array is the root of the tree. If a parent node is located at X, then the children nodes are located at 2X+1 and 2X+2.

2. Insertion: O(log n) When you insert a number, you first check to see if the queue is full (You haven't exceded the limited size of your array). If your queue isn't full, you want to add the next number in the array. You'll need to keep track of the last index so you know where to place the next node in the array. (Start) After that you want to check if the parent has a larger priority than child node you just inserted. If the child has lower priority than the parent than your done. If not, then you need to check the index. If the index is odd (starting from 0) then you know where the parent is located based on the 2X+1. If the index is even then you know where the parent is located based on the 2X+2. You will then swap the child for the parent. Now you want to go back to the (Start) I placed in this paragraph, and repeat the same check on the new parent.

Example: Insert 2 in the example heap

Step 1
Heap: [1,3,5,6,7,8,9,2] =

Binary Tree:
Code:
        1
       /  \
     3     5
   /   \    / \
  6    7  8  9
  /
2


Step 2
Heap: [1,3,5,2,7,8,9,6] =

Binary Tree:
Code:
        1
       /  \
     3     5
   /   \    / \
  2    7  8  9
  /
6


Step 3
Heap: [1,2,5,3,7,8,9,6] =

Binary Tree:
Code:
        1
       /  \
     2     5
   /   \    / \
  3    7  8  9
  /
6

3. Deletion: O(log n) When you pop off the highest priority you take the last element in the array and move it to the root of the tree (element 0 in the array). You'll then need to heap sort the heap.

Pop Highest Priority Element
Heap: [6,2,5,3,7,8,9] =

Binary Tree:
Code:
        6
       /  \
     2     5
   /   \    / \
  3    7  8  9

Heap Sort: O(n log n)

Code:
#include<iostream>

using namespace std;

void shift_down(int heap[], int top, int bottom) {

bool finished = false;
int temp, max ;

while( (top*2 <= bottom) && !finished ) {

        if ( top*2 == bottom ) {
                max = top * 2 ;
        } else if ( heap[top * 2] > heap[top * 2 + 1] ) {
                max = top * 2 ;
        } else {
                max = top * 2 + 1 ;
        }

        if( heap[top] < heap[max] ) {
                temp = heap[top] ;
                heap[top] = heap[max] ;
                heap[max] = temp ;
                top = max ;
        } else {
                finished = true ;
        }
}}

void heap_sort( int heap[] , int heap_size ) {

int temp ;

for( int index = (heap_size / 2)-1 ; index >= 0 ; index-- ) {
        shift_down(heap, index, heap_size) ;
}

for( int index = heap_size-1 ; index >= 1 ; index-- ) {
        temp = heap[0] ;
        heap[0] = heap[index] ;
        heap[index] = temp ;
        shift_down(heap, 0, index-1) ;
}}

int main( int argc, char *argv[] ) {

int heap[7] = {6,2,5,3,7,8,9};

for( int index = 0 ; index < 6 ; index++ ) {
        cout << heap[index];
}

cout << endl;

heap_sort(heap , 7);

for( int index = 0 ; index < 6 ; index++ ) {
        cout << heap[index];
}

cout << endl;

return 0;

}

Here is the code for heap sort. It will sort my example and print out the results (before and after). I encourage you to take the time to examine it and figure out how it works. This is a pretty standard algorithm. If you want the detailed explantion of how heap sort works or have any additional questions please ask.
 
Last edited:
Goalie_Ca said:
lol, ya, i got the terms mixed up... but I'm not the only one. I've seen priority queue used to describe that priority is given to first in.

TenNen, firstly can you write a simple queue? (or a linked list?, reference or array based?) From there you simply add a priority variable to each node and change the behaviour of insertion and deletion to accommodate. You can also add other functions.

Here's the c++ STL implementation. Play around with this first, then if you get comfortable write your own (write a linked list first if you don't know how!)
http://www.sgi.com/tech/stl/priority_queue.html
lol,
Okay, I am sorry too,
Thanks for your help, lol
 
dduardo, that's really nice of you, Thanks a lot,
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
18
Views
9K
Replies
65
Views
4K
Replies
15
Views
11K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 8 ·
Replies
8
Views
900
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 4 ·
Replies
4
Views
1K