Introduction to Algorithms
Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
Question
Book Icon
Chapter 18, Problem 1P

(a)

Program Plan Intro

Describe thenumber of disk accesses and CPU time for n stack operations in worse-case by using the simple implementation.

(a)

Expert Solution
Check Mark

Explanation of Solution

In worse case for every stack operation it requires disk access for implementation sothe number of disk accesses for n stack operations using this simple implementation is asymptotically equals to θ(m) .

The CPU time in worse case for n -stack operations is θ(mn) .

(b)

Program Plan Intro

Describethe number of diskaccesses and the CPU time required for n -PUSH operations in worse-case.

(b)

Expert Solution
Check Mark

Explanation of Solution

The new page starts only when everymthpushes therefore the number of disk operations is n/m. The diskaccess requires time of asymptotically θ(n) .

Hence,the number of disk accesses and CPU time for n - PUSH operations in worse-case is θ(n) .

(c)

Program Plan Intro

Describe the disk accesses and the CPU time required for n -stack operations in worse-case.

(c)

Expert Solution
Check Mark

Explanation of Solution

Stack operations include PUSH and POP. The PUSH and POP operations requires alternate disk accesses that means mdisk accesses for performing stack operation so the number of disk accesses asymptotically is θ(m) .

The CPU time for performing stack operations is equal to θ(mn) ,where m is number of words andn is number of stack operations.

(d)

Program Plan Intro

Describe that the amortized number of disk accesses is O (1 /m ) and the amortized CPU time is O (1) for any stack operation .

(d)

Expert Solution
Check Mark

Explanation of Solution

At the initial function called as Potential Function of stack defined by difference of size of stack and recent pass which is multiple of m. The value of potential function is 0 at initial state.

When any stack operation is performed the value is decreased and increased according to the operation by one.Loading new pages requires the position which is at leastm position away from recent position to cross the page boundary therefore loading and storing of new stack page requires CPU time of O ( m ).

By dropping appropriate Potential function of order O ( m ) and selecting the suitable value for potential value results to managed the stack pages so that the amortized number of disk accesses for any stack operation is O (1 /m ) and the amortized CPU time for any stack operation is O (1).

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Imagine a stack of dishes, literally. The stack may collapse if it becomes too tall.Therefore, when the prior stack above a certain threshold in real life, we probably start a new stack. Create a data structure called SetOfStacks to resemble this. When a stack's capacity is reached, a new stack should be created and added to the SetOfStacks. SetOfStacks. SetOfStacks with push(). In other words, pop() should return the same values as it would if there were only one stack. It should operate exactly like a single stack.TAKE ACTIONCreate a function called popAt(int index) that does a pop operation on a certain substack.
Consider the following stack, which can be written as [70, 10, 50, 80, 20] i.e. the "top" of the stack is the first element in the list   Consider the following code (written as pseudocode): myfunction( stack1 )      stack2 = createStack()      while stack1 is NOT empty             value = top(stack1)             pop(stack1)             while stack2 is NOT empty and top(stack2) > value                    push(stack1, top(stack2))                    pop(stack2)             push(stack2, value)             print(stack2) // Prints the current stack as [element_1, element_2, ..., element_n]       return stack2   From the answers below, select the correct output for the first 4 print statements if myFunction is executed with: myFunction( [ 70, 10, 50, 80, 20] )
Write a program in c or c++ to implement a Stack using array. In order to implement the stack, programfor following stack operations has to be written: • void push(int): to insert data onto the stack.• Int pop(): to remove and return the last inserted element from the stack.• Int top(): Returns the last inserted element without removing it.• int Size(): Returns the number of elements stored in the stack.• int IsEmptyStack(): Indicates whether any elements are stored in the stack or not.• int IsFullStack(): Indicates whether the stack is full or not.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning