




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Dynamic ProgrammingCopyright Li Zimao 2007-2008-1 SCUECExpected OutcomesStudents should be able toSolve the all pairs shortest paths problem by dynamic programming, that is the Floyds algorithmSolve the transitive closure problem by dynamic programming, that is the Warshalls algorithmSolve the knaps
2、ack problem by dynamic programmingSolve the matrix chain multiplication problem by dynamic programmingCopyright Li Zimao 2007-2008-1 SCUECTransitive ClosureThe transitive closure of a directed graph with n vertices can be defined as the nxn matrix T = tij, in which the element in the ith row (1 i n)
3、 and the jth column (1 j n) is 1 if there exists a nontrivial directed path (i.e., a directed path of a positive length) from the ith vertex to the jth vertex; otherwise, tij is 0. Graph traversal-based algorithm and Warshalls algorithm34210 0 1 01 0 0 10 0 0 00 1 0 00 0 1 01 1 1 10 0 0 01 1 1 13421
4、Adjacency matrixTransitive closureCopyright Li Zimao 2007-2008-1 SCUECWarshalls Algorithm Main idea: Use a bottom-up method to construct the transitive closure of a given digraph with n vertices through a series of nxn boolean matrices:T(0), T(k-1), T(k) , , T(n)The question is: how to obtain T(k) f
5、rom T(k-1) ?3421342134213421 T(0)0 0 1 01 0 0 10 0 0 00 1 0 0T(1)0 0 1 01 0 1 10 0 0 00 1 0 0T(2)0 0 1 01 0 1 10 0 0 01 1 1 1T(3)0 0 1 01 0 1 10 0 0 01 1 1 1T(4)0 0 1 01 1 1 10 0 0 01 1 1 13421 T(k) : tij(k) = 1 in T(k) , iff there is an edge from i to j; or there is a path from i to j going through
6、 vertex 1; or there is a path from i to j going through vertex 1 and/or 2; or. there is a path from i to j going through 1, 2, and/or kAllow 1 to be an intermediate nodeAllow 1,2 to be an intermediate nodeAllow 1,2,3 to be an intermediate nodeAllow 1,2,3,4 to be an intermediate nodeDoes not allow an
7、 intermediate nodeCopyright Li Zimao 2007-2008-1 SCUECWarshalls AlgorithmIn the kth stage: to determine T(k) is to determine if a path exists between two vertices i, j using just vertices among 1,k tij(k-1) = 1 (path using just 1 ,k-1)tij(k) = 1: or (tik(k-1) = 1 and tkj(k-1) = 1) (path from i to k
8、and from k to i using just 1 ,k-1)ijkkth stageRule to determine whether tij (k) should be 1 in T(k) : If an element tij is 1 in T(k-1) , it remains 1 in T(k) . If an element tij is 0 in T(k-1) ,it has to be changed to 1 in T(k) iff the element in its row i and column k and the element in its column
9、j and row k are both 1s in T(k-1). Copyright Li Zimao 2007-2008-1 SCUECCompute Transitive ClosureTime ComplexitySpace Complexity less space?Copyright Li Zimao 2007-2008-1 SCUECFloyds Algorithm: All pairs shortest paths All pairs shortest paths problem: In a weighted graph, find shortest paths betwee
10、n every pair of vertices.Applicable to: undirected and directed weighted graphs; no negative weight.Same idea as the Warshalls algorithm : construct solution through series of matrices D(0) , D(1), , D(n) Example:34214161530 4 1 0 6 3 0 5 1 00 4 1 0 4 3 0 6 5 1 0weight matrixdistance matrixdij(k) =
11、length of the shortest path from i to j with each vertex numbered no higher than k.Copyright Li Zimao 2007-2008-1 SCUECFloyds AlgorithmD(k) : allow 1, 2, , k to be intermediate vertices.In the kth stage, determine whether the introduction of k as a new eligible intermediate vertex will bring about a
12、 shorter path from i to j. dij(k) = mindij(k-1) , dik(k-1) + dkj(k-1) for k 1, dij(0) = wijijkkth stagedij(k-1)dik(k-1)dkj(k-1)Copyright Li Zimao 2007-2008-1 SCUECFloyd AlgorithmTime Complexity: O(n3), Space ?Copyright Li Zimao 2007-2008-1 SCUECFloyd algorithm (less space)Copyright Li Zimao 2007-200
13、8-1 SCUECConstructing a shortest pathFor k=0For k=1Copyright Li Zimao 2007-2008-1 SCUECPrint all-pairs shortest pathsCopyright Li Zimao 2007-2008-1 SCUECExample:15432347-481-526Copyright Li Zimao 2007-2008-1 SCUECD(0)= (0)=D(1)= (1)=Copyright Li Zimao 2007-2008-1 SCUECD(2)= (2)=D(3)= (3)=Copyright L
14、i Zimao 2007-2008-1 SCUECD(4)= (4)=D(5)= (5)=Copyright Li Zimao 2007-2008-1 SCUEC15432347-481-526Shortest path from 1 to 2 in Copyright Li Zimao 2007-2008-1 SCUECThe Knapsack ProblemThe problemFind the most valuable subset of the given n items that fit into a knapsack of capacity W.Consider the foll
15、owing subproblem P(i, j)Find the most valuable subset of the first i items that fit into a knapsack of capacity j, where 1 i n, and 1 j WLet Vi, j be the value of an optimal solution to the above subproblem P(i, j). Goal: Vn, W The question: What is the recurrence relation that expresses a solution
16、to this instance in terms of solutions to smaller subinstances?Copyright Li Zimao 2007-2008-1 SCUECThe Knapsack ProblemThe RecurrenceTwo possibilities for the most valuable subset for the subproblem P(i, j)It does not include the ith item: Vi, j = Vi-1, jIt includes the ith item: Vi, j = vi+ Vi-1, j
17、 wi Vi, j = maxVi-1, j, vi+ Vi-1, j wi , if j wi 0 Vi-1, jif j wi 0V0, j = 0 for j 0 and Vi, 0 = 0 for i 0 Copyright Li Zimao 2007-2008-1 SCUECDynamic Matrix MultiplicationIf we have a series of matrices of different sizes that we need to multiply, the order we do it can have a big impact on the num
18、ber of operationsFor example, if we need to multiply four matrices M1, M2, M3, and M4 of sizes 205, 5 35, 35 4, and 4 25, depending on the order this can take between 3100 and 24,500 multiplicationsCopyright Li Zimao 2007-2008-1 SCUECCopyright Li Zimao 2007-2008-1 SCUECDynamic Matrix Multiplication
19、ProblemInput: a series of matrices M1, M2, , MN with different sizes s1s2, s2s3, , sNsN+1Output: the order of multiplication of these matrices such that the total number of multiplications is minimizedCan you give a solution to this problem?Try all possibilities to find the order, butIt is time cons
20、uming because the number of all possibilities is a Catalan number C(N-1) where Copyright Li Zimao 2007-2008-1 SCUECDynamic Matrix Multiplication: IdeaWe look at the waysto pair the matrices and then combine these into groups of three, and then fourkeep track of the efficiency of these partial result
21、sCopyright Li Zimao 2007-2008-1 SCUECCharacterize the optimal solutionDenote Mi:j (ij)as MiMi+1Mj, our problem is to find the optimal multiplication order of M1: N.Consider Mi:j (ij) and i kj:The optimal value for Mi:j must be the minimum one of the optimal values for Mi:k plus for Mk+1:j plus sisk+1sj+1 for i kj.A key property:If Mi: j is divide into Mi:k and Mk+1:j, then the optimal order for Mi: j contains the optimal order for Mi:k and Mk+1:j.Copyright Li Zim
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 节假日互联网零售价格策略制定考核试卷
- 新零售背景下家电渠道整合模式创新考核试卷
- 跨文化视角下的初等德育考核试卷
- 智能农业技术标准化与推广策略考核试卷
- 创业企业外部合作模式探索考核试卷
- 财务会计档案管理办法
- 2025年中国PVC凉拖数据监测报告
- 2025年中国PP抹布数据监测研究报告
- 2025年中国N、N-二甲基苯胺数据监测研究报告
- 2025年中国G.T.S运动鞋数据监测研究报告
- 2025年中考物理终极押题猜想(广东省卷专用)(原卷版)
- 《公路建设项目文件管理规程》
- 无人机物流运输操作规程
- 国家开放大学电大《药剂学》期末试题题库及答案
- 国家开放大学《Web开发基础》形考任务实验1-5参考答案
- 高考英语考纲词汇3500词(珍藏版)
- 小学三到六年级全册单词默写(素材)-2023-2024学年译林版(三起)小学英语
- 2024年烟台蓝天投资发展集团有限公司招聘笔试冲刺题(带答案解析)
- 管理学基础(第3版)全套教学课件
- 【混合式教学模式探究文献综述2600字】
- 养老护理员四级理论试题及答案
评论
0/150
提交评论