Consider an extension of the queue abstract data type, called a minqueue, which supports
operations ENQUEUE, DEQUEUE, and FINDMIN. Assume that the elements to be stored are
integers (or any other totally ordered data type). FINDMIN simply returns the smallest element
in the minqueue, but does not remove it. Using a standard implementation of a queue, the
FINDMIN operation takes O(n) time in the worst case. However, consider a more clever data
structure for this abstract data type which works as follows: There are two regular queues used.
The first queue is called the ”real queue” and the second queue is called the ”helper queue”.
When the ENQUEUE(x) operation is performed, the element x is enqueued in the regular way on
the real queue. The element x is also enqueued at the end of the helper queue. However, if the
element y immediately ”in front” of x on the helper queue is larger than x, then y is removed
from the helper queue. This process of x annihilating the element immediately in front of it is
repeated until the element immediately in front of x is less than or equal to it or x is at the front
of the helper queue.
Describe how the DEQUEUE and FINDMIN operation are implemented in this data structure.
Give the worst case running time for each of ENQUEUE, DEQUEUE, and FINDMIN using this
data structure.
Give an argument using the potential method to show that any sequence of n ENQUEUE,
DEQUEUE, and FINDMIN operations requires only O(n) time. Or namely, the amortized
complexity of these operations is O(1).
Consider an extension of the queue abstract data type, called a minqueue, which supports
operations ENQUEUE, DEQUEUE, and FINDMIN. Assume that the elements to be stored are
integers (or any other totally ordered data type). FINDMIN simply returns the smallest element
in the minqueue, but does not remove it. Using a standard implementation of a queue, the
FINDMIN operation takes O(n) time in the worst case. However, consider a more clever data
structure for this abstract data type which works as follows: There are two regular queues used.
The first queue is called the ”real queue” and the second queue is called the ”helper queue”.
When the ENQUEUE(x) operation is performed, the element x is enqueued in the regular way on
the real queue. The element x is also enqueued at the end of the helper queue. However, if the
element y immediately ”in front” of x on the helper queue is larger than x, then y is removed
from the helper queue. This process of x annihilating the element immediately in front of it is
repeated until the element immediately in front of x is less than or equal to it or x is at the front
of the helper queue.
Describe how the DEQUEUE and FINDMIN operation are implemented in this data structure.
Give the worst case running time for each of ENQUEUE, DEQUEUE, and FINDMIN using this
data structure.
Give an argument using the potential method to show that any sequence of n ENQUEUE,
DEQUEUE, and FINDMIN operations requires only O(n) t.
Andreas Schleicher presents at the launch of What does child empowerment mean...
Consider an extension of the queue abstract data type, called a .pdf
1. Consider an extension of the queue abstract data type, called a minqueue, which supports
operations ENQUEUE, DEQUEUE, and FINDMIN. Assume that the elements to be stored are
integers (or any other totally ordered data type). FINDMIN simply returns the smallest element
in the minqueue, but does not remove it. Using a standard implementation of a queue, the
FINDMIN operation takes O(n) time in the worst case. However, consider a more clever data
structure for this abstract data type which works as follows: There are two regular queues used.
The first queue is called the ”real queue” and the second queue is called the ”helper queue”.
When the ENQUEUE(x) operation is performed, the element x is enqueued in the regular way on
the real queue. The element x is also enqueued at the end of the helper queue. However, if the
element y immediately ”in front” of x on the helper queue is larger than x, then y is removed
from the helper queue. This process of x annihilating the element immediately in front of it is
repeated until the element immediately in front of x is less than or equal to it or x is at the front
of the helper queue.
Describe how the DEQUEUE and FINDMIN operation are implemented in this data structure.
Give the worst case running time for each of ENQUEUE, DEQUEUE, and FINDMIN using this
data structure.
Give an argument using the potential method to show that any sequence of n ENQUEUE,
DEQUEUE, and FINDMIN operations requires only O(n) time. Or namely, the amortized
complexity of these operations is O(1).
Consider an extension of the queue abstract data type, called a minqueue, which supports
operations ENQUEUE, DEQUEUE, and FINDMIN. Assume that the elements to be stored are
integers (or any other totally ordered data type). FINDMIN simply returns the smallest element
in the minqueue, but does not remove it. Using a standard implementation of a queue, the
FINDMIN operation takes O(n) time in the worst case. However, consider a more clever data
structure for this abstract data type which works as follows: There are two regular queues used.
The first queue is called the ”real queue” and the second queue is called the ”helper queue”.
2. When the ENQUEUE(x) operation is performed, the element x is enqueued in the regular way on
the real queue. The element x is also enqueued at the end of the helper queue. However, if the
element y immediately ”in front” of x on the helper queue is larger than x, then y is removed
from the helper queue. This process of x annihilating the element immediately in front of it is
repeated until the element immediately in front of x is less than or equal to it or x is at the front
of the helper queue.
Describe how the DEQUEUE and FINDMIN operation are implemented in this data structure.
Give the worst case running time for each of ENQUEUE, DEQUEUE, and FINDMIN using this
data structure.
Give an argument using the potential method to show that any sequence of n ENQUEUE,
DEQUEUE, and FINDMIN operations requires only O(n) time. Or namely, the amortized
complexity of these operations is O(1).
Consider an extension of the queue abstract data type, called a minqueue, which supports
operations ENQUEUE, DEQUEUE, and FINDMIN. Assume that the elements to be stored are
integers (or any other totally ordered data type). FINDMIN simply returns the smallest element
in the minqueue, but does not remove it. Using a standard implementation of a queue, the
FINDMIN operation takes O(n) time in the worst case. However, consider a more clever data
structure for this abstract data type which works as follows: There are two regular queues used.
The first queue is called the ”real queue” and the second queue is called the ”helper queue”.
When the ENQUEUE(x) operation is performed, the element x is enqueued in the regular way on
the real queue. The element x is also enqueued at the end of the helper queue. However, if the
element y immediately ”in front” of x on the helper queue is larger than x, then y is removed
from the helper queue. This process of x annihilating the element immediately in front of it is
repeated until the element immediately in front of x is less than or equal to it or x is at the front
of the helper queue.
Describe how the DEQUEUE and FINDMIN operation are implemented in this data structure.
3. Give the worst case running time for each of ENQUEUE, DEQUEUE, and FINDMIN using this
data structure.
Give an argument using the potential method to show that any sequence of n ENQUEUE,
DEQUEUE, and FINDMIN operations requires only O(n) time. Or namely, the amortized
complexity of these operations is O(1).
Solution
# include
# include
struct node
{
int data;
struct node *ptr;
};
struct node *Real_Front = NULL;
struct node *Real_Rear =NULL;
struct node *Helper_Front = NULL;
struct node *Helper_Rear = NULL;
struct node *Front1 = NULL;
void print_queue()
{
Front1 = Real_Front;
if((Front1 == NULL) && (Real_Rear == NULL))
{
printf(" Queue is Empty ");
return;
}
printf(" Queue is : ");
while(Front1 != Real_Rear)
{
printf("%d -->", Front1->data);
5. //Inserting Nodes other than first in Real queue
Real_Rear->ptr = temp;
Real_Rear = temp;
//Enqueue into Helper queue
Helper_Rear->ptr = temp1;
Helper_Rear = temp1;
/*
Logic Here is we are first inserting node into Helper Queue and if enqueued element is having
greater elements ahead
we are removing that elements.
*/
cur = Helper_Front;
for(;cur->ptr != Helper_Rear; cur = cur->ptr)
{
for(prev = cur->ptr; prev != Helper_Rear;prev = prev->ptr)
{
if(cur->data > prev->data)
{
value = cur->data;
cur->data = prev->data;
prev->data = value;
}
}
}
//Removing greater elements ahead after enqued the element
cur = Helper_Front;
while(cur->data != num)
{
cur = cur->ptr;
}
cur->ptr = Helper_Rear;
prev = cur->ptr;
6. while(prev != Helper_Rear)
{
next = prev->ptr;
free(prev);
prev = next;
}
cur = NULL;
}
}
void Dequeue ()
{
Front1 = Real_Front;
int dequeue_ele;
struct node *cur,*prev;
//Condition for Queue is empty
if(Front1 == NULL)
{
printf("Queue is Empty ");
return;
}
else
{
//Dequeue an element from real queue
if(Front1->ptr != NULL)
{
Front1 = Front1->ptr;
dequeue_ele = Real_Front->data;
printf(" Value Dequeued: %d", Real_Front->data);
free(Real_Front);
Real_Front = Front1;
}
else
{
dequeue_ele = Real_Front->data;
7. printf(" Value Dequeue: %d", Real_Front->data);
free(Real_Front);
Real_Front = NULL;
Real_Rear = NULL;
}
//Dequeue an element from Helper Queue
/*
Purpose of below snippet is that if the element dequeue from real queue then it should not
present
in Helper queue also.Hence removing from Helper queue
*/
if(Helper_Front->data == dequeue_ele)
{
if(Helper_Front->ptr == Helper_Rear)
return;
Helper_Front->data = Helper_Front->ptr->data;
cur = Helper_Front->ptr;
Helper_Front->ptr = cur->ptr;
free(cur);
return;
}
prev = Helper_Front;
while((prev->ptr != Helper_Rear) && (prev->ptr->data != dequeue_ele))
prev = prev->ptr;
if(prev->ptr == Helper_Rear)
return;
cur = prev->ptr;
8. prev->ptr = prev->ptr->ptr;
free(cur);
}
}
int Findmin()
{
return Helper_Front->data; // Return min element from Helper
}
int main()
{
int num,min_ele;
Real_Front=Real_Rear=Helper_Front=Helper_Rear=NULL;
char choice;
int test;
do
{
printf("Enter the Below choice ");
printf(" 1. Enqueue 2. Dequeue 3. Findmin ");
scanf("%d", &test);
if(test == 1)
{
printf(" Enter element into queue ");
scanf("%d", &num);
Enqueue(num); //Enqueue the elements
print_queue();
}
else if(test == 2)
{
Dequeue(); //Dequeue the elements
print_queue();
}
else if(test == 3)
{
9. min_ele = Findmin(); //Findmin element from Helper queue
printf(" Min element is %d", min_ele);
}
else
printf("Invalid Choice. Please select proper choice ");
printf(" Do You want to Continue Y/N :");
scanf(" %c",&choice);
}while((choice=='Y') || (choice=='y'));
return 0;
}
--------------------------------------------------------------------------------------------------------------------
---------------------------------------------
1)Describe how the DEQUEUEand FINDMIN operation are implimented in this data structure
Answer: Please see above code for explanation
--------------------------------------------------------------------------------------------------------------------
----------------------------------------------
2) Give the worst case running time for each ENQUEUE,DEQUEUE and FINDMIN using this
data structure
Ans:
Worst case complexities are
a) ENQUEUE - O(1) as we are simply enqueing data using rear pointer
b) DEQUEUE - O(1) as we simply dequeue using Front pointer
c) FINDMIN - O(1) as we enqueue the element and bringing minimum element at front while
deleting all the elements greater than enqueuing element.
--------------------------------------------------------------------------------------------------------------------
---------------------------------------------
3) Give an argument using the potential method to show that any sequence of n
ENQUEUE,DEQUEUE and FINDMIN operations requires only O(n) time Or Namely the
amortized complexity of these operations is O(1)
Ans: From (2)
Complexity of ENQUEUE - O(1)
To enqueue n such elements in queue requires
n* O(1) operations
Hence total complexity of ENQUEUE is
ENQUEUE - O(n) as we have to do n such operations
10. same for DEQUEUE() and FINDMIN()
N dequeue elements complexity is
DEQUEUE - n* O(1)
- O(n)
FINDMIN - n * O(1)
- O(n)
Amortized complexity from above for ENQUEUE, DEQUEUE and FINDMIN is O(1).
--------------------------------------------------------------------------------------------------------------------
-----------------------------------------------