Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

General Problem - Data Structures - Solved Problems, Exams of Data Structures and Algorithms

Main points of this exam paper are: General Problem, Young Tableau, Each Row, Containing The Elements, Minimum Element, General Problem, Corresponding Entry

Typology: Exams

2012/2013

Uploaded on 04/07/2013

seshu_lin3
seshu_lin3 🇮🇳

4

(3)

59 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE3358 Problem Set 5
Solution
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 rmn 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]<.
ANSWER: Assume Y[1,1] = . consider Y[i, j] for any iand j. By the property of a Young
tableau, Y[i, j]Y[1, j ]Y[1,1]. Therefore, Y[i, j] = . This means that the Young tableau is
empty because the above is true for every iand j.
Now assume Y[m, n]<. Consider Y[i, j ] for any iand j. By the property of a Young tableau,
Y[i, j]Y[m, j ]Y[m, n]. Therefore, Y[i, j]<. This means that the Young tableau is full because
the above is true for every iand j.
(b) (5 points) Argue that the minimum element of a Young tableau Yis always in Y[1,1].
ANSWER: Consider Y[i, j] for any iand j. By the property of a Young tableau, Y[1,1] Y[i, 1]
Y[i, j]. Therefore, Y[1,1] is the minimum because the above is true for every iand j.
(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 (m1)×nor an m×(n1) 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.
ANSWER: Since the minimum element is always in Y[1,1], EXTRACT-MIN can return the element
stored in Y[1,1] and set Y[1,1] = to indicate that the element does not exist anymore. However,
this might put the Young tableau in an inconsistent state, namely, Y[1,1] = and Yis not empty.
Here’s the Young tableau above after extracting the minimum element:
1
pf3
pf4
pf5

Partial preview of the text

Download General Problem - Data Structures - Solved Problems and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSE3358 Problem Set 5 Solution

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

ANSWER: Assume Y [1, 1] = ∞. consider Y [i, j] for any i and j. By the property of a Young tableau, Y [i, j] ≥ Y [1, j] ≥ Y [1, 1]. Therefore, Y [i, j] = ∞. This means that the Young tableau is empty because the above is true for every i and j.

Now assume Y [m, n] < ∞. Consider Y [i, j] for any i and j. By the property of a Young tableau, Y [i, j] ≤ Y [m, j] ≤ Y [m, n]. Therefore, Y [i, j] < ∞. This means that the Young tableau is full because the above is true for every i and j.

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

ANSWER: Consider Y [i, j] for any i and j. By the property of a Young tableau, Y [1, 1] ≤ Y [i, 1] ≤ Y [i, j]. Therefore, Y [1, 1] is the minimum because the above is true for every i and j.

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

ANSWER: Since the minimum element is always in Y [1, 1], EXTRACT-MIN can return the element stored in Y [1, 1] and set Y [1, 1] = ∞ to indicate that the element does not exist anymore. However, this might put the Young tableau in an inconsistent state, namely, Y [1, 1] = ∞ and Y is not empty. Here’s the Young tableau above after extracting the minimum element:

∞ 4 9 ∞

(^3 8 16) ∞

(^5 14) ∞ ∞

(^12) ∞ ∞ ∞

To fix this, we will look at the following general problem: Y [i, j] = ∞ but Y [i + 1..m, j..n] is a Young tableau and Y [i..m, j + 1..n] is a Yong tableau. We recursively exchange Y [i, j] and min(Y [i + 1 , j], Y [j + 1, j]) until we obtain a Young tableau. We can call this recursive procedure YOUNGIFY (analogous to HEAPIFY).

EXTRACT-MIN(Y ) x←Y [1, 1] Y [1, 1]←∞ YOUNGIFY(Y, 1 , 1) return x

YOUNGIFY(Y, i, j) smallesti←i smallestj ←j if i + 1 ≤ m and Y [i, j] > Y [i + 1, j] then smallesti←i + 1 smallestj ←j if j + 1 ≤ n and Y [smallesti, smallestj ] > Y [i, j + 1] then smallesti←i smallestj ←j + 1 if smallesti 6 = i or smallestj 6 = j then exchange Y [i, j] ↔ Y [smallesti, smallestj ] YOUNGIFY(Y, smallesti, smallestj )

Obviously, the running time of YOUNGIFY is O(m + n) because YOUNGIY exchanges Y [i, j] with either Y [i + 1, j] or Y [i, j + 1] and recursively YONGIFies the tableau at the corresponding entry. Therefore, with every recursive step, i + j is incremented by 1. However, i + j cannot be more than m + n and this gives the time bound of O(m + n). More precisely, we can say that YOUNGIFYing a tableau of size m × n will recursively YOUNGIFY a tableau of size (m − 1) × n or of size m × (n − 1).

Therefore, if we let p = m + n to be the size of our problem:

T (p) = O(1) + T (p − 1)

The solution of the above recurrence is T (p) = O(p) = O(m + n).

We can also use a recursive procedure similar to YOUNGIFY that works backward. Let’s call it YOUNGIFY’.

INSERT(Y, k) Y [m, n]←k YOUNGIFY’(Y, m, n)

YOUNGIFY’(Y, i, j) largesti←i largestj ←j if i − 1 ≥ 1 and Y [i, j] < Y [i − 1 , j] then largesti←i − 1 largestj ←j if j − 1 ≥ 1 and Y [largesti, largestj ] < Y [i, j − 1] then largesti←i largestj ←j − 1 if largesti 6 = i or largestj 6 = j then exchange Y [i, j] ↔ Y [largesti, largestj ] YOUNGIFY(Y, largesti, largestj )

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

ANSWER: Given n^2 numbers, we can insert them into an n × n Young tableau using INSERT. The sequence of n^2 inserts will take n^2 .O(n + n) = O(n^3 ) time. After that, we can repeatedly perform an EXTRACT-MIN n^2 times to obtain the numbers in order. This will take n^2 .O(n + n) = O(n^3 ) time also.

Assume A has length m = n^2 and Y is an empty n × n Young tableau.

SORT(A) for i←1 to m do INSERT(Y, A[i]) for i←1 to m do A[i]←EXTRACT-MIN(Y )

In general, given n numbers to sort, let a = d

ne. We can create an empty a × a Young tableau and perform the above algorithm (with m = n). Each operation on the Young tableau will take O(a + a) = O(

n) time. Therefore, the running time of the algorithm will be O(n

n) = O(n^1.^5 ).

(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

ANSWER: The algorithm will use a counter array C[0..k] and set C[i] to be the number of elements less or equal to i. This is identical to the first part of Counting sort and will take Θ(n + k) time. After that, to obtain the number of elements in the range [a..b], it is enough to compute C[b] − C[a − 1] which can be done in O(1) time.

pre-processing

for i←0 to k do C[i]← 0 for i←1 to n do C[A[i]]←C[A[i]] + 1 for i←1 to k do C[i]←C[i − 1] + C[i]

query

if a = 0 then return C[b] else return C[b] − C[a − 1]

a b

0 a-1 k

C[b]

C[a]