Docsity

Divide and Conquer - Algorithm and Complexity Analysis - Lecture Slides, Slides of Computer Science

Toggle sidebar
bg1
Divide-and-Conquer
"Divide et impera"
"Veni, vidi, vici"
-
Julius Caesar
100BC - 44BC
2
Divide-and-Conquer
Most widespread application of divide-and-conquer.
■Break up problem into two pieces of equal size.
■Solve two sub-problems independently by recursion.
■Combine two results in overall solution in linear time.
Consequence.
■Brute force / naïve solution: N2.
■Divide-and-conquer: N log N.
Familiar example.
■Mergesort.
This course.
■Counting inversions, closest pair of points, order statistics, fast
matrix multiplication, fast integer multiplication, FFT.
3
Why Does It Matter?
1000
Time to
solve a
problem
of size
10,000
100,000
million
10 million
1.3 seconds
22 minutes
15 days
41 years
41 millennia
920
3,600
14,000
41,000
1,000
Run time
(nanoseconds) 1.3 N3
second
Max size
problem
solved
in one
minute
hour
day
10 msec
1 second
1.7 minutes
2.8 hours
1.7 weeks
10,000
77,000
600,000
2.9 million
100
10 N2
0.4 msec
6 msec
78 msec
0.94 seconds
11 seconds
1 million
49 million
2.4 trillion
50 trillion
10+
47 N log2N
0.048 msec
0.48 msec
4.8 msec
48 msec
0.48 seconds
21 million
1.3 billion
76 trillion
1,800 trillion
10
48 N
N multiplied by 10,
time multiplied by
4
Orders of Magnitude
10-10
Meters Per
Second
10-8
10-6
10-4
10-2
1
102
1.2 in / decade
Imperial
Units
1 ft / year
3.4 in / day
1.2 ft / hour
2 ft / minute
2.2 mi / hour
220 mi / hour
Continental drift
Example
Hair growing
Glacier
Gastro-intestinal tract
Ant
Human walk
Propeller airplane
104
106
108
370 mi / min
620 mi / sec
62,000 mi / sec
Space shuttle
Earth in galactic orbit
1/3 speed of light
1
Seconds
102
103
104
105
106
107
108
109
1010
1 second
Equivalent
1.7 minutes
17 minutes
2.8 hours
1.1 days
1.6 weeks
3.8 months
3.1 years
3.1 decades
3.1 centuries
forever
1021 age of
universe
210 thousand
220 million
230 billion
. . .
10 10 seconds
Powers
of 2
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Divide and Conquer - Algorithm and Complexity Analysis - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Divide-and-Conquer

"Divide et impera""Veni, vidi, vici"

  • Julius Caesar

100BC - 44BC

Divide-and-Conquer

Most widespread application of divide-and-conquer.^ n

Break up problem into two pieces of equal size. n^

Solve two sub-problems independently by recursion. n^

Combine two results in overall solution in linear time. Consequence.^ n

Brute force / naïve solution: N

n^

Divide-and-conquer: N log N. Familiar example.^ n

Mergesort. This course.^ n

Counting inversions, closest pair of points, order statistics, fastmatrix multiplication, fast integer multiplication, FFT.

3

Why Does It Matter?

Time tosolve aproblemof size

10,000100,000million 10 million

1.3 seconds22 minutes

15 days41 years 41 millennia

Run time (nanoseconds)

1.3 N

3

second

Max sizeproblemsolved

in one

minutehour

day

10 msec1 second 1.7 minutes2.8 hours1.7 weeks

2.9 million

10 N^100

2

0.4 msec6 msec78 msec 0.94 seconds11 seconds

1 million49 million2.4 trillion50 trillion

47 N log

N 2

0.048 msec0.48 msec

4.8 msec48 msec 0.48 seconds

21 million1.3 billion76 trillion 1,800 trillion

48 N^10

N multiplied by 10,time multiplied by

Orders of Magnitude

Meters Per

Second

  • 10

10

10

2

1.2 in / decade

Imperial

Units 1 ft / year3.4 in / day 1.2 ft / hour2 ft / minute2.2 mi / hour220 mi / hour

Continental drift

Example Hair growing

Glacier

Gastro-intestinal tract

Ant

Human walk Propeller airplane

4 10

6 10

8

370 mi / min620 mi / sec 62,000 mi / sec

Space shuttle

Earth in galactic orbit

1/3 speed of light

Seconds

2 10

3 10

4 10

5 10

6 10

7 10

8 10

9 10

10

Equivalent1 second 1.7 minutes17 minutes

2.8 hours1.1 days1.6 weeks3.8 months3.1 years 3.1 decades3.1 centuries

forever

21

age of universe

thousand

million

billion

10 seconds

Powers

of 2

Docsity.com

5

A Useful Recurrence Relation

T(N) = worst case running time on input of size N.^ n

Note: O(1) is "standard" abuse of notation. Alternate informal form: T(N)

T(N/2) + O(N).

n^

Implicitly assumes N is a power of 2. n^

Implicitly assume T(N)

O(1) for sufficiently small N.

Solution.^ n

Any function satisfying above recurrence is

O(N log

2

N).

n^

Equivalently, O(log

b^

 N) for any b > 1.

(^

)^

^

(^

)

otherwise

if

T(

combine

half

right

solve

half left

solve

0

Q

P

I

R

Q

R

P

I

RQ

RP

I

N O N T N T

n

N

O

N

Analysis of Recurrence: Recursion Tree

Assuming N is a power of 2.

T(N)

T(N/2)

T(N/2)

T(N/4)

T(N/4)

T(N/4)

T(N/4)

T(2)

T(2)

T(2)

T(2)

T(2)

T(2)

T(2)

T(2)

cN

T(N / 2

k^ )

2(cN/2)4(cN/4) 2

k^

(cN / 2

k^ )

...... N/2 (2c)

otherwise

if

T(

cN

N T

N

N

log

N 2

cN log

N 2

7

Analysis of Recurrence

Proof by induction on N.^ n

Base case: N = 1. n^

Define n

1

n / 2

^

, n

2

n / 2

n^

Induction step: assume true for 1, 2,... , N

^

(^

)^

^

(^

)^

^

N

cN

N

T

cN

N

T

N

T

N

N

2

combine

half

right

solve

half left

solve

log

otherwise

if

T(

R

Q

R

P

I

RQ

RP

I

^

^

^

^

^

^

^

^

^

n

cn

cn

n

cn

cn

n

cn

cn

n

cn

n

cn

cn

n

cn

n

cn

cn

n T n T N T

2

2

2 2

2 2 2 2 2 1 2 2 2 1 2 1

2

1 log

log

log

log

log

log

log

^

 ^

^

^

^

log

log

2

2 2

log

2

2

n

n n

n

n

Counting Inversions

Web site tries to match your preferences with others on Internet.^ n

You rank N songs. n^

Web site consults database to find people with similar rankings. Closeness metric.^ n

My rank = { 1, 2,... , N }. n^

Your rank = { a

, a 1

,... , a 2

N^

n^

Number of inversions between two preference lists. n^

Songs i and j inverted if i < j, but a

ai^

j

You

Me

A

B

C

D

E

Songs

Inversion

Inversions{3, 2}, {4, 2} Docsity.com

13

Counting Inversions: Divide-and-Conquer

Divide-and-conquer.^ n

Divide: separate list into two pieces. n^

Conquer: recursively count inversions in each half. n^

Combine: count inversions where a

and ai^

are in different halves.j

5 blue-blue inversions

8 green-green inversions

9 blue-green inversions:{5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7}

O(1) 2T(N / 2) O(N

Counting Inversions: Divide-and-Conquer

Divide-and-conquer.^ n

Divide: separate list into two pieces. n^

Conquer: recursively count inversions in each half. n^

Combine: count inversions where a

and ai^

are in different halves.j

n^

Return sum of three quantities.

5 blue-blue inversions9 blue-green inversions:{5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7}

8 green-green inversions

O(1) 2T(N / 2) O(N

Total = 5 + 8 + 9 = 22.

O(1)

15

Counting Inversions: Divide-and-Conquer

Divide-and-conquer.^ n

Divide: separate list into two pieces. n^

Conquer: recursively count inversions in each half. n^

Combine: count inversions where a

and ai^

are in different halves.j

n^

Return sum of three quantities. 9 blue-green inversions:{5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7}

Total = 5 + 8 + 9 = 22.

5 blue-blue inversions

8 green-green inversions

Can we do this step insub-quadratic time?

O(1) 2T(N / 2) O(1)

9 blue-green inversions: 4 + 2 + 2 + 1 + 0 + 0

Counting Inversions: Good Combine

Combine: count inversions where a

and ai

are in different halves.j

n^

Key idea: easy if each half is sorted. n^

Sort each half. n^

Count inversions.

O(N log N)O(N)

^

(^

)^

^

(^

)^

log (

T(

log (

T(

2

N N O N N N O N T N T N

Docsity.com

17

13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0

Counting Inversions: Better Combine

Combine: count inversions where a

and ai

are in different halves.j

n^

Assume each half is pre-sorted. n^

Count inversions. n^

Merge two sorted halves into sorted whole.

O(N) O(N)

^

(^

)^

^

(^

)^

log (

T(

N N O N N O N T N T N T

Closest Pair

Given N points in the plane, find a pair that is closest together.^ n

For concreteness, we assume Euclidean distances. n^

Foundation of then-fledgling field of computational geometry. n^

Graphics, computer vision, geographic information systems,molecular modeling, air traffic control. Brute force solution.^ n

Check all pairs of points p and q. n^

(N

2 ) comparisons.

One dimensional version (points on a line).^ n

O(N log N) easy. Assumption to make presentation cleaner.^ n

No two points have same x coordinate.

19

Closest Pair

Algorithm.^ n

Divide: draw vertical line so that roughly N / 2 points on each side.

Closest Pair

Algorithm.^ n

Divide: draw vertical line so that roughly N / 2 points on each side. n^

Conquer: find closest pair in each side recursively.

Docsity.com

25

Closest Pair

Key step: find closest pair with one point in each side.^ n

Extra information: closest pair entirely in one side had distance

δ

n^

Observation: only need to consider points S within

δ

of line.

δ

= min(12, 21)

δ

Closest Pair

Key step: find closest pair with one point in each side.^ n

Extra information: closest pair entirely in one side had distance

δ

n^

Observation: only need to consider points S within

δ

of line.

n^

Sort points in strip S by their y coordinate.^ –

suffices to compute distances for pairs within constant numberof positions of each other in sorted list!

δ

= min(12, 21)

δ

1

2

(^43)

5

6 7

27

Closest Pair

S = list of points in the strip sorted by their y coordinate.Crucial fact: if p and q are in S, and ifd(p, q) <

δ

, then they are within 11

positions of each other in S.^ n

No two points lie in same box. n^

Two points at least 2 rows aparthave distance

δ

δ

27

29

30

31

28

26

25

δ

δ^

2 rows

δ^

δ^

39

Closest Pair

Compute separation line x = x

med

such that half the points

have x coordinate less than x

med

, and half are greater.

δ^1

= ClosestPair(left half) δ^2

= ClosestPair(right half) δ^

= min (

δ

1

,^

δ^2

Delete all points further than

δ

from separation line.

Sort remaining points in strip by y coordinate.Scan in y order, and compute distance between each pointand next 11 neighbors.If any of these distances is less than

δ

, update

δ

δ^

= ClosestPair (p

, p 1

2

,... , p

N^

O(N log N)

O(N) O(N) O(N)

O(N log N)2T(N / 2)

^

(^

)^

^

(^

)^

log (

T(

log (

T(

2

N N O N N N O N T N T N

Docsity.com

29

Closest Pair

Can we achieve O(N log N)?^ n

Yes. Don’t sort points in strip from scratch each time. n^

Each recursive call should return two lists: all points sorted by ycoordinate, and all points sorted by x coordinate. n^

Sorting is accomplished by merging two already sorted lists.

^

(^

)^

^

(^

)^

log (

T(

N N O N N O N T N T N T

Integer Arithmetic

Given two N-digit integers a and b, compute a + b.^ n

O(N) bit operations. Multiplication: given two N-digit integers a and b,compute ab.^ n

Brute force solution:

(N

2 ) bit operations.

Application.^ n

Cryptography.

31

To multiply two N-digit integers:^ n

Multiply four N/2-digit integers. n^

Add two N/2-digit integers, and shift to obtain result.

Divide-and-Conquer Multiplication: First Attempt

0

3

6

0

3

6

3

3

×

×

xz

xy

wz

wy

z

y

x

w

w x y^ z

(^

)^

T(

T(

2

shift

add,

calls

recursive

(^2) /

(^2) /

N N N N T N

z

y

x

w

ab

N

N

43

42 1

4

3

4

2

1

N is a power of 2

To multiply two N-digit integers:^ n

Add two N/2 digit integers. n^

Multiply three N/2-digit integers. n^

Subtract two N/2-digit integers, and shift to obtain result.

Karatsuba Multiplication

z

y x

w

r

xz

q

wy

p

q p r

xy

wz

0

3

6

0

3

6

0

3

6

3

3

×

×

q

q

p r

p

xz

xy

wz

wy

z

y

x

w

w x y z

Docsity.com

37

Matrix Multiplication: Strassen

Generalize to matrices.^ n

Divide: partition A and B into N/2 x N/2 blocks. n^

Compute: 14 N/2 x N/2 matrices via 10 matrix add/subtract. n^

Conquer: multiply 7 N/2 x N/2 recursively. n^

Combine: 7 products into 4 terms using 8 matrix add/subtract. Analysis.^ n

Assume N is a power of 2. n^

T(N) = # arithmetic operations.

(^

)^

T(

T(

(^81). 2

7

log

subtract

add,

2

calls

recursive

2

N O N N N N T N

4 4 3

(^4) 4 2 1

4

3

4

2

1

22

21

12

11

22

21

12

11

22

21

12

11

B

B

B

B

A

A

A

A

C

C

C

C

Beyond Strassen

Can you multiply two 2 x 2 matrices with only 7 scalar multiplications?

Yes! Strassen (1969).

Can you multiply two 2 x 2 matrix with only 6 scalar multiplications?

Impossible (Hopcroft and Kerr, 1971).

Two 3 x 3 matrices with only 21 scalar multiplications?

Also impossible.

Two 70 x 70 matrices with only 143,640 scalar multiplications?

Yes! (Pan, 1980).

Decimal wars.

December, 1979: O(N

January, 1980:

O(N

Coppersmith-Winograd (1987): O(N

(^

(^77). 2

21

log

3

N

O

N

(^

143640

log

70

N

O

N

(^

(^59). 2

6

log

2

N

O

N

(^

(^81). 2

7

log

2

N

O

N

39

Strassen in Practice?

Practical considerations.^ n

Stop recursion around N = 100. n^

Numerical stability. n^

Harder to parallelize. n^

Caching effects.

Order Statistics

Given N linearly ordered elements, find i

th

smallest element.

n^

Minimum if i = 1. n^

Maximum if i = N. n^

Median:^ –

i = (N+1) / 2 if N is odd – i = N/2 or i = N/2 + 1

n^

Easy to do with O(N) comparisons if i or N

- i is a constant.

n^

Easy to do in general with O(N log

N) comparisons by sorting. 2

Can we do in worst-case O(N) comparisons?^ n

Yes. (Blum, Floyd, Pratt, Rivest, Tarjan, 1973) n^

Cool and simple idea. Ahead of its time. Assumption to make presentation cleaner.^ n

All items have distinct values.

Docsity.com

41

Fast Select

Similar to quicksort, but throw away useless "half" at each iteration.^ n

Select i

th

smallest element from a

, a 1

,... , a 2

.N

x

FastPartition(N,

a

a

..., a

)N

k

rank(x)

if

(i

k)

return

x

else

if (i

< k)

b[]

all items

of a[]

less

than

x

return

FastSelect(i

th

k-1, b

, b 1

..., b

k-

else

if (i

> k)

c[]

all items

of a[]

greater than x

return

FastSelect((i-k)

th

N-k,

c

c

, ..., c 2

N-k

FastSelect (i

th

, N, a

, a 1

,... , a 2

N

x = partition elementis kth smallest

Fast Partition

FastPartition()

n^

Divide N elements into

N/

^

groups of 5 elements each, plus extra.

N = 54

43

Fast Partition

FastPartition()

n^

Divide N elements into

N/

^

groups of 5 elements each, plus extra.

n^

Brute force sort each of the 5-element groups.

Fast Partition

FastPartition()

n^

Divide N elements into

N/

^

groups of 5 elements each, plus extra.

n^

Brute force sort each of the 5-element groups. n^

Find x = "median of medians" using

FastSelect()

recursively.

Docsity.com

49

Fast Selection Analysis

Crux of proof: at least 25% of elements thrown away at each step.^ n

At least 1/2 of 5 element medians

x

-^

at least

N / 5

^

^

N / 10

^

medians

x

n^

At least 3

N / 10

^

elements

x.

n^

At least 3

N / 10

^

elements

x.

FastSelect()

called recursively with at most N - 3

N / 10

elements in last step

^

(^

)^

^

T(

sort

insertion

select

recursive

medians of

median

N

O

N

T

N O N N T N T N

≤^

43

4

2 1

4 4 4

3

4 4 4

2

1

43

42 1

Fast Selection Analysis

Analysis of recurrence.Claim: T(N)

20cN.

n^

Base case: N < 50. n^

Inductive step: assume true for 1, 2,... , N-1.

^

^

^

otherwise

if

T(

cN

N

N

T

N

T

N

c

N

^

^

^

^

^

^

(^

)

cN

cN

N c N c N c

cN

N

N

c

N

c

cN

N N T N T N T

For n

N / 10

^

N / 4.

51

Linear Time Median Finding Postmortem

Practical considerations.^ n

Constant (currently) too large to be useful. n^

Practical variant: choose random partition element.^ –

O(N) expected running time ala quicksort.

n^

Open problem: guaranteed O(N) with better constant. Quicksort.^ n

Worst case O(N log N) if always partition on median. n^

Justifies practical variants: median-of-3, median-of-5.

Docsity.com