cosc 3100 transform and conquer instructor: tanvir

25
COSC 3100 Transform and Conquer Instructor: Tanvir

Upload: morgan-griffin-preston

Post on 17-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COSC 3100 Transform and Conquer Instructor: Tanvir

COSC 3100Transform and

Conquer

COSC 3100Transform and

Conquer

Instructor: Tanvir

Page 2: COSC 3100 Transform and Conquer Instructor: Tanvir

What is Transform and Conquer ?• The 4th algorithm design technique we are

going to study• Three major variations

– Instance Simplification: Transform to a simpler or more convenient instance of the same problem

– Representation Change: Transform to a different representation of the same instance

– Problem Reduction: Transform to an instance of a different problem for which you know an efficient algorithm

• Two-step process:– Step 1: Modify problem instance to something

easier to solve– Step 2: Conquer!

Page 3: COSC 3100 Transform and Conquer Instructor: Tanvir

What is Transform and Conquer ?

Problem’s instance

Simpler instance Or

Another representationOr

Another problem’s instance

Solution

1. Presorting

1. Heapsort2. Horner’s Rule

3. Binary Exponentiation

1. Computing Least Common Multiple

2. Counting Paths in a Graph3. Reduction to Optimization

Problems4. Reduction to Graph Problems

Page 4: COSC 3100 Transform and Conquer Instructor: Tanvir

Trns. & Conq.: Presorting• Many questions about a list are

easier to answer if the list is sorted• We shall see three examples• Example 1: Checking element

uniqueness in an array

4 1 8 9 3 7 10

2 3 1

What is the brute-force idea ?

E.g.,

Take say the first element and check whether it is in the rest of the array, take the second element and check

if it is in the rest of the array, and so on…

T(n) = (n-1)+(n-2)+…+1= є Θ(n2)

Page 5: COSC 3100 Transform and Conquer Instructor: Tanvir

• Let us apply sorting firstALGORITHM PresortElementUniqueness(A[0..n-1])sort the array Afor i <- 0 to n-2 do

if A[i] = A[i+1] return false

return true

Trns. & Conq.: Presorting

T(n) = Tsort(n) + Tscan(n) є Θ(nlgn) + Θ(n) = Θ(nlgn)

4 1 8 9 3 7 10

2 3 1

1 1 2 3 3 4 7 8 9 10

SORT

Page 6: COSC 3100 Transform and Conquer Instructor: Tanvir

• Example 2: Computing a mode• A mode is a value that occurs most often in a

list• E.g., in , mode is ?

Trns. & Conq.: Presorting

5 1 5 7 6 5 7

Brute-force: scan the list and compute the frequencies of all distinct values, then find

the value with the highest frequencyHow to implement brute-force ?

Store the valuesalready encounteredalong with their frequencies,in a separate list.

5

3

1

1

7

2

6

1

What is the worst-caseinput ?

An array with no equalelements…C(n) = 0 + 1 + … + (n-1)

= є Θ(n2)

Value:

Frequency:

5

Page 7: COSC 3100 Transform and Conquer Instructor: Tanvir

• Example 2: Computing a mode (contd.)

• If we sort the array first, all equal values will be adjacent to each other.

• To compute the mode, all we need to know is to find the longest run of adjacent equal values in the sorted array

Trns. & Conq.: Presorting

Page 8: COSC 3100 Transform and Conquer Instructor: Tanvir

Example 2: Computing a mode (contd.)ALGORITHM PresortMode( A[0..n-1] )sort the array Ai <- 0modefrequency <- 0while i ≤ n-1 do

runlength <- 1; runvalue <- A[i]while i+runlength ≤ n-1 and A[i+runlength] = runvalue

runlength <- runlength+1if runlength > modefrequency

modefrequency <- runlength; modevalue <- runvalue

i <- i+runlengthreturn modevalue

Trns. & Conq.: Presorting

while loop takes linear time, so the overall runtimeis dominated by the time for sorting, Θ(nlgn)

1 1 2 3 3 3 7 8 9 10

Page 9: COSC 3100 Transform and Conquer Instructor: Tanvir

• Another O(nlgn) sorting algorithm• Uses a clever data structure called “Heap”• It transforms an array into a Heap and then

sorting becomes very easy• Heap has other important uses, like in

implementing “priority queue”• Recall, priority queue is a multiset of items

with an orderable characteristic called “priority”, with the following operations:– Find an item with the highest priority– Deleting an item with the highest priority– Adding a new item to the multiset

Trns. & Conq.: Heapsort

Let usstudy Heapfirst…

Page 10: COSC 3100 Transform and Conquer Instructor: Tanvir

• A “heap” can be described as a binary tree, with one key per node, provided the following two conditions are met:– Shape property: Binary tree is essentially

complete, all levels are full except possibly the last level, where only some right most leaves may be missing

– Parental dominance (or heap property): key in each node is greater than or equal to the keys in its children (considered automatically satisfied for the leaves)

Trns. & Conq.: Heap

This is actually the “max heap”, There is a corresponding “min heap”…

Page 11: COSC 3100 Transform and Conquer Instructor: Tanvir

Trns. & Conq.: Heap

10

5

24

7

1

10

5 7

2 1

10

5 7

26 1

Is it a Heap ?

Shape and Heap properties…

Page 12: COSC 3100 Transform and Conquer Instructor: Tanvir

10

8 7 5 2 1 6 3 5 1

Trns. & Conq.: Heap

10

8

25

7

1 6

3 5 1

0 1 2 3 4 5 6 7 8 9 10index:

value:

parents leaves

Note: Sequence of values on a path fromthe root to a leaf is nonincreasing

There is no left-to-right order in key values, though

ALGORITHM Parent(i)return

ALGORITHM Left(i)return 2i

ALGORITHM Right(i)return

Page 13: COSC 3100 Transform and Conquer Instructor: Tanvir

Trns. & Conq.: Heap

8

25

7

1 6

3 5 1

9

9 8 7 5 2 1 6 3 5 1

0 1 2 3 4 5 6 7 8 9 10

Important properties of heaps:1. There exists one essentially completebinary tree with n nodes, its height is

2. The root of a heap contains the largest element3. A node with all its descendants is also a heap4. Heap can be implemented as an arrayBy recording its elements in the top-down,left-right fashion. H[0] is unused or containsa number bigger than all the rest.

a. Parental node keys will be infirst positions, leafs willBe in the last positions

b. Children of key at index i (1 ≤ i ≤ ) will be in positions2i and 2i+1. The parent of a key at index i (2 ≤ i ≤ n) will be inPosition .

H:

Which gives: H[i] ≥ max{ H[2i], H[2i+1] } for i = 1, … ,

Page 14: COSC 3100 Transform and Conquer Instructor: Tanvir

• How can we transform an array into

a heap? Two ways:

Trns. & Conq.: Heap

2

9

56

7

8

1. Start atthe last parent at index

2 9 7 6 5 8

0 1 2 3 4 5 6n

2. Check if parental dominance holds for this node’s key

3. If it does not,Exchange the node’skey K with the largerkey of its children

4. Check if parental dominance holds for Kin its new position and so on…Stop after doing for root.

This process is called “Heapifying”This method is called “bottom-up heap construction”

2

9

56

8

7

2

9

56

8

7

9

2

56

8

7

6

2

Page 15: COSC 3100 Transform and Conquer Instructor: Tanvir

ALGORITHM HeapBottomUp(H[1..n])//Input: An array H[1..n] of orderable items//Output: A heap H[1..n]for i <- downto 1 do

k <- i; v <- H[k]heap <- falsewhile not heap and 2*k ≤ n do

j <- 2*kif j < n // there are 2 children

if H[j] < H[j+1]j <- j+1

if v ≥ H[j] // only left childheap <- true

elseH[k] <- H[j]k <- j

H[k] <- v

Trns. & Conq.: HeapH

eap

ify

on

e p

are

nt

2

9

56

7

8

2 9 7 6 5 8

0 1 2 3 4 5 6

n

Page 16: COSC 3100 Transform and Conquer Instructor: Tanvir

• Let us analyze the HeapBottomUp(H[1..n])’s worst case

Trns. & Conq.: Heap

Let’s take a heap whose binary tree is full

So, number of nodes, n = 2m-1largest possible # of nodes ateach level…

n = 22-1

n = 23-1 So what is the height ?

h = = -1 = m-1 Let’s look at a worst-case…

7

4

56

2

3 9

Why ?

A key on level i must travel to the leaf-level h; Level i has (h-i) lower levels. Going down by 1 level requires 2 comparisons: One to determine the larger child, the other to determine if exchange is needed.

Cworst(n) = =

∑𝒊=𝟏

𝒏

𝒊𝟐𝒊=(𝒏−𝟏)𝟐𝒏+𝟏+𝟐

= 2h(2h-1) – 2(h-2)2h + 4= h2h+1-2h-h2h+1+2*2h+1+4= 2 ( n – lg(n+1) + 4 )

So, fewerthan 2n comparisonsare required

Page 17: COSC 3100 Transform and Conquer Instructor: Tanvir

• Building a heap, Method 2• It’s called “top-down heap construction”• Idea: Successively insert a new key into a

previously constructed heap • Attach a new node with key K after the last

leaf of the existing heap• Sift K up until the parental dominance is

established

Trns. & Conq.: Heap

9

6

52 7 10

8

9

6

52 7 8

10

10

6

52 7 8

9

Insertion operation cannot take more than twice the height of the heap’s tree, so it is in O(lgn)

Page 18: COSC 3100 Transform and Conquer Instructor: Tanvir

• Maximum key deletion from a heap– Exchange root’s key with the last key K in

the heap– Decrease the heap’s size by 1– “Heapify” the smaller tree by sifting K down

establishing “parental dominance” as long as it is needed

Trns. & Conq.: Heap

9

8

52 1

6

We wantto deletethe root

1

8

52 9

6

1

8

52

6

8

1

52

68

5

12

6

Efficiency isO(lgn)

Page 19: COSC 3100 Transform and Conquer Instructor: Tanvir

• An interesting algorithm discovered by J. W. J. Williams in 1964

• It has 2 stages– Stage 1: Costruct a heap from a

given array– Stage 2: Apply the root-deletion

operation n-1 times to the remaining heap

Trns. & Conq.: Heapsort

Page 20: COSC 3100 Transform and Conquer Instructor: Tanvir

2 9 7 6 5 8

Trns. & Conq.: HeapsortStage 1: Heap construction

0 1 2 3 4 5 62

9 7

6 5 8

2 9 8 6 5 7

0 1 2 3 4 5 6

2

9 8

6 5 7

2 9 8 6 5 7

0 1 2 3 4 5 6

2

9 8

6 5 7

9 2 8 6 5 7

0 1 2 3 4 5 6

9

2 8

6 5 7

9

6 8

2 5 7

9 6 8 2 5 7

0 1 2 3 4 5 6

Page 21: COSC 3100 Transform and Conquer Instructor: Tanvir

Trns. & Conq.: HeapsortStage 2: Maximum deletions

9 6 8 2 5 7

0 1 2 3 4 5 6

7 6 8 2 5 9

0 1 2 3 4 5 6

8 6 7 2 5 9

0 1 2 3 4 5 6

5 6 7 2 8 9

0 1 2 3 4 5 6

7 6 5 2 8 9

0 1 2 3 4 5 6

2 6 5 7 8 9

0 1 2 3 4 5 6

6 2 5 7 8 9

0 1 2 3 4 5 6

5 2 6 7 8 9

0 1 2 3 4 5 6

2 5 6 7 8 9

0 1 2 3 4 5 6

Page 22: COSC 3100 Transform and Conquer Instructor: Tanvir

• What is Heapsort’s worst-case complexity ?

Trns. & Conq.: Heapsort

Stage 1: Heap construction is in ?O(n)

Stage 2: Maximum deletions is in ?Let’s see…

Let C(n) be the # of comparisons needed in Stage 2

C(n) ≤ 2+ 2 + … … + 2 ≤ 2

Recall, height of heap’s tree is

C(n) ≤ 2 = 2(n-1)lg(n-1) ≤ 2nlgn So, C(n) є O(nlgn)

So, for two stages, O(n) + O(nlgn) = O(nlgn)

Page 23: COSC 3100 Transform and Conquer Instructor: Tanvir

• More careful analysis shows that Heapsort’s worst and best cases are in Θ(nlgn) like Mergesort

• Additionally Heapsort is in-place• Timing experiments on random

files show that Heapsort is slower than Quicksort, but can be competitive with Mergesort

• What sorting algorithm is used by Collections.sort(List<T> list) of Java?

Trns. & Conq.: Heapsort

Page 24: COSC 3100 Transform and Conquer Instructor: Tanvir

• Compute least common multiple of two nonnegative integers, lcm(m, n)

• Say m = 24, and n = 60• 24 = 2*2*2*3 and 60 = 2*2*3*5• Take product of all common factors of m and n, and all

factors of m not in n, and all factors of n not in m• lcm(24, 60) = (2*2*3)*2*5 = 120

Trns. & Conq.: Problem Reduction

Problem 1 Problem 2 Solution to Problem 2

reduction Alg. A

Solvable by Alg. ATo be solved

Is it a good algorithm?

Notice, 24*60 = (2*2*2*3) * (2*2*3*5) = (2*2*3)2*2*5

So, m*n = lcm(m, n) * gcd(m, n)

lcm(m, n) = !

Page 25: COSC 3100 Transform and Conquer Instructor: Tanvir

THANK YOU…