D&C, DP

1 / 28

# D&C, DP - PowerPoint PPT Presentation

D&amp;C, DP. Divide and Conquer Dynamic Programming. Recursive Method…. Big Problem!!!. Big Problem!!!. Sub-Problem. Sub-Problem. Sub-Problem. Three major steps. Big Problem!!!. Solved!!!. Conquer. Divide. Merge. Sub-Problem. Solved. Sub-Problem. Solved. Sub-Problem. Solved.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'D&C, DP' - larue

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### D&C, DP

Divide and Conquer

Dynamic Programming

Recursive Method…

Big Problem!!!

Big Problem!!!

Sub-Problem

Sub-Problem

Sub-Problem

Three major steps

Big Problem!!!

Solved!!!

Conquer

Divide

Merge

Sub-Problem

Solved

Sub-Problem

Solved

Sub-Problem

Solved

Solved

Solved

Solved

Stairs Climbing

=

=

+

+

=

+

f(3)

3

=

f(2)

2

f(1)

f(4)

=

f(3)

f(2)

+

1

+

5

3

2

f(4) = f(3) + f(2)

Fast Exponentiation

7^13=96889010407

7^13=?

7^13=7^7 * 7^6

• 7^7= 7^4 * 7^3
• 7^6=7^3 * 7^3
• 7^4= 7^2 * 7^2

7^3=7^2 * 7^1

• 7^2= 7^1 * 7^1
• 7^1= 7
Exception(Prune and search)

Kth smallest(related to Quick sort)

Binary Search

Dynamic Programming(動態規劃)

Programming  最佳化(Optimization)

DP  運用”動態”的方式求最佳解

Dynamic 動態

DP可視為 D&C 的延伸

f(4)

f(2)

f(3)

f(0)

f(1)

f(1)

f(2)

f(0)

f(1)

Overlapping

f(4)

f(2)

f(3)

f(0)

f(1)

f(1)

f(2)

f(3) = f(2) + f(1)

f(4) = f(3) + f(2)

f(0)

f(1)

f(2) = f(0) + f(1)

f(4)

f(2)

f(3)

f(0)

f(1)

f(1)

f(2)

f(3) = 3

f(4) = 5

f(1) = 1

f(0)

f(1)

f(2) = 2

f(0) = 1

DP的時間、空間複雜度

f(n) = f(n-1) + f(n-2)

DP的時間、空間複雜度

Note on DP

2.決定問題的計算順序。

3.確認初始值、問題的計算範圍。

1.發現相似的求解方式，紀錄重複的子問題資訊 。

Practice in Dp

2.Bottom-up, Iterative way.

1.Top-down, Recursive way.

Stairs Climbing(初步思考)

f(n) =

{ 1 , if n = 1

{ 2 , if n = 2

{ f(n-1) + f(n-2) , if n >= 3

Stairs Climbing(美觀)

f(n) =

{ 0 , if n < 0 [Exterior]

{ 1 , if n = 0 [Initial]

{ 1 , if n = 1 [Initial]

{ f(n-1) + f(n-2) , if n >= 2 and n <= 5 [Compute]

{ 0 , if n > 5 [Exterior]

Stairs Climbing(不好的示範)

int f(int n)

{

if (n == 0 || n == 1)

return1;

else

return f(n-1) + f(n-2);

}

Stairs Climbing(Two ways)

f(4)

f(2)

f(3)

f(0)

f(1)

f(1)

f(2)

f(0)

f(1)

2. Bottom-up, Iterative.

1.Top-down, Recursive.

Stairs Climbing(Top-down)

inttable[5];// 表格，儲存全部問題的答案。

boolsolve[5];// 記錄問題是否已計算

int f(int n)

{

if (n == 0 || n == 1)

return1;

if (solve[n])

return table[n];

table[n] = f(n-1) + f(n-2); // 將答案存入表格

solve[n] = true;            // 紀錄已計算

return table[n];

}

Stairs Climbing(Top-down)

f(n) = f(n-1) + f(n-2)

1.好處是不必斤斤計較計算順序。

2.只計算必要的問題，而不必計算整個陣列。

Stairs Climbing(Top-down)

f(4)

f(2)

f(3)

f(n) = f(n-1) + f(n-2)

f(0)

f(1)

f(1)

f(2)

f(0)

f(1)

1.壞處是不斷呼叫函式，執行效率較差。

2.無法自由地控制計算順序，無法妥善運用記憶體。

Stairs Climbing(Bottom-up)

int table[5];

voiddp()

{

// initial

for (inti=0; i<=4;i++)

table[i] = 0;

table[0] = 1;

table[1] = 1;

// compute

for (inti=2; i<=4;i++)

table[i] = table[i-1] + table[i-2];

}

Int table[5];

table[0] = 1;

table[ 1] = 1;

Stairs Climbing(Bottom-up)

for (int i=0; i<=5; i++)

table[i] = 0;

table[2] = ?

table[3] = ?

for (int i=2; i<=5; i++)

table[i] = table[i-1] + table[i-2];

table[4] = ?

The Summary of D&C, DP

2.可考慮使用表格紀錄出現頻繁的子問題數值。

3.To iterate is man, to recurse is divine.

+智慧

1.找出結構相似之處(recursion)，設計一套公式(recurrence)。

UVA

DP

623

900

11069

10446

495

D&C

11038

620

10230

920

374