Question

In: Computer Science

Consider an almost-priority-queue data structure, which allows two operations: insert and extract almost min. extract almost...

Consider an almost-priority-queue data structure, which allows two operations: insert and extract almost min. extract almost min operation outputs either the first minimum or a second minimum item from the current structure (and doesn’t tell you which it outputted). Prove that not both these operations can be done in o(log n) time even if amortization is allowed.

Solutions

Expert Solution

A priority queue is an abstract data type similar to regular queue or stack data structure in which each elements are stored based on priority . In a priority queue, an element with high priority is placed before an element with low priority. In some implementations, if two elements have the same priority, they are placed in the order they are entered.

Proof

Suppose we have N elements and we have to insert these elements in the priority queue. We can use list and can insert elements in O(N) time and can sort them to maintain a priority queue in O(N logN ) time.

But here we don't need sorting to extract the min value.

Only one for()loop is required for insertion and also for extract the minimum value. The code for inserting an element is

void insert_value (int Arr[ ], int val)

{

length = length + 1; Arr[ length ] = -1;

//assuming all the numbers greater than 0 are to be inserted in queue.

increase_val (Arr, length, val1);

}

The time complexity for the code is o(log n);

The code for extracting minimum value is given

int extract_minimum (int Arr[ ])

{

if(length == 0)

{

cout<< “Can’t remove element as queue is empty”;

return -1;

}

int max = Arr[1];

Arr[1] = Arr[length];

length = length -1;

min_heapify(Arr, 1);

return min;

}

The code takes first element as minimum value and places the last element in its position. Then min_heapify function will be called where each value is comapred n-1 times and the time complexity is same as o(log n) even if it is amortized.


Related Solutions

Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
A max-heap can be used as a max-priority queue, which is an abstract data type having...
A max-heap can be used as a max-priority queue, which is an abstract data type having the operations Insert, Maximum, Extract-Max, and Increase-Key. a. Describe how to implement a FIFO queue with a priority queue, and analyze the time complexity of the implementation (Insert and Delete operations), assuming that a heap is used for the priority queue. b. Describe how to implement a LIFO queue (i.e. a stack) with a priority queue, and analyze the time complexity of the implementation...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
Consider the following two-server one-queue system from time = 0 to time = 20 min. If...
Consider the following two-server one-queue system from time = 0 to time = 20 min. If both servers are available when a customer arrives, the customer will choose server1. Customers waiting in the queue enter service whenever any one of the two servers becomes available (first come, first serve). Arrivals and service times are: • Customer #1 arrives at t = 0 and requires 2 minutes of service time • Customer #2 arrives at t = 1 and requires 5...
Consider two compounds for which tm = 1 min, t1 = 14.3 min and t2 =...
Consider two compounds for which tm = 1 min, t1 = 14.3 min and t2 = 15.0 min. The peak widths at half heights are 13.0 sec and 14.9 sec, respectively. What are the peak widths? (This requires that you think about the mathematics of a Gaussian curve.) Calculate the resolution of this separation. How many theoretical plates does the column have for these analytes? What is the value of the selectivity factor, α?
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
1. A double-ended queue, or deque, is a data structure consisting of a list of items...
1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x): insert item x on the back end of the queue addToFront(x): insert item x on the front end of the queue getBack(): returns the element on the back end of the queue getFront(): returns the element on the front end of the queue removeBack(): remove the back item from the queue removeFront(): remove the front item...
Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
Consider a binomial heap where only insert and findmin operations are allowed (no extractmin). Show that...
Consider a binomial heap where only insert and findmin operations are allowed (no extractmin). Show that both of these operations can be done in amortized O(1) time. Hint: For findmin consider explicitely storing a pointer to the tree with minimum root and then update it during insert if needed. For insert, you’ll need to use amortization and develop a good potential function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT