1 / 10

动态规划 (Dynamic Programming)

动态规划 (Dynamic Programming). 1. 什么是动态规划?. Fibonacci 序列. Fibonacci 序列被定义如下: F(n) = F(n - 1) + F(n - 2), n ≥ 3 F(2) = 1 F(1) = 1 问题:如何计算第 n 个 Fibonacci 数 F(n)?. F(1) = 1 F(2) = 1 F(n) = F(n - 1) + F(n - 2), n ≥ 3. F (n) if (n == 1) return 1; if (n == 2) return 1;

reese-morse
Download Presentation

动态规划 (Dynamic Programming)

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 动态规划(Dynamic Programming)

  2. 1. 什么是动态规划?

  3. Fibonacci序列 Fibonacci序列被定义如下: • F(n) = F(n - 1) + F(n - 2), n≥ 3 • F(2) = 1 • F(1) = 1 问题:如何计算第n个Fibonacci数F(n)?

  4. F(1) = 1 F(2) = 1 F(n) = F(n - 1) + F(n - 2), n≥ 3 F(n) if (n == 1) return 1; if (n == 2) return 1; return F(n - 1) + F(n - 2); 算法1

  5. F(n) if (n == 1) return 1; if (n == 2) return 1; return F(n - 1) + F(n - 2); 算法1的效率

  6. F(n) if (n == 1) return 1; if (n == 2) return 1; return F(n - 1) + F(n - 2); 目标:计算F(100)。 问题:在计算F(100)的过程中, F(99)被调用(计算)多少次? F(98)被计算多少次? … … F(3)被计算多少次? 算法1的效率

  7. F(100)的执行过程 … …

  8. 算法2 F(n) Fib[1] = 1; Fib[2] = 1; for i = 3 to n Fib[i] = Fib[i – 1] + Fib[i – 2]; return Fib[n]; Fib[1..n]是一维数组, 用Fib[i]来存储F(i)的值。

  9. 算法1 vs 算法2

  10. 什么是动态规划? • 对于有些问题,解决该问题的过程中需要重复解决该问题的子问题。 • 动态规划是一种解决此类问题的方法。其核心思想是:“重用子问题的计算结果,避免重复计算”。

More Related