Top Banner
Instructor: Shengyu Zhang 1
53

Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

Dec 17, 2015

Download

Documents

Eugene Sharp
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

1

Instructor: Shengyu Zhang

Page 2: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

2

Example 1: Merge sort

Page 3: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

3

Starting example

Sorting: We have a list of numbers . We want to sort them in the increasing order.

Page 4: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

4

An algorithm: merge sort

Merge sort: Cut it into two halves with equal size.

Suppose divides for simplicity. Suppose the two halves are sorted: Merge them.

Use two pointers, one for each half, to scan them, during which course do the appropriate merge.

How to sort each of the two halves? Recursively.

Page 5: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

5

Complexity?

Suppose this algorithm takes time for an input with numbers.

Thus each of the two halves takes time. The merging?

Scanning elements, an time operation needed for each.

Total amount of time: .

Page 6: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

6

How to solve/bound this recurrence relation?

Page 7: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

7

A general method for designing algorithm: Divide and conquer Breaking the problem into subproblems

that are themselves smaller instances of the same type of problem

Recursively solving these subproblems

Appropriately combining their answers

Page 8: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

8

Complexity

Running time on an input of size : Break problem into subproblems, each of

the same size . In general, is not necessarily equal to .

Time to recursively solve each subproblem: Time for breaking problem (into

subproblems) and combining the answers:

Page 9: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

9

Master theorem

, , and are all constants. Then

Proof in textbook. Not required. But you need to know how to apply it.

Page 10: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

10

Merge sort: . . So . By the master theorem: .

Page 11: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

11

Example 2: Selection

Page 12: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

12

Selection

Problem: Given a list of numbers, find the -th smallest.

We can sort the list, which needs . Can we do better, say, linear time? After all, sorting gives a lot more information

than we requested. Not always a waste: consider dynamic

programming where solutions to subproblems are also produced.

Page 13: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

13

Idea of divide and conquer

Divide the numbers into 3 parts

, , Depending on the size of each part, we know

which part the -th element lies in.

Then search in that part.

Question: Which to choose?

Page 14: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

14

Pivot

Suppose we use a number in the given list as a pivot.

As said, we divide the list into three parts. : Those numbers smaller than : Those numbers equal to : Those numbers larger than

Page 15: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

15

After the partition

The division is simple: just scan the list and put elements into the corresponding part. time.

To select the -th smallest value, it becomes

Complexity?

Page 16: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

16

Divide and conquer

Note: though there are two subproblems (of sizes and ), we need to solve only one of them. Compare: in quicksort, we need to sort both

substrings! Complexity:

A new issue: and are not determined Depends on the pivot.

Page 17: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

17

If the pivot is the median:

Thus finally , better than by sorting.

Page 18: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

18

If the pivot is at one end (say, the smallest)

What’s the complexity?

Complexity:

Page 19: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

19

The similarity to quicksort tells us: a random pivot performs well It’s away from either end by with const. prob.

To be more precise, it’s in with probability . And in this case, the recursion becomes

Page 20: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

20

Recall:

So

Page 21: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

21

Thus we can use the following simple strategy: Pick a random pivot, do the recursion

Each random pivot falls in w/ prob. . . It is enough to get good pivots to make the

problem size to drop to 1. Thus .

Page 22: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

22

Example 3: Matrix multiplication

Page 23: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

23

Matrix multiplication

Recall: the product of two matrices is another matrix.

Question: how fast can we multiply two matrices?

Recall: : the entry in the matrix . Similar for ,

Page 24: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

24

This takes multiplications (of numbers).

For a long time, people thought this was the best possible.

Until Straussen came up with the following.

Page 25: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

25

If we break the matrix into blocks

Then the product is just block multiplication

8 matrix multiplications of dimension Plus additions.

Page 26: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

26

Thus the recurrence is

This gives exactly the same , not interesting.

Page 27: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

27

However, Straussen observed that we can actually use only 7 (instead of 8) multiplications of matrices with dimension .

Page 28: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

28

God knows how he came up with it. And here is how:

where

Page 29: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

29

Thus the recurrence becomes

And solving this gives

Page 30: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

30

Best in theory? .

Conjecture: !

In practice? People still use the algorithm most of the time. (For example, in matlab.) It’s simple and robust.

Page 31: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

31

Fast Fourier Transform (FFT)

Page 32: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

32

Multiplication of polynomials

Many applications need to multiply two polynomials. e.g. signal processing.

A degree- polynomial is

The degree is at most . (The degree is exactly if .) The summation of two polynomials is easy

---------------------------------------------------------------- which still has degree at most .

Page 33: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

33

Multiplication

Multiplication of two polynomials: a different story.

) --------------------------------------------------------------- where (convolution)

Try an example: The multiplication can have degree . If we directly use this formula to multiply two

polynomials, it takes time.

Page 34: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

34

Better? YES!

By FFT: We can do it in time . FFT: Fast Fourier Transform

Page 35: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

35

What determines/represents a polynomial? Let’s switch to degree-() for later simplicity. We already used coefficients to represent a

polynomial. Coefficient representation

Other ways? Yes: By giving values on (distinct) points.

Point-value representation. [Fact] A degree-() polynomial is uniquely determined

by values on distinct points.

Page 36: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

36

Reason

We have coefficients to determine

We know values on distinct points.

How to get the coefficients? Just solve the system of equations:

Page 37: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

37

In matrix form:

The matrix is Vandermonde matrix, which is invertible. Thus it has a unique solution for the coefficients .

unknowns

Page 38: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

38

Advantage of point-value representation? It makes the multiplication extremely easy:

Though is hard For any fixed point , is simply multiplication of two

numbers. So given

and , it’s really easy to get

. time.

Thus we have the following interesting idea for polynomial multiplication…

Page 39: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

39

Go to an easy world and come back HK used to have many industries. Later found mainland has less expensive labor. So:

moved the companies to mainland, did the work there, and then shipped the products back to HK to sell

This is worth doing if: it’s cheaper in mainland, and the traveling/shipping is not expensive either. which turned out to be true.

Page 40: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

40

In our case

We need to investigate: the cost of traveling. Both way.

Page 41: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

41

Traveling

From coefficient representation to point-value representation: Evaluate two polynomials both on points.

--- evaluation. From point-value representation to coefficient

representation: Compute one polynomial back from point values.

--- interpolation. Both can be done in time.

Page 42: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

42

Evaluation

One point?

Directly by the above: multiplications Horner’s rule:

--- multiplications.

Page 43: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

43

Number of points

How many points we need to evaluate on? Since we later want to reconstruct the

product polynomial , which has degree . So (point,value) pairs are enough to

recover . We’ll evaluate and on points , and get .

points are enough. We use for convenience. Then recover from .

Page 44: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

44

Now evaluation on one point needs time, so evaluations on points need time.

Too bad. We want . Important fact: cost(evaluations on points) can be

cheaper than cost(evaluation on point). If we choose the points carefully.

And it turns out that the chosen points also make the (later) interpolation easier.

This powerful tool is called Fourier Transform.

Page 45: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

Complex roots of unity

1 has a complex root , satisfing . Discrete Fourier Transform (DFT):

where Try now.

Note: evaluates polynomial on .

So our evaluation task is just DFT.

Page 46: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

46

All the essences are here…

We want to evaluate and on , , …, . Suppose for simplicity that is a power of . For , define two new polynomials:

Then Divide and Conquer.

So we can evaluate by evaluating and on .

Page 47: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

47

Distinct points

Note:

are not all distinct.

Only values, each repeating twice!

Page 48: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

48

So we evaluate only points (instead of ). Recursion:

: To compute for . Rewriting recursion:

. Applying master theorem: . Since , the cost of evaluating is , as claimed.

Page 49: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

49

Interpolation

How about interpolation? i.e., to get the coefficients by point values.

Almost the same process!

.

Page 50: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

50

DFT matrix: . What’s the inverse of the matrix ?

Pretty much the same matrixreplace with .

…and then divide the whole matrix by for normalization.

Page 51: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

51

Why? You can directly check by multiplying the two

matrices and get (the identity matrix). e.g.

Page 52: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

52

Or

DFT matrix is unitary. Namely

: transpose. : complex conjugate DFT is symmetric: . So taking complex conjugate () gives

inverse.

Page 53: Instructor: Shengyu Zhang 1. Example 1: Merge sort 2.

53

Summary

Divide and conquer is a general method to design algorithms.

Master theorem to compute the complexity. Several examples.

Merge sort Selection Matrix multiplication FFT