Divide and Conquer Algorithms Divide-and-Conquer The most-well - - PowerPoint PPT Presentation

divide and conquer algorithms divide and conquer
SMART_READER_LITE
LIVE PREVIEW

Divide and Conquer Algorithms Divide-and-Conquer The most-well - - PowerPoint PPT Presentation

Divide and Conquer Algorithms Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger)


slide-1
SLIDE 1

Divide and Conquer Algorithms

slide-2
SLIDE 2

Divide-and-Conquer

The most-well known algorithm design strategy:

  • 1. Divide instance of problem into two or more

smaller instances

  • 2. Solve smaller instances recursively
  • 3. Obtain solution to original (larger) instance by

combining these solutions

slide-3
SLIDE 3

Divide-and-Conquer Technique (cont.)

subproblem 2

  • f size n/2

subproblem 1

  • f size n/2

a solution to subproblem 1 a solution to the original problem a solution to subproblem 2

a problem of size n (instance) It general leads to a recursive algorithm!

slide-4
SLIDE 4

4

Linear Search

Problem: Given a list of N values, determine whether a given value X occurs in the list.

1 2 3 4 5 6 7 8 17 31 9 73 55 12 19 7

For example, consider the problem of determining whether the value 55 occurs in: There is an obvious, correct algorithm: start at one end of the list, if the current element doesn't equal the search target, move to the next element, stopping when a match is found or the opposite end of the list is reached. Basic principle: divide the list into the current element and everything before (or after) it; if current isn't a match, search the other case

slide-5
SLIDE 5

Linear Search

5

algorithm LinearSearch takes number X, list number L, number Sz # Determines whether the value X occurs within the list L. # Pre: L must be initialized to hold exactly Sz values # # Walk from the upper end of the list toward the lower end, # looking for a match: while Sz > 0 AND L[Sz] != X Sz := Sz - 1 endwhile if Sz > 0 # See if we walked off the front of the list display true # if not, got a match else display false # if so, no match halt

slide-6
SLIDE 6

Binary Search

Repeatedly halving the size of the “search space” is the main idea behind the method of binary search. An item in a sorted array of length n can be located with just log2 n comparisons.

slide-7
SLIDE 7

Binary Search

Repeatedly halving the size of the “search space” is the main idea behind the method of binary search. An item in a sorted array of length n can be located with just log2 n comparisons. “Savings” is significant!

n log2(n) 100 7 1000 10 10000 13

slide-8
SLIDE 8

12 15 35 33 42 45 51 73 62 75 86 98

Binary search: target x = 70 v L: Mid: R:

1 6 12 1 2 3 4 5 6 7 8 9 10 11 12

v(Mid) <= x

So throw away the left half…

slide-9
SLIDE 9

12 15 35 33 42 45 51 73 62 75 86 98

v L: Mid: R:

6 9 12

x < v(Mid)

So throw away the right half… Binary search: target x = 70

1 2 3 4 5 6 7 8 9 10 11 12

slide-10
SLIDE 10

12 15 35 33 42 45 51 73 62 75 86 98

v L: Mid: R:

6 7 9

v(Mid) <= x So throw away the left half… Binary search: target x = 70

1 2 3 4 5 6 7 8 9 10 11 12

slide-11
SLIDE 11

12 15 35 33 42 45 51 73 62 75 86 98

v L: Mid: R:

7 8 9

v(Mid) <= x So throw away the left half… Binary search: target x = 70

1 2 3 4 5 6 7 8 9 10 11 12

slide-12
SLIDE 12

12 15 35 33 42 45 51 73 62 75 86 98

v L: Mid: R:

8 8 9

Done because

R-L = 1

Binary search: target x = 70

1 2 3 4 5 6 7 8 9 10 11 12

slide-13
SLIDE 13

function L = BinarySearch(a,x) # x is a row n-vector with x(1) < ... < x(n) # where x(1) <= a <= x(n) # L is the index such that x(L) <= a <= x(L+1) L = 1; R = length(x); # x(L) <= a <= x(R) while R-L > 1 mid = floor((L+R)/2); # Note that mid does not equal L or R. if a < x(mid) # x(L) <= a <= x(mid) R = mid; else # x(mid) <= a <== x(R) L = mid; end end

slide-14
SLIDE 14

Binary search is efficient, but how do we sort a vector in the first place so that we can use binary search?

  • Many different algorithms out there...
  • Let’s look at merge sort
  • An example of the “divide and conquer”

approach

slide-15
SLIDE 15

Merge sort: Motivation

What if those two helpers each had two sub-helpers? If I have two helpers, I’d…

  • Give each helper half the array

to sort

  • Then I get back the sorted

subarrays and merge them. And the sub-helpers each had two sub-sub-helpers? And…

slide-16
SLIDE 16

Subdivide the sorting task

J N R C P D F L A Q B K M G H E A Q B K M G H E J N R C P D F L

slide-17
SLIDE 17

Subdivide again

A Q B K M G H E J N R C P D F L M G H E A Q B K P D F L J N R C

slide-18
SLIDE 18

And again

M G H E A Q B K P D F L J N R C M G H E A Q B K P D F L J N R C

slide-19
SLIDE 19

And one last time

J N R C P D F L A Q B K M G H E

slide-20
SLIDE 20

Now merge

G M E H A Q B K D P F L J N C R J N R C P D F L A Q B K M G H E

slide-21
SLIDE 21

And merge again

H M E G K Q A B L P D F N R C J G M E H A Q B K D P F L J N C R

slide-22
SLIDE 22

And again

M Q H K E G A B P R L N F J C D H M E G K Q A B L P D F N R C J

slide-23
SLIDE 23

And one last time

M Q H K E G A B P R L N F J C D E F C D A B J K G H N P L M Q R

slide-24
SLIDE 24

Done!

E F C D A B J K G H N P L M Q R

slide-25
SLIDE 25

function y = mergeSort(x) # x is a vector. y is a vector # consisting of the values in x # sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL(x(1:m)); yR = mergeSortR(x(m+1:n)); y = merge(yL,yR); end

slide-26
SLIDE 26

The central sub-problem is the merging of two sorted arrays into one single sorted array 12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 55 75 65

slide-27
SLIDE 27

12 33 45 35 15 42 65 55 75 x: y: z: 1 1 1 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

slide-28
SLIDE 28

12 33 45 35 15 42 65 55 75 12 x: y: z: 1 1 1 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

slide-29
SLIDE 29

12 33 45 35 15 42 65 55 75 12 x: y: z: 2 1 2 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

slide-30
SLIDE 30

12 33 45 35 15 42 65 55 75 12 15 x: y: z: 2 1 2 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) NO

slide-31
SLIDE 31

12 33 45 35 15 42 65 55 75 12 15 x: y: z: 2 2 3 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

slide-32
SLIDE 32

12 33 45 35 15 42 65 55 75 12 15 33 x: y: z: 2 2 3 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

slide-33
SLIDE 33

12 33 45 35 15 42 65 55 75 12 15 33 x: y: z: 3 2 4 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

slide-34
SLIDE 34

12 33 45 35 15 42 65 55 75 12 15 35 33 x: y: z: 3 2 4 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

slide-35
SLIDE 35

12 33 45 35 15 42 65 55 75 12 15 35 33 x: y: z: 4 2 5 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

slide-36
SLIDE 36

12 33 45 35 15 42 65 55 75 12 15 35 33 42 x: y: z: 4 2 5 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) NO

slide-37
SLIDE 37

12 33 45 35 15 42 65 55 75 12 15 35 33 42 x: y: z: 4 3 5 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

slide-38
SLIDE 38

12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 x: y: z: 4 3 5 ix: iy: iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

slide-39
SLIDE 39

12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 x: y: z: 5 3 6 ix: iy: iz:

Merge ix > 4

slide-40
SLIDE 40

12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 55 x: y: z: 5 3 6 ix: iy: iz:

Merge ix > 4: take y(iy)

slide-41
SLIDE 41

12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 55 x: y: z: 5 4 8 ix: iy: iz:

Merge iy <= 5

slide-42
SLIDE 42

12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 55 65 x: y: z: 5 4 8 ix: iy: iz:

Merge iy <= 5

slide-43
SLIDE 43

12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 55 65 x: y: z: 5 5 9 ix: iy: iz:

Merge iy <= 5

slide-44
SLIDE 44

12 33 45 35 15 42 65 55 75 12 15 35 33 42 45 55 75 65 x: y: z: 5 5 9 ix: iy: iz:

Merge iy <= 5

slide-45
SLIDE 45

function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1,nx+ny); ix = 1; iy = 1; iz = 1;

slide-46
SLIDE 46

function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; while ix<=nx && iy<=ny end # Deal with remaining values in x or y

slide-47
SLIDE 47

function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; while ix<=nx && iy<=ny if x(ix) <= y(iy) z(iz)= x(ix); ix=ix+1; iz=iz+1; else z(iz)= y(iy); iy=iy+1; iz=iz+1; end end # Deal with remaining values in x or y

slide-48
SLIDE 48

function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; while ix<=nx && iy<=ny if x(ix) <= y(iy) z(iz)= x(ix); ix=ix+1; iz=iz+1; else z(iz)= y(iy); iy=iy+1; iz=iz+1; end end while ix<=nx # copy remaining x-values z(iz)= x(ix); ix=ix+1; iz=iz+1; end while iy<=ny # copy remaining y-values z(iz)= y(iy); iy=iy+1; iz=iz+1; end

slide-49
SLIDE 49

function y = mergeSort(x) # x is a vector. y is a vector # consisting of the values in x # sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL(x(1:m)); yR = mergeSortR(x(m+1:n)); y = merge(yL,yR); end

slide-50
SLIDE 50

function y = mergeSortL(x) # x is a vector. y is a vector # consisting of the values in x # sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL_L(x(1:m)); yR = mergeSortL_R(x(m+1:n)); y = merge(yL,yR); end

slide-51
SLIDE 51

function y = mergeSortL_L(x) # x is a vector. y is a vector # consisting of the values in x # sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL_L_L(x(1:m)); yR = mergeSortL_L_R(x(m+1:n)); y = merge(yL,yR); end

There should be just one mergeSort function!

slide-52
SLIDE 52

function y = mergeSort(x) # x is a vector. y is a vector # consisting of the values in x # sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSort(x(1:m)); yR = mergeSort(x(m+1:n)); y = merge(yL,yR); end

slide-53
SLIDE 53

function y=mergeSort(x) n=length(x); if n==1 y=x; else m=floor(n/2); yL=mergeSort(x(1:m)); yR=mergeSort(x(m+1:n)); y=merge(yL,yR); end

slide-54
SLIDE 54

Divide-and-Conquer Examples

  • Sorting: mergesort and quicksort
  • Binary tree traversals
  • Binary search
  • Multiplication of large integers
  • Matrix multiplication: Strassen’s algorithm
  • Closest-pair algorithm