CS583 Lecture 10. Graph Algorithms Shortest Path Algorithms Dynamic Programming. Many slides here are based on D. Luebke slides.

Size: px
Start display at page:

Download "CS583 Lecture 10. Graph Algorithms Shortest Path Algorithms Dynamic Programming. Many slides here are based on D. Luebke slides."

Transcription

1 // S58 Lecture Jana Kosecka Graph lgorithms Shortest Path lgorithms Dynamic Programming Many slides here are based on D. Luebke slides Previously Depth first search DG s - topological sort - strongly connected components MST - Prim s - Kruskal s Shortest path with non-negative weights

2 // Shortest Path lgorithms Single-Source Shortest Path Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v Shortest-path = minimum weight Weight of path is sum of edges E.g., a road map: what is the shortest path from Faixfax to Washington D?

3 // Shortest Path Properties gain, we have optimal substructure: the shortest path consists of shortest subpaths: Proof: suppose some subpath is not a shortest path There must then exist a shorter subpath ould substitute the shorter subpath for a shorter path but then overall path is not shortest path. ontradiction Optimal substructure property hallmark of dynamic programming Shortest Path Properties In graphs with negative weight cycles, some shortest paths will not exist (Why?): <

4 // Relaxation key technique in shortest path algorithms is relaxation Idea: for all v, maintain upper bound d[v] on δ(s,v) Relax(u,v,w) { if (d[v] > d[u]+w) then d[v]=d[u]+w; } Relaxing an edge checking if it can improve The cost of the path Relax Relax Shortest Path Properties Define δ(u,v) to be the weight of the shortest path from u to v Shortest paths satisfy the triangle inequality: δ(u,v) δ(u,x) + δ(x,v) Proof : x u v This path is no longer than any other path

5 // Shortest Path Properties Triangle inequality Upper bound property No path property onvergence Property Path relaxation property Predecessor subgraph property Dijkstra s lgorithm If no negative edge weights, we can beat ellman-ford Similar to breadth-first search Grow a tree gradually, advancing from vertices taken from a queue lso similar to Prim s algorithm for MST Use a priority queue keyed on d[v] 5

6 // ellman Ford lgorithm Single source shortest path algorithm Weights can be negative lgorithm returns NIL if there is negative weight cycle Otherwise returns produces shortest paths from source to all other vertices ellman-ford lgorithm ellmanford() for each v V d[v] = ; d[s] = ; for i= to V - for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return no solution ; Initialize d[], which will converge to shortest-path value δ Relaxation: Make V - passes, relaxing each edge Test for solution Under what condition do we get a solution? Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w 6

7 // ellman-ford lgorithm ellmanford() for each v V d[v] = ; d[s] = ; for i= to V - for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return no solution ; What will be the running time? Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w ellman-ford lgorithm ellmanford() for each v V d[v] = ; d[s] = ; for i= to V - for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return no solution ; What will be the running time? : O(VE) Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w 7

8 // ellman-ford lgorithm ellmanford() for each v V s d[v] = ; d[s] = ; for i= to V - for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return no solution ; - E - D 5 Ex: work on board Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w ellman-ford Running time O(VE) Not so good for large dense graphs Still very practical 8

9 // ellman-ford Prove: lgorithm after V - passes of the for loop, all d values (shortest path values) are correct (for all vertices) onsider shortest path from s to v, since the path is simple (no loops) there are at most V - edges s v v v v v Initially, d[s] = is correct, and doesn t change (Why?) fter pass through edges, d[v ] is correct (Why?) and doesn t change fter passes, d[v ] is correct and doesn t change Terminates in V - passes: (Why?) What if it doesn t? ellman-ford Note that order in which edges are processed affects how quickly it converges orrectness: show d[v] = δ(s,v) for all vertices or returns FLSE (negative weight cycle) y previous lemma at the termination we have d[v] = δ(s,v) <= δ(s,u) + w(u,v) by triangle inequality = d[u] + w(u,v) nd none of the test in the last loop will return no solution, i.e. TRUE. if (d[v] > d[u] + w(u,v)) return no solution ; If there is a negative weight cycle prove by contradiction that ellman Ford algorithm will return FLSE (see book) 9

10 // DG Shortest Paths Problem: finding shortest paths in DG ellman-ford takes O(VE) time. How can we do better? Idea: use topological sort If were lucky and process vertices on each shortest path from left to right, would be done in one pass Every path in a DG is subsequence of topologically sorted vertex order, so processing vertices in that order, we will do each path in forward order (will never relax edges out of vertex before doing all edges into vertex). Thus: just one pass. What will be the running time? Review: Dijkstra s lgorithm Dijkstra(G) for each v V d[v] = ; d[s] = ; S = ; Q = V; 5 while (Q ) u = ExtractMin(Q); S = S U {u}; for each v u->dj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Note: this is really a call to Q->DecreaseKey() Ex: run the algorithm D Relaxation Step

11 // orrectness Of Dijkstra's lgorithm s p x y p u. See the description of the proof in the book Show that Dijkstra s algorithm will terminate with The cost of each node to be the cost of shortest path. Idea: show that when the vertex is added to the set the cost of that vertex is the length of the shortest path Reminder: We always add the vertex with minimal cost orrectness Of Dijkstra's lgorithm s p x y p u Want to show that when vertex is added to set S, d[u] = δ(s,u) and throughout note that d[u] δ(s,u) u Proof by contradiction d[u] is not equal to δ(s,u) efore u gets added, some other vertex y on that shortest path needs to be added; claim that d[y] = δ(s,y) when added. Know that d[x] = δ(s,x) and δ(s,y) <= δ(s,u) and d[y] = δ(s,y), so d[y] <= d[u] ut both y and u are outside of S when is chosen so d[u] <= d[y] Hence d[y] = d[u] = δ(s,y) = δ(s,y)

12 // Previously Shortest path algorithms: given single source compute shortest path to all other vertices. Dijkstra O( E + V lg V) ellman-ford O(VE) DG O(V+E) ll pairs shortest path: compute shortest path between each pair of nodes Option. Run single source shortest path from each node using previous algorithms Run ellman-ford once for each vertex O(V E) an we do better? See ll-pairs-shortest path Dynamic Programming hap 5.

13 // Dynamic Programming nother strategy for designing algorithms is dynamic programming metatechnique, not an algorithm (like divide & conquer) The word programming is historical and predates computer programming Use when problem breaks down into recurring small subproblems Dynamic Programming History ellman. Pioneered the systematic study of dynamic programming in the 95s. Etymology. Dynamic programming = planning over time. Secretary of Defense was hostile to mathematical research. ellman sought an impressive name to avoid confrontation. "it's impossible to use dynamic in a pejorative sense" "something not even a ongressman could object to" Reference: ellman, R. E. Eye of the Hurricane, n utobiography.

14 // Dynamic Programming pplications reas. ioinformatics. ontrol theory. Information theory. Operations research. omputer science: theory, graphics, I, systems,. Some famous dynamic programming algorithms. Viterbi for hidden Markov models. Unix diff for comparing two files. Smith-Waterman for sequence alignment. ellman-ford for shortest path routing in networks. ocke-kasami-younger for parsing context free grammars. More Examples Dynamic Programming Matrix chain multiplication Longest common subsequence Optimal triangulation ll-pairs-shortest path

15 // Dynamic Programming Problem solving methodology (as divide and conquer) Idea: divide into sub-problems, solve sub-problems pplicable to optimization problems Ingredients. haracterize the optimal solution. Recursively define a value of the optimal solution. ompute values of optimal solution bottom up. onstruct an optimal solution from computed inf. Dynamic programming It is used, when the solution can be recursively described in terms of solutions to sub-problems (optimal substructure) lgorithm finds solutions to sub-problems and stores them in memory for later use More efficient than brute-force methods, which solve the same sub-problems over and over again 5

16 // Weighted Interval Scheduling Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two jobs compatible if they don't overlap. Goal: find maximum weight subset of mutually compatible jobs. a b c d e f g h Time 6

17 // Unweighted Interval Scheduling Review Observation. Greedy algorithm can fail spectacularly if arbitrary weights are allowed. weight = 999 weight = a b Time Weighted Interval Scheduling Notation. Label jobs by finishing time: f f... f n. Def. p(j) = largest index i < j such that job i is compatible with j. Ex: p(8) = 5, p(7) =, p() = Time 7

18 // Dynamic Programming: inary hoice Notation. OPT(j) = value of optimal solution to the problem consisting of job requests,,..., j. optimal substructure ase : OPT selects job j. can't use incompatible jobs { p(j) +, p(j) +,..., j - } must include optimal solution to problem consisting of remaining compatible jobs,,..., p(j) ase : OPT does not select job j. must include optimal solution to problem consisting of remaining compatible jobs,,..., j- # if j = OPT( j) = $ % max v j + OPT( p( j)), OPT( j ) { } otherwise Weighted Interval Scheduling: rute Force rute force recursive implemenation Input: n, s,,s n, f,,f n, v,,v n Sort jobs by finish times so that f f... f n. ompute p(), p(),, p(n) ompute-opt(j) { if (j = ) return else return max(v j + ompute-opt(p(j)), ompute- Opt(j-)) } 8

19 // Weighted Interval Scheduling: rute Force Observation. Recursive algorithm fails spectacularly because of redundant sub-problems exponential algorithms. Ex. Number of recursive calls for family of "layered" instances grows like Fibonacci sequence. 5 5 p() =, p(j) = j- Weighted Interval Scheduling: Memoization Memoization. Store results of each sub-problem in a cache; lookup as needed. Input: n, s,,s n, f,,f n, v,,v n Sort jobs by finish times so that f f... f n. ompute p(), p(),, p(n) for j = to n M[j] = empty M[j] = global array M-ompute-Opt(j) { if (M[j] is empty) M[j] = max(w j + M-ompute-Opt(p(j)), M-ompute- Opt(j-)) return M[j] } 9

20 // Weighted Interval Scheduling: Running Time laim. Memoized version of algorithm takes O(n log n) time. Sort by finish time: O(n log n). omputing p( ) : O(n) after sorting by start time. M-ompute-Opt(j): each invocation takes O() time and either (i) returns an existing value M[j] (ii) fills in one new entry M[j] and makes two recursive calls Progress measure Φ = # nonempty entries of M[]. initially Φ =, throughout Φ n. (ii) increases Φ by at most n recursive calls. Overall running time of M-ompute-Opt(n) is O(n). Remark. O(n) if jobs are pre-sorted by start and finish times. Weighted Interval Scheduling: Finding a Solution Q. Dynamic programming algorithms computes optimal value. What if we want the solution itself?. Do some post-processing. Run M-ompute-Opt(n) Run Find-Solution(n) Find-Solution(j) { if (j = ) output nothing else if (v j + M[p(j)] > M[j-]) print j Find-Solution(p(j)) else Find-Solution(j-) }

21 // Weighted Interval Scheduling: ottom-up ottom-up dynamic programming. Unwind recursion. Input: n, s,,s n, f,,f n, v,,v n Sort jobs by finish times so that f f... f n. ompute p(), p(),, p(n) Iterative-ompute-Opt { M[] = for j = to n M[j] = max(v j + M[p(j)], M[j-]) } Dynamic Programming Example: Longest ommon Subsequence Longest common subsequence (LS) problem: Given two sequences x[..m] and y[..n], find the longest subsequence which occurs in both Ex: x = { D }, y = { D } { } and { } are both subsequences of both What is the LS? rute-force algorithm: For every subsequence of x, check if it s a subsequence of y How many subsequences of x are there? What will be the running time of the brute-force alg?

22 // Longest ommon Subsequence (LS) pplication: comparison of two DN strings Ex: X= { D }, Y= { D } Longest ommon Subsequence: X = D Y = D rute force algorithm would compare each subsequence of X with the symbols in Y LS lgorithm rute-force algorithm: m subsequences of x to check against n elements of y: O(n m ) We can do better: for now, let s only worry about the problem of finding the length of LS When finished we will see how to backtrack from this solution back to the actual LS Notice LS problem has optimal substructure Subproblems: LS of pairs of prefixes of x and y

23 // LS lgorithm First we ll find the length of LS. Later we ll modify the algorithm to find LS itself. Define X i, Y j to be the prefixes of X and Y of length i and j respectively Define c[i,j] to be the length of LS of X i and Y j Then the length of LS of X and Y will be c[m,n] c[ i, c[ i, j ] + j] = max( c[ i, j ], c[ i, j]) if x[ i] = y[ j], otherwise c[ i, LS recursive solution c[ i, j ] + j] = max( c[ i, j ], c[ i, if x[ i] = j]) otherwise y[ j], We start with i = j = (empty substrings of x and y) Since X and Y are empty strings, their LS is always empty (i.e. c[,] = ) LS of empty string and any other string is empty, so for every i and j: c[, j] = c[i,] =

24 // c[ i, LS recursive solution c[ i, j ] + j] = max( c[ i, j ], c[ i, if x[ i] = j]) otherwise y[ j], When we calculate c[i,j], we consider two cases: First case: x[i]=y[j]: one more symbol in strings X and Y matches, so the length of LS X i and Y j equals to the length of LS of smaller strings X i- and Y i-, plus LS recursive solution c[ i, j ] + c[ i, j] = max( c[ i, j ], c[ i, if x[ i] = j]) otherwise y[ j], Second case: x[i]!= y[j] s symbols don t match, our solution is not improved, and the length of LS(X i, Y j ) is the same as before (i.e. maximum of LS(X i, Y j- ) and LS(X i-,y j ) Why not just take the length of LS(X i-, Y j- )?

25 // LS Length lgorithm LS-Length(X, Y). m = length(x) // get the # of symbols in X. n = length(y) // get the # of symbols in Y. for i = to m c[i,] = // special case: Y. for j = to n c[,j] = // special case: X 5. for i = to m // for all X i 6. for j = to n // for all Y j 7. if ( X i == Y j ) 8. c[i,j] = c[i-,j-] + 9. else c[i,j] = max( c[i-,j], c[i,j-] ). return c LS Example We ll see how LS algorithm works on the following example: X = Y = D What is the Longest ommon Subsequence of X and Y? LS(X, Y) = X = Y = D 5

26 // i LS Example () j 5 Yj D Xi D X = ; m = X = Y = D; n = Y = 5 llocate array c[5,] i LS Example () j 5 Yj D D Xi for i = to m c[i,] = for j = to n c[,j] = 6

27 // i LS Example () j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) i LS Example () j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) 7

28 // i LS Example () j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) i LS Example (5) j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) 8

29 // i LS Example (6) j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) i LS Example (7) j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) 9

30 // i LS Example (8) j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) i LS Example () j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] )

31 // i LS Example () j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) i LS Example () j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] )

32 // i LS Example () j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) LS Example () D i j 5 Yj D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] )

33 // i LS Example (5) j 5 Yj D D Xi if ( X i == Y j ) c[i,j] = c[i-,j-] + else c[i,j] = max( c[i-,j], c[i,j-] ) LS lgorithm Running Time LS algorithm calculates the values of each entry of the array c[m,n] So what is the running time? O(m*n) since each c[i,j] is calculated in constant time, and there are m*n elements in the array

34 // How to find actual LS So far, we have just found the length of LS, but not LS itself. We want to modify this algorithm to make it output Longest ommon Subsequence of X and Y Each c[i,j] depends on c[i-,j] and c[i,j-] or c[i-, j-] For each c[i,j] we can say how it was acquired: For example, here c[i,j] = c[i-,j-] + = += How to find actual LS - continued Remember that c[ i, j ] + c[ i, j] = max( c[ i, j ], c[ i, j]) So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-, j-]+, remember x[i] (because x[i] is a part of LS) if x[ i] = otherwise When i= or j= (i.e. we reached the beginning), output remembered letters in reverse order y[ j],

35 // i Finding LS j 5 Yj D Xi i Finding LS () j 5 Yj D Xi LS (reversed order): LS (straight order): 5

36 // Review: Dynamic Programming Summary of the basic idea: Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems Overlapping subproblems: few subproblems in total, many recurring instances of each Solve bottom-up, building a table of solved subproblems that are used to solve larger ones Variations: Table could be -dimensional, triangular, a tree, etc. omputer Vision: Energy minimization: dynamic programming v v v v v 5 v 6 With this form of the energy function, we can minimize using dynamic programming, with the Viterbi algorithm. Iterate until optimal position for each point is the center of the box, i.e., the snake is optimal in the local search space constrained by boxes. Fig from Y. oykov [mini, Weymouth, Jain, 99] 6

37 // Energy minimization: dynamic programming Possible because snake energy can be rewritten as a sum of pair-wise interaction potentials: n E total (ν,,ν n ) = E i (ν i,ν i+ ) i= Or sum of triple-interaction potentials. n E total (ν,,ν n ) = E i (ν i,ν i,ν i+ ) i= Viterbi algorithm Main idea: determine optimal position (state) of predecessor, for each possible position of self. Then backtrack from best state for last vertex. E total = E (v,v ) + E (v,v ) E n (v n,v n ) states m vertices E (v,v ) E (v,v ) E (v,v ) v v v v E () = E () = E () = E (m) = E ( m) E ( ) E ( ) m m E (v,v n ) E () E () E () E n () E () E () E () E n () E () E () E () E n () v n E n (m) omplexity: O(nm ) vs. brute force search? Example adapted from Y. oykov 7

38 // Viterbi alg. dynamic programming We are interested in the assignment of states for vertices, v,v,...v n such that total energy is minimized. Each vertex can be in one of the m states s,s,...s m min vn min vn... min v = [E (v,v ) + E (v,v ) E n (v n,v n )] min vn min vn = [E n (v n,v n ) E (v,v ) + min v E (v,v )] min vn min vn = [E n (v n,v n ) E (v,v ) + M, (v )].. min vn = [E n (v n,v n ) + M n,n (x n )] memoize the partial solution Matrix hain Multiplication Given sequence of matrices n nd their dimensions p, p, p, p n What is the optimal order of multiplication Example: why two different orders matter? rute force strategy examine all possible parenthezations n P(n)= P(k)P(n k) k= Solution to the recurrence P(n)= Ω( n /n / ) 8

39 // Matrix hain Multiplication Substructure property k k + n Total cost will be cost of solving the two subproblems and multiplying the two resulting matrices ( k )( k + n ) Optimal substructure find the split which will yield the minimal total cost Idea: try to define it recursively Matrix hain Multiplication Define the cost recursively m[i,j] cost of multiplying i j $ & if i = j, m[i, j] = % min{m[i,k] + m[k +, j] + p i p k p j } otherwise '& i k< j 9

40 // Matrix hain Multiplication Option : ompute the cost recursively, remember good splits Draw the recurrence tree for Matrix hain Multiplication Look up the pseudo-code in the textbook ore is the recursive call = RecursiveMatrixhain(p,i,k) + RecursiveMatrixhain(p,k+,j) + Prove by substitution p i p k p j T(n) + (T(k) + T(n k) +) T(n) + n k= n i= T(n) = Ω( n ) T(i) + n Recursive solution would still take exponential time

41 // Matrix hain Multiplication Idea: memoization Look at the recursion tree, many of the sub-problems repeat Remember then and reuse in the n + n How many sub-problems do we have? Why? T(n) = Θ(n ) ompute the solution to all subproblems bottom up Memoize in the table store intermediate cost m[i,j] Matrix hain Multiplication. n = length(p)-. for i= to n m[i,i] = ; % initialize. for l= to n % l is the chain length. for i= to n-l+ % first compute all m[i,i +], then m[i,i+] 5. do j := i+- 6. m[i,j] inf 7. for k = i to j- 8. do q = m[i,k] + m[k+,j] + p(i-)p(k)p(j) 9. if q < m[i,j] then. m[i,j] = q;. s[i,j] = k; % remember k with min cost. end. end. end 5. Return m and s

42 // Example Matrix hain Multiplication Dynamic Programming What is the structure of the sub-problem ommon pattern: Optimal solution requires making a choice which leads to optimal solution Hard part: what is the optimal subproblem structure How many subproblems? How many choices we have which sub-problem to use? Matrix chain multiplication LS

43 // Dynamic Programming What is the structure of the sub-problem ommon pattern: Optimal solution requires making a choice which leads to optimal solution Hard part: what is the optimal subproblem structure How many sub-problems? How many choices we have which sub-problem to use? Matrix chain multiplication: subproblems, j-i choices LS: suproblems choices Subtleties (graph examples) shortest path, longest path Previously Shortest path algorithms: given single source compute shortest path to all other vertices. Dijkstra O( E + V lg V) ellman-ford O(VE) DG O(V+E) ll pairs shortest path: compute shortest path between each pair of nodes Option. Run single source shortest path from each node using previous algorithms Run ellman-ford once for each vertex O(V E) an we do better?

44 // ll pairs shortest path Final representation of the solution is in adjacency matrix δ(i,j) will be the length of the shortest path from i to j Structure of the optimal solution # d ij = $ % if i = j otherwise (m d ) (m ij = min(d ) (m ij,min k n {d ) ik + w kj }) Weight of the shortest path with m- edges and minimum of the weight of any path consisting of at most m edges

45 // Example all shortest paths D () D () D () = W D () = D () W = WW D () = D () W = D () WW Matrix multiplication Repeated Squaring Θ(n ) Θ(n lgn) (m d ) (m ij = min(d ) (m ij,min k n {d ) ik + w kj }) Like matrix multiplication + => min. => + Example all shortest paths D () D () D () D () D () = W D () = D () W = WW D () = D () W = D () WW Matrix multiplication Θ(n ) Repeated Squaring Θ(n lgn) 5

Algorithmic Paradigms

Algorithmic Paradigms Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer. Break up a problem into two or more sub -problems, solve each sub-problem

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 16 Dynamic Programming (plus FFT Recap) Adam Smith 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Discrete Fourier Transform

More information

Algorithmic Paradigms. Chapter 6 Dynamic Programming. Steps in Dynamic Programming. Dynamic Programming. Dynamic Programming Applications

Algorithmic Paradigms. Chapter 6 Dynamic Programming. Steps in Dynamic Programming. Dynamic Programming. Dynamic Programming Applications lgorithmic Paradigms reed. Build up a solution incrementally, only optimizing some local criterion. hapter Dynamic Programming Divide-and-conquer. Break up a problem into two sub-problems, solve each sub-problem

More information

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16 Dynamic Programming Introduction, Weighted Interval Scheduling, Knapsack Tyler Moore CSE, SMU, Dallas, TX Lecture /6 Greedy. Build up a solution incrementally, myopically optimizing some local criterion.

More information

Chapter 6. Dynamic Programming. Modified from slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 6. Dynamic Programming. Modified from slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 6 Dynamic Programming Modified from slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 Think recursively (this week)!!! Divide & conquer and Dynamic programming

More information

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456 5.4 Shortest Paths CH08-320201: Algorithms and Data Structures 456 Definitions: Path Consider a directed graph G=(V,E), where each edge e є E is assigned a non-negative weight w: E -> R +. A path is a

More information

Shortest path problems

Shortest 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 information

CS Algorithms. Dynamic programming and memoization. (Based on slides by Luebke, Lim, Wenbin)

CS 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 information

Dynamic Programming. Applications. Applications. Applications. Algorithm Design 6.1, 6.2, 6.3

Dynamic Programming. Applications. Applications. Applications. Algorithm Design 6.1, 6.2, 6.3 Set of weighted intervals with start and finishing times Goal: find maimum weight subset of non-overlapping intervals Dnamic Programming Algorithm Design.,.,. j j j j8 Given n points in the plane find

More information

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS473-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 information

Lectures 12 and 13 Dynamic programming: weighted interval scheduling

Lectures 12 and 13 Dynamic programming: weighted interval scheduling Lectures 12 and 13 Dynamic programming: weighted interval scheduling COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski Lectures 12-13: Dynamic Programming 1 Overview Last week: Graph

More information

COMP251: Single source shortest paths

COMP251: Single source shortest paths COMP51: Single source shortest paths Jérôme Waldispühl School of Computer Science McGill University Based on (Cormen et al., 00) Problem What is the shortest road to go from one city to another? Eample:

More information

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming Dynamic Programming 1 Introduction to Dynamic Programming weighted interval scheduling the design of a recursive solution memoizing the recursion 2 Principles of Dynamic Programming memoization or iteration

More information

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

Elements 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 information

12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares

12 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 information

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest 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 information

Algorithms Dr. Haim Levkowitz

Algorithms 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 information

CMSC 451: Dynamic Programming

CMSC 451: Dynamic Programming CMSC 41: Dynamic Programming Slides By: Carl Kingsford Department of Computer Science University of Maryland, College Park Based on Sections 6.1&6.2 of Algorithm Design by Kleinberg & Tardos. Dynamic Programming

More information

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest 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 information

Single Source Shortest Path

Single Source Shortest Path Single Source Shortest Path A directed graph G = (V, E) and a pair of nodes s, d is given. The edges have a real-valued weight W i. This time we are looking for the weight and the shortest path from s

More information

Shortest Path Problem

Shortest Path Problem Shortest Path Problem CLRS Chapters 24.1 3, 24.5, 25.2 Shortest path problem Shortest path problem (and variants) Properties of shortest paths Algorithmic framework Bellman-Ford algorithm Shortest paths

More information

Algorithms for Data Science

Algorithms 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 information

Minimum Spanning Trees

Minimum 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 information

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems Chapter 24. Shortest path problems We are given a directed graph G = (V,E) with each directed edge (u,v) E having a weight, also called a length, w(u,v) that may or may not be negative. A shortest path

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming and data-structures CS/ENGRD 20 SUMMER 208 Lecture 3: Shortest Path http://courses.cs.cornell.edu/cs20/208su Graph Algorithms Search Depth-first search Breadth-first search

More information

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

IN101: 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 information

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

CS420/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 information

Shortest Paths. Shortest Path. Applications. CSE 680 Prof. Roger Crawfis. Given a weighted directed graph, one common problem is finding the shortest

Shortest Paths. Shortest Path. Applications. CSE 680 Prof. Roger Crawfis. Given a weighted directed graph, one common problem is finding the shortest Shortest Path Introduction to Algorithms Shortest Paths CS 60 Prof. Roger Crawfis Given a weighted directed graph, one common problem is finding the shortest path between two given vertices Recall that

More information

Unit-5 Dynamic Programming 2016

Unit-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 information

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck Theory of Computing Lecture 7 MAS 714 Hartmut Klauck Shortest paths in weighted graphs We are given a graph G (adjacency list with weights W(u,v)) No edge means W(u,v)=1 We look for shortest paths from

More information

Dynamic Programming II

Dynamic 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 information

from notes written mostly by Dr. Carla Savage: All Rights Reserved

from 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 information

Tutorial 6-7. Dynamic Programming and Greedy

Tutorial 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 information

Last week: Breadth-First Search

Last 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 information

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree CS3 Lecture 0 Jana Kosecka Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree Many slides here are based on E. Demaine, D. Luebke, Kleinberg-Tardos slides Graph Algs.

More information

So far... Finished looking at lower bounds and linear sorts.

So 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 information

Dynamic Programming (Part #2)

Dynamic 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 information

Chapter 6. Dynamic Programming

Chapter 6. Dynamic Programming Chapter 6 Dynamic Programming CS 573: Algorithms, Fall 203 September 2, 203 6. Maximum Weighted Independent Set in Trees 6..0. Maximum Weight Independent Set Problem Input Graph G = (V, E) and weights

More information

Week 11: Minimum Spanning trees

Week 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 information

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar.. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar.. Shortest Path Problems Algorithms on Graphs: Part III Path in a graph. Let G = V,E be a graph. A path p = e 1,...,e k, e i E,

More information

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16 600.463 Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16 11.1 Introduction Dynamic programming can be very confusing until you ve used it a

More information

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017 CMSC 45 CMSC 45: Lecture Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct, Reading: Section. in KT. Dynamic Programming: In this lecture we begin our coverage of an important algorithm design

More information

Lecture 4: Dynamic programming I

Lecture 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 information

Special Topics on Algorithms Fall 2017 Dynamic Programming. Vangelis Markakis, Ioannis Milis and George Zois

Special Topics on Algorithms Fall 2017 Dynamic Programming. Vangelis Markakis, Ioannis Milis and George Zois Special Topics on Algorithms Fall 2017 Dynamic Programming Vangelis Markakis, Ioannis Milis and George Zois Basic Algorithmic Techniques Content Dynamic Programming Introduc

More information

Data Structures and Algorithms Week 8

Data 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 information

Title. 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. 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 information

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4. Weighted Graphs 1 Outline (single-source) shortest path Dijkstra (Section 4.4) Bellman-Ford (Section 4.6) (all-pairs) shortest path Floyd-Warshall (Section 6.6) minimum spanning tree Kruskal (Section 5.1.3)

More information

Introduction to Algorithms. Lecture 11

Introduction to Algorithms. Lecture 11 Introduction to Algorithms Lecture 11 Last Time Optimization Problems Greedy Algorithms Graph Representation & Algorithms Minimum Spanning Tree Prim s Algorithm Kruskal s Algorithm 2 Today s Topics Shortest

More information

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Dynamic 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 information

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2 CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 11: Dynamic Programming, Part 2 Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Goal: continue with DP (Knapsack, All-Pairs SPs, )

More information

Lecture 8. Dynamic Programming

Lecture 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 information

COT 6405 Introduction to Theory of Algorithms

COT 6405 Introduction to Theory of Algorithms COT 6405 Introduction to Theory of Algorithms Topic 16. Single source shortest path 11/18/2015 1 Problem definition Problem: given a weighted directed graph G, find the minimum-weight path from a given

More information

More Graph Algorithms: Topological Sort and Shortest Distance

More 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

Single Source Shortest Path (SSSP) Problem

Single 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 information

Dynamic Programming CS 445. Example: Floyd Warshll Algorithm: Computing all pairs shortest paths

Dynamic 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 information

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12 Algorithms Theory 14 Dynamic Programming (2) Matrix-chain multiplication P.D. Dr. Alexander Souza Optimal substructure Dynamic programming is typically applied to optimization problems. An optimal solution

More information

Final Exam Review. Final Exam 5/7/14

Final Exam Review. Final Exam 5/7/14 Final Exam Review Final Exam Coverage: second half of the semester Requires familiarity with most of the concepts covered in first half Goal: doable in 2 hours Cheat sheet: you are allowed two 8 sheets,

More information

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Dynamic 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 information

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

Module 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 information

4.1 Interval Scheduling

4.1 Interval Scheduling 41 Interval Scheduling Interval Scheduling Interval scheduling Job j starts at s j and finishes at f j Two jobs compatible if they don't overlap Goal: find maximum subset of mutually compatible jobs a

More information

Dijkstra s Shortest Path Algorithm

Dijkstra s Shortest Path Algorithm Dijkstra s Shortest Path Algorithm DPV 4.4, CLRS 24.3 Revised, October 23, 2014 Outline of this Lecture Recalling the BFS solution of the shortest path problem for unweighted (di)graphs. The shortest path

More information

Dynamic Programming. CSE 421: Intro Algorithms. Some Algorithm Design Techniques, I. Techniques, II. Outline:

Dynamic Programming. CSE 421: Intro Algorithms. Some Algorithm Design Techniques, I. Techniques, II. Outline: Dynamic Programming CSE 42: Intro Algorithms Summer 2007 W. L. Ruzzo Dynamic Programming, I Fibonacci & Stamps Outline: General Principles Easy Examples Fibonacci, Licking Stamps Meatier examples RNA Structure

More information

Input: n jobs (associated start time s j, finish time f j, and value v j ) for j = 1 to n M[j] = empty M[0] = 0. M-Compute-Opt(n)

Input: n jobs (associated start time s j, finish time f j, and value v j ) for j = 1 to n M[j] = empty M[0] = 0. M-Compute-Opt(n) Objec&ves Dnamic Programming Ø Wrapping up: weighted interval schedule Ø Ø Subset Sums Summar: Proper&es of Problems for DP Polnomial number of subproblems Solu&on to original problem can be easil computed

More information

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Introduction 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 information

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

CMPS 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 information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.) Dynamic programming is a problem solving method that is applicable to many different types of problems. I think it is best learned by example, so we will mostly do examples today. 1 Rod cutting Suppose

More information

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects

More information

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4. 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.) to produce an ordering of the items that satisfies the given

More information

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CSC 373 Lecture # 3 Instructor: Milad Eftekhar Huffman encoding: Assume a context is available (a document, a signal, etc.). These contexts are formed by some symbols (words in a document, discrete samples from a signal, etc). Each symbols s i is occurred

More information

1 Dijkstra s Algorithm

1 Dijkstra s Algorithm Lecture 11 Dijkstra s Algorithm Scribes: Himanshu Bhandoh (2015), Virginia Williams, and Date: November 1, 2017 Anthony Kim (2016), G. Valiant (2017), M. Wootters (2017) (Adapted from Virginia Williams

More information

Dynamic Programming part 2

Dynamic 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 information

Analysis of Algorithms, I

Analysis 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 information

Introduction to Algorithms

Introduction 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 information

Data Structures and Algorithms. Werner Nutt

Data Structures and Algorithms. Werner Nutt Data Structures and Algorithms Werner Nutt nutt@inf.unibz.it http://www.inf.unibz/it/~nutt Chapter 10 Academic Year 2012-2013 1 Acknowledgements & Copyright Notice These slides are built on top of slides

More information

Chapter 3 Dynamic programming

Chapter 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 information

CSE 421: Intro Algorithms. Winter 2012 W. L. Ruzzo Dynamic Programming, I Intro: Fibonacci & Stamps

CSE 421: Intro Algorithms. Winter 2012 W. L. Ruzzo Dynamic Programming, I Intro: Fibonacci & Stamps CSE 421: Intro Algorithms Winter 2012 W. L. Ruzzo Dynamic Programming, I Intro: Fibonacci & Stamps 1 Dynamic Programming Outline: General Principles Easy Examples Fibonacci, Licking Stamps Meatier examples

More information

Algorithm Design Techniques part I

Algorithm 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 information

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Practical Session No. 12 Graphs, BFS, DFS, Topological sort Practical Session No. 12 Graphs, BFS, DFS, Topological sort Graphs and BFS Graph G = (V, E) Graph Representations (V G ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in

More information

CS 231: Algorithmic Problem Solving

CS 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 information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs

More information

Presentation 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, 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 information

Lecture 11: Analysis of Algorithms (CS ) 1

Lecture 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 information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

CSE 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 information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 07 Single-Source Shortest Paths (Chapter 24) Stephen Scott and Vinodchandran N. Variyam sscott@cse.unl.edu 1/36 Introduction

More information

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Algorithms. 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

Dynamic Programming. December 15, CMPE 250 Dynamic Programming December 15, / 60

Dynamic Programming. December 15, CMPE 250 Dynamic Programming December 15, / 60 Dynamic Programming December 15, 2016 CMPE 250 Dynamic Programming December 15, 2016 1 / 60 Why Dynamic Programming Often recursive algorithms solve fairly difficult problems efficiently BUT in other cases

More information

Introduction to Algorithms

Introduction 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 information

Classical Shortest-Path Algorithms

Classical Shortest-Path Algorithms DISTANCE PROBLEMS IN NETWORKS - THEORY AND PRACTICE Classical Shortest-Path Algorithms Nesrine Damak October 10, 2010 Abstract In this work, we present four algorithms for the shortest path problem. The

More information

Unit 2: Algorithmic Graph Theory

Unit 2: Algorithmic Graph Theory Unit 2: Algorithmic Graph Theory Course contents: Introduction to graph theory Basic graph algorithms Reading Chapter 3 Reference: Cormen, Leiserson, and Rivest, Introduction to Algorithms, 2 nd Ed., McGraw

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 018 D/Q Greed SP s DP LP, Flow B&B, Backtrack Metaheuristics P, NP Design and Analysis of Algorithms Lecture 8: Greed Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Optimization

More information

ECE608 - Chapter 15 answers

ECE608 - Chapter 15 answers ¼ À ÈÌ Ê ½ ÈÊÇ Ä ÅË ½µ ½ º¾¹¾ ¾µ ½ º¾¹ µ ½ º¾¹ µ ½ º ¹½ µ ½ º ¹¾ µ ½ º ¹ µ ½ º ¹¾ µ ½ º ¹ µ ½ º ¹ ½¼µ ½ º ¹ ½½µ ½ ¹ ½ ECE608 - Chapter 15 answers (1) CLR 15.2-2 MATRIX CHAIN MULTIPLY(A, s, i, j) 1. if

More information

Week 12: Minimum Spanning trees and Shortest Paths

Week 12: Minimum Spanning trees and Shortest Paths Agenda: Week 12: Minimum Spanning trees and Shortest Paths Kruskal s Algorithm Single-source shortest paths Dijkstra s algorithm for non-negatively weighted case Reading: Textbook : 61-7, 80-87, 9-601

More information

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24 /8/11 Single-Source Shortest Path Chapter 1 Shortest Paths Finding the shortest path between two nodes comes up in many applications o Transportation problems o Motion planning o Communication problems

More information

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u]. Tutorial Question 1 A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source

More information

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review 1 Review 1 Something I did not emphasize enough last time is that during the execution of depth-firstsearch, we construct depth-first-search trees. One graph may have multiple depth-firstsearch trees,

More information

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)

Recursive-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 information

Module 5 Graph Algorithms

Module 5 Graph Algorithms Module 5 Graph lgorithms Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 97 E-mail: natarajan.meghanathan@jsums.edu 5. Graph Traversal lgorithms Depth First

More information