SlideShare a Scribd company logo
1 of 10
Download to read offline
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) 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) 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);
Front1 = Front1->ptr;
}
if(Front1 == Real_Rear)
printf("%d -->", Front1->data);
}
void Enqueue(int num)
{
struct node *cur,*next,*prev,*temp,*temp1;
int value;
//Creating Node for insertion(Enqueue) in the list representation of Queue
temp = (struct node*)malloc(1 * sizeof(struct node));
temp->ptr = NULL;
temp->data = num;
temp1 = (struct node*)malloc(1 * sizeof(struct node));
temp1->data = num;
temp1->ptr = NULL;
if((Real_Rear == NULL) || (Helper_Rear == NULL)) //First Node insertion in Real Queue
{
if((Real_Rear == NULL) && (Real_Front == NULL))
{
Real_Front = temp;
Real_Rear = temp;
}
if((Helper_Rear == NULL) && (Helper_Front == NULL)) //First Node insertion in Helper
Queue
{
Helper_Front = temp1;
Helper_Rear = temp1;
}
}
else
{
//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;
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;
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;
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)
{
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
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).
--------------------------------------------------------------------------------------------------------------------
-----------------------------------------------

More Related Content

Similar to Consider an extension of the queue abstract data type, called a .pdf

JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
lohithkart
 

Similar to Consider an extension of the queue abstract data type, called a .pdf (20)

Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
stack & queue
stack & queuestack & queue
stack & queue
 
Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Pointer
PointerPointer
Pointer
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
 
Queue
QueueQueue
Queue
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
linkedlist.pptx
linkedlist.pptxlinkedlist.pptx
linkedlist.pptx
 
data structure
data structuredata structure
data structure
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Queues
QueuesQueues
Queues
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Data structures
Data structuresData structures
Data structures
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
 

More from arorasales234

Our best evidence to data suggests that the Earth formed 4.6 billion .pdf
Our best evidence to data suggests that the Earth formed 4.6 billion .pdfOur best evidence to data suggests that the Earth formed 4.6 billion .pdf
Our best evidence to data suggests that the Earth formed 4.6 billion .pdf
arorasales234
 
Money laundering has become a mechanism for financing terrorist acti.pdf
Money laundering has become a mechanism for financing terrorist acti.pdfMoney laundering has become a mechanism for financing terrorist acti.pdf
Money laundering has become a mechanism for financing terrorist acti.pdf
arorasales234
 
Imperialism and Nationalism are often incongruous concepts that exis.pdf
Imperialism and Nationalism are often incongruous concepts that exis.pdfImperialism and Nationalism are often incongruous concepts that exis.pdf
Imperialism and Nationalism are often incongruous concepts that exis.pdf
arorasales234
 
Explain why it can be problematic to base morality and ethics only o.pdf
Explain why it can be problematic to base morality and ethics only o.pdfExplain why it can be problematic to base morality and ethics only o.pdf
Explain why it can be problematic to base morality and ethics only o.pdf
arorasales234
 
Cordia Corporation is planning a 15 year project with an initial inv.pdf
Cordia Corporation is planning a 15 year project with an initial inv.pdfCordia Corporation is planning a 15 year project with an initial inv.pdf
Cordia Corporation is planning a 15 year project with an initial inv.pdf
arorasales234
 
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdf
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdfCase study on Queensland Health Payroll Debacle & Queensland Governm.pdf
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdf
arorasales234
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
arorasales234
 
All businesses, small or large, depend on data centers. Answer the f.pdf
All businesses, small or large, depend on data centers. Answer the f.pdfAll businesses, small or large, depend on data centers. Answer the f.pdf
All businesses, small or large, depend on data centers. Answer the f.pdf
arorasales234
 
4. Records of the missions of Baja California in the form of baptism.pdf
4. Records of the missions of Baja California in the form of baptism.pdf4. Records of the missions of Baja California in the form of baptism.pdf
4. Records of the missions of Baja California in the form of baptism.pdf
arorasales234
 

More from arorasales234 (20)

Our best evidence to data suggests that the Earth formed 4.6 billion .pdf
Our best evidence to data suggests that the Earth formed 4.6 billion .pdfOur best evidence to data suggests that the Earth formed 4.6 billion .pdf
Our best evidence to data suggests that the Earth formed 4.6 billion .pdf
 
one human resource manager recently got a thank you note on her iPho.pdf
one human resource manager recently got a thank you note on her iPho.pdfone human resource manager recently got a thank you note on her iPho.pdf
one human resource manager recently got a thank you note on her iPho.pdf
 
Mutations rII1 and rII2 are known to fall into the rIIA and rIIB cis.pdf
Mutations rII1 and rII2 are known to fall into the rIIA and rIIB cis.pdfMutations rII1 and rII2 are known to fall into the rIIA and rIIB cis.pdf
Mutations rII1 and rII2 are known to fall into the rIIA and rIIB cis.pdf
 
Money laundering has become a mechanism for financing terrorist acti.pdf
Money laundering has become a mechanism for financing terrorist acti.pdfMoney laundering has become a mechanism for financing terrorist acti.pdf
Money laundering has become a mechanism for financing terrorist acti.pdf
 
Let X and Y be continuous r.v. with joint density functionPlease g.pdf
Let X and Y be continuous r.v. with joint density functionPlease g.pdfLet X and Y be continuous r.v. with joint density functionPlease g.pdf
Let X and Y be continuous r.v. with joint density functionPlease g.pdf
 
Independent auditors must provide dual opinions. What are these two .pdf
Independent auditors must provide dual opinions. What are these two .pdfIndependent auditors must provide dual opinions. What are these two .pdf
Independent auditors must provide dual opinions. What are these two .pdf
 
Imperialism and Nationalism are often incongruous concepts that exis.pdf
Imperialism and Nationalism are often incongruous concepts that exis.pdfImperialism and Nationalism are often incongruous concepts that exis.pdf
Imperialism and Nationalism are often incongruous concepts that exis.pdf
 
Imagine that you plant the massive seed from inside an avocado. You p.pdf
Imagine that you plant the massive seed from inside an avocado. You p.pdfImagine that you plant the massive seed from inside an avocado. You p.pdf
Imagine that you plant the massive seed from inside an avocado. You p.pdf
 
Identify three traits each that distinguish channels from carriers. .pdf
Identify three traits each that distinguish channels from carriers. .pdfIdentify three traits each that distinguish channels from carriers. .pdf
Identify three traits each that distinguish channels from carriers. .pdf
 
i am trying to get this library file income.js using this, to be loa.pdf
i am trying to get this library file income.js using this, to be loa.pdfi am trying to get this library file income.js using this, to be loa.pdf
i am trying to get this library file income.js using this, to be loa.pdf
 
Explain why it can be problematic to base morality and ethics only o.pdf
Explain why it can be problematic to base morality and ethics only o.pdfExplain why it can be problematic to base morality and ethics only o.pdf
Explain why it can be problematic to base morality and ethics only o.pdf
 
Discuss the reasons why the Human Microbiome Project is a step forwa.pdf
Discuss the reasons why the Human Microbiome Project is a step forwa.pdfDiscuss the reasons why the Human Microbiome Project is a step forwa.pdf
Discuss the reasons why the Human Microbiome Project is a step forwa.pdf
 
Describe what is happening in Italy (or you may substitute Argentina.pdf
Describe what is happening in Italy (or you may substitute Argentina.pdfDescribe what is happening in Italy (or you may substitute Argentina.pdf
Describe what is happening in Italy (or you may substitute Argentina.pdf
 
Cordia Corporation is planning a 15 year project with an initial inv.pdf
Cordia Corporation is planning a 15 year project with an initial inv.pdfCordia Corporation is planning a 15 year project with an initial inv.pdf
Cordia Corporation is planning a 15 year project with an initial inv.pdf
 
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdf
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdfCase study on Queensland Health Payroll Debacle & Queensland Governm.pdf
Case study on Queensland Health Payroll Debacle & Queensland Governm.pdf
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
 
All businesses, small or large, depend on data centers. Answer the f.pdf
All businesses, small or large, depend on data centers. Answer the f.pdfAll businesses, small or large, depend on data centers. Answer the f.pdf
All businesses, small or large, depend on data centers. Answer the f.pdf
 
A) The linearity condition is not satisfied. B) The linearity cond.pdf
A) The linearity condition is not satisfied. B) The linearity cond.pdfA) The linearity condition is not satisfied. B) The linearity cond.pdf
A) The linearity condition is not satisfied. B) The linearity cond.pdf
 
4. Records of the missions of Baja California in the form of baptism.pdf
4. Records of the missions of Baja California in the form of baptism.pdf4. Records of the missions of Baja California in the form of baptism.pdf
4. Records of the missions of Baja California in the form of baptism.pdf
 
4. Cafazzo et al. studied a group of stray dogs in Rome, and ranked t.pdf
4. Cafazzo et al. studied a group of stray dogs in Rome, and ranked t.pdf4. Cafazzo et al. studied a group of stray dogs in Rome, and ranked t.pdf
4. Cafazzo et al. studied a group of stray dogs in Rome, and ranked t.pdf
 

Recently uploaded

Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
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);
  • 4. Front1 = Front1->ptr; } if(Front1 == Real_Rear) printf("%d -->", Front1->data); } void Enqueue(int num) { struct node *cur,*next,*prev,*temp,*temp1; int value; //Creating Node for insertion(Enqueue) in the list representation of Queue temp = (struct node*)malloc(1 * sizeof(struct node)); temp->ptr = NULL; temp->data = num; temp1 = (struct node*)malloc(1 * sizeof(struct node)); temp1->data = num; temp1->ptr = NULL; if((Real_Rear == NULL) || (Helper_Rear == NULL)) //First Node insertion in Real Queue { if((Real_Rear == NULL) && (Real_Front == NULL)) { Real_Front = temp; Real_Rear = temp; } if((Helper_Rear == NULL) && (Helper_Front == NULL)) //First Node insertion in Helper Queue { Helper_Front = temp1; Helper_Rear = temp1; } } else {
  • 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). -------------------------------------------------------------------------------------------------------------------- -----------------------------------------------