finsolgg

11 10 pts consider a basic queue interface with the

Info iconThis preview shows pages 9–11. Sign up to view the full content.

View Full Document Right Arrow Icon
11. (10 pts) Consider a basic Queue interface with the methods enqueue and dequeue . Suppose we want to extend this to a new interface MinQueue that includes these methods and two more methods called, findMin and deleteMin . Assuming that the elements in the queue are of type Comparable and distinct (to avoid complications), findMin must return the smallest element in the queue without deleting it, while deleteMin must delete the smallest element in the queue. Describe an implementation of MinQueue such that each of the four operations takes O (1) worst-case time. Answer: In the original version this problem had only findMin . Adding deleteMin to the interface was an embarrassing mistake. As one student astutely observed, it is not possible to have both enqueue and deleteMin in constant time: if that were so then we could sort in linear time! Anyway, since there was no fair way to grade this we decided to toss the problem, every student was scored 10pts on it. Still, read on! In what follows we give a solution for just findMin . Moreover, we give it for stacks, not for queues, but it is straightforward to adapt it to queues. DISCUSSION (not required): A naive implementation would use a regular stack implementation S and a reference theMin main- tained to always give the smallest element currently in S : | | | 3 | | 5 | ---------- | 1 |<------| theMin | | 4 | ---------- ----- S 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Every time we push an element e onto S , we compare it with the element referred to by theMin and if it is smaller we update theMin to refer to e . In this naive implementation it is not possible to pop the stack in O (1) worst-case time. Indeed, if it so happens that the current minimum element is at the top of the stack and we pop it, then we need to update the theMin reference to point to the smallest element in the rest of the stack. This element could be anywhere so we need to traverse the whole stack to find it. This cannot be done in time O (1). ACTUAL SOLUTION: For the implementation we will use instead of the single reference theMin a data structure Aux containing several references to certain elements currently in S . Aux is another stack. NOT REQUIRED: Here is a picture of the contents of S and Aux after the sequence of operations push(3), push(4), push(2), push(5), push(1) assuming the stack was empty before this. | | | | | | | 4 | | | | 3 |<------|-* | | 3 |<------|-* | ----- ----- ----- ----- S Aux S Aux | | | | | 5 | | 2 |<--\ | | | 2 |<--\ | | | 4 | \--|-* | | 4 | \--|-* | | 3 |<------|-* | | 3 |<------|-* | ----- ----- ----- ----- S Aux S Aux | | | 1 |<--\ | 5 | \ | | | 2 |<--\ \-|-* | | 4 | \--|-* | | 3 |<------|-* | ----- ----- S Aux ACTUAL SOLUTION, CONTINUED: Now here is how we implement pop() : 10
Background image of page 10
(1) If the top of Aux points to the top of S then pop Aux . (2) Pop S . push(e) : (1) If e is bigger than the element in S to which the top of Aux points then push e onto S . (2) Otherwise ( e is smaller) push e onto S and push onto Aux a reference that points to the new top of S (containing e ). For findMin we return the element referred to by the top of Aux . 11
Background image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page9 / 11

11 10 pts Consider a basic Queue interface with the methods...

This preview shows document pages 9 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online