design and analysis of algorithmswkhon/algo08-tutorials/tutorial2a.pdf · question4 (c) show how to...

71
Design And Analysis of Algorithms Homework 2

Upload: others

Post on 26-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Design And Analysis ofAlgorithms

Homework 2

Page 2: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Outline

Question 1. BasicQuestion 2. BasicQuestion 3. ModerateQuestion 4. ModerateQuestion 5. Challenging

Page 3: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

Our genius friend, John, has inventedthe algorithm for sorting the arrayA[i..j]:

Page 4: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

JohnSort(A, i, j)Set l = j −i + 1, k = l % 3, m = (l −k)/3;if (k != 0) {

Find smallest k items;Swap them with items in A[i..i + k −1]in increasing order ;

}if (m == 0) return;

/* Sort remaining 3m items by recursion */JohnSort(A, i+k, j−m) // Sort first 2m itemsJohnSort(A, i + k+m, j) // Sort last 2m itemsJohnSort(A, i+k, j−m) // Sort first 2m items

Page 5: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

(a) Show that the above algorithm is correct.(b) Give a recurrence for the worst-case

running time of JohnSort.(c) Obtain a tight asymptotic (Θ-notation)

bound on the worst-case running time.–How does it compare with the worst-case running

time of insertion sort and merge sort?

Page 6: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

15826734

K(=2)

The first 2m elements (m=2)

The last 2m elements

An example:

Page 7: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

15826734

Page 8: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

15826734

Page 9: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

48 526731

Page 10: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45826731

Page 11: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45836721

Page 12: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45836721

Page 13: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45836721

Page 14: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45876321

Page 15: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45876321

Page 16: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45876321

Page 17: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45876321

Page 18: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45876321

Page 19: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

45876321

Page 20: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

75846321

Page 21: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

75846321

Page 22: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

75846321

Page 23: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

78546321

Page 24: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

78546321

Page 25: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

78546321

Page 26: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87546321

Page 27: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87546321

Page 28: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87546321

Page 29: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87546321

Page 30: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87546321

Page 31: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87564321

Page 32: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87564321

Page 33: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87564321

Page 34: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87654321

Page 35: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87654321

Page 36: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

87654321

Page 37: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question1

In (b), only need to give the recurrence

In (c), use Master Theorem to bound thetime complexity

Page 38: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question2

What’s RadixSort?An example:

491

262

233

252

Page 39: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question2

262

491

233

252

Page 40: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question2

262

491

252

233

Page 41: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question2

262

491

233

252

Page 42: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question2

233

491

262

252

Page 43: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question2

262

233

252

491

Page 44: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question2Illustrate the operation of RadixSort on thefollowing list of English words:

COW, DOG, SEA, RUG, ROW, MOB, BOX, TAB,BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX

Show clearly how RadixSort works on those data

Page 45: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question3• Given an array A[1..k] of k strings, with each

string representing an integer. Totally thereare n digits in these strings.

Show how to sort them in O(n) time

Page 46: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question3Example:If the input array is

{ “235”, “8”, “17”,“652”,“490”, “231562955”,“940”, “2”},

then n = 25

2049

559265132094256

718

532

Page 47: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question3

After sorting, weshould obtain

{ “2”, “8”, “17”, “235”,“490”, “652”, “940”,“231562955”}

049256094532

718

559265132

2

Page 48: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question3

A simple RadixSort is not capable of solvingthis problem

Why?

Page 49: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

191596141274136528531

What’s so specialabout this matrix?

Page 50: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

Young Tableau is an m × n matrix with(i) each row is sorted(ii)each column is sorted order

Some entries = ∞ Indicate an empty entry

Young Tableau can hold ≤mn finite #s

Page 51: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4Young Tableau

161097141264136539541

Is this a Young Tableau?

Page 52: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4Young Tableau

161097141264136539541

No !

Page 53: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

(a) Draw three 4 × 4 Young tableauxcontaining exactly the elements{9, 16, 3, 2, 4, 8, 5, 14, 12}.

(b) Given an m× n Young tableau Y-- Show Y is empty if Y [1, 1] = ∞-- Show Y is full if Y [m, n] < ∞

Page 54: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

(c) Show how to do Extract-Min on aYoung tableau in O(m + n) timeHint: Extract-Min in a heap

Show that your algorithm is correct.

(d) Show how to use an n × n Youngtableau to sort n2 numbers in O(n3)time

Page 55: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4Young Tableau is similar

to a HEAP

151271185941

1

5 4

7 8 8 9

Page 56: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

Recalling how we do Extract-Min in Heap

Page 57: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

Page 58: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

This node mayviolate the heapstructure

Page 59: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

This node mayviolate the heapstructure

Observe that each timeat most one node mayviolate the structure

Page 60: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

9

Question4

5 2

6 7 3 4

5

9 2

6 7 3 4

2

5 9

6 7 3 4

dochi?

Page 61: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

9

Question4

5 2

6 7 3 4

if we choose to swap9 and 5, anotherviolation occurs.

Page 62: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

5

Question4

9 2

6 7 3 4

New violation !

So we choose to swapthe minimum amongnearby nodes

Page 63: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

A3 A2

A4 A5

A3

A2

A4 A5

A2

A3 A4

A5

A2<A3

A4<A5

A2<A4

A2<A3<A4<A5

Page 64: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

Page 65: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

Page 66: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

Page 67: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

Page 68: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question4

(d) You need to clearly show why youralgorithm’s time complexity is O(n3)

Page 69: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question5 (Bonus)

Finding a number in O(m+n) time inside aYoung tableau

Page 70: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question5 (Bonus)

HINT 1:

161097141264136539541

This is a boundarythat the ↖ directioncontains all elementsthat is less than 8

Page 71: Design And Analysis of Algorithmswkhon/algo08-tutorials/tutorial2a.pdf · Question4 (c) Show how to do Extract-Min on a Young tableau in O(m + n) time Hint: Extract-Min in a heap

Question5 (Bonus)

HINT 2:

161097141264136539541 5 is the greatest

element in the redsquare, 12 is thegreatest element inthe pink square