slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
D&C, DP PowerPoint Presentation
Download Presentation
D&C, DP

Loading in 2 Seconds...

play fullscreen
1 / 28

D&C, DP - PowerPoint PPT Presentation


  • 169 Views
  • Uploaded on

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.

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

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


An Image/Link below is provided (as is) to download presentation

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

D&C, DP

Divide and Conquer

Dynamic Programming

recursive method
Recursive Method…

Big Problem!!!

Big Problem!!!

Sub-Problem

Sub-Problem

Sub-Problem

three major steps
Three major steps

Big Problem!!!

Solved!!!

Conquer

Divide

Merge

Sub-Problem

Solved

Sub-Problem

Solved

Sub-Problem

Solved

Solved

Solved

Solved

stairs climbing
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
Fast Exponentiation

7^13=96889010407

7^13=?

slide8

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
Exception(Prune and search)

Kth smallest(related to Quick sort)

Binary Search

dynamic programming
Dynamic Programming(動態規劃)

Programming  最佳化(Optimization)

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

Dynamic 動態

dp d c
DP可視為 D&C 的延伸

f(4)

f(2)

f(3)

f(0)

f(1)

f(1)

f(2)

f(0)

f(1)

Overlapping

slide12
讀取、計算、儲存

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)

slide13
讀取、計算、儲存

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

slide14
DP的時間、空間複雜度

時間複雜度:O(1)

時間複雜度:O(n)

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

時間複雜度:O(s)

時間複雜度:O(n*s*1)

slide15
DP的時間、空間複雜度

空間複雜度:O(n)

空間複雜度:以子問題的出現期間決定。

空間複雜度:O(1)

note on dp
Note on DP

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

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

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

practice in dp
Practice in Dp

2.Bottom-up, Iterative way.

1.Top-down, Recursive way.

stairs climbing1
Stairs Climbing(初步思考)

f(n) =

{ 1 , if n = 1

{ 2 , if n = 2

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

stairs climbing2
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 climbing3
Stairs Climbing(不好的示範)

int f(int n)

{

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

return1;

else

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

}

stairs climbing two ways
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
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 down1
Stairs Climbing(Top-down)

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

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

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

stairs climbing top down2
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
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 up1
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
The Summary of D&C, DP

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

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

+智慧

靈感 =

經驗

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

slide28
UVA

DP

623

900

11069

10446

495

D&C

11038

620

10230

920

374