This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 473 Homework 5 Solutions Spring 2010 1. (a) In the worst case, how long does it take to push one more element onto a multistack containing n elements? Solution: Every element already in the multistack is popped and pushed at most once when a new element is introduced. On the other hand, if every stack is full, every element will be moved at least once on the next push. Thus, the worstcase time for a push is ( n ) . Rubric: 2 points max: 1 point for answer + 1 point for proof. (b) Prove that if the user never pops anything from the multistack, the amortized cost of a push operation is O ( log n ) , where n is the maximum number of elements in the multistack during its lifetime. Solution: A multistack containing n elements has at most log 3 n nonempty stacks. Elements move to later stacks only as new elements are added, so each of the n elements is moved O ( log n ) times over the lifetime of the data structure. Thus, the total cost of n pushes is O ( n log n ) . The aggregate method implies that the amortized time per push is at most O ( n log n ) / n = O ( log n ) . Rubric: 3 points max: 2 for recognizing an element only moves forward + 1 for remaining details. This is not the only correct proof. Full credit for referring to part (c) is the given solution to part (c) is correct. (c) Prove that in any intermixed sequence of pushes and pops, each push or pop operation takes O ( log n ) amortized time, where n is the maximum number of elements in the multistack during its lifetime. Solution: Consider a single stack S i in the multistack. Say that a MP USH or MPOP operation is relevant if the largest stack modified by the operation is S i . Each relevant MP USH operation moves i 1 j = 3 j = ( 3 i 1 ) / 2 elements up to the next stack, and each relevant MP OP moves i 1 j = 3 j ( 3 i 1 ) / 2 elements down to the previous stack. Thus, each relevant operation runs in O ( 3 i ) time. Just before any relevant MP USH, every smaller stack S j with j &lt; i is full, but S i is at most twothirds full. Just after any relevant MP USH, each smaller stack S j (except S ) is exactly twothirds full, and S i is at least onethird full. Just before any relevant MP OP, every smaller stack S j with j &lt; i is empty, and S i is at least onethird full. Just after any relevant MP OP, every smaller stack S j (except S ) is exactly onethird full, and S i is at most twothirds full. The first relevant operation must be an M PUSH, which must be preceded by at least i 1 j = 3 j = ( 3 i 1 ) / 2 irrelevant MP USHes to fill all the smaller stacks. Between any two relevant operations, there must be either i 1 j = 1 3 j 1 = ( 3 i 1 1 ) / 2 M PUSHes or MPOPs, to either fill up or empty all the smaller stacks. Thus, every relevant operation is preceded by ( 3 i ) irrelevant operations....
View
Full Document
 Spring '08
 Chekuri,C

Click to edit the document details