Lecture 21: Matrix Operations and All-pair Shortest Paths

Slides:



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

© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
Directed Graphs Directed Graphs Shortest Path 4/10/ :45 AM BOS
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
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.
1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§6.4) Reachability (§6.4.1) Directed DFS Strong connectivity Transitive.
Lecture 16 Cramer’s Rule, Eigenvalue and Eigenvector Shang-Hua Teng.
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.
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
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.
1 Dijkstra’s Algorithm Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
All-Pairs Shortest Paths
Graph Theory. undirected graph node: a, b, c, d, e, f edge: (a, b), (a, c), (b, c), (b, e), (c, d), (c, f), (d, e), (d, f), (e, f) subgraph.
1 Digraphs Reachability Connectivity Transitive Closure Floyd-Warshall Algorithm JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 v 7.
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§12.4) Reachability (§12.4.1) Directed DFS Strong connectivity Transitive.
13.3 Product of a Scalar and a Matrix.  In matrix algebra, a real number is often called a.  To multiply a matrix by a scalar, you multiply each entry.
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Minimum-cost spanning tree
Directed Graphs 12/7/2017 7:15 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Minimum-Cost Spanning Tree and Kruskal’s Algorithm
Shortest Paths.
Shortest Paths C B A E D F Shortest Paths
CSC317 Shortest path algorithms
Algorithm Analysis Fall 2017 CS 4306/03
Directed Graphs 9/20/2018 1:45 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
All-Pairs Shortest Paths (26.0/25)
Directed Graphs 5/1/15 12:25:22 PM
Lecture 22: Parallel Algorithms
Data Structures & Algorithms Digraphs and DAGs
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
Shortest Paths C B A E D F
CMSC 341 Lecture 21 Graphs (Introduction)
Directed Graphs (digraphs)
Chapter 25: All-Pairs Shortest Paths
Lecture 16 Cramer’s Rule, Eigenvalue and Eigenvector
Warshall’s and Floyd’sAlgorithm
Lecture 7 All-Pairs Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Shortest Paths.
Lecture 7 Shortest Path Shortest-path problems
CS200: Algorithm Analysis
Chapter 13 Graph Algorithms
Floyd-Warshall Algorithm
Shortest Path Algorithms
Directed Graphs Directed Graphs Directed Graphs 2/23/ :12 AM BOS
Dynamic Programming.
Advanced Algorithms Analysis and Design
Single-source shortest paths
Algorithms (2IL15) – Lecture 7
Algorithms: Design and Analysis
Data Structures & Algorithms Digraphs and DAGs
Presented by-Kapil Kumar Cse-iii rd year
Shortest Paths.
Graphs.
Dynamic Programming.
Shortest Paths.
Linear Algebra review (optional)
Graph Vocabulary.
All Pairs Shortest Path Examples While the illustrations which follow only show solutions from vertex A (or 1) for simplicity, students should note that.
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng

Matrix Basic Vector: array of numbers; unit vector Inner product, outer product, norm Matrix: rectangular table of numbers, square matrix; Matrix transpose All zero matrix and all one matrix Identity matrix 0-1 matrix, Boolean matrix, matrix of graphs

Matrix of Graphs Else A(i, j) = 0. Adjacency Matrix: If A(i, j) = 1: edge exists Else A(i, j) = 0. 1 1 2 3 4 2 -3 4 3

Matrix of Graphs Else A(i, j) = infty. Weighted Matrix: If A(i, j) = w(i,j): edge exists Else A(i, j) = infty. 1 1 2 3 4 2 -3 4 3

Matrix Operations Matrix-vector operation Matrix operations System of linear equations Eigenvalues and Eigenvectors Matrix operations

Matrix Addition:

2. Scalar Multiplication:

3. Matrix Multiplication

Add and Multiply Rings: Commutative, Associative Distributive Other rings

Matrix Multiplication Can be Defined on any Ring

Two Graph Problems Transitive closure: whether there exists a path between every pair of vertices generate a matrix closure showing all transitive closures for instance, if a path exists from i to j, then closure[i, j] =1 All-pair shortest paths: shortest paths between every pair of vertices Doing better than Bellman-Ford O(|V|2|E|) They are very similar

Transitive Closure D E Given a digraph G, the transitive closure of G is the digraph G* such that G* has the same vertices as G if G has a directed path from u to v (u  v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph B G C A D E B C A G*

Transitive Closure and Matrix Multiplication Let A be the adjacency matrix of a graph G 1 2 3 4 -3 A

Floyd-Warshall, Iteration 2 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

Transitive Closure and Matrix Multiplication

A Better Idea

Even Better idea: Dynamic Programming; Floyd-Warshall Number the vertices 1, 2, …, n. Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: Uses only vertices numbered 1,…,k (add this edge if it’s not already in) i j Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 k

Floyd-Warshall’s Algorithm A is the original matrix, T is the transitive matrix T  A for(k=1:n) for(j=1:n) for(i=1:n) T[i, j] = T[i, j] OR (T[i, k] AND T[k, j]) It should be obvious that the complexity is (n3) because of the 3 nested for-loops T[i, j] =1 if there is a path from vertex i to vertex j

Floyd-Warshall Example BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

Floyd-Warshall, Iteration 1 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

Floyd-Warshall, Iteration 3 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

Floyd-Warshall, Iteration 4 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

Floyd-Warshall, Iteration 5 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

Floyd-Warshall, Iteration 6 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5

Floyd-Warshall, Conclusion BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5