• 沒有找到結果。

Algorithm Design and Analysis Divide and Conquer (2)

N/A
N/A
Protected

Academic year: 2022

Share "Algorithm Design and Analysis Divide and Conquer (2)"

Copied!
38
0
0

全文

(1)

Algorithm Design and Analysis Divide and Conquer (2)

http://ada.miulab.tw

(2)

Outline

• Recurrence (遞迴)

• Divide-and-Conquer

• D&C #1: Tower of Hanoi (河內塔)

• D&C #2: Merge Sort

• D&C #3: Bitonic Champion

• D&C #4: Maximum Subarray

• Solving Recurrences

• Substitution Method

• Recursion-Tree Method

• Master Method

• D&C #5: Matrix Multiplication

• D&C #6: Selection Problem

• D&C #7: Closest Pair of Points Problem

Divide-and-Conquer 之神乎奇技

Divide-and-Conquer 首部曲

(3)

What is Divide-and-Conquer?

• Solve a problem recursively

• Apply three steps at each level of the recursion

1. Divide the problem into a number of subproblems that are smaller instances of the

same problem (比較小的同樣問題)

2. Conquer the subproblems by solving them recursively

If the subproblem sizes are small enough

• then solve the subproblems

• else recursively solve itself

3. Combine

the solutions to the subproblems into the solution for the original problem

base case recursive case

(4)

Solving Recurrences

Textbook Chapter 4.3 – The substitution method for solving recurrences Textbook Chapter 4.4 – The recursion-tree method for solving recurrences Textbook Chapter 4.5 – The master method for solving recurrences

(5)

D&C Algorithm Time Complexity

• 𝑇 𝑛 : running time for input size 𝑛

• 𝐷 𝑛 : time of Divide for input size 𝑛

• 𝐶 𝑛 : time of Combine for input size 𝑛

• 𝑎: number of subproblems

• 𝑛/𝑏: size of each subproblem

(6)

Solving Recurrences

1. Substitution Method (取代法)

• Guess a bound and then prove by induction

2. Recursion-Tree Method (遞迴樹法)

• Expand the recurrence into a tree and sum up the cost

3. Master Method (套公式大法/大師法)

• Apply Master Theorem to a specific form of recurrences

• Useful simplification tricks

• Ignore floors, ceilings, boundary conditions (proof in Ch. 4.6)

• Assume base cases are constant (for small n)

(7)

Substitution Method

Textbook Chapter 4.3 – The substitution method for solving recurrences

(8)

Review

• Time Complexity for Merge Sort

• Theorem

• Proof

• There exists positive constant 𝑎, 𝑏 s.t.

• Use induction to prove

• n = 1, trivial

• n > 1,

Substitution Method (取代法)

guess a bound and then prove by induction

(9)

Substitution Method (取代法)

• Guess the form of the solution

• Verify by mathematical induction (數學歸納法)

• Prove it works for 𝑛 = 1

• Prove that if it works for 𝑛 = 𝑚, then it works for 𝑛 = 𝑚 + 1

→ It can work for all positive integer 𝑛

• Solve constants to show that the solution works

• Prove 𝑂 and Ω separately

1. Guess

2. Verify

3. Solve

(10)

Substitution Method Example

• Proof

There exists positive constants 𝑛

0

, 𝑐 s.t. for all 𝑛 ≥ 𝑛

0

,

• Use induction to find the constants 𝑛

0

, 𝑐

• n = 1, trivial

• n > 1,

• holds when

e.g.

Inductive hypothesis

Guess

Verify

Solve

(11)

Substitution Method Example

• Proof

There exists positive constants 𝑛

0

, 𝑐 s.t. for all 𝑛 ≥ 𝑛

0

,

• Use induction to find the constants 𝑛

0

, 𝑐

• n = 1, trivial

• n > 1,

Inductive hypothesis

Tighter upper bound?

証不出來…

猜錯了?還是推導錯了?

沒猜錯 推導也沒錯 這是取代法的小盲點

(12)

Substitution Method Example

• Proof

There exists positive constants 𝑛

0

, 𝑐

1

, 𝑐

2

s.t. for all 𝑛 ≥ 𝑛

0

,

• Use induction to find the constants 𝑛

0

,𝑐

1

, 𝑐

2

• n = 1, holds for

• n > 1,

• holds when

e.g.

Inductive hypothesis

Guess

Verify

Solve Strengthen the inductive hypothesis

by subtracting a low-order term

(13)

Useful Tricks

• Guess based on seen recurrences

• Use the recursion-tree method

• From loose bound to tight bound

• Strengthen the inductive hypothesis by subtracting a low-order term

• Change variables

• E.g.,

1. Change variable:

2. Change variable again:

3. Solve recurrence

(14)

Recursion-Tree Method

Textbook Chapter 4.4 – The recursion-tree method for solving recurrences

(15)

Review

• Time Complexity for Merge Sort

• Theorem

• Proof

2nd expansion 1st expansion

kth expansion

The expansion stops when 2𝑘 = 𝑛

Recursion-Tree Method (遞迴樹法)

Expand the recurrence into a tree and sum up the cost

(16)

Recursion-Tree Method (遞迴樹法)

• Expand a recurrence into a tree

• Sum up the cost of all nodes as a good guess

• Verify the guess as in the substitution method

• Advantages

• Promote intuition

• Generate good guesses for the substitution method

1. Expand

2. Sumup

3. Verify

(17)

Recursion-Tree Example

(18)

Recursion-Tree Example

(19)

Recursion-Tree Example

(20)

Recursion-Tree Example

+

(21)

Master Theorem

Textbook Chapter 4.4 – The recursion-tree method for solving recurrences

(22)

Master Theorem

log 𝑎

divide a problem of size 𝑛 into 𝑎 subproblems, each of size 𝑛

𝑏 is solved in time 𝑇 𝑛

𝑏 recursively

The proof is in Ch. 4.6

Should follow this format

(23)

Recursion-Tree for Master Theorem

+

𝑎 𝑎

(24)

Three Cases

• 𝑎 ≥ 1, the number of subproblems

• 𝑏 > 1, the factor by which the subproblem size decreases

• 𝑓(𝑛) = work to divide/combine subproblems

• Compare 𝑓 𝑛 with 𝑛log𝑏 𝑎

1. Case 1: 𝑓 𝑛 grows polynomially slower than 𝑛log𝑏𝑎 2. Case 2: 𝑓 𝑛 and 𝑛log𝑏 𝑎 grow at similar rates

3. Case 3: 𝑓 𝑛 grows polynomially faster than 𝑛log𝑏 𝑎

(25)

Case 1:

Total cost dominated by the leaves

𝑎 𝑎

(26)

Case 1:

Total cost dominated by the leaves

(27)

𝑎 𝑎

Case 2:

Total cost evenly distributed among levels

(28)

Case 2:

Total cost evenly distributed among levels

(29)

Case 3:

Total cost dominated by root cost

𝑎 𝑎

log 𝑎

(30)

Case 3:

Total cost dominated by root cost

(31)

Master Theorem

divide a problem of size 𝑛 into 𝑎 subproblems, each of size 𝑛

𝑏 is solved in time 𝑇 𝑛

𝑏 recursively

The proof is in Ch. 4.6

(32)

Examples

compare 𝑓 𝑛 with 𝑛log𝑏𝑎

(33)

Floors and Ceilings

• Master theorem can be extended to recurrences with floors and ceilings

• The proof is in the Ch. 4.6

(34)

Theorem 1

• Case 2

(35)

Theorem 2

• Case 1

(36)

Theorem 3

• Case 2

(37)

To Be Continue…

(38)

Question?

Important announcement will be sent to

@ntu.edu.tw mailbox & post to the course website

Course Website: http://ada.miulab.tw Email: ada-ta@csie.ntu.edu.tw

參考文獻

相關文件

4 Solving optimization problems Kernel: decomposition methods Linear: coordinate descent method Linear: second-order methods Experiments. Chih-Jen Lin (National Taiwan Univ.) 86

Textbook Chapter 33.4 – Finding the closest pair of points.. Closest Pair of

Textbook Chapter 4.3 – The substitution method for solving recurrences Textbook Chapter 4.4 – The recursion-tree method for solving recurrences Textbook Chapter 4.5 – The master

In this case Lagrange’s method is to look for extreme values by solving five equations in the five unknowns x, y, z, λ, and µ ... Example 5

See Chapter 5, Interrupt and Exception Handling, in the IA-32 Intel Architecture Software Developer’s Manual, Volume 3, for a detailed description of the processor’s mechanism

• The start node representing the initial configuration has zero in degree.... The Reachability

This theorem does not establish the existence of a consis- tent estimator sequence since, with the true value θ 0 unknown, the data do not tell us which root to choose so as to obtain

The underlying idea was to use the power of sampling, in a fashion similar to the way it is used in empirical samples from large universes of data, in order to approximate the

In this chapter we develop the Lanczos method, a technique that is applicable to large sparse, symmetric eigenproblems.. The method involves tridiagonalizing the given

For the proposed algorithm, we establish a global convergence estimate in terms of the objective value, and moreover present a dual application to the standard SCLP, which leads to

Abstract In this paper, we consider the smoothing Newton method for solving a type of absolute value equations associated with second order cone (SOCAVE for short), which.. 1

Like the proximal point algorithm using D-function [5, 8], we under some mild assumptions es- tablish the global convergence of the algorithm expressed in terms of function values,

[r]

Textbook Chapter 15.4 – Longest common subsequence Textbook Problem 15-5 – Edit

Multiple images from a sequence tracked with 6DOF SLAM on a client, while a localization server provides the global pose used to overlay the building outlines with transparent

Biases in Pricing Continuously Monitored Options with Monte Carlo (continued).. • If all of the sampled prices are below the barrier, this sample path pays max(S(t n ) −

Moreover, this chapter also presents the basic of the Taguchi method, artificial neural network, genetic algorithm, particle swarm optimization, soft computing and

Approach and a Boundary Element Method for the Calculation of Sound Fields in the Human Ear Canal, " Journal of the Acoustical Society of America, 118(4), pp. Axelsson,

When collecting data on problem-solving processes the investigator used one textbook problem (revised into 2 formats) then interviewed 6 students (3 solved word problem with

problem-solving; 3) analyze the type of problems posed by children; and, 4) display categories of misconceptions exhibited when children did problem posing. The stages

[22] Guillermo Gonzalez, Microwave Transistor Amplifier Analysis and Design: Chapter 4, Noise, Broadband, And High-Power Design Methods, New Jersey, Prentice Hall Inc,

In the Chapter 2, it introduces specification of 802.11a about planning and regulation of the system structure.In the Chapter 3, it introduces the Mixer structure and the

Input data is the window data and the fall motion technique is the machine learning model with the extension of the checkpoint ensemble method...