CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2
|
|
- Roger Garrison
- 5 years ago
- Views:
Transcription
1 CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 11: Dynamic Programming, Part 2 Class URL: Goal: continue with DP (Knapsack, All-Pairs SPs, )
2 DP: Longest Common Subsequence (PA3) (Treatment from Cormen et al., Chapter 15) Problem: Given x[1..m] and y[1..n], find LCS x: A B C B D A B y: B D C A B A B C B A Brute-force algorithm: for every subsequence of x, check if it is in y O(n2 m ) time 2 m subsequences of x (each element is either in or out of the subsequence) O(n) for scanning y with x-subsequence (m n) What is a subproblem of LCS(x,y)?
3 Small Examples, Intuition Let s use to denote the empty string (zero chars) LCS (A, ) = LCS (A, B) = LCS (A, BD) = LCS (AB, B) = B LCS (AB, BD) = B = LCS (AB, B) LCS (ABC, BDC) = LCS (AB, BD) + C = BC
4 Recurrent Formula for LCS Let c[i,j] = length of LCS of X[i]=x[1..i],Y[j]= y[1..j] Then c[m,n] = length of LCS of x and y Claim: c[ i, c[ i 1, j 1] 1 j] max( c[ i, j 1], c[ i 1, if x[i] = y[j] otherwise Proof: x[i] = y[j] LCS([X[i],Y[j]) = LCS(X[i-1],Y[j-1]) + x[i] IN OTHER WORDS 0) c(i,j) c(i-1, j-1) 1) c(i,j) = c(i-1, j-1) + 1 if x i = y j when x i and y j are both in LCS 2) c(i,j) c(i-1, j) when x i is not in LCS 3) c(i,j) c(i, j-1) when y j is not in LCS j])
5 DP for LCS: Pseudocode For i = 0..m: T[i,0] = 0 // initialize left col For j = 0..n: T[0,j] = 0 // initialize top row For j = 1..n: // column For i = 1..m: // row if x[i] = y[j] then T[i,j] = 1 + T[i-1,j-1] else T[i,j] = max(t[i-1,j], T[i,j-1]) Return T[m,n] Other DP solutions for string problems (e.g., minimum edit distance ) will look similar
6 DP Table: Efficient Ordering of Subproblems After computing solution of a subproblem, store in table Time = O(mn) When computing c[i,j] we need O(1) time if we have: x[i], y[j] c[i,j-1] c[i-1,j] c[i-1,j-1]
7 DP Table for LCS x A B C B D A B y B D C A B A
8 DP Table for LCS x A B C B D A B y B D C A B A
9 DP Table for LCS x A B C B D A B y B D C A B A
10 Before Going On Recurrence =? Subproblem =? Table =? Dimension =? Order =?
11 DP: Knapsack (Section 6.4) KNAPSACK: You are going camping. Each item type i that can go into the knapsack has a weight w i and a value v i. You can carry up to a given weight limit b. What should go into the knapsack so as to maximize the total value? N.B.: This is a type of integer program that has just one constraint Notation: Define F k (y) = max v x j j (0 k n) k j 1 with w j x j (0 y b) j 1 F k (y) is the maximum value possible using only the first k item types, when the weight limit is y. k total weight of items chosen total value of items chosen x j = number of copies of j th item type used in solution
12 DP: Knapsack B.C. s: 1. F 0 (y) = 0 y when 0 item types are allowed 2. F k (0) = 0 k when weight limit is 0 3. F 1 (y) = y/w 1 v 1 first row is easy DP recurrence: F k (y) = max{ F k-1 (y), F k (y-w k )+v k } k th item not used k th item used once Build a DP table
13 DP: Knapsack Example: k = 4, b =10 4 item types, 10-pound limit v 1 =1, w 1 =2; v 2 =3,w 2 =3; v 3 =5, w 3 =4; v 4 =9, w 4 =7 Table of F k (y) values: y = #pounds limit, k = #item types allowed Recurrence: F k (y) = max{f k-1 (y), F k (y w k ) + v k } F k (y) n b table: Note: B.C s from previous slide: 0 th row and 0 th column Y K Note: value 12 = max(11, 9+ F 4 (3)) = max(11,9+3) = 12
14 DP: Knapsack (Reconstructing Solution) What is missing? As in SP: We know the achievable solution quality, but not the solution itself So, we need another table: i(k,y) = maximum index j such that item type j is used in F k (y), i.e., i(k,y) = j x j 1 and x q = 0 q > j B.C. s: i(1,y) = 0 if F 1 (y) = 0 i(1,y) = 1 if F 1 (y) 0 General: k k k k k k k k v w y F y F if k v w y F y F if y k i y k i ) ( ) ( ) ( ) ( ) 1, ( ), ( 1 1
15 DP: Knapsack (Reconstructing Solution) Trace Back: if i(k,y) =q, use item q once, check i(k,y - w(q)) Example: Y K F 4 (10) = 12 i(4,10) = 4 4 th item is used once i(4, 10 w 4 ) = i(4,3) = 2 2 nd item used once i(4, 3 w 2 ) = i(4,0) = 0 done Note: i(4,8) = 3 didn t use item with highest v/w when weight limit = 8
16 DP: Knapsack: Many Flavors Example: Same as in previous slides, but limited supply of certain items Example: Use as few stamps of denominations d 1, d 2,, d n as possible to make up exactly C cents of postage Slides 6-7 of Lec 10: denominations 1c, 5c, 10c, 12c, Various change-making exercises in textbook HW4
17 Chapter 6 sections DP Examples Longest Increasing Subsequence Minimum Edit Distance (Week 6 discussion) Knapsack (Lecture) Matrix Chain Product (Lecture) Shortest Paths (Floyd-Warshall) (Lecture) Independent Set in Trees Other Rental Car / Canoe Problem Longest Common Subsequence (PA3 #3) Optimum Polygon Triangulation Transitive Closure Change-Making (PA3 #4) Maximum Consecutive Subsequence Optimal Parenthesization (Week 6 discussion)
18 All-Pairs Shortest Paths Input: Directed graph G = (V,E), weight E Goal: Create n n matrix of SP distances (u,v) Running Bellman-Ford once from each vertex O( V * V * E ) = O( V 4 ) for dense graphs
19 All-Pairs Shortest Paths Input: Directed graph G = (V,E), weight E n = V Goal: Create n n matrix of SP distances (u,v) Running Bellman-Ford once from each vertex O( V * V * E ) = O( V 4 ) for dense graphs n 2 values (u,v) to fill in Assume Input = adjacency matrix n n matrix W = (w ij ) of edge weights assume w ii = 0 i SP to self has no edges, as long as there are no negative cycles
20 Simple APSP Dynamic Programming Relaxation or Induction idea: d ij (m) = weight of SP from i to j with m edges ( relax m from 0 to n-1) Base Case: d ij (0) = 0 if i = j and d ij (0) = Recurrence: d ij (m) = min k {d ik + w kj } m-1 if i j these are vertices k Runtime = O(n 4 ) i m-1 n-1 passes, each computing n 2 d ij s in O(n) time j
21 DP for APSP: Floyd-Warshall Algorithm Also DP, but faster O(n 3 ) c (m) ij = weight of SP from i to j with intermediate vertices in the set {1, 2,..., m} desired (i, j) = c (n) ij DP: compute c (m) ij in terms of smaller c ij Base Case: Recurrence: c im m c mj i c ij j intermediate nodes in {1, 2,..., m}
22 DP for APSP: Floyd-Warshall Algorithm Also DP, but faster O(n 3 ) c (m) ij = weight of SP from i to j with intermediate vertices in the set {1, 2,..., m} desired (i, j) = c (n) ij DP: compute c (m) ij in terms of smaller c ij Base Case: c (0) ij = w ij Recurrence: c (m) ij = min {c ij, c im + c mj } c im m c mj i c ij j intermediate nodes in {1, 2,..., m}
23 Floyd-Warshall Algorithm for m = 1..n do for i = 1..n do for j = 1..n do c ij (m) = min {c ij, c im + c mj } Runtime O(n 3 ) c im m c mj i c ij j
24 Floyd-Warshall Algorithm Finds All SPs for m = 1..n do for i = 1..n do for j = 1..n do c ij (m) = min {c ij, c im + c mj } v i v 3 v 7 v 32 v 16 v 1 v 92 v 42 v j v i' v 13 v 33 v 95 v 42 v 12 v 19 v 4 v j
25 Floyd-Warshall Algorithm Difference from previous Simple APSP DP: we do not check all possible intermediate vertices. for m=1..n do for i=1..n do for j = 1..n do c ij (m) = min {c ij, c im + c mj } Runtime O(n 3 ) Application: Transitive Closure G* of graph G: (i,j) G* iff path from i to j in G Adjacency matrix, elements on {0,1} Floyd-Warshall with min OR, + AND Runtime O(n 3 ) Useful in many problems
26 Transitive Closure G* of Directed Graph G (i,j) G* iff there exists a path from i to j in G Input: adjacency matrix of G with elements in {0, 1} Basically same algorithm as Floyd-Warshall APSP (exercise, or see later slides)
27 DP: Matrix Chain Product (Section 6.5) Matrix-chain multiplication problem: Give a chain of n matrices A 1, A 2,, A n to be multiplied, how to get the product A 1 A 2 A n. with minimum number of scalar multiplications. Associativity of matrix multiplication many possible orderings to calculate a given matrix chain product: Only one way to multiply A 1 A 2 Best way for triple: Cost (A 1, A 2 ) + Cost((A 1 A 2 ) A 3 ) or Cost (A 2, A 3 ) + Cost(A 1 (A 2 A 3 )).
28 DP: Matrix Chain Product (Section 6.5) Example with 4 matrices: d = (13, 5, 89, 3, 34) Best order of multiplication: 2856 multiplies Worst order of multiplication: multiplies Meaningful problem!
29 Problem instance chain of matrices What is a subproblem? What is the corresponding subsolution?
30
31 DP: Matrix Chain Product How do we build bottom-up? 1) From last example: Best way for triple: Cost (A 1, A 2 ) + Cost((A 1 A 2 ) A 3 ) or Cost (A 2, A 3 ) + Cost(A 1 (A 2 A 3 )). Save the best solutions for contiguous groups of A i. 2) Cost of ( i j )( j k) is ijk E.g., Each of 3 10 entries requires 5 multiplies (+ 4 adds)
32 DP: Matrix Chain Product Cost of final multiplication? A 1 A 2 A 3 A k-1 A k A n. d 1 d k d k d n+1 Find the best splitting point k over all possibilities Each subproblem has been already solved optimally we just need to look up in a table
33 DP: Matrix Chain Product FORMULATION: Table entries a ij, 1 i j n, where a ij = optimal solution (i.e., minimum # of multiplications) for A i A i+1 A j-1 A j DP will fill up the table of a ij values Matrix dimensions are given by vector of d i values, 1 i n+1, i.e., matrix A i has dimensions d i d i+1
34 DP: Matrix Chain Product Build Table: Diagonal S contains all a ij with j - i = S. S = 0: a ij =0, i=1, 2,, n S = 1: a i, i+1 = d i d i+1 d i+2, i=1, 2,, n-1 1 S n: a i, i+s = (a i, k + a k+1, i+s + d i d k d i+s ) min i k i s Example: 4 matrices, d = (13, 5, 89, 3, 34) S = 1: a 12 = 5785 (= 13 x 5 x 89) a 23 = 1335 (= 5 x 89 x 3) a 34 = 9078 (= 89 x 3 x 34)
35 DP: Matrix Chain Product S = 2: a 13 = min(a 11 + a , S = 3: a 12 + a ) = 1530 a 24 = min(a 22 + a , a 23 + a ) = 1845 a 14 = min({k=1} a 11 + a , {k=2} a 12 + a , {k=3} a 13 + a ) = 2856 (Note: max cost is multiplies!) Complexity: For S>0, choose among S choices for each of n-s elements in diagonal, so runtime is (n 3 ). Justification: i = 1 to n i(n-i) = i = 1 to n (ni i 2 ) = n(n(n+1)/2) (n(n+1)(2n+1)/6) = (n 3 )
36 DP: Optimal Polygon Triangulation Polygon has sides and vertices Polygon is simple = not self-intersecting. Polygon P is convex if any line segment with ends in P lies entirely in P Triangulation of P is partition of P with chords into triangles. Problem: Given a convex polygon and weight function defined on triangles (e.g. the perimeter). Find triangulation of minimum weight (of minimum total length). # of triangles: Always have n-2 triangles with n-3 chords
37 DP: Optimal Polygon Triangulation What is a subproblem? How do we combine solutions to smaller subproblems into the solution to a given (larger) subproblem?
38 DP: Optimal Polygon Triangulation Optimal sub-triangulation of optimal triangulation v[i] v[i] v[i-1] v[i-1] v[j] v[k] v[j] v[k] Recurrent formula: t[i,j] = the weight of the optimal triangulation of the polygon <v[i-1],v[i],...,v[j]>; If i=j, then t[i,j]=0; else t[ i, j] min i k j 1 { t[ i, k ] t[ k 1, j] w( v[ i 1] v[ k ] v[ j]) Runtime = O(n 3 ), space = O(n 2 )
39 Maximum Consecutive Subsequence (Manber, Introduction to Algorithms: A Creative Approach, 1989) Problem: Given a sequence x 1, x 2,, x n of real numbers (not necessarily positive), find a i.e., contiguous in the sequence subsequence x i, x i+1,, x j (of consecutive elements) such that the sum of the numbers in it is maximum over all subsequences of consecutive elements. Example 1: 3, 5, 2, 9, 8, 40, 1 Example 2: 3, 5, 2, 9, 18, 40, 1
40 Maximum Consecutive Subsequence What are subproblems? What do I need to know inductively?
41 Complexities How many previously-calculated subsolutions do you need to look at when calculating a new subsolution? Fibonacci (F i ): Bellman-Ford SSSP (d (k) j ): Knapsack (k,y): Longest Common Subsequence (i,j): String Reconstruction (i): Matrix Chain Product (i,i+s): Naïve APSP (grow #edges) (d (m) ij ): Floyd-Warshall APSP (grow set of allowed v s) (d (m) ij ):
42 Reconstructing Solutions Knapsack: auxiliary array of highest-index food type used Shortest Paths: auxiliary array of prev information Parenthesizing (MCP, Polygon Triangulation, ): auxiliary array of split point information
43 More
44 Longest Common Substring (DPV 6.8) Subproblem definition Goal Recurrence
45 Longest Palindromic Subsequence (DPV 6.7) Subproblem definition Goal Recurrence
46 Min-Edit Palindromization Given a string, find an efficient algorithm that computes the minimum number of letters that must be inserted to make the string into a palindrome. loyal loayaol (2 insertions) aaaabbb bbbaaaabbb (3 insertions) Subproblem Goal Recurrence
47 Transitive Closure G* of Directed Graph G (i,j) G* iff path from i to j in G Input = adjacency matrix, elements {0,1}
48 Transitive Closure G* of Directed Graph G (i,j) G* iff path from i to j in G Input = adjacency matrix, elements {0,1} c im m c mj i c ij j
49 Transitive Closure G* of Directed Graph G (i,j) G* iff path from i to j in G Input = adjacency matrix, elements {0,1} for m=1..n do for i=1..n do for j = 1..n do c ij (m) = OR {c ij, [c im AND c mj ] } c im m c mj i c ij j
50 All-Pairs Shortest Paths What are the two successive approximations that we had previously seen for shortest paths? (Dijkstra) (Bellman-Ford)
51 Even More (from discussion, Lecture 10 slides)
52 DP: Minimum Edit Distance Minimum edit distance (Section 6.3) Given two strings x[1..m] and y[1..n], what is the minimum number of edit operations (insert, delete, replace) needed to transform one string into the other? Example: PINE TREE: PINE-TINE-TRNE-TREE Example: BLACK CAT: BLACK-LACK-LAC-CAC-CAT Charles L. Dodgson (= Lewis Carroll) invented parlor game of Doublets (shortest path in graph of words) PINE TREE, BLACK WHITE, etc. See Knuth, The Stanford GraphBase DP Recurrence E[i,j] = min{ 1 + E[i-1,j], 1 + E[i,j-1], diff(i,j) + E[i-1,j-1] } B.C.s: E[i,0] = i for i = 0..m; E[0,j] = j for j = 0..n (E.g., it takes j edit operations to transform the first zero characters of X into the first j characters of Y)
53 DP: Minimum Edit Distance E[i,j] = min{ 1 + E[i-1,j], 1 + E[i,j-1], diff(i,j) + E[i-1,j-1] } 1 + E[i-1,j] : edit distance of first i-1 characters of X to first j characters of Y, then add the last character of X 1 + E[i,j-1] : edit distance of first j-1 characters of Y to first i characters of X, then add the last character of Y diff(i,j) + E[i-1,j-1] : edit distance of first i-1 characters of X to first j-1 characters of Y, then add the edit distance between the last character of X and the last character of Y B.C.s: E[i,0] = i for i = 0..m; E[0,j] = j for j = 0..n
54 DP Table for Minimum Edit Distance y T R E E x P I N E Red numbers show base cases Red arrows show which term is minimum in the recurrence E.g., E[2,3] = 1 + E[2,2]
55 Car Rental / Canoe Problem Situation: You need to drive along a highway using rental cars. There are n rental car agencies 1, 2,, n along the highway. At any of the agencies, you can rent a car that can be returned at any other agency down the road. You cannot backtrack, i.e., you can drive only in one direction along the highway. So, a car rented at agency i can be returned only at some agency j > i. For each pair of agencies i,j with j > i, the cost of the i-to-j car rental is known. Problem: What is the minimum-cost sequence of car rentals that gets you from 1 to n?
Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance
Dynamic Programming: 1D Optimization Fibonacci Sequence To efficiently calculate F [x], the xth element of the Fibonacci sequence, we can construct the array F from left to right (or bottom up ). We start
More informationCS 231: Algorithmic Problem Solving
CS 231: Algorithmic Problem Solving Naomi Nishimura Module 5 Date of this version: June 14, 2018 WARNING: Drafts of slides are made available prior to lecture for your convenience. After lecture, slides
More informationUnit-5 Dynamic Programming 2016
5 Dynamic programming Overview, Applications - shortest path in graph, matrix multiplication, travelling salesman problem, Fibonacci Series. 20% 12 Origin: Richard Bellman, 1957 Programming referred to
More informationShortest path problems
Next... Shortest path problems Single-source shortest paths in weighted graphs Shortest-Path Problems Properties of Shortest Paths, Relaxation Dijkstra s Algorithm Bellman-Ford Algorithm Shortest-Paths
More informationModule 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm
Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm This module 27 focuses on introducing dynamic programming design strategy and applying it to problems like chained matrix
More informationOptimization II: Dynamic Programming
Chapter 12 Optimization II: Dynamic Programming In the last chapter, we saw that greedy algorithms are efficient solutions to certain optimization problems. However, there are optimization problems for
More informationDynamic Programming CS 445. Example: Floyd Warshll Algorithm: Computing all pairs shortest paths
CS 44 Dynamic Programming Some of the slides are courtesy of Charles Leiserson with small changes by Carola Wenk Example: Floyd Warshll lgorithm: Computing all pairs shortest paths Given G(V,E), with weight
More informationDynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006
Dynamic Programming What is Dynamic Programming? Technique for avoiding redundant work in recursive algorithms Works best with optimization problems that have a nice underlying structure Can often be used
More informationHomework3: Dynamic Programming - Answers
Most Exercises are from your textbook: Homework3: Dynamic Programming - Answers 1. For the Rod Cutting problem (covered in lecture) modify the given top-down memoized algorithm (includes two procedures)
More informationSingle Source Shortest Path (SSSP) Problem
Single Source Shortest Path (SSSP) Problem Single Source Shortest Path Problem Input: A directed graph G = (V, E); an edge weight function w : E R, and a start vertex s V. Find: for each vertex u V, δ(s,
More informationComputer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Dynamic Programming
Computer Science 385 Design and Analysis of Algorithms Siena College Spring 29 Topic Notes: Dynamic Programming We next consider dynamic programming, a technique for designing algorithms to solve problems
More informationDynamic Programming part 2
Dynamic Programming part 2 Week 7 Objectives More dynamic programming examples - Matrix Multiplication Parenthesis - Longest Common Subsequence Subproblem Optimal structure Defining the dynamic recurrence
More informationDynamic Programming II
June 9, 214 DP: Longest common subsequence biologists often need to find out how similar are 2 DNA sequences DNA sequences are strings of bases: A, C, T and G how to define similarity? DP: Longest common
More informationRecursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)
Dynamic Programming Any recursive formula can be directly translated into recursive algorithms. However, sometimes the compiler will not implement the recursive algorithm very efficiently. When this is
More informationMA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati
MA 252: Data Structures and Algorithms Lecture 36 http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html Partha Sarathi Mandal Dept. of Mathematics, IIT Guwahati The All-Pairs Shortest Paths Problem
More informationDesign and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 15, 2015 洪國寶
Design and Analysis of Algorithms 演算法設計與分析 Lecture 7 April 15, 2015 洪國寶 1 Course information (5/5) Grading (Tentative) Homework 25% (You may collaborate when solving the homework, however when writing
More informationAlgorithms: COMP3121/3821/9101/9801
NEW SOUTH WALES Algorithms: COMP3121/3821/9101/9801 Aleks Ignjatović School of Computer Science and Engineering University of New South Wales TOPIC 5: DYNAMIC PROGRAMMING COMP3121/3821/9101/9801 1 / 38
More informationCMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk
CMPS 00 Fall 04 Dynamic Programming Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 9/30/4 CMPS 00 Intro. to Algorithms Dynamic programming Algorithm design technique
More informationF(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)
Algorithms Dana Shapira Lesson #4: Dynamic programming Fibonacci Series F()= F()= F(n)=F(n-)+F(n-) Write a Divide and Conquer Algorithm! What is its running time? Binomial Coefficients n! n = ( )! n! Recursive
More informationSo far... Finished looking at lower bounds and linear sorts.
So far... Finished looking at lower bounds and linear sorts. Next: Memoization -- Optimization problems - Dynamic programming A scheduling problem Matrix multiplication optimization Longest Common Subsequence
More informationAlgorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University
Algorithms IV Dynamic Programming Guoqiang Li School of Software, Shanghai Jiao Tong University Dynamic Programming Shortest Paths in Dags, Revisited Shortest Paths in Dags, Revisited The special distinguishing
More informationDesign and Analysis of Algorithms
CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 6: BFS, SPs, Dijkstra Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Distance in a Graph The distance between two nodes is the length
More informationData Structures and Algorithms Week 8
Data Structures and Algorithms Week 8 Dynamic programming Fibonacci numbers Optimization problems Matrix multiplication optimization Principles of dynamic programming Longest Common Subsequence Algorithm
More informationLecture 4: Dynamic programming I
Lecture : Dynamic programming I Dynamic programming is a powerful, tabular method that solves problems by combining solutions to subproblems. It was introduced by Bellman in the 950 s (when programming
More informationDesign and Analysis of Algorithms
CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 6: BFS, SPs, Dijkstra Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Distance in a Graph The distance between two nodes is the length
More informationAdvanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis 3.2.12 simas@cs.aau.dk All-pairs shortest paths Main goals of the lecture: to go through one more example of dynamic programming
More informationIntroduction to Algorithms
Introduction to Algorithms Dynamic Programming Well known algorithm design techniques: Brute-Force (iterative) ti algorithms Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic
More informationElements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples
Elements of Dynamic Programming COSC 3A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken from Monica Nicolescu,
More informationDynamic Programming (Part #2)
Dynamic Programming (Part #) Introduction to Algorithms MIT Press (Chapter 5) Matrix-Chain Multiplication Problem: given a sequence A, A,, A n, compute the product: A A A n Matrix compatibility: C = A
More informationAnalysis of Algorithms, I
Analysis of Algorithms, I CSOR W4231.002 Eleni Drinea Computer Science Department Columbia University Thursday, March 8, 2016 Outline 1 Recap Single-source shortest paths in graphs with real edge weights:
More informationArtificial Intelligence
Artificial Intelligence Shortest Path Problem G. Guérard Department of Nouvelles Energies Ecole Supérieur d Ingénieurs Léonard de Vinci Lecture 3 GG A.I. 1/42 Outline 1 The Shortest Path Problem Introduction
More informationGraphs II - Shortest paths
Graphs II - Shortest paths Single Source Shortest Paths All Sources Shortest Paths some drawings and notes from prof. Tom Cormen Single Source SP Context: directed graph G=(V,E,w), weighted edges The shortest
More informationAlgorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University
Algorithms All-Pairs Shortest Paths Dong Kyue Kim Hanyang University dqkim@hanyang.ac.kr Contents Using single source shortest path algorithms Presents O(V 4 )-time algorithm, O(V 3 log V)-time algorithm,
More information(Feodor F. Dragan) Department of Computer Science Kent State University. Advanced Algorithms, Feodor F. Dragan, Kent State University 1
$GYDQFH $OJRULWKPV (Feodor F. Dragan) Department of Computer Science Kent State University Advanced Algorithms, Feodor F. Dragan, Kent State University Textbook: Thomas Cormen, Charles Leisterson, Ronald
More informationLongest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017
CS161 Lecture 13 Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017 1 Overview Last lecture, we talked about dynamic programming
More informationLecture 8. Dynamic Programming
Lecture 8. Dynamic Programming T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018
More informationAlgorithm Design Techniques part I
Algorithm Design Techniques part I Divide-and-Conquer. Dynamic Programming DSA - lecture 8 - T.U.Cluj-Napoca - M. Joldos 1 Some Algorithm Design Techniques Top-Down Algorithms: Divide-and-Conquer Bottom-Up
More informationDynamic programming II - The Recursion Strikes Back
Chapter 5 Dynamic programming II - The Recursion Strikes Back By Sariel Har-Peled, December 17, 2012 1 Version: 0.4 No, mademoiselle, I don t capture elephants. I content myself with living among them.
More information1 Dynamic Programming
CS161 Lecture 13 Dynamic Programming and Greedy Algorithms Scribe by: Eric Huang Date: May 13, 2015 1 Dynamic Programming The idea of dynamic programming is to have a table of solutions of subproblems
More informationChain Matrix Multiplication
Chain Matrix Multiplication Version of November 5, 2014 Version of November 5, 2014 Chain Matrix Multiplication 1 / 27 Outline Outline Review of matrix multiplication. The chain matrix multiplication problem.
More informationfrom notes written mostly by Dr. Carla Savage: All Rights Reserved
CSC 505, Fall 2000: Week 9 Objectives: learn about various issues related to finding shortest paths in graphs learn algorithms for the single-source shortest-path problem observe the relationship among
More informationAlgebraic method for Shortest Paths problems
Lecture 1 (06.03.2013) Author: Jaros law B lasiok Algebraic method for Shortest Paths problems 1 Introduction In the following lecture we will see algebraic algorithms for various shortest-paths problems.
More informationPresentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 25 Dynamic Programming Terrible Fibonacci Computation Fibonacci sequence: f = f(n) 2
More informationThe Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation
The Shortest Path Problem jla,jc@imm.dtu.dk Department of Management Engineering Technical University of Denmark The Shortest Path Problem Given a directed network G = (V,E,w) for which the underlying
More informationIN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech
IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech License CC BY-NC-SA 2.0 http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Outline Previously on IN101 Python s anatomy Functions,
More informationAlgorithms for Data Science
Algorithms for Data Science CSOR W4246 Eleni Drinea Computer Science Department Columbia University Thursday, October 1, 2015 Outline 1 Recap 2 Shortest paths in graphs with non-negative edge weights (Dijkstra
More informationUML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM
NAME: This exam is open: - books - notes and closed: - neighbors - calculators ALGORITHMS QUALIFYING EXAM The upper bound on exam time is 3 hours. Please put all your work on the exam paper. (Partial credit
More informationThomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms
Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Introduction to Algorithms Preface xiii 1 Introduction 1 1.1 Algorithms 1 1.2 Analyzing algorithms 6 1.3 Designing algorithms 1 1 1.4 Summary 1 6
More informationAlgorithms for Data Science
Algorithms for Data Science CSOR W4246 Eleni Drinea Computer Science Department Columbia University Shortest paths in weighted graphs (Bellman-Ford, Floyd-Warshall) Outline 1 Shortest paths in graphs with
More informationECE250: Algorithms and Data Structures Dynamic Programming Part B
ECE250: Algorithms and Data Structures Dynamic Programming Part B Ladan Tahvildari, PEng, SMIEEE Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University
More informationCSE 417 Dynamic Programming (pt 4) Sub-problems on Trees
CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees Reminders > HW4 is due today > HW5 will be posted shortly Dynamic Programming Review > Apply the steps... 1. Describe solution in terms of solution
More informationFramework for Design of Dynamic Programming Algorithms
CSE 441T/541T Advanced Algorithms September 22, 2010 Framework for Design of Dynamic Programming Algorithms Dynamic programming algorithms for combinatorial optimization generalize the strategy we studied
More informationCS420/520 Algorithm Analysis Spring 2009 Lecture 14
CS420/520 Algorithm Analysis Spring 2009 Lecture 14 "A Computational Analysis of Alternative Algorithms for Labeling Techniques for Finding Shortest Path Trees", Dial, Glover, Karney, and Klingman, Networks
More informationLecture 13: Chain Matrix Multiplication
Lecture 3: Chain Matrix Multiplication CLRS Section 5.2 Revised April 7, 2003 Outline of this Lecture Recalling matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm
More informationShortest Paths. Nishant Mehta Lectures 10 and 11
Shortest Paths Nishant Mehta Lectures 0 and Finding the Fastest Way to Travel between Two Intersections in Vancouver Granville St and W Hastings St Homer St and W Hastings St 6 Granville St and W Pender
More informationLast week: Breadth-First Search
1 Last week: Breadth-First Search Set L i = [] for i=1,,n L 0 = {w}, where w is the start node For i = 0,, n-1: For u in L i : For each v which is a neighbor of u: If v isn t yet visited: - mark v as visited,
More informationMinimum Spanning Trees
Minimum Spanning Trees Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no
More informationTutorial 6-7. Dynamic Programming and Greedy
Tutorial 6-7 Dynamic Programming and Greedy Dynamic Programming Why DP? Natural Recursion may be expensive. For example, the Fibonacci: F(n)=F(n-1)+F(n-2) Recursive implementation memoryless : time= 1
More informationEND-TERM EXAMINATION
(Please Write your Exam Roll No. immediately) Exam. Roll No... END-TERM EXAMINATION Paper Code : MCA-205 DECEMBER 2006 Subject: Design and analysis of algorithm Time: 3 Hours Maximum Marks: 60 Note: Attempt
More informationEE/CSCI 451 Spring 2018 Homework 8 Total Points: [10 points] Explain the following terms: EREW PRAM CRCW PRAM. Brent s Theorem.
EE/CSCI 451 Spring 2018 Homework 8 Total Points: 100 1 [10 points] Explain the following terms: EREW PRAM CRCW PRAM Brent s Theorem BSP model 1 2 [15 points] Assume two sorted sequences of size n can be
More informationDynamic Programming. Nothing to do with dynamic and nothing to do with programming.
Dynamic Programming Deliverables Dynamic Programming basics Binomial Coefficients Weighted Interval Scheduling Matrix Multiplication /1 Knapsack Longest Common Subsequence 6/12/212 6:56 PM copyright @
More informationDesign and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶
Design and Analysis of Algorithms 演算法設計與分析 Lecture 7 April 6, 2016 洪國寶 1 Course information (5/5) Grading (Tentative) Homework 25% (You may collaborate when solving the homework, however when writing up
More informationCOL 702 : Assignment 1 solutions
COL 70 : Assignment 1 solutions 1(a ( n T (n = T + bn log n ( n = T + b n ( n log + bn log n ( n = T + b n ( n 8 log + b n log ( n + bn log n = bn log n + b n log n + bn log n +... log n terms = nb [(log
More informationMemoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018
CS124 Lecture 11 Spring 2018 Memoization/Dynamic Programming Today s lecture discusses memoization, which is a method for speeding up algorithms based on recursion, by using additional memory to remember
More informationIntroduction to Algorithms
Introduction to Algorithms 6.046J/18.401J LECTURE 12 Dynamic programming Longest common subsequence Optimal substructure Overlapping subproblems Prof. Charles E. Leiserson Dynamic programming Design technique,
More informationS. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 165
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 165 5.22. You are given a graph G = (V, E) with positive edge weights, and a minimum spanning tree T = (V, E ) with respect to these weights; you may
More informationShortest Paths. Nishant Mehta Lectures 10 and 11
Shortest Paths Nishant Mehta Lectures 0 and Communication Speeds in a Computer Network Find fastest way to route a data packet between two computers 6 Kbps 4 0 Mbps 6 6 Kbps 6 Kbps Gbps 00 Mbps 8 6 Kbps
More information12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares
12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares Optimal substructure Dynamic programming is typically applied to optimization problems. An optimal solution to the original
More informationEfficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms
Efficient Sequential Algorithms, Comp309 Part 1: Algorithmic Paradigms University of Liverpool References: T. H. Cormen, C. E. Leiserson, R. L. Rivest Introduction to Algorithms, Second Edition. MIT Press
More informationWeek 11: Minimum Spanning trees
Week 11: Minimum Spanning trees Agenda: Minimum Spanning Trees Prim s Algorithm Reading: Textbook : 61-7 1 Week 11: Minimum Spanning trees Minimum spanning tree (MST) problem: Input: edge-weighted (simple,
More informationAll Pairs Shortest Paths
All Pairs Shortest Paths Given a directed, connected weighted graph G(V, E), for each edge u, v E, a weight w(u, v) is associated with the edge. The all pairs of shortest paths problem (APSP) is to find
More information1 Non greedy algorithms (which we should have covered
1 Non greedy algorithms (which we should have covered earlier) 1.1 Floyd Warshall algorithm This algorithm solves the all-pairs shortest paths problem, which is a problem where we want to find the shortest
More information1 More on the Bellman-Ford Algorithm
CS161 Lecture 12 Shortest Path and Dynamic Programming Algorithms Scribe by: Eric Huang (2015), Anthony Kim (2016), M. Wootters (2017) Date: May 15, 2017 1 More on the Bellman-Ford Algorithm We didn t
More informationCS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department
CS473-Algorithms I Lecture 1 Dynamic Programming 1 Introduction An algorithm design paradigm like divide-and-conquer Programming : A tabular method (not writing computer code) Divide-and-Conquer (DAC):
More informationCSE 373 Analysis of Algorithms, Fall Homework #3 Solutions Due Monday, October 18, 2003
Piyush Kumar CSE 373 Analysis of Algorithms, Fall 2003 Homework #3 Solutions Due Monday, October 18, 2003 Problem 1 Find an optimal parenthesization of a matrix chain product whose sequence of dimensions
More informationAlgorithms Dr. Haim Levkowitz
91.503 Algorithms Dr. Haim Levkowitz Fall 2007 Lecture 4 Tuesday, 25 Sep 2007 Design Patterns for Optimization Problems Greedy Algorithms 1 Greedy Algorithms 2 What is Greedy Algorithm? Similar to dynamic
More informationCS521 \ Notes for the Final Exam
CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )
More informationDynamic Programming Algorithms
Dynamic Programming Algorithms Introduction In our study of divide-and-conquer algorithms, we noticed that a problem seemed conducive to a divide-and-conquer approach provided 1. it could be divided into
More informationCS173 Longest Increasing Substrings. Tandy Warnow
CS173 Longest Increasing Substrings Tandy Warnow CS 173 Longest Increasing Substrings Tandy Warnow Today s material The Longest Increasing Subsequence problem DP algorithm for finding a longest increasing
More informationChapter 25: All-Pairs Shortest-Paths
Chapter : All-Pairs Shortest-Paths When no negative edges Some Algorithms Using Dijkstra s algorithm: O(V ) Using Binary heap implementation: O(VE lg V) Using Fibonacci heap: O(VE + V log V) When no negative
More information15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015
15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015 Dynamic Programming is a powerful technique that allows one to solve many different types
More informationDynamic Programming Homework Problems
CS 1510 Dynamic Programming Homework Problems 1. Consider the recurrence relation T(0) = T(1) = 2 and for n > 1 n 1 T(n) = T(i)T(i 1) i=1 We consider the problem of computing T(n) from n. (a) Show that
More informationAll Shortest Paths. Questions from exercises and exams
All Shortest Paths Questions from exercises and exams The Problem: G = (V, E, w) is a weighted directed graph. We want to find the shortest path between any pair of vertices in G. Example: find the distance
More informationCS Algorithms. Dynamic programming and memoization. (Based on slides by Luebke, Lim, Wenbin)
CS 7 - lgorithms Dynamic programming and memoization (ased on slides by Luebke, Lim, Wenbin) When to Use Dynamic Programming Usually used to solve Optimization problems Usually the problem can be formulated
More informationLecture 11: Analysis of Algorithms (CS ) 1
Lecture 11: Analysis of Algorithms (CS583-002) 1 Amarda Shehu November 12, 2014 1 Some material adapted from Kevin Wayne s Algorithm Class @ Princeton 1 2 Dynamic Programming Approach Floyd-Warshall Shortest
More informationCMSC351 - Fall 2014, Homework #4
CMSC351 - Fall 2014, Homework #4 Due: November 14th at the start of class PRINT Name: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used,
More informationDynamic Programming Homework Problems
CS 1510 Dynamic Programming Homework Problems 1. (2 points) Consider the recurrence relation T (0) = T (1) = 2 and for n > 1 n 1 T (n) = T (i)t (i 1) i=1 We consider the problem of computing T (n) from
More information2.3 Optimal paths. Optimal (shortest or longest) paths have a wide range of applications:
. Optimal paths Optimal (shortest or longest) paths have a wide range of applications: Google maps, GPS navigators planning and management of transportation, electrical and telecommunication networks project
More informationChapter 3 Dynamic programming
Chapter 3 Dynamic programming 1 Dynamic programming also solve a problem by combining the solutions to subproblems. But dynamic programming considers the situation that some subproblems will be called
More informationShortest Paths. CSE 373 Data Structures Lecture 21
Shortest Paths CSE 7 Data Structures Lecture Readings and References Reading Section 9., Section 0.. Some slides based on: CSE 6 by S. Wolfman, 000 //0 Shortest Paths - Lecture Path A path is a list of
More informationCSE 101- Winter 18 Discussion Section Week 6
CSE 101- Winter 18 Discussion Section Week 6 Administrative Introducing 1:1 Sessions: https://docs.google.com/spreadsheets/d/1kgxt_rzbzlibbdijiczs_ o1wxdwa9hhvxccprn8_bwk/edit?usp=sharing Please see the
More informationIntroduction to Parallel & Distributed Computing Parallel Graph Algorithms
Introduction to Parallel & Distributed Computing Parallel Graph Algorithms Lecture 16, Spring 2014 Instructor: 罗国杰 gluo@pku.edu.cn In This Lecture Parallel formulations of some important and fundamental
More informationEDIT DISTANCE. Given two words (strings), how can we define a notion of closeness. For example: Is PELICAN closer to PENGUIN or POLITICIAN?
CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.ucsd.edu Office 4208 CSE Building Lecture 22: Dynamic Programming Examples (Edit Distance/Knapsack) EDIT DISTANCE Given two words (strings),
More informationIntroduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15
600.363 Introduction to Algorithms / 600.463 Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15 14.1 Introduction Today we re going to talk about algorithms for computing shortest
More informationNotes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 4 Notes. Class URL:
Notes slides from before lecture CSE 21, Winter 2017, Section A00 Lecture 4 Notes Class URL: http://vlsicad.ucsd.edu/courses/cse21-w17/ Notes slides from before lecture Notes January 23 (1) HW2 due tomorrow
More informationPower Set of a set and Relations
Power Set of a set and Relations 1 Power Set (1) Definition: The power set of a set S, denoted P(S), is the set of all subsets of S. Examples Let A={a,b,c}, P(A)={,{a},{b},{c},{a,b},{b,c},{a,c},{a,b,c}}
More informationTitle. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.
Title Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis Nesrine Damak Fakultät für Informatik TU München 20. September 2010 Nesrine Damak: Classical Shortest-Path Algorithms 1/ 35
More informationDynamic Programmming: Activity Selection
Dynamic Programmming: Activity Selection Select the maximum number of non-overlapping activities from a set of n activities A = {a 1,, a n } (sorted by finish times). Identify easier subproblems to solve.
More informationDynamic Programming. CSE 101: Design and Analysis of Algorithms Lecture 19
Dynamic Programming CSE 101: Design and Analysis of Algorithms Lecture 19 CSE 101: Design and analysis of algorithms Dynamic programming Reading: Chapter 6 Homework 7 is due Dec 6, 11:59 PM This Friday
More informationMore Graph Algorithms: Topological Sort and Shortest Distance
More Graph Algorithms: Topological Sort and Shortest Distance Topological Sort The goal of a topological sort is given a list of items with dependencies, (ie. item 5 must be completed before item 3, etc.)
More information