Download Assignment Questions - Algorithm Design and Analysis | CSE 202 and more Assignments Computer Science in PDF only on Docsity!
Design and Analysis of Algorithms — CSE 202
Divide and Conquer and Sorting
October 12, 2003
The problems are collected from several books including CLR and “Introduction to Algorithms” by Udi Mander.
- Stooge Sort: Professors Howard, Fine, and Howard have proposed the following ‘elegant’ sorting algorithm:
STOOGE-SORT
1 if
2 then exchange
3 if
4 then return
6 STOOGE-SORT
7 STOOGE-SORT
8 STOOGE-SORT
(a) Argue that STOOGE-SORT
correctly sorts the input array
, where
A@
2 314687B9 C
(b) Give a recurrence for the worst-case running time of STOOGE-SORT and a tight asymptotic D
-notation ) bound on the worst-case running time. (c) Compare the worst-case running time of STOOGE-SORT with that of insertion sort, merge sort, heapsort, and quicksort. Do the professors deserve tenure?
2.! -way merging: Give an E
<4F%G
algorithm to merge! sorted lists into one sorted list, where
is
the total number of elements in all the input lists. Hint: Use a heap for! -way merging. Analyze the
time complexity of your algorithm.
Prove a tight lower bound on the number of comparisons for !-way merging.
- Max-sum: Use the divide-and-conquer approach to write an efficient recursive algorithm that finds the maximum sum in any contiguous sublist of a given list of
real (positive or negative) values. Analyze your algorithm, and show the results in order notation. Can you do better?
4. Mode: <HIKJ KL HIK-The mode of a set of numbers is the number that occurs most frequently in the set. The set 1+
has a mode of 4.
(a) Give an efficient and correct algorithm to compute the mode of a set of
numbers. Analyze the time complexity of your algorithm.
(b) Suppose that we know that there is an (unknown) element that occurs more than
,L
times in the set. Give a worst-case linear-time algorithm to find the mode. For partial credit, your algorithm may run in an expected linear time. Analyze the time complexity of your algorithm.
- Searching: Write an algorithm that searches a sorted list of
items by dividing it into three sublists of almost
items. This algorithm finds the sublist that might contain the given item, and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Analyze your algorithm, and give the results using order notation.
6. Probabilities for multi-faceted dice: There are dice, and the
^ -th die has its faces numbered from
to ! where each ! is a positive integer less than . Assuming that on each die, each number is
equally likely, we want to find the probability that when we roll all dice, the sum of the numbers is
exactly . To do so, we need to count exactly how many ways we can choose integers
with ^ !^ , such that ^
. (The probability is this count divided by the product of the ! ’s.)
Find an efficient algorithm, and give its time analysis in terms of and .
- Weighted median: For
distinct elements
with positive weights
such
that
, the weighted median is the element ^ satisfying
+,L
and
+,L
(a) Argue that the median of
is the weighted median of the * with weights
for
KL
(b) Show how to compute the weighted median of
elements in E
<4F%G 4
worst-case time using sorting. (c) Show how to compute the weighted median in
D
worst-case time using a linear-time median algorithm.
-th largest element: Show that, for any constant
, the
-th largest element of an array can be found with
E
F G 4
comparisons.
9. Tree isomorphism: Give an efficient algorithm to decide, given two rooted binary trees and ,+^ ,
whether and + are isomorphic as rooted trees.
10.! -th quantiles: The! -th quantiles of an
-element set are the!
order statistics that divide the
sorted set into! equal-sized (to within 1.) Give an E
<4F%G
-time algorithm to list the! -th quantiles
of a set.
- Black jack: Give the best algorithm you can for the following problem:
Name Blackjack Hand Card Counting Instance An array
of
positive integers (cards with face values) with values from
to! , and
positive integers
Problem Count the number of sets of
array positions (hands of
cards) whose total value is equal to
- Matrix stable sort: Consider a rectangular array. Sort the elements in each row into increasing order. Next sort the elements in each column into increasing order. Prove that the elements in each row remain sorted.
26. Modular stable sort: Let
be a sequence of elements and let and be positive integers.
Consider the subsequences formed by selecting every -th element. Sort these subsequences. Repeat
the process for . Prove that the subsequences of distance remain sorted.
- Not-in-the-set: The input is an array A of
real numbers. Design an E
algorithm to find a number that is not in the array
. Prove that
is a lower bound on the number of steps required to solve this problem.
- Sort-sum: The input is an array A containing
real numbers, and a real number .
(a) Design an algorithm to determine whether there are two elements of
whose sum is exactly .
The algorithm should run in time E
<4F G 4
(b) Suppose now that the array
is given in a sorted order. Design an algorithm to solve this
problem in time E
- Disjoint sets: Design an algorithm to determine whether two sets of numbers (presented as arrays)
are disjoint. State the complexity of your algorithm in terms of the sizes and
of the given sets.
Make sure to consider the case where is substantially smaller than
- Faster sorting: The input is a sequence of
integers with many duplications, such that the number
of distinct integers in the sequence is E
F%G 4
(a) Design a sorting algorithm to sort such sequences using at most E
<4F%G F%G 4
comparisons in the worst case.
(b) Why is the lower bound of
<4F G 4
not satisfied in this case?
- Celebrity problem: Given an
adjacency matrix, determine whether there exists an
such that all the enties in the
th column (except for the
th entry) are 1, and all the entries in the
th row (except for the
-th entry) are 0. This problem is also referred to as a the celebrity identification problem. Among
persons, a celebrity is defined as someone who is known by everyone but does not know anyone.
32. Given a set of integers
, find a nonempty subset such that
modulo
- Communication complexity: Let
and two sets wwith both with
elements, such that
resides
in computer and in . and can communicate by sending messages, and they can perform
any kind of local computation. Design an algorithm to find the
th smallest element of the union of
and (that is, the median). You can assume that all the elements are distinct. Your goal is to
minimize the number of messages, where a message can contain one element or one integer. What is hte number of messages in the worst case?
34.! th largest in a heap: The input is a heap of size
(in which the largest element in on top), given
as an array, and a real number . Design an algorithm to determine whether the !th largest element
in the heap is less than or equal to . The worst-case running time of your algorithm should be E
independent of the size of the heap.
35.! th smallest element in E
! space: The input is a sequence of elements
, given one
at a time. Design an E
expected time algorithm to compute the! th smallest element using only
E
! memory cells. The value of! is known ahead of time, but the value of
is not known until the last element is seen.
- Lower bound on merging: Show a lower bound of
L
on the worst-case number of comparisons required to merge two sorted lists, each containing
items.
- Sorting variable length integers: You are given an array of integers, where different integers may have different numbers of digits, but the total number of digits over all the integers in the array is
Show how to sort the array in E
time.
- Polynomial Multiplication: Give a divide-and-conquer algorithm for multiplying polynomials of degree
in the same variable,