You are on page 1of 34

Design and Analysis of

Algorithms
Divide and Conquer

Tromino Puzzle
To play the 8-by-8 Tromino Puzzle, place the square tile on one of the
64 square grid cells, then move the L-shaped trominoes into position
one at a time. Continue placing trominoes until no empty cells remain.
https://www3.amherst.edu/~nstarr/trom/puzzle-8by8/

Fake Coin Problem


You are given n coins. They all look identical. They
should all be the same weight, too -- but one is a fake,
made of a lighter metal.
Your neighbor has an old-fashioned balance scale that
enables you to compare any two sets of coins.
Sadly, you aren't on speaking terms with the neighbor, so
he charges you each time you weigh anything.
Task: Design an algorithm to find the fake coin in the
fewest number of weighings.

Fake Coin Problem


You may well have realized that you can divide
the pile in half, weigh the halves, and narrow
your focus to the pile that is lighter (like binary
search)
But we can do better than a factor of 2
Suppose we divide the coins into three piles, where at least
two of them contain the same number of coins. After weighing
the equal-sized piles, we can eliminate ~2/3 of the coins!

Fake Coin Problem

If n mod 3 = 0, we can divide the coins into three piles of exactly n/3 apiece.
If n mod 3 = 1, then n = 3k + 1 for some k. We can divide the coins into three
piles of k, k, and k+1. It will simplify our algorithm, though, if we split them
into three piles of k+1, k+1, and k-1.
If n mod 3 = 2, then n = 3k + 2 for some k. We can divide the coins into three
piles of k+1, k+1, and k.
INPUT : integer n
if n = 1 then the coin is fake
else
divide the coins into piles of A = ceiling(n/3), B = ceiling(n/3),
and C = n-2*ceiling(n/3)
weigh A and B
if the scale balances then
How many weighings
iterate with C
does this require?
else
iterate with the lighter of A and B
5

Variations of Fake Coin Problem


Lighter or heavier? You have n > 2 identical-looking
coins and a two-pan balance scale with no weights. One
of the coins is a fake, but you do not know whether it is
lighter or heavier than the genuine coins, which all weigh
the same
Design an algorithm to determine whether the fake coin is lighter
or heavier than the others.
Design an algorithm to find the fake coin in minimum weighings

You are given 10 bags of gold coins. Nine bags contain


coins that each weigh 10 grams. One bag contains all
false coins that weigh one gram less. You must identify
this bag in just one weighing. You have a digital balance
that reports the weight of what is placed on it.
6

Divide-and-Conquer
Divide the problem into a number of sub-problems
Similar sub-problems of smaller size

Conquer the sub-problems


Solve the sub-problems recursively
Sub-problem size small enough solve the problems in
straightforward manner

Combine the solutions of the sub-problems


Obtain the solution for the original problem

Variation of Divide and Conquer - Decrease and conquer


7

Analyzing Divide-and Conquer Algorithms


The recurrence is based on the three steps of
the paradigm:
T(n) running time on a problem of size n
Divide the problem into a subproblems, each of size
n/b: takes D(n)
Conquer (solve) the subproblems aT(n/b)
Combine the solutions C(n)

T(n) =

(1)
if n c
aT(n/b) + D(n) + C(n) otherwise

Divide and Conquer Examples


Sorting: mergesort and quicksort
Searching and selection
Multiplication (Multiplying large integers,
Matrices - Strassens algorithm)
Counting inversions
Closest Pair of point in 2 Dimensions

Merge Sort Approach


To sort an array A[p . . r]:
Divide
Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each

Conquer
Sort the subsequences recursively using merge sort

When the size of the sequences is 1 there is nothing


more to do

Combine
Merge the two sorted subsequences
10

Merge Sort
p

Alg.: MERGE-SORT(A, p, r)
if p < r
then q (p + r)/2

q
2

5 2

Check for base case


Divide

MERGE-SORT(A, p, q)

Conquer

MERGE-SORT(A, q + 1, r)

Conquer

MERGE(A, p, q, r)

Combine

Initial call: MERGE-SORT(A, 1, n)


11

Example n Power of 2
1

Divide
1

5 2

5 2

5 2

q=4

1 3

1 3

12

Example n Power of 2
1

Conquer
and
Merge

1 2

2 4

2 5

1 2

1 3

13

Example n Not a Power of 2


Divide
q=3

10

11

q=6

6
10

11

10

2
10

q=9

11

6
11

14

Example n Not a Power of 2


Conquer
and
Merge

10

11

7
10

11

10

2
10

11

6
11

15

Merging
p
1

q
2

2 4

Input: Array A and indices p, q, r such that


pq<r
Subarrays A[p . . q] and A[q + 1 . . r] are sorted

Output: One single sorted subarray A[p . . r]

16

Merging
Idea for merging:
Two piles of sorted cards

p
1

q
2

2 4

Choose the smaller of the two top cards


Remove it and place it in the output pile

Repeat the process until one pile is empty


Take the remaining input pile and place it face-down

onto the output pile


A1 A[p, q]
A[p, r]
A2 A[q+1, r]

17

Example: MERGE(A, 9, 12, 16)


p

18

Example: MERGE(A, 9, 12, 16)

19

Example (cont.)

20

Example (cont.)

21

Example (cont.)

Done!

22

Merge - Pseudocode
Alg.: MERGE(A, p, q, r)

p
1

q
2

2 4

1. Compute n1 and n2
2. Copy the first n1 elements into
n2
n1
L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]
p
q
3. L[n1 + 1] ; R[n2 + 1]
4. i 1; j 1
2 4 5 7
L
q+1
r
5. for k p to r
R
1 2 3 6
6.
do if L[ i ] R[ j ]
7.
then A[k] L[ i ]
8.
i i + 1
9.
else A[k] R[ j ]
10.
jj+1
23

Running Time of Merge


Initialization (copying into temporary arrays):
(n1 + n2) = (n)

Adding the elements to the final array:


- n iterations, each taking constant time (n)

Total time for Merge:


(n)

24

MERGE-SORT Running Time


Divide:
compute q as the average of p and r: D(n) = (1)

Conquer:
recursively solve 2 subproblems, each of size n/2
2T (n/2)

Combine:
MERGE on an n-element subarray takes (n) time
C(n) = (n)

T(n) =

(1)
if n =1
2T(n/2) + (n) if n > 1
25

Solve the Recurrence


T(n) =

c
2T(n/2) + cn

if n = 1
if n > 1

Use Masters Theorem:


Compare n with f(n) = cn
Case 2: T(n) = (nlgn)

26

Merge Sort - Discussion


Running time insensitive of the input
Advantages:
Guaranteed to run in (nlgn)

Disadvantage
Requires extra space N

27

Sorting Challenge 1
Problem: Sort a file of huge records with tiny
keys
Example application: Reorganize your MP-3 files
Which method to use?
A.
B.
C.
D.
E.

merge sort, guaranteed to run in time NlgN


selection sort
bubble sort
a custom algorithm for huge records/tiny keys
insertion sort
28

Sorting Files with Huge Records and


Small Keys
Insertion sort or bubble sort?
NO, too many exchanges

Selection sort?
YES, it takes linear time for exchanges

Merge sort or custom method?


Probably not: selection sort simpler, does less swaps
29

Sorting Challenge 2
Problem: Sort a huge randomly-ordered file of
small records
Application: Process transaction record for a
phone company
Which sorting method to use?
A.
B.
C.
D.

Bubble sort
Selection sort
Mergesort guaranteed to run in time NlgN
Insertion sort
30

Sorting Huge, Randomly - Ordered Files


Selection sort?
NO, always takes quadratic time

Bubble sort?
NO, quadratic time for randomly-ordered keys

Insertion sort?
NO, quadratic time for randomly-ordered keys

Mergesort?
YES, it is designed for this problem
31

Sorting Challenge 3
Problem: sort a file that is already almost in
order
Applications:
Re-sort a huge database after a few changes
Doublecheck that someone else sorted a file

Which sorting method to use?


A.
B.
C.
D.
E.

Mergesort, guaranteed to run in time NlgN


Selection sort
Bubble sort
A custom algorithm for almost in-order files
Insertion sort
32

Sorting Files That are Almost in Order


Selection sort?
NO, always takes quadratic time

Bubble sort?
NO, bad for some definitions of almost in order
Ex: B C D E F G H I J K L M N O P Q R S T U V W X Y Z A

Insertion sort?
YES, takes linear time for most definitions of almost
in order

Mergesort or custom method?


Probably not: insertion sort simpler and faster
33

H/W Exercise
What is the time complexity of merging two
sorted sequence of size m and n?
What will be the time complexity of merging K
sorted lists of n elements?
Can we do in place merge? If yes, what will be
the time complexity?
Find the kth element in the merge sequence in
O(log(max(m,n))?

34

You might also like