This action might not be possible to undo. Are you sure you want to continue?

Prof. Bernard Moret

Homework Assignment #1

due Sunday night, Sept. 30

Group members

Afroze Baqapuri Nicolas Stucki Thadd´ e Tyl e

Question 1. Implement a queue using two ordinary stacks and analyze the amortized cost of each InsertQueue and Delete-Queue operation. (Assume the cost of each Pop or Push operation is 1). Now implement a double-ended queue with two ordinary stacks and give a counterexample to show that the amortized cost of its four operations (Insert-Back, Insert-Front, Delete-Back, and Delete-Front) is no longer O(1). A counterexample is a sequence of operations that takes time growing faster than the number of operations in the sequence. Indicate which single operation(s) are such that their removal makes the the amortized cost of the remaining three operations O(1). Solution 1. Simple queue To implement the queue with two stacks we use the ﬁrst stack as an enqueueing stack and the second will be used as a dequeueing stack. All elements will be dequeued by popping it out from the dequeueing stack and all elements will be enqueued by pushing it into the enqueueing stack. If the dequeueing stack is empty and we want to delete the next element we need to do an additional step before dequeueing, this new step consist of popping each element of the enqueueing stack and pushing it into the dequeueing stack. The operation I NSERT-Q UEUE is clearly O(1) because it consists of only one PUSH operation. On the other hand the D ELETE -Q UEUE operations can be O(1) if the dequeueing stack is not empty or O(n) if it is necessary to transfer all the n elements form the enqueueing stack to the dequeueing stack before deleting the element. Therefore it is trivial to see that the I NSERT-Q UEUE operetion O(1) in an amortised analysis. because it is O(1) in any case. To prove that the D ELETE -Q UEUE is operation O(1) we will show that n calls to that operation will produce a cost of only O(n) instead of the O(n2 ) that would be expected by the non-amortised analysis. To do so we will count the operation from the perspective of each element during it’s life in the queue. So we assume that there are n calls to D ELETE -Q UEUE and we assume that any element that is in the queue was inserted by a I NSERT-Q UEUE operation. Therefor every element is or has been in the enqueuing stack. We also know that any element has to be dequeued from the dequeueing stack. So each deleted element has to be passed form the enqueueing to the dequeueing stack and this only happens once. Therefore each of the n elements will produce (at most) two PUSH and two POP operation, this means at most 4n operations for the n elements. Therefore the D ELETE -Q UEUE has an O(1) amortised cost.

We assume that all elements of the queue are in only one of the stacks. If either F0 or F1 is 0. The costs of these operations are the same of those form the equivalent operations from the single queue. Fk+2∗l+1 = 0 and Fk+2∗(n+1) = 0. Now the problem arises if we are calling D ELETE -F RONT and then D ELETE -BACK repeatedly (in a cyclic manner) until the queue is empty. this means that the other stack is completely empty.Solution 1. 01011111F get an n of 1. we ﬂip it to a one and the increase operation is done.F3 F2 F1 F0 where Fi ∈ {0. if both F0 and F1 are set to 1.. D ELETE -BACK and D ELETE F RONT are O(n) (or O(1) if the deleted end of the stack is not empty). Using the deﬁnition of Fibonacci numbers. Double-ended queue To implement the double-ended queue is similar to the implementation of the simple queue. we notice that this does not 2 . We will implement the same functions from the simple queue changing the names of the stacks to front (instead of enqueueing) an back (instead of dequeueing) and will implement a second version of these operations with the stacks inverses. This will create a situation where every call to a delete operation will have to transfer all the elements from one stack to the other. Otherwise. The result of applying this transformation yields 01101001F . where the ith least signiﬁcant “ﬁt” indicates whether the sum includes the ith Fibonacci number Fi . To make the operations O(1) in a amortised analysis we have to delete one of the two delete operations. instead of using sums of powers of two. Question 2. We will now consider the increase operation. Solution 2. Let a ﬁt string representation of a number n be . which can either be 11F or 100F .Fi .. This will leave us with the same case that we had with the simple queue plus the operation that pushes directly to the dequeueing stack. call it n) and ﬂip them to Fk = 1. In other words. For that we will show on case that is ’amortised’ to O(n2 ) by calling D ELETE -F RONT and D ELETE -BACK operations. Therefore the analysis of the simple queue still holds up and all three operations of the semi-double-endend queue will be amortised to O(1). Fk+2∗l+1 = 1 and Fk+2∗(l+1) = 1 (for k > 0 and l from 0 to the maximum integer we can ﬁnd. What happened here is that each time a delete operation is called the all the elements are on the other stack. With this we showed a sequence of operations that won’t be amortised the single operations to O(1). For instance. Suppose that. we keep an array of 0/1 “ﬁts”. For example. Verify that a number need not have a unique ﬁtstring representation (in contrast to bitstrings) and describe an algorithm to implement increase and decrease operations on a single ﬁtstring in constant amortized time. instead of an array of 0/1 bits. then we will ﬁnd the ﬁrst case (starting from the right) where Fk = 0. we represent integers as sums of Fibonacci numbers... the “ﬁtstring” 101110F represents the number F6 + F4 + F3 + F2 = 8 + 3 + 2 + 1 = 14. This means that all calls to delete operations will be O(n) and therefore the total time for the n calls will be O(n2 ). To show that these operations can’t be amortised to O(1) we will show that n calls to operation will not necessarily be O(n). But this new operation only simpliﬁes things because now if an element is inserted it could have at most 4 operations (the ones from the simple queue analysis) or at most 2 operations (PUSH into and POP out of the stack that has a delete operation). 1}. I NSERT-BACK and I NSERT-F RONT are O(1). An example of a number that has more than one “ﬁtstring” representation is that of the number 2.

• When the next insertion encounters a full array. and m be its capacity (or size). in the context of a data structure that grows or shrinks one element at a time. To prove the amortisation. since we combine the allocation of an array of double the capacity and an insertion. Therefore consider the following solution. We may claim that all insertions take a constant time of 2 ∗ k. the amortized cost per operation of array doubling and array halving is constant. Then. Inserting elements costs a constant time k1 . say. Using arrays. In order to insert the element that required a 3 . with k := k1 + 2 ∗ k2 . You notice that the real cost is k/2 + 3 where k is the number of consecutive ones on the right. At worst. but that creating a new array takes time proportional to its size. This will eventually end when F0 or F1 become one.change the number. The change in potential ∆Φ = −(k/2 − 1) so the amortised running time is k/2 + 3 − (k/2 − 1) = 4. Using the deﬁnition of Fibonacci numbers. the array is copied into one half its size (and the old array returned to free storage) and the deletion then proceeds. • Why not halve the array as soon as the number of elements falls below one half of the allocated size? Solution 3. if both F0 and F1 are set to 0. Therefore the amortised cost to decrease one is O(1) over n decreases. To prove the amortisation. except when m = n. as in a simple queue or a binary heap. 0111111F to 1010101F . It is important to note that after every iteration the number of consecutive zeros on the right decrees. creates a problem: when the needs grow beyond the allocated array size. let the potential Φ(i) be the number of zeros in the ﬁt of i. The change in potential ∆Φ = −(k/2 − 1) so the amortised running time is k/2 + 3 − (k/2 − 1) = 4. the array is copied into one twice its size (and the old array returned to free storage) and the insertion then proceeds. we ﬂip it to a zero and the decrease operation is done. the transformation goes from. Question 3. Fk+2∗l+1 = 0 and Fk+2∗(l+1) = 0 (for k > 0 and l from 0 to the maximum integer we can ﬁnd. let the potential Φ(i) be the number of ones in the ﬁtstring representation. Let n be the number of elements in the array. then we will ﬁnd the ﬁrst case (from the left) where Fk = 1. we start the procedure over (tail-recursion yet again). Indeed. Assume that returning an array to free storage takes constant time. Then. The number of operations taken to decrease one is k/2 + 3 where k is the number of consecutive ones on the right. • When the next deletion reduces the number of elements below one quarter of the allocated size. • Prove that. half of the bits get ﬂipped. we notice that this does not change the number. Fk+2∗l+1 = 1 and Fk+2∗(n+1) = 1. in which case the cost is actually 2∗k2 ∗n+k1 . Obviously. call it n) and ﬂip them to Fk = 0. let’s use the cost-accounting analysis. This will eventually end when F0 or F1 become 0. Therefore the amortised cost to increase one is O(1) over n increases. plus one. there is no support for simply increasing the array. However. It is important to note that after every iteration the number of consecutive ones on the right decrees. we start the procedure over (tail-call recursion). m ≥ n. Otherwise. If either F0 or F1 is one. The algorithm to decrease one is very similar to the increase operation.

we had to insert at least n elements 2 (half of the previous size of the array. As a result. and we are now removing n + 1 elements from the array. The actual cost of each removal is k1 (constanttime). due to the halving process. It is important to note that after each growth or shrink of the array. plus the element that requires a reallocation. ie. reguardless of the value of n. except for that last one. which is Θ(1). At worse. We may claim that all deletions take a constant time of 2 ∗ k. Indeed. let’s use the cost-accounting analysis. the amortized cost per insertion is actually 2 ∗ k. Θ(1) (constant time). This will assure that with combinations of the two operations will work as well because the numbers there will always be the necessary distance of to the next growth or shrink operation since the last operation. If we add those two elements and then we remove them in a repeated manner. The amortized total cost is trivially higher than the real total cost. Similarly. ie. which is k2 ∗ 2 ∗ n + k1 . On the other hand. This. The problem arises when we just reduced the array and then we want to add two elements it will take O(n) because it will need to increase the size again. The reduction of size isn’t made when the number of elements falls below one half of the allocated size because that solution doesn’t amortise to constant time. we just inserted that one element that made the array double in size up to 4 ∗ n.reallocation of the array when inserting in an array of size n. the array is halved to 2 ∗ n. and an amortized cost of 2 ∗ k ∗ n. then we will have 2n operations but it will take O(n2 ) to do it. and the real total cost is (k1 + 2 ∗ k2 ) ∗ n + 2 ∗ (k1 + k2 ). forcing it to be halved. As a result. which is clearly bigger. Yet again. upon reaching a quarter of the size of the array (n). When inserting all those elements. the amortized cost per deletion is actually 2 ∗ k again. Upon removing those n + 1 elements. the point where it previously had to increase the size of the array). the amortized cost is 2 ∗ k ∗ n + 2 ∗ k. we will consider the case of decrease operations. the array will be half-full (or half-empty). 4 . causes a real cost of k1 ∗ n. the amortized total cost is 2 ∗ k ∗ n + 2 ∗ k. we observe a real cost of k1 ∗ n + k2 ∗ 2 ∗ n + k1 . with k := k1 + k2 . in itself.

- Data Structure Programs-Final Rishabh Jain
- Data Structure
- DSA_1.2
- Programs
- Queue
- Queue data structure
- Queue
- Homework 4
- Lec10 Stacks&Queues
- Module a Power Point > ModA1_ch10_eng
- sol3
- Homework 4
- Data+Structure
- Lists Stacks Queues from Goodrich
- Module a Power Point > ModA2_ch10_eng
- (les05)-linkedlistshashtables(1)
- c++ Programs
- 40 programs of c++
- Module a Exercise > ModA2_QB10_eng
- ap comp sci testBankCh09
- Data Structures
- VTU 3RD SEM CSE DATA STRUCTURES WITH C NOTES 10CS35
- queue1
- CSCI2320 Chapter 16 Part2
- NEW
- vbhjhjv
- c Programs
- Stacks
- Question Bank(DS)
- Ch4.Stacks Queues
- Hwk1 Solved

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd