Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Due: Mar 12th at the start of class Homework #3 CMSC351 - Spring 2013 PRINT Name : o Grades depend on neatness and clarity. o Write your answers with enough detail about your approach and concepts used, so that the grader will be able to understand it easily. You should ALWAYS prove the correctness of your algorithms either directly or by referring to a proof in the book. o Write your answers in the spaces provided. If needed, attach other pages. o The grades would be out of 16. Four problems would be selected, and everyone’s grade would be based only on those problems. You will also get 4 bonus points for trying to solve all problems. 1. [Prob 6.4,Pg 176] Construct an example for which interpolation search will use Ω() comparisons for searching in a table of size n. See Section 6.3 of the book. Define the input table as = 2 be = − 1 + 1 = 2 for 1 ≤ ≤ and the search key to + 1. In this case the Next_Guess in interpolation search is always Left+1 and Right=n throughout the procedure since ()() < 1 for all !"#$ ∈ 1, − 1.This means that at each iteration only one element is removed, and hence Ω() comparisons are made. 2. Consider the following sequence of numbers as the input: 6 2 8 5 10 9 12 1 15 5 a) Sort the sequence using quicksort algorithm (Fig 6.11). Rewrite the sequence after each swap operation. What is the number of comparisons between elements of the input array used by quicksort? 48 comparisons. 6 Pivot=6; 6 Pivot=6; 6 Pivot=6; 1 Pivot=1; 1 Pivot=2; 1 Pivot=5; 1 Pivot=9; 1 Pivot=9; 1 Pivot=10; 1 Pivot=15; 1 2 2 2 2 2 2 2 2 2 2 2 8 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 10 10 1 6 6 6 6 6 6 6 6 9 9 9 9 9 9 9 9 8 8 8 12 1 12 1 12 10 12 10 12 10 12 10 12 10 8 10 9 10 9 10 9 10 15 15 15 15 15 15 15 15 15 15 12 5 8 8 8 8 8 8 12 12 12 15 0 4 6 6 6 5 4 7 6 5 3 b) Count the number of comparisons between elements of the input array if we use mergesort (Fig 6.7). About 22 comparisons. 3. You are given an array of n elements, and you notice that some of them are duplicates, that is, they appear more than once in the array. Show how to remove all duplicates from the array in time '( log ). Sort the elements of the array using mergesort in '( log ) time. Then remove the duplicate elements by traversing the sorted array. 4. The Skyline Problem: Consider a sequence of n buildings in 2 dimensional space. The + building is specified by a triple (,- , ℎ- , /- ) where,- , /- are the starting and ending x-coordinates and ℎ- is the height. Construct the skyline of these buildings in '( log ) time, which is a sequence of values of x-coordinates and heights connecting them arranged by order of the x-coordinates. See the example below. We write the heights in red color. 8 0 6 0 5 3 2 3 4 5 6 8 9 For this example the Input is (2,5,4), (3,3,6), (5,6,8) and (9,8,11) The output should be (2,5,4,3,5,6,8,0,9,8,11) This is solved in Section 5.6 of the book! 11 5. [Prob 6.23,Pg 177] Given two sets 01 and 0 , and a real number , find whether there exists an element from 01 and an element from 0 whose sum is exactly . The algorithm should run in time '( log ), where is the total number of elements in both sets. We are looking for a pair (2, 3) such that 2 ∈ 01 , 3 ∈ 0 and 2 + 3 = . So given an element 2 ∈ 01 , we should check if there exist an element with value − 2 in 0 . This means that we should be able to search quickly in 0 . Therefore, first we sort the elements of 0 in time '(|0 | ⋅ log |0 |) and then for every value 2 ∈ 01 , we check whether the value − 2 exists in the sorted set 0 using binary search in time '(log |0 |). Therefore the total time taken is '(|0 | ⋅ log |0 |) + |01 | ⋅ O(log|0 |) = '( log ) since = |01 | + |0 | 6. [Prob 6.24,Pg 177] Design an efficient algorithm to determine whether two sets with sizes n and m are disjoint. State the complexity of your algorithm. Make sure to consider the case where m is substantially smaller than n. The idea is very similar to that in Problem 5. Let arrays 71 , 7 be of sizes m, n respectively. Sort 71 and search for elements of 7 in the sorted array 71 . The running time is '(8 log 8 + log 8), when m is much smaller than n this is ≈ '( log 8). This indicates that it is better to sort the smaller array. 7. Show how to sort integers in the range 0 to − 1 in '() time. (Hint: Consider the numbers in base , i.e., each number in the range 0. . ( − 1) can be considered in the form (23) where 2 = ;< and 3 = 8=;.) Refer to Figure 6.6 of the book. If we have numbers in base 3, each with at most ; digits, we can sort all of them in time '(;( + 3)) by using radix sort. Using the hint, we can write all the numbers in the input in base with at most 2 digits. Therefore the running time of radix sort would be '>2( + )? = '().