Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
expand_more
expand_more
format_list_bulleted
Question
Chapter 17.2, Problem 1E
Program Plan Intro
To show that the cost of n stack operations including copying the stack is
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
S1 and S2 are two sorted stacks of n and m numbers, with the top elements of each stack pointing to the smallest number in their list. To ensure that all of the components in stacks S1 and S2 are available in MERGE in decreasing order, with the largest element at the top, create a stack MERGE that combines the items in stacks S1 and S2.Remember that there are (n + m) components in the stack MERGE.
Please assume that you have been given an implementation of a stack that supports both push and pop in O(1) time. With this information, you would like to implement a queue with these stacks.
(a) In what way can you efficiently implement a queue using two of these stacks? “Efficiently” in this case means in a way which will allow you to do part B.
(b) Please prove that the amortized cost of each dequeue and enqueue operation is O(1) for your stack-based queue by using the aggregate amortized analysis technique.
a. Propose a data structure that supports the stack push and pop operations and a third operation FindMin, which returns the smallest element in the data structure, all in O(1) worst case time.
b. Prove that if we add the fourth operation DeleteMin which finds and removes the smallest element, then at least one of the operations must take Ω(log n) time.
Knowledge Booster
Similar questions
- S1 and S2 are two sorted stacks of n and m numbers sorted in decreasing order, with their top items pointing to the smallest in their lists, respectively. Create a stack MERGE that merges the items in stacks S1 and S2, so that at the conclusion of the merge, all of the elements in S1 and S2 are available in MERGE in decreasing order, with the largest element at the top.Keep in mind that the number of components in stack MERGE is (n + m).arrow_forward3. A stack can be implemented with an array A[0..N-1] and a variable pos. The push and pop operations are defined by the following code. push (x) A[pos] + x 1. pos + pos end push pop () pos + pos + 1 return A[pos] end pop Which of the following will initialize an empty stack with capacity N for this implementation?arrow_forwardThe two sorted stacks S1 and S2 each contain n and m numbers arranged in decreasing order, with the top elements of each stack pointing to the list item that is smallest. Make a stack MERGE that combines the items in stacks S1 and S2 so that all of the elements in S1 and S2 are accessible in MERGE in descending order, with the largest element as its top element, at the conclusion of the merge.Keep in mind that stack MERGE would have (n + m) elements.arrow_forward
- Let S1, S2 and S3 be three stacks with |S1|=|S2|=|S3|= n (i.e) all of them will have same capacity. Assume that only two of the stacks are filled to the capacity with arbitrary non-negative integers and one of them is empty. Re-arrange the elements in the stack so that even integers are placed in one stack and odd integers in another stack. In case the number of even elements exceeds the number of odd elements, then the extra odd elements that do not fit onto one stack can be placed on the third. The case where odd elements are greater than even is analogous.arrow_forwardWe can store k stacks in a single array if we use the data structure suggested in Figure 1 shown below, for the case k = 3. We push and pop from each stack as suggested in connection with Figure 2 below. However, if pushing onto stack i causes TOP(i) to equal BOTTOM(i – 1), we first move all the stacks so that there is an appropriate size gap between each adjacent pair of stacks. For example, we might make the gaps above all stacks equal, or we might make the gap above stack i proportional to the current size of stack i (on the theory that larger stacks are likely to grow sooner, and we want to postpone as long as possible the next reorganization). Figure 1 Figure 2 1. On the assumption that there is a procedure reorganize to call when stacks collide, write code for the five stack operations. 2. On the assumption that there is a procedure MakeNewTops that computes newtop[i], the "appropriate" position for the top of stack i, for 1 ≤ i ≤ k, write the procedure…arrow_forwardThe two sorted stacks S1 and S2 each contain n and m numbers arranged in decreasing order, with the top components of each stack pointing to the list item that is smallest. Create a stack MERGE that combines the items in stacks S1 and S2 so that all of the elements in S1 and S2 are available in MERGE in descending order, with the largest element as its top element, at the conclusion of the merge.Keep in mind that stack MERGE would have (n + m) items.arrow_forward
- Consider a fixed-size circular array-based implementation of the LRU quasi-stack. Explain step-by-step the algorithm to keep the last referenced page at the top of the stack. Note that the page being referenced may, but does not need to, be already in the stack. Your algorithm must handle both cases. What is the complexity of updating the stack on each new page reference? That is, how many operations are required to add a new page number at the top, or to move an already existing page number from some location in the stack to the top?arrow_forwardProblem Statement: We can store k stacks in a single array if we use the data structure suggested in Figure 1 shown below, for the case k = 3. We push and pop from each stack as suggested in connection with Figure 2 below. However, if pushing onto stack i causes TOP(i) to equal BOTTOM(i – 1), we first move all the stacks so that there is an appropriate size gap between each adjacent pair of stacks. For example, we might make the gaps above all stacks equal, or we might make the gap above stack i proportional to the current size of stack i (on the theory that larger stacks are likely to grow sooner, and we want to postpone as long as possible the next reorganization). 1 stack 1 3 stack 2 bottom stack 3 3 top stackspace Figure 1 top first element second element maxlength last element elementsarrow_forwardYou are allowed to operate on a stack WORK and a temporary stack TEMP (ifneeded) supporting their ADT operations of PUSH (S,X), POP (S, X) andEMPTYSTACK (S) only, where X represents an element/variable to be pushed in orpopped out of the stack and S represents a stack. You are also permitted to use onevariable if needed to carry out the operations.i) Given n distinct random numbers that are to be pushed into WORK, how canyou find the minimum element that was pushed into it? You are permitted to use alone variable.ii) Given n distinct random numbers that are to be pushed into WORK, how canyou find the maximum element that was pushed into it, all the while ensuring thatthe elements stored in WORK are in their descending order with the maximumelement beginning at the bottom of stack? You are permitted to use a lone variableand a temporary stack TEMP.iii) Given an array A[1: n] of distinct random numbers how can you obtain thesorted list in the array, making use of stacks alone?arrow_forward
- 21. Given the function for f(n) =n!, na natural number and defined EITHER as in fi(n)=n(n-1)! With 0!=1 OR f2(n)=1x2x3 x.... xn A fi is an iterative definition of the factorial function B fi executes with no internal stack while fdoes C f2 and fi will have the same runtime efficiency D fzis more efficient than fiarrow_forwardConsider the following infix expression.( 5 + 8 ) * 9 – 7 * 10 + 9.Apply infix-to-postfix conversion algorithm to generate the correspondingpostfix expression from the given infix expression. You have to show thecontent of stack at each stage of conversion.arrow_forwardWhile all basic operations on stack are O(1), which of the following may have a complexity of O(n)? Allowing push in an empty array Allowing push in a full array Allowing pop from a full array Allowing pop from an empty arrayarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education