Professional Documents
Culture Documents
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/
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
Divide-and-Conquer
Divide the problem into a number of sub-problems
Similar sub-problems of smaller size
T(n) =
(1)
if n c
aT(n/b) + D(n) + C(n) otherwise
Conquer
Sort the subsequences recursively using merge sort
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
MERGE-SORT(A, p, q)
Conquer
MERGE-SORT(A, q + 1, r)
Conquer
MERGE(A, p, q, r)
Combine
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
10
11
q=6
6
10
11
10
2
10
q=9
11
6
11
14
10
11
7
10
11
10
2
10
11
6
11
15
Merging
p
1
q
2
2 4
16
Merging
Idea for merging:
Two piles of sorted cards
p
1
q
2
2 4
17
18
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
24
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
c
2T(n/2) + cn
if n = 1
if n > 1
26
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.
Selection sort?
YES, it takes linear time for exchanges
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
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
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
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