《计算模型与算法技术》课程教学资源(PPT讲稿)Chapter 8 Dynamic Programming

Chapter 8 introduction to The Design & Dynamic Programming Analysis of Algorithms 2ND EDITION Anany Levitin PEARSON Addison Wesley Copyright @ 2007 Pearson Addison-Wesley. All rights reserved
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated as recurrences with overlapping subinstances Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by cs °“ Programming” here means" planning” Main idea: set up a recurrence relating a solution to a larger instance to solutions of some smaller instances solve smaller instances once record solutions in a table extract solution to the initial instance from that table Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-1 Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated as recurrences with overlapping subinstances • Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by CS • “Programming” here means “planning” • Main idea: - set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once - record solutions in a table - extract solution to the initial instance from that table

Example: Fibonacci numbers Recall definition of fibonacci numbers: F(n)=F(n-1)+F(n=2) F(0)=0 F()=1 Computing the nth Fibonacci number recursively(top-down): F(n-2 F(2)+P(n-3)F(3)+F(n-4 Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8 8-2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-2 Example: Fibonacci numbers • Recall definition of Fibonacci numbers: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 • Computing the nth Fibonacci number recursively (top-down): F(n) F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4)

Example: Fibonacci numbers(cont) Computing the nth Fibonacci number using bottom-up iteration and recording results F(0)=0 F(1) F(2)=1+0=1 F(n-2) F(n-1)= F(n)=F(n-1)+F(n-2) 01 F(n-2)F(n-1)F(n) Efficiency: time n What if we solve space n it recursively? Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-3 Example: Fibonacci numbers (cont.) Computing the n th Fibonacci number using bottom-up iteration and recording results: F(0) = 0 F(1) = 1 F(2) = 1+0 = 1 … F(n-2) = F(n-1) = F(n) = F(n-1) + F(n-2) Efficiency: - time - space 0 1 1 . . . F(n-2) F(n-1) F(n) n n What if we solve it recursively?

Examples of DP algorithms Computing a binomial coefficient Longest common subsequence Warshall's algorithm for transitive closure Floyd's algorithm for all-pairs shortest paths Constructing an optimal binary search tree Some instances of difficult discrete optimization problems: traveling salesman knapsack Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8 8-4
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-4 Examples of DP algorithms • Computing a binomial coefficient • Longest common subsequence • Warshall’s algorithm for transitive closure • Floyd’s algorithm for all-pairs shortest paths • Constructing an optimal binary search tree • Some instances of difficult discrete optimization problems: - traveling salesman - knapsack

Computing a binomial coefficient by DP Binomial coefficients are coefficients of the binomial formula: (a+b)=C(n,0)a b0+.+C(n, k)a/-kbk+.+C(n, n)ab" Recurrence: C(,k)=C(n-1, )+C(n-1, k-1) for n>k>0 C(,0)=1,C(,m)=1forn≥0 Value of c(n, k) can be computed by filling a table 012 /-1 (-1,kx-1)C(m-1,k) n Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8 8-5
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-5 Computing a binomial coefficient by DP Binomial coefficients are coefficients of the binomial formula: (a + b) n = C(n,0)a nb 0 + . . . + C(n,k)a n-kb k+ . . . + C(n,n)a 0b n Recurrence: C(n,k) = C(n-1,k) + C(n-1,k-1) for n > k > 0 C(n,0) = 1, C(n,n) = 1 for n 0 Value of C(n,k) can be computed by filling a table: 0 1 2 . . . k-1 k 0 1 1 1 1 . . . n-1 C(n-1,k-1) C(n-1,k) n C(n,k)

Computing C(n, h): pseudocode and analysis ALGORITHM Binomial(n, k) //Computes C(n, k)by the dynamic programming algorithm ∥nput: a pair of nonnegative integers n≥k≥0 //Output: The value of C(n, k) fori←0 to n do forj←-0 to min(i,k)do if j=for j=i C[i,j←1 else C[i, j]+C[i-1,j-1+C[i-1,jI return CIn, k Time efficiency: o(nk) Space efficiency: o(nk) Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-6 Computing C(n,k): pseudocode and analysis Time efficiency: Θ(nk) Space efficiency: Θ(nk)

Knapsack Problem by DP Given n items of integer weights: Wi W2 values a knapsack of integer capacity W find most valuable subset of the items that fit into the knapsack Consider instance defined by first i items and capacity j(s w Let vi,j be optimal value of such an instance. Then 1=mx1y,+刚120 i-1,∥ ifj<0 Initial conditions: 0j=0 and v,0=0 Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8 8-7
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-7 Knapsack Problem by DP Given n items of integer weights: w1 w2 … wn values: v1 v2 … vn a knapsack of integer capacity W find most valuable subset of the items that fit into the knapsack Consider instance defined by first i items and capacity j (j W). Let V[i,j] be optimal value of such an instance. Then max {V[i-1,j], vi + V[i-1,j- wi ]} if j- wi 0 V[i,j] = V[i-1,j] if j- wi < 0 Initial conditions: V[0,j] = 0 and V[i,0] = 0 {

Knapsack Problem by DP(example) Example: Knapsack of capacity W=5 item weight value s12 S10 3 2132 S20 s15 capacity/ 012345 0000 v1=1210012 W2=1,V2=10201012222222 Backtracing Wa=3. v2=20 3 0 10 12 22 30 32 finds the actual optimal subset W4=2,v4=1540101525302ie. solution Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-8 Knapsack Problem by DP (example) Example: Knapsack of capacity W = 5 item weight value 1 2 $12 2 1 $10 3 3 $20 4 2 $15 capacity j 0 1 2 3 4 5 0 w1 = 2, v1= 12 1 w2 = 1, v2= 10 2 w3 = 3, v3= 20 3 w4 = 2, v4= 15 4 ? 0 0 0 0 0 12 0 10 12 22 22 22 0 10 12 22 30 32 0 10 15 25 30 37 Backtracing finds the actual optimal subset, i.e. solution

Knapsack Problem by DP(pseudocode Algorithm DPKnapsack(wlLn], vll.n, w ar yo.n,O.w PlL.n, 1.W: int forj: =o to wdo V,j:=0 fori: =0 to n do Running time and space VO:=0 D(nW) fori: =I to n do forj: =I to w do ifwi≤jand+ilwl引>Hz-l,then 阿:叫+i1-iP:jw可 1:=i-l,il;P[:=j return VIn, M and the optimal subset by backtracing Copyright@ 2007 Pearson Addison-Wesley. All rights reserved. A Levitin "Intoduction b the Design& Analysis of Algorithms, 2nd ed, Ch 8
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 8 8-9 Knapsack Problem by DP (pseudocode) Algorithm DPKnapsack(w[1..n], v[1..n], W) var V[0..n,0..W], P[1..n,1..W]: int for j := 0 to W do V[0,j] := 0 for i := 0 to n do V[i,0] := 0 for i := 1 to n do for j := 1 to W do if w[i] j and v[i] + V[i-1,j-w[i]] > V[i-1,j] then V[i,j] := v[i] + V[i-1,j-w[i]]; P[i,j] := j-w[i] else V[i,j] := V[i-1,j]; P[i,j] := j return V[n,W] and the optimal subset by backtracing Running time and space: O(nW)
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 清华大学:图神经网络及其应用(PPT讲稿)Graph Neural Networks and Applications.pptx
- 《计算机网络》课程PPT教学课件(英文版)Chapter 4 物理层 PHYSICAL LAYER.pptx
- 南京大学:《数据结构 Data Structures》课程教学资源(PPT课件讲稿)Chapter 1 基本概念和算法分析.ppt
- 安徽理工大学:《算法导论》课程教学资源(PPT课件讲稿)第4章 分治法——“分”而治之.ppt
- 南京大学:《形式语言与自动机 Formal Languages and Automata》课程教学资源(PPT课件讲稿)Transition System(主讲:卜磊).pptx
- 南京大学:《编译原理》课程教学资源(PPT课件讲稿)第四章 语法分析.ppt
- 《计算机网络》课程教学资源(PPT课件讲稿)第四章 网络层.pptx
- 《ASP动态网页设计实用教程》教学资源(PPT课件讲稿)第3章 Web页面制作基础.ppt
- 《编译原理》课程教学资源(PPT课件讲稿)第四章 语法制导的翻译.ppt
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)顺序同一性的存储器模型.pptx
- 马尔可夫链蒙特卡洛算法(PPT讲稿)Hamiltonian Monte Carlo on Manifolds,HMC.pptx
- SOFT COMPUTING Evolutionary Computing(PPT讲稿).ppt
- 《计算机情报检索原理》课程教学资源(PPT课件)第五章 自动标引.ppt
- 《计算机网络》课程教学资源(PPT课件讲稿)Chapter 04 网络层 Network Layer.ppt
- 湖南科技大学:分布式工作流系统的时间管理模型研究(PPT讲稿,周春姐).ppt
- 《编译原理》课程教学资源(PPT课件讲稿)第九章 独立于机器的优化.ppt
- 西安电子科技大学:《现代密码学》课程教学资源(PPT课件讲稿)第七章 数字签名和密码协议.ppt
- 南京大学:移动Agent系统支撑(PPT讲稿)Mobile Agent Communication——Software Agent.pptx
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)第五章 存储层次.ppt
- 合肥工业大学:《网络安全概论》课程教学资源(PPT课件讲稿)第一讲 网络安全概述.ppt
- Network and System Security Risk Assessment(PPT讲稿)Firewall.ppt
- 东北大学:《可信计算基础》课程教学资源(PPT课件讲稿)第三讲 认证技术与数字签名.ppt
- 《计算机网络》课程教学资源(PPT课件讲稿)Chapter 04 网络层 Network Layer.ppt
- 《时间序列分析及应用》课程教学资源(PPT课件讲稿)第二章 时间序列的预处理.ppt
- 中国科学技术大学:《算法基础》课程教学资源(PPT课件讲稿)算法基础习题课(二).pptx
- 中国科学技术大学:《计算机编程入门》课程PPT教学课件(讲稿)An Introduction to Computer Programming.ppt
- 上海交通大学:《挖掘海量数据集 Mining Massive Datasets》课程教学资源(PPT讲稿)Lecture 03 Frequent Itemsets and Association Rules Mining Massive Datasets.ppt
- 《Computer Networking:A Top Down Approach》英文教材教学资源(PPT课件讲稿,6th edition)Chapter 3 传输层 Transport Layer.ppt
- 分布式数据库系统的体系结构与设计(PPT讲稿)Architecture and Design of Distributed Database Systems.pptx
- 南京大学:Conceptual Architecture View(PPT讲稿).ppt
- 北京师范大学:《计算机应用基础》课程教学资源(PPT课件讲稿)第1章 计算机常识(主讲:马秀麟).pptx
- 《编译原理》课程教学资源(PPT课件讲稿)中间代码生成.pptx
- TTCN3工具培训(PPT讲稿)TTCN-3简介.ppt
- 《Java Web编程技术》课程教学资源(PPT课件讲稿)第4章 JDBC数据库访问技术.ppt
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)第三章 流水线技术.ppt
- 《计算机网络》课程教学资源(PPT课件讲稿)第2章 物理层.ppt
- 《计算机视觉》课程教学资源(PPT课件讲稿)基于灭点几何的深度图重建、基于焦点变换的深度图重建.ppt
- 中国科学技术大学:《数据结构及其算法》课程电子教案(PPT课件讲稿)第七章 图.pps
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)第4章 存储层次结构设计.pptx
- 大连工业大学:《计算机文化与软件基础》课程教学资源(PPT课件讲稿)绪论、计算机系统的组成、计算机中数的表示.pps