Question

4. Priority queues are very useful structures. Outline a scheme that lets a priority queue function as a FIFO queue. 5. Priority queues are very useful structures. Outline a scheme that lets a priority queue function as a stack.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Priority queues have three basic operations:

  1. IS_EMPTY() : returns whether the queue is empty
  2. INSERT(value, priority) : inserts a new element with the given priority into the queue
  3. PULL() : removes and returns the element with the highest priority in the queue

Based on these basic operations, stack and FIFO queue can be implemented using a priority queue. This will require a global counter to assign the priority to a new element that goes in.

Answer 4.

In a FIFO queue, the first element to go in is the first to come out. Therefore in terms of priority, if element A comes before element B, then A must have a higher priority. This can be implemented as follows:

The FIFO-QUEUE must have a global value priority initialized to MAX_VALUE, which is a very high value, or the highest value if there is a maximum value for priority. For every enqueue operation, this value is updated. The pseudo code for the various operations is given below.

A FIFO-QUEUE is made up of a priority queue Q and a value priority:

INITIALIZATION:

  • Q = EMPTY
  • priority = MAX_VALUE

IS_EMPTY():

  • Return Q.IS_EMPTY()

ENQUEUE(value):

  • Q.INSERT(value, priority)
  • Decrement priority by 1

DEQUEUE()

  • Value v = Q.PULL()
  • If Q.IS_EMPTY() then set priority := MAX_VALUE
  • Return v

Answer 5

Stack is a LIFO structure. Using a priority queue, it can be constructed similar to a FIFO queue, but here, the last element inserted must have the highest priority. This is accomplished as follows:

A STACK consists of a priority queue Q and a value priority:

INITIALIZATION:

  • Q = EMPTY
  • priority = 0

IS_EMPTY():

  • Return Q.IS_EMPTY()

PUSH(value):

  • Q.INSERT(value, priority)
  • Increment priority by 1

POP()

  • Value v = Q.PULL()
  • If Q.IS_EMPTY() then set priority := 0
  • Return v
Add a comment
Know the answer?
Add Answer to:
4. Priority queues are very useful structures. Outline a scheme that lets a priority queue function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

    1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push( "Jane" ); push( "Jess" ); push( "Jill" ); push( pop() ); push( "Jim" ); String name = pop(); push( peek() ); Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation. What is in...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • 2. Stacks and Queues Reinernber: Consider the following function: void systery( int num) { int current:...

    2. Stacks and Queues Reinernber: Consider the following function: void systery( int num) { int current: /* create Stack stk and Queue que / Stacks: the function push places the given parameter on the top of the stack, the function pop removes and returns the top of the stack, and the function empty returns true (i.c., 1) if the stack is empty or false (i.e., 0) otherwise. Queues: the function insert places the given parameter on the end of the...

  • Module 4 follow the materials available at Topic - Stacks and Queues. You should have a...

    Module 4 follow the materials available at Topic - Stacks and Queues. You should have a good understanding of Lists at  Topic - Basic Data structures as a Queue and a Stack are simply implementation of a List with specific properties. Assignment - Implement a Stack computer in Javascript (you will turn in a link to your program in JSFiddle). This is a simple computer that keeps a stack, when a number is entered it goes onto the top of the...

  • operating system Describe the kernel within the data structures stack queue has function trees and bitmap...

    operating system Describe the kernel within the data structures stack queue has function trees and bitmap describe the entities used in each other?

  • Question 4. a.)Given the graph below from part 4.b. you are using the priority queue prims...

    Question 4. a.)Given the graph below from part 4.b. you are using the priority queue prims algorithm. Let the priority queue currently contains nodes A, B. What is the value of node you u=extract_min(Q). b.) Run 1 iteration of Prims while loop, showing the priority queue. 5 A E 1 10 7 2 B с D 3 4 c.) What if you read on the news prioriy queue was improved such that both update_key, and extract min became O(log?(n)) how...

  • a.)Given the graph below from part 4.b. you are using the priority queue prims algorithm. Let...

    a.)Given the graph below from part 4.b. you are using the priority queue prims algorithm. Let the priority queue currently contains nodes A, B. What is the value of node you u=extract_min(Q). b.) Run 1 iteration of Prims while loop, showing the priority queue. 5 А E 1 10 7 2 B с D 3 4 c.) What if you read on the news prioriy queue was improved such that both update_key, and extract min became O(logʻ(n)) how would Prims,...

  • 4. Outline a separation scheme, similar to the one given for this experiment, for separating a...

    4. Outline a separation scheme, similar to the one given for this experiment, for separating a mixture of 2-naphthol (C,H,OH, a very weak acid), 2-naphthylamine (CH,NH2, a weak base) and 1,4-dichlorobenzene. Give all the reaction equations.

  • C++ Data Strucutres and Algorithm Topic: Queues ADT Write an application level function with the following...

    C++ Data Strucutres and Algorithm Topic: Queues ADT Write an application level function with the following signature: QueueType joinQueuesSeg (Const QueueType& q1, const QueueType& q2): It should return a new queue containing all q1 items followed by all q2 items sequentially, while leaving q1 and q2 unchanged. For example: If q1 contains {1. 3, 5, 7} and q2 contains {2, 4} the new queue will contain {1, 3, 5, 7, 2, 4}.

  • Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

    Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {        //TODO 1: Create a priority queue of Strings and assign it to variable queue1               //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1                      System.out.println("Priority queue using Comparable:");        //TODO 3: remove from queue1 all the strings one by one        // with the "smaller" strings (higher priority) ahead of "bigger"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT