Docsity

Assignment Questions - Algorithm Design and Analysis | CSE 202, Assignments of Computer Science

Toggle sidebar
bg1
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.
1. 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
5
!#" $
%'&()*+,-/.
6 STOOGE-SORT
&
!
7 STOOGE-SORT

!
0
8 STOOGE-SORT
&
!
(a) Argue that STOOGE-SORT
'123546879: ;<
correctly sorts the input array
=>%>?4
, where
4A@
2314687B9 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-SORTwith that of insertion sort, merge sort,
heapsort, and quicksort. Do the professors deserve tenure?
2.
!
-way merging: Give an
E
<4F%G
!
algorithm to merge
!
sorted lists into one sorted list, where
4
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.
3. 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
4
real (positive or negative) values.
Analyze your algorithm, and show the results in order notation. Can you do better?
4. Mode: The mode of a set of numbers is the number that occurs most frequently in the set. The set
<HIKJKLHIK-1+
has a mode of 4.
(a) Give an efficient and correct algorithm to compute the mode of a set of
4
numbers. Analyze the
time complexity of your algorithm.
1
pf3
pf4
pf5

Partial preview of the text

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.

  1. 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 asymptoticD

-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.

  1. 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.

  1. 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 .

  1. 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.

  1. 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

  1. 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.

  1. 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.

  1. 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

  1. 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

  1. 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?

  1. 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

  1. 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.

  1. 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.

  1. 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.

  1. Polynomial Multiplication: Give a divide-and-conquer algorithm for multiplying polynomials of degree

in the same variable, 

^

and 

4. Assume that the inputs are each given as arrays of integers of dimension

, and assume that

is a power of 2. Also assume that integer operations on the co-efficients are

E

B+

time. Your output should be a dimension

L

array representing the polynomial 

  so that 

. Can you speed your algorithm up by using tricks like

in integer multiplication?

  1. Noncontiguous maximum sum: Give and solve up to order a recurrence relation for the time taken by the following divide-and-conquer algorithm. We are given an array of real numbers

. We wish to find a subset of array positions, 

that maximizes  

@ =  1 HI11L KJ 11- H  subject to no two consecutive array positions being in^ . For example, say

, the best solution is to take elements

K- 

to get a total of

1L  1-@ - 

If instead, we try to take the 14 in position 2, we must exclude the 10 and 12 in positions 1 and 3, leaving us with the second best choice

L

giving a total of

H

*1-'@ L

  1. Multiplication in threes:

Describe and analyze a divide-and-conquer algorithm for integer multiplication based on partitioning up each input into 3 blocks of digits. Is this better than the divide and conquer approach in the text? If so, is it possible to get further improvements along the same lines?

  1. Base Conversion: Give an algorithm that inputs an array of

base

 digits representing a positive

integer in base

 and outputs an array of base

digits representing the same integer in base

. Get as close as possible to linear time. Assume

are fixed constants.

If we do the conversion digit-by-digit, the number of operations is really E

, since, for example,

once we convert each digit we need to add up all the resulting E

bit numbers. We can do better using a divide-and-conquer algorithm using a FFT integer multiplication algorithm as a sub-routine. Assume

is a power of 2; otherwise, pad with 0’s in the most significant digits.

42. Skylines: In a video game, a city’s buildings are rectangles along an axis, specified as  

K

for

, for a building starting at 

and going to 

at height

. When seen from

a distance, the graphics is supposed to plot the skyline, which at each vertical point  has height the