Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures.

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Chapter 25: All-Pairs Shortest-Paths
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
RAIK 283: Data Structures & Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Dynamic Programming Reading Material: Chapter 7..
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
Dynamic Programming Dynamic Programming algorithms address problems whose solution is recursive in nature, but has the following property: The direct implementation.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:
Algorithms All pairs shortest path
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design techniqueDynamic Programming is a.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Analysis of Algorithms
All-Pairs-Shortest-Paths for Large Graphs on the GPU Gary J Katz 1,2, Joe Kider 1 1 University of Pennsylvania 2 Lockheed Martin IS&GS.
1 Dynamic programming algorithms for all-pairs shortest path and longest common subsequences We will study a new technique—dynamic programming algorithms.
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
More Dynamic Programming Floyd-Warshall Algorithm.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
RAIK 283: Data Structures & Algorithms
1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
MA/CSSE 473 Day 28 Dynamic Programming Binomial Coefficients Warshall's algorithm Student questions?
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
1 Chapter Equivalence, Order, and Inductive Proof.
Algorithmics - Lecture 121 LECTURE 11: Dynamic programming - II -
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
1 Section 4.1 Properties of Binary Relations A binary relation R over a set A is a subset of A  A. If (x, y)  R we also write x R y. Example. Some sample.
All-Pairs Shortest Paths
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
MA/CSSE 473 Day 30 B Trees Dynamic Programming Binomial Coefficients Warshall's algorithm No in-class quiz today Student questions?
All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya.
MA/CSSE 473 Day 27 Dynamic Programming Binomial Coefficients
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Algorithm Analysis Fall 2017 CS 4306/03
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
CS330 Discussion 6.
All-Pairs Shortest Paths (26.0/25)
Warshall’s and Floyd’sAlgorithm
Lecture 7 All-Pairs Shortest Paths
Analysis and design of algorithm
Chapter 8 Dynamic Programming
Floyd-Warshall Algorithm
Dynamic Programming.
Dynamic Programming.
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Dynamic Programming.
Dynamic Programming.
Lecture 21: Matrix Operations and All-pair Shortest Paths
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures & Algorithms

Design and Analysis of Algorithms - Chapter 82 b Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook’s companion websiteMost of the lecture notes are based on the slides from the Textbook’s companion website – Some of the notes are based on slides created by Dr. Philippe Laborie, ILOG and Mark Llewellyn, University of Central Florida.Some of the notes are based on slides created by Dr. Philippe Laborie, ILOG and Mark Llewellyn, University of Central Florida. I have modified many of their slides and added new slides.I have modified many of their slides and added new slides. RAIK 283: Data Structures & Algorithms

3 Examples of dynamic programming algorithms Coin-row problem Coin-row problem Computing binomial coefficients Computing binomial coefficients Longest Common Subsequence (LCS) Longest Common Subsequence (LCS) Warshall’s algorithm for transitive closureWarshall’s algorithm for transitive closure Floyd’s algorithm for all-pairs shortest paths Floyd’s algorithm for all-pairs shortest paths Some instances of difficult discrete optimization problems:Some instances of difficult discrete optimization problems: 0-1 knapsack0-1 knapsack

Design and Analysis of Algorithms - Chapter 84 Warshall’s algorithm: transitive closure Computes the transitive closure of a relation Computes the transitive closure of a relation (Alternatively: all paths in a directed graph) (Alternatively: all paths in a directed graph) Example of transitive closure: Example of transitive closure:

Design and Analysis of Algorithms - Chapter 85 Warshall’s algorithm Main idea: a path exists between two vertices i, j, iff Main idea: a path exists between two vertices i, j, iff there is an edge from i to j; orthere is an edge from i to j; or there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1}; orthere is a path from i to j going through intermediate vertices which are drawn from set {vertex 1}; or there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2}; orthere is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2}; or …

Design and Analysis of Algorithms - Chapter 86 Warshall’s algorithm Main idea: a path exists between two vertices i, j, iff Main idea: a path exists between two vertices i, j, iff there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2, … k-1}; orthere is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2, … k-1}; or there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2, … k}; orthere is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2, … k}; or there is a path from i to j going through any of the other verticesthere is a path from i to j going through any of the other vertices

Design and Analysis of Algorithms - Chapter 87 b Idea: dynamic programming Let V={1, …, n} and for k≤n, V k ={1, …, k}Let V={1, …, n} and for k≤n, V k ={1, …, k} For any pair of vertices i, j  V, identify all paths from i to j whose intermediate vertices are all drawn from V k : P ij k ={p1, p2, …}, if P ij k  then R k [i, j]=1For any pair of vertices i, j  V, identify all paths from i to j whose intermediate vertices are all drawn from V k : P ij k ={p1, p2, …}, if P ij k  then R k [i, j]=1 For any pair of vertices i, j: R n [i, j], that is R nFor any pair of vertices i, j: R n [i, j], that is R n Starting with R 0 =A, the adjacency matrix, how to get R 1  …  R k-1  R k  …  R nStarting with R 0 =A, the adjacency matrix, how to get R 1  …  R k-1  R k  …  R n ij P1 VkVk Warshall’s algorithm p2

Design and Analysis of Algorithms - Chapter 88 b Idea: dynamic programming p  P ij k : p is a path from i to j with all intermediate vertices in V kp  P ij k : p is a path from i to j with all intermediate vertices in V k If k is not on p, then p is also a path from i to j with all intermediate vertices in V k-1 : p  P ij k-1If k is not on p, then p is also a path from i to j with all intermediate vertices in V k-1 : p  P ij k-1 Warshall’s algorithm ij p V k-1 VkVk k

Design and Analysis of Algorithms - Chapter 89 b Idea: dynamic programming p  P ij k : p is a path from i to j with all intermediate vertices in V kp  P ij k : p is a path from i to j with all intermediate vertices in V k If k is on p, then we break down p into p 1 and p 2If k is on p, then we break down p into p 1 and p 2 –What are P 1 and P 2 ? Warshall’s algorithm ij V k-1 p VkVk k p1 p2

Design and Analysis of Algorithms - Chapter 810 b Idea: dynamic programming p  P ij k : p is a path from i to j with all intermediate vertices in V kp  P ij k : p is a path from i to j with all intermediate vertices in V k If k is on p, then we break down p into p 1 and p 2 whereIf k is on p, then we break down p into p 1 and p 2 where –p 1 is a path from i to k with all intermediate vertices in V k-1 –p 2 is a path from k to j with all intermediate vertices in V k-1 Warshall’s algorithm ij V k-1 p VkVk k p1 p2

Design and Analysis of Algorithms - Chapter 811 Warshall’s algorithm In the k th stage determine if a path exists between two vertices i, j using just vertices among 1, …, k In the k th stage determine if a path exists between two vertices i, j using just vertices among 1, …, k R (k-1) [i,j] (path using just 1, …, k-1) R (k) [i,j] = or R (k) [i,j] = or (R (k-1) [i,k] and R (k-1) [k,j]) (path from i to k (R (k-1) [i,k] and R (k-1) [k,j]) (path from i to k and from k to j and from k to j using just 1, …, k-1) using just 1, …, k-1) i j k k th stage {

Design and Analysis of Algorithms - Chapter 812 Warshall’s algorithm R R R R R

Design and Analysis of Algorithms - Chapter 813 Warshall’s algorithm R 0 = A R R R R

Design and Analysis of Algorithms - Chapter 814 In-class exercises b Apply Warshall’s algorithm to find the transitive closure of the digraph defined by the following adjacency matrix

In-class exercises b Problem 2 a. What is the time efficiency of Warshall’s algorithm? Design and Analysis of Algorithms - Chapter 815

In-class exercises b Problem 2 a. What is the time efficiency of Warshall’s algorithm? b. How to solve this “finding all paths in a directed graph” problem by a traversal-based algorithm (BFS-based or DFS-based)? Design and Analysis of Algorithms - Chapter 816

In-class exercises b Problem 2 a. What is the time efficiency of Warshall’s algorithm? b. How to solve this “finding all paths in a directed graph” problem by a traversal-based algorithm (BFS-based or DFS-based)? c. Explain why the time efficiency of Warshall’s algorithm is inferior to that of traversal-based algorithm for sparse graphs represented by their adjacency lists. Design and Analysis of Algorithms - Chapter 817

Design and Analysis of Algorithms - Chapter 818 Floyd’s algorithm: all pairs shortest paths In a weighted graph, find shortest paths between every pair of vertices In a weighted graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matrices D (0), D (1), … using an initial subset of the vertices as intermediaries.Same idea: construct solution through series of matrices D (0), D (1), … using an initial subset of the vertices as intermediaries. In D (k), d ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in an initial subset {u 1, u 2, … u k }In D (k), d ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in an initial subset {u 1, u 2, … u k } Example: Example:

Design and Analysis of Algorithms - Chapter 819 b Idea: dynamic programming Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k }Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k } To construct D (k), we need to get d ij kTo construct D (k), we need to get d ij k For any pair of vertices u i, u j  V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij kFor any pair of vertices u i, u j  V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij k uiui ujuj p VkVk Floyd’s algorithm: all pairs shortest paths

Design and Analysis of Algorithms - Chapter 820 b Idea: dynamic programming Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k }Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k } To construct D (k), we need to get d ij kTo construct D (k), we need to get d ij k For any pair of vertices u i, u j  V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij kFor any pair of vertices u i, u j  V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij k If u k is not on p, then what is d ij k equal to?If u k is not on p, then what is d ij k equal to? uiui ujuj p VkVk Floyd’s algorithm: all pairs shortest paths

Design and Analysis of Algorithms - Chapter 821 b Idea: dynamic programming If u k is not on p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1).If u k is not on p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1). If u k is on p, then we break down p into p 1 and p 2If u k is on p, then we break down p into p 1 and p 2 –What are p1, p2? Floyd’s algorithm: all pairs shortest paths

Design and Analysis of Algorithms - Chapter 822 b Idea: dynamic programming If u k is not in p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1).If u k is not in p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1). If u k is in p, then we break down p into p 1 and p 2 whereIf u k is in p, then we break down p into p 1 and p 2 where –p 1 is the shortest path from u i to u k with all intermediate vertices in V k-1 –p 2 is the shortest path from u k to u j with all intermediate vertices in V k-1 –i.e., d ij (k) = d ik (k-1) + d kj (k-1). Floyd’s algorithm: all pairs shortest paths

Design and Analysis of Algorithms - Chapter 823 b Idea: dynamic programming Construct matrices D (0), D (1), … D (k-1), D (k) … D (n)Construct matrices D (0), D (1), … D (k-1), D (k) … D (n) d ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in V kd ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in V k d ij (0) =w ijd ij (0) =w ij d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1 Floyd’s algorithm: all pairs shortest paths

Design and Analysis of Algorithms - Chapter 824 FLOYD(G) for i,j in [1..n] d[i,j]=w(u i,u j ) for k in [1..n] for i in [1..n] for j in [1..n] d[i,j]=min(d[i,j],d[i,k]+d[k,j]) Time complexity: O(n 3 ) Floyd’s algorithm: all pairs shortest paths

Design and Analysis of Algorithms - Chapter 825 In-Class Exercises u Apply the Floyd’s algorithm to the following problem instance: u D (0) = 0  3  2 0      0 b How to enhance Floyd’s algorithm to actually output the shortest path from node i to node j?

Enhanced Floyd’s Algorithm Design and Analysis of Algorithms - Chapter 826 FloydEnhanced(w[1..n, 1..n]) for i,j in [1..n] d[i,j]=w(u i,u j ) for i in [1..n] for j in [1..n] p[i,j]=0 for k in [1..n] for i in [1..n] for j in [1..n] if d[i,j] >d[i,k]+d[k,j] d[i,j]=d[i,k]+d[k,j] p[i,j] = k ShortestPath(i, j, p[1..n, 1..n]) k = p[i, j] if k  0 ShortestPath(i, k) print(k) ShortestPath(k, j)

Design and Analysis of Algorithms - Chapter 827 In-Class Exercises u Apply the enhanced Floyd’s algorithm, which finds the shortest paths and their lengths, to the following problem instance: u D (0) = 0  3  2 0      0

Dynamic programming b Dynamic programming is a technique for solving problems with overlapping subproblems. It suggests solving each smaller subproblem once and recording the results in a table from which a solution to the original problem can be then obtained. Design and Analysis of Algorithms - Chapter 828

Dynamic programming b Dynamic programming is a technique for solving problems with overlapping subproblems. It suggests solving each smaller subproblem once and recording the results in a table from which a solution to the original problem can be then obtained. b What are the overlapping subproblems in Floyd’s algorithm? Design and Analysis of Algorithms - Chapter 829 FLOYD(G) for i,j in [1..n] d[i,j]=w(u i,u j ) for k in [1..n] for i in [1..n] for j in [1..n] d[i,j]=min(d[i,j],d[i,k]+d[k,j])

Floyd’s Algorithm ij V k-1 p VkVk k p1 p2 p d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1 solutions for smaller subproblems  solution for a larger subproblem

Floyd’s Algorithm ij V k-1 p VkVk k p1 p2 d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1 d il (k) =min (d il (k-1), d ik (k-1) + d kl (k-1) ) for k≥1 solution for a smaller subproblem is used for getting solutions for multiple bigger subproblems l p3

Design and Analysis of Algorithms - Chapter 832 In-class exercise b What does dynamic programming have in common with divide-and-conquer? What is a principal difference between them?