Sorting Exercises-Complexity of Algorithms-Assignment Solution, Exercises of Advanced Algorithms

This assignment was submitted to Prof. Madhulata Nirmal at Chhattisgarh Swami Vivekanand Technical University for Theory of Complexity and Algorithms course. It includes: Sorting, MergeSort, Variation, QuickSort, Algorithms, Randomized, Sequence, Inversions

Typology: Exercises

2011/2012

Uploaded on 07/17/2012

toshi-sabri
toshi-sabri 🇮🇳

4.4

(20)

44 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Solutions to Exercises related to Sorting
Note: As you might suspect, these exercises can be solved essentially
using a variation of the MergeSort or (randomized) QuickSort algorithms.
1. Suppose that Sis a set of nintegers. Describe an efficient algorithm to
determine whether there are two (or more) equal integers in S. What
is the running time of your algorithm?
(Note: This can easily be done in time O(n2). (How?) Can you do
better?)
Solution: This one is quite easy to do. All we really do is we can
start performing the MergeSort or randomized Quicksort algorithm.
Suppose we’re using the MergeSort algorithm. Then, as we’re merging
the lists (or some of the sublists in one of the recursive calls), we merely
check to see if the two items we’re comparing as we merge the lists
are equal to each other. If so, we can stop and report “Yes”. If we
never find an equal pair of integers, then when we’re finished with the
sorting, we report “No”.
The same thing can be done with the randomized QuickSort algorithm,
but when we’re creating the sublists using the pivot element, if the
sublist we called Eis ever non-empty at some step, then we again
report “Yes”. Otherwise, we do the same thing as above, i.e. as we’re
merging the sorted sublists, we check to see if we find a pair of elements
that are equal.
Using the first method takes time O(nlog n) in the worst case as
we are basically performing the MergeSort algorithm. The second
method takes expected time O(nlog n) using the randomized Quick-
Sort method.
2. Give a sequence of nintegers with Ω(n2) inversions. (Recall the defi-
nition of an inversion from the class notes.)
Solution: The easiest example is the sequence
n, n 1, n 2, . . . , 2,1
where everything is out of order. This has ¡n
2¢=n(n1)
2=O(n2)
inversions.
There are many other examples possible like (assuming nis even)
n
2,n
21,n
22, . . . , 1,n
2+ 1, . . . , n.
1
docsity.com
pf3
pf4

Partial preview of the text

Download Sorting Exercises-Complexity of Algorithms-Assignment Solution and more Exercises Advanced Algorithms in PDF only on Docsity!

Solutions to Exercises related to Sorting

Note: As you might suspect, these exercises can be solved essentially using a variation of the MergeSort or (randomized) QuickSort algorithms.

  1. Suppose that S is a set of n integers. Describe an efficient algorithm to determine whether there are two (or more) equal integers in S. What is the running time of your algorithm? (Note: This can easily be done in time O(n^2 ). (How?) Can you do better?)

Solution: This one is quite easy to do. All we really do is we can start performing the MergeSort or randomized Quicksort algorithm. Suppose we’re using the MergeSort algorithm. Then, as we’re merging the lists (or some of the sublists in one of the recursive calls), we merely check to see if the two items we’re comparing as we merge the lists are equal to each other. If so, we can stop and report “Yes”. If we never find an equal pair of integers, then when we’re finished with the sorting, we report “No”. The same thing can be done with the randomized QuickSort algorithm, but when we’re creating the sublists using the pivot element, if the sublist we called E is ever non-empty at some step, then we again report “Yes”. Otherwise, we do the same thing as above, i.e. as we’re merging the sorted sublists, we check to see if we find a pair of elements that are equal. Using the first method takes time O(n log n) in the worst case as we are basically performing the MergeSort algorithm. The second method takes expected time O(n log n) using the randomized Quick- Sort method.

  1. Give a sequence of n integers with Ω(n^2 ) inversions. (Recall the defi- nition of an inversion from the class notes.)

Solution: The easiest example is the sequence

n, n − 1 , n − 2 ,... , 2 , 1

where everything is out of order. This has

(n 2

) = n(n 2 − 1) = O(n^2 ) inversions. There are many other examples possible like (assuming n is even) n 2

n 2

n 2

n 2

  • 1,... , n.

(In other words, reverse, say the first half of the list, but leave the second half in the natural numerical order.) This sequence has

(n/ 2 2

)

1 2 (^

n 2 )(^

n 2 −^ 1) =^ O(n

(^2) ) inversions.

  1. Suppose you are given two sequences X and Y of k integers each, possibly containing duplicates. Describe an efficient algorithm to de- termine if X and Y contain the same set of elements (possibly in different orders). What is the running time of your procedure? (Note: There are two ways this could be interpreted. Suppose that X = (1, 1 , 1 , 1 , 2) and Y = (2, 2 , 1 , 2 , 1). If we take only the collection of distinct elements into account, then since X and Y both contain 1 and 2 (only “counting” a duplicated item once), we might consider them to “contain the same set of elements”. If, on the other hand, we want to consider both the elements and the number of occurrences, then X and Y are do not contain the same set of elements, as the number of occurrences of 1 and 2 in the lists are clearly different. Consider both of these cases in your analysis.)

Solution: Surprise, surprise... Apply the MergeSort algorithm on each list X and Y separately. Then when we’re done with sorting each list, we compare them element by element to see if they are equal. The only difference between the two interpretations is how we might consider merging the sublists. If we want to exclude the duplicates to consider X and Y equal, then we need only include one copy when we merge the lists. In other words, if we have X = (1, 1 , 1 , 1 , 2) and Y = (2, 2 , 1 , 2 , 1), when we sort and “merge” the lists, we can end up with the new lists X′^ = (1, 2) and Y ′^ = (1, 2) which we compare and see they are equal. If we wish to include the duplicates, then when we sort the lists we will end up with (1, 1 , 1 , 1 , 2) and (1, 1 , 2 , 2 , 2) which we then compare element by element and see they are different lists. In any case, depending upon how we wish to compare the lists, we can still do this in time O(n log n), as we apply MergeSort twice (or some small variation of it if we remove the duplicates).

  1. Bob has a set A of n nuts and a set B of n bolts, such that each nut in A has a unique matching bolt in B. Unfortunately, the nuts in A all look the same, and the bolts in B all look the same as well. The only kind of comparison that Bob can make is to take a nut-bolt pair (a, b) (such that a ∈ A and b ∈ B) and test them to see if the threads of a are larger, smaller, or a perfect match with the threads of b.

What’s the idea? Pick a pivot element for A, and divide A into the three sets LA, RA, EA as normal. Suppose the value of this pivot el- ement is k 1. Since we’re given the value x, use x − k 1 as the pivot element for B. As we subdivide B into the sets LB , RB , EB , if the set EB is non-empty, we can stop and we’re done with a positive answer. This is because k 1 is in A and we would have just shown that the value x − k 1 is in B.

Otherwise we can continue into the next recursive function calls. We would pick a pivot value for the subset LA. Say this value is k 2. We would then use the value x − k 2 as a pivot value for the set RB. Why? Because, once again, as we subdivide the set RB we will determine if there is element having value x − k 2 in it. If so, we combine this with k 2 to find our solution to x = a + b as desired.

So we can continue in this fashion to determine if there is solution of the form we’re looking for. Note that at some stages we might be able to further short-circuit this search process. If, for example, during the first steps, we find that the set RB (consisting of items that are greater than x − k 1 ) is empty, then we need not consider any elements in LA. (Why?)

Again, since we’re doing the randomized Quicksort algorithm, this whole procedure will run in (expected) time O(n log n).