This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Introduction to Algorithms December 1, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Piotr Indyk and Charles E. Leiserson Quiz 2 Solutions Quiz 2 Solutions 2 4 6 8 10 12 14 16 18 30-39 40-49 50-59 60-69 70-79 80-89 90-99 100-109 110-119 120-129 Score Count Median = 74.5 Mean = 76.4 Std. Dev. = 22.3 6.046J/18.410J Quiz 2 Solutions 2 Problem 1. A Dynamic Set A dynamic set Q contains items, where each item x has an associated key key [ x ] . The dynamic set Q supports the following operations: INSERT ( x, Q ) : Insert item x into Q . x EXTRACT-OLDEST ( Q ) : Remove and return the oldest item x Q . (The oldest item is the one inserted least recently.) x FIND-MAX ( Q ) : Return (but do not remove) the item x Q for which key [ x ] is maximal. Design a data structure for Q that can perform any sequence of n operations efficiently. Solution: The goal of the problem is to come up with an efficient implementation of a queue , augmented with the additional FIND-MAX operation. Thus, it makes sense take a queue implementation 1 (e.g., a linked-list) and extend it so that it supports FIND-MAX as well. For starters, we assume that all elements are distinct. This assumption is not crucial, but sim- plifies the description and the analysis of the algorithms. Note that, in any case, we can break ties by replacing each element x by ( x, t x ) , where t x is the time when x was inserted, and using lexicographic ordering. We will see that there are, essentially, three basic ways of supporting FIND-MAX: 1.Scan all elements in the list to find the maximum. This gives ( n 2 ) total worst-case running time. This solution was worth up to 8 points. A slightly better implementation maintains a pointer to the maximum element; when the ele- ment is deleted, the EXTRACT-OLDEST procedure searches for the new maximum. Although this implementation still has ( n 2 ) worst-case time bound, it gives a much better time bound in the average case 2 . Thus, this solution was worth up to 10 points. Also, this was by far the most popular solution. 2.Augment the queue with a search tree (description below). In this way, all three operations can be performed in O (log n ) time. This gives O ( n log n ) total worst-case running time. This solution was worth up to 15 points. 3.Augment the queue with a queue-stack, that is, a hybrid of a queue and a stack. This is the best solution - it gives O ( n ) total time in the worst-case (although individual operations could take ( n ) time). Description below. A remark: our augmentation is actually very non-invasive. That is, we do not modify the code or structures of the queue; instead, we add a new data structure that operates in parallel to the queue....
View Full Document
This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.
- Fall '08