Docsity

Pseudocode - Data Structures - Problems, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CSE3358 Problem Set 5
02/11/05
Due 02/18/05
Problem 1: Young tableau
An mร—nYoung tableau is an mร—nmatrix such that the entries of each row are in sorted order
from left to right and the entries of each column are in sorted order from top to bottom. Some of the
entries of a Young tableau may be โˆž, which we treat as nonexistent elements. Thus a Young tableau
can be used to hold rโ‰คmn numbers.
Hereโ€™s an example of a 4x4 Young tableau containing the elements {9,16,3,2,4,8,5,14,12}. Note that
this is not unique.
2 4 9
โˆž
3 8 16
โˆž
5 14
โˆž
โˆž
12
โˆž
โˆž
โˆž
(a) (5 points) Argue that an mร—nYoung tableau Yis empty if Y[1,1] = โˆž. Argue that Yis full
(contains mn elements) if Y[m, n]<โˆž.
(b) (5 points) Argue that the minimum element of a Young tableau Yis always in Y[1,1].
(c) (10 points) Give an algorithm to implement EXTRACT-MIN on a nonempty mร—nYoung tableau
that runs in O(m+n) time. Your algorithm should use a recursive subroutine that solves an mร—n
problem by recursively solving either an (mโˆ’1)ร—nor an mร—(nโˆ’1) subproblem. Define T(p), where
p=m+n, to be the maximum running time of EXTRACT-MIN on any mร—nYoung tableau. Give
and solve a recurrence for T(p) that yields the O(m+n) time bound.
(d) (10 points) Show how to insert a new element into a nonfull mร—nYoung tableau in O(m+n)
time.
(d) (10 points) Using no other sorting method as subroutine, show how to use an nร—nYoung tableau
to sort n2numbers in O(n3) time. Based on this, describe a sorting algorithm that has an O(n1.5)
worst-case running time.
(e) (0 points) I will not ask: given ndistinct elements, how many Young tableaus can you make?
1
pf2

Partial preview of the text

Download Pseudocode - Data Structures - Problems and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSE3358 Problem Set 5 02/11/ Due 02/18/

Problem 1: Young tableau An m ร— n Young tableau is an m ร— n matrix such that the entries of each row are in sorted order from left to right and the entries of each column are in sorted order from top to bottom. Some of the entries of a Young tableau may be โˆž, which we treat as nonexistent elements. Thus a Young tableau can be used to hold r โ‰ค mn numbers.

Hereโ€™s an example of a 4x4 Young tableau containing the elements { 9 , 16 , 3 , 2 , 4 , 8 , 5 , 14 , 12 }. Note that this is not unique.

(^2 4 9) โˆž

(^3 8 16) โˆž

5 14 โˆž โˆž

12 โˆž โˆž โˆž

(a) (5 points) Argue that an m ร— n Young tableau Y is empty if Y [1, 1] = โˆž. Argue that Y is full (contains mn elements) if Y [m, n] < โˆž.

(b) (5 points) Argue that the minimum element of a Young tableau Y is always in Y [1, 1].

(c) (10 points) Give an algorithm to implement EXTRACT-MIN on a nonempty m ร— n Young tableau that runs in O(m + n) time. Your algorithm should use a recursive subroutine that solves an m ร— n problem by recursively solving either an (m โˆ’ 1) ร— n or an m ร— (n โˆ’ 1) subproblem. Define T (p), where p = m + n, to be the maximum running time of EXTRACT-MIN on any m ร— n Young tableau. Give and solve a recurrence for T (p) that yields the O(m + n) time bound.

(d) (10 points) Show how to insert a new element into a nonfull m ร— n Young tableau in O(m + n) time.

(d) (10 points) Using no other sorting method as subroutine, show how to use an n ร— n Young tableau to sort n^2 numbers in O(n^3 ) time. Based on this, describe a sorting algorithm that has an O(n^1.^5 ) worst-case running time.

(e) (0 points) I will not ask: given n distinct elements, how many Young tableaus can you make?

Problem 2: Sometimes you just have to count (10 points) Describe an algorithm that, given n integers in the rangle 0 to k, preprocesses its input and then answers any query about how many of the n integers fall into a rangle [a..b] in O(1) time. Your algorithm should use ฮ˜(n + k) prepreocessing time. For full credit you should

  • Describe your algorithm in english
  • Provide a pseudocode
  • Provide arguments for the correctness of your algorithm
  • Provide arguments for the running times of the preprocessing and the query operations

Problem 3: S0rt1ing revisited Suppose that we have an array of n data records to sort and that the key of each record has the value 0 or 1. An algorithm for sorting such a set of records might possess some subset of the following three desirable properties:

  1. The algorithm runs in O(n) time
  2. The algorithm is stable
  3. The algorithm sorts in place, using no more than a constant amount of storate space in addition to the original array

(a) (5 points) Describe an algorithm that possesses properties (1) and (2).

(b) (5 points) Describe an algorithm that possesses properties (2) and (3).

(c) (10 points) Describe an algorithm that possesses properties (1) and (3).

For full credit you should in each case:

  • Describe your algorithm in english
  • Provide a pseudocode, unless you are describing an algorithm for which we have seen the pseu- docode in class
  • Provide arguments that your algorithm possesses the desired properties