divide and conquer l.
Download
Skip this Video
Download Presentation
Divide And Conquer

Loading in 2 Seconds...

play fullscreen
1 / 49

Divide And Conquer - PowerPoint PPT Presentation


  • 167 Views
  • Uploaded on

Divide And Conquer. Distinguish between small and large instances. Small instances solved differently from large ones. Small And Large Instance. Small instance. Sort a list that has n <= 10 elements. Find the minimum of n <= 2 elements. Large instance.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Divide And Conquer' - zlhna


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
divide and conquer

Divide And Conquer

Distinguish between small and large instances.

Small instances solved differently from large ones.

small and large instance

Small And Large Instance

Small instance.

Sort a list that has n <= 10 elements.

Find the minimum of n <= 2 elements.

Large instance.

Sort a list that has n > 10 elements.

Find the minimum of n > 2 elements.

solving a small instance

Solving A Small Instance

A small instance is solved using some direct/simple strategy.

Sort a list that has n <= 10 elements.

Use count, insertion, bubble, or selection sort.

Find the minimum of n <= 2 elements.

When n = 0, there is no minimum element.

When n = 1, the single element is the minimum.

When n = 2, compare the two elements and determine which is smaller.

solving a large instance

Solving A Large Instance

A large instance is solved as follows:

Divide the large instance into k >= 2 smaller instances.

Solve the smaller instances somehow.

Combine the results of the smaller instances to obtain the result for the original large instance.

sort a large list

Sort A Large List

Sort a list that has n > 10 elements.

Sort 15 elements by dividing them into 2 smaller lists.

One list has 7 elements and the other has 8.

Sort these two lists using the method for small lists.

Merge the two sorted lists into a single sorted list.

find the min of a large list

Find The Min Of A Large List

Find the minimum of 20 elements.

Divide into two groups of 10 elements each.

Find the minimum element in each group somehow.

Compare the minimums of each group to determine the overall minimum.

recursion in divide and conquer

Recursion In Divide And Conquer

Often the smaller instances that result from the divide step are instances of the original problem (true for our sort and min problems). In this case,

If the new instance is a small instance, it is solved using the method for small instances.

If the new instance is a large instance, it is solved using the divide-and-conquer method recursively.

Generally, performance is best when the smaller instances that result from the divide step are of approximately the same size.

recursive find min

Recursive Find Min

Find the minimum of 20 elements.

Divide into two groups of 10 elements each.

Find the minimum element in each group recursively. The recursion terminates when thenumber of elements is<= 2. At this time the minimum is found using the method for small instances.

Compare the minimums of the two groups to determine the overall minimum.

divide and conquer sorting

Divide-And-Conquer Sorting

Small instance.

n <= 1 elements.

n <= 10 elements.

We’ll use n <= 1 for now.

Large instance.

Divide into k >= 2 smaller instances.

k = 2, 3, 4, … ?

What does each smaller instance look like?

Sort smaller instances recursively.

How do you combine the sorted smaller instances?

insertion sort

a[0]

a[n-1]

a[n-2]

Insertion Sort
  • k = 2
  • First n - 1 elements (a[0:n-2]) define one of the smaller instances; last element (a[n-1]) defines the second smaller instance.
  • a[0:n-2] is sorted recursively.
  • a[n-1] is a small instance.
insertion sort11

a[0]

a[n-1]

a[n-2]

Insertion Sort
  • Combining is done by inserting a[n-1] into the sorted a[0:n-2] .
  • Complexity is O(n2).
  • Usually implemented nonrecursively.
divide and conquer12
Divide And Conquer
  • Divide-and-conquer algorithms generally have best complexity when a large instance is divided into smaller instances of approximately the same size.
  • When k = 2 and n = 24, divide into two smaller instances of size 12 each.
  • When k = 2 and n = 25, divide into two smaller instances of size 13 and 12, respectively.
merge sort
Merge Sort
  • k = 2
  • First ceil(n/2) elements define one of the smaller instances; remaining floor(n/2) elements define the second smaller instance.
  • Each of the two smaller instances is sorted recursively.
  • The sorted smaller instances are combined using a process called merge.
  • Complexity is O(nlog n).
  • Usually implemented nonrecursively.
merge two sorted lists
Merge Two Sorted Lists
  • A = (2, 5, 6)

B = (1, 3, 8, 9, 10)

C = ()

  • Compare smallest elements of A and B andmerge smaller into C.
  • A = (2, 5, 6)

B = (3, 8, 9, 10)

C = (1)

merge two sorted lists15
Merge Two Sorted Lists
  • A = (5, 6)

B = (3, 8, 9, 10)

C = (1, 2)

  • A = (5, 6)

B = (8, 9, 10)

C = (1, 2, 3)

  • A = (6)

B = (8, 9, 10)

C = (1, 2, 3, 5)

merge two sorted lists16
Merge Two Sorted Lists
  • A = ()

B = (8, 9, 10)

C = (1, 2, 3, 5, 6)

  • When one of A and B becomes empty, appendthe other list to C.
  • O(1) time needed to move an element into C.
  • Total time is O(n + m), where n and m are, respectively, the number of elements initially in A and B.
merge sort17
Merge Sort

[8, 3, 13, 6, 2, 14, 5, 9, 10, 1, 7, 12, 4]

[8, 3, 13, 6, 2, 14, 5]

[9, 10, 1, 7, 12, 4]

[8, 3, 13, 6]

[2, 14, 5]

[9, 10, 1]

[7, 12, 4]

[8, 3]

[13, 6]

[2, 14]

[5]

[9, 10]

[1]

[7, 12]

[4]

[8]

[3]

[13]

[6]

[2]

[14]

[9]

[10]

[7]

[12]

merge sort18
Merge Sort

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13,14]

[2, 3, 5, 6, 8, 13, 14]

[1, 4, 7, 9, 10,12]

[3, 6, 8, 13]

[2, 5, 14]

[1, 9, 10]

[4, 7, 12]

[3, 8]

[6, 13]

[2, 14]

[5]

[9, 10]

[1]

[7, 12]

[4]

[8]

[3]

[13]

[6]

[2]

[14]

[9]

[10]

[7]

[12]

time complexity
Time Complexity
  • Let t(n) be the time required to sort n elements.
  • t(0) = t(1) = c, where c is a constant.
  • When n > 1,

t(n) = t(ceil(n/2)) + t(floor(n/2)) + dn,

where d is a constant.

  • To solve the recurrence, assume n is a power of 2 anduserepeated substitution.
  • t(n) = O(n log n).
nonrecursive version
Nonrecursive Version
  • Eliminate downward pass.
  • Start with sorted lists of size 1 and do pairwise merging of these sorted lists as in the upward pass.
nonrecursive merge sort

[8]

[3]

[13]

[6]

[2]

[14]

[5]

[9]

[10]

[1]

[7]

[12]

[4]

[3, 8]

[6, 13]

[2, 14]

[5, 9]

[1, 10]

[7, 12]

[4]

[4]

[3, 6, 8, 13]

[2, 5, 9, 14]

[1, 7, 10, 12]

[1, 4, 7, 10, 12]

[2, 3, 5, 6, 8, 9, 13, 14]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14]

Nonrecursive Merge Sort
complexity
Complexity
  • Sorted segment size is 1, 2, 4, 8, …
  • Number of merge passes is ceil(log2n).
  • Each merge pass takes O(n) time.
  • Total time is O(n log n).
  • Need O(n) additional space for the merge.
  • Merge sort is slower than insertion sort when n <= 15 (approximately). So define a small instance to be an instance with n <= 15.
  • Sort small instances using insertion sort.
  • Start with segment size = 15.
natural merge sort
Natural Merge Sort
  • Initial sorted segments are the naturally ocurring sorted segments in the input.
  • Input = [8, 9, 10, 2, 5, 7, 9, 11, 13, 15, 6, 12, 14].
  • Initial segments are:

[8, 9, 10] [2, 5, 7, 9, 11, 13, 15] [6, 12, 14]

  • 2 (instead of 4) merge passes suffice.
  • Segment boundaries have a[i] > a[i+1].
quick sort
Quick Sort
  • Small instance has n <= 1. Every small instance is a sorted instance.
  • To sort a large instance, select a pivot element from out of the n elements.
  • Partition the n elements into 3 groups left, middle and right.
  • The middle group contains only the pivot element.
  • All elements in the left group are <= pivot.
  • All elements in the rightgroup are >= pivot.
  • Sort left and right groups recursively.
  • Answer is sorted left group, followed by middle group followed by sorted right group.
example

6

2

8

5

11

10

4

1

9

7

3

2

5

4

1

3

6

7

9

10

11

8

Example

Use 6 as the pivot.

Sort left and right groups recursively.

choice of pivot
Choice Of Pivot
  • Pivot is leftmost element in list that is to be sorted.
    • When sorting a[6:20], use a[6] as the pivot.
  • Randomly select one of the elements to be sorted as the pivot.
    • When sorting a[6:20], generate a random number r in the range [6, 20]. Use a[r] as the pivot.
choice of pivot27
Choice Of Pivot
  • Median-of-Three rule. From the leftmost, middle, and rightmost elements of the list to be sorted, select the one with median key as the pivot.
    • When sorting a[6:20], examine a[6], a[13] ((6+20)/2),and a[20]. Select the element with median (i.e., middle) key.
    • If a[6].key = 30, a[13].key = 2,and a[20].key = 10, a[20] becomes the pivot.
    • If a[6].key = 3, a[13].key = 2,and a[20].key = 10, a[6] becomes the pivot.
choice of pivot28

swap

pivot

Choice Of Pivot
    • If a[6].key = 30, a[13].key = 25,and a[20].key = 10, a[13] becomes the pivot.
  • When the pivot is picked at random or when the median-of-three rule is used, we can use the quick sort code of the text provided we first swap the leftmost element and the chosen pivot.
partitioning example using additional array

a

6

2

8

5

11

10

4

1

9

7

3

b

2

5

4

1

3

6

7

9

10

11

8

Partitioning Example Using Additional Array

Sort left and right groups recursively.

in place partitioning example

a

6

6

2

8

8

5

11

10

4

1

9

7

3

3

a

6

6

2

3

5

11

11

10

4

1

1

9

7

8

a

6

6

2

3

5

1

10

10

4

4

11

9

7

8

a

6

6

2

3

5

1

4

4

10

10

11

9

7

8

a

4

2

3

5

1

4

6

6

10

11

9

7

8

In-Place Partitioning Example

bigElementis not to left ofsmallElement, terminate process. Swap pivotand smallElement.

complexity31
Complexity
  • O(n) time to partition an array of n elements.
  • Let t(n) be the time needed to sort n elements.
  • t(0) = t(1) = c, where c is a constant.
  • When t > 1,

t(n) = t(|left|) + t(|right|) + dn,

where d is a constant.

  • t(n) is maximum wheneither |left| = 0 or |right| = 0 following each partitioning.
complexity32
Complexity
  • This happens, for example, when the pivot is always the smallest element.
  • For the worst-case time,

t(n) = t(n-1) + dn, n > 1

  • Use repeated substitution to get t(n) = O(n2).
  • The best case arises when |left| and |right| are equal (or differ by 1) following each partitioning.
  • For the best case, the recurrence is the same as for merge sort.
complexity of quick sort
Complexity Of Quick Sort
  • So the best-case complexity is O(n log n).
  • Average complexity is also O(n log n).
  • To help get partitions with almost equal size, change in-place swap rule to:
    • Find leftmost element (bigElement) >= pivot.
    • Find rightmost element (smallElement) <= pivot.
    • Swap bigElement and smallElement provided bigElement is to the left of smallElement.
  • O(n) space is needed for the recursion stack. May be reduced to O(log n) (see Exercise 19.22).
complexity of quick sort34
Complexity Of Quick Sort
  • To improve performance, define a small instance to be one with n <= 15 (say) and sort small instances using insertion sort.
slide35

Rank of an element is its position in ascending key order.

[2,6,7,8,10,15,18,20,25,30,35,40]

rank(2) = 0

rank(15) = 5

rank(20) = 7

Rank

selection problem
Selection Problem
  • Given n unsorted elements, determine the k’th smallest element. That is, determine the element whose rank is k-1.
  • Applications
    • Median score on a test.
      • k = ceil(n/2).
    • Median salary of Computer Scientists.
    • Identify people whose salary is in the bottom 10%. First find salary at the 10% rank.
selection by sorting
Selection By Sorting
  • Sort the n elements.
  • Pick up the element with desired rank.
  • O(n log n) time.
d c selection example

3

2

8

0

11

10

1

2

9

7

1

a

a

1

2

1

0

2

4

3

10

11

9

7

8

D&C Selection Example

Find kth element of:

Use 3 as the pivot and partition.

rank(pivot) = 5. So pivot is the 6’th smallest element.

d c selection example39

a

1

2

1

0

2

4

3

10

11

9

7

8

D&C Selection Example
  • If k = 6 (k-1 = rank(pivot)), pivot is the element we seek.
  • If k < 6 (k-1 < rank(pivot)), find k’th smallest element in left partition.
  • If k > 6 (k-1 > rank(pivot)), find (k-rank(pivot)-1)’th smallest element in right partition.
time complexity40
Time Complexity
  • Worst case arises when the partition to be searched always has all but the pivot.
    • O(n2)
  • Expected performance is O(n).
  • Worst case becomes O(n) when the pivot is chosen carefully.
    • Partition into n/9 groupswith 9 elements each (last group may have a few more)
    • Find the median element in each group.
    • pivot is the median of the group medians.
    • This median is found using select recursively.
our definition of a chessboard

1x1

2x2

4x4

8x8

Our Definition Of A Chessboard

A chessboard is an n x n grid, where n is a power of 2.

slide43

1x1

2x2

4x4

8x8

A Defective Chessboard

A defective chessboard is a chessboard that has one unavailable (defective) position.

a triomino
A Triomino

A triominois an L shaped object thatcan cover three squares of a chessboard.

A triomino has four orientations.

tiling a defective chessboard45

1x1

2x2

4x4

8x8

Tiling A Defective Chessboard

Place (n2 - 1)/3 triominoes on an n x n defective chessboard so that all n2 - 1nondefective positions are covered.

tiling a defective chessboard46
Tiling A Defective Chessboard

Divide into four smaller chessboards. 4 x 4

One of these is a defective 4 x 4 chessboard.

tiling a defective chessboard47
Tiling A Defective Chessboard

Make the other three 4 x 4 chessboardsdefective by placing a triomino at their common corner.

Recursively tile the four defective 4 x 4 chessboards.

complexity48
Complexity
  • Let n = 2k.
  • Let t(k) be the time taken to tile a 2k x 2kdefective chessboard.
  • t(0) = d, where d is a constant.
  • t(k) = 4t(k-1) + c, when k > 0. Here c is a constant.
  • Recurrence equation for t().
substitution method
Substitution Method

t(k) = 4t(k-1) + c

= 4[4t(k-2) + c] + c

= 42 t(k-2) + 4c + c

= 42[4t(k-3) + c] + 4c + c

= 43 t(k-3) + 42c + 4c + c

= …

= 4k t(0) + 4k-1c + 4k-2c + ... + 42c + 4c + c

= 4k d + 4k-1c + 4k-2c + ... + 42c + 4c + c

= Theta(4k)

= Theta(number of triominoes placed)