Directed Graphs (Part II)

Slides:



Advertisements
Similar presentations
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Advertisements

Single Source Shortest Paths
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Shortest Paths Definitions Single Source Algorithms
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
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.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Dijkstra's algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on.
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
Prim’s MST Djikstra SP. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some.
Aaron Bloomfield CS 202 Rosen, section 7.4
Instructor: Lilian de Greef Quarter: Summer 2017
May 12th – Minimum Spanning Trees
Shortest Paths and Minimum Spanning Trees
CSC317 Shortest path algorithms
CSE373: Data Structures & Algorithms
Minimal Spanning Trees
Minimum Spanning Trees and Shortest Paths
Algorithms and Data Structures Lecture XIII
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
Jan 2007.
Combinations COURSE 3 LESSON 11-3
CSCE350 Algorithms and Data Structure
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Shortest Paths.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Analysis and design of algorithm
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
Graphs.
Algorithms and Data Structures Lecture XIII
Shortest Path Algorithms
Minimum Spanning Tree Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
AB AC AD AE AF 5 ways If you used AB, then, there would be 4 remaining ODD vertices (C, D, E and F) CD CE CF 3 ways If you used CD, then, there.
Slide Courtesy: Uwash, UT
Fundamental Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Text Book: Introduction to algorithms By C L R S
CSE332: Data Abstractions Lecture 17: Shortest Paths
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Shortest Paths.
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Richard Anderson Spring 2016
Closures of Relations Epp, section 10.1,10.2 CS 202.
Directed Graphs (Part II)
Topological Sorting Minimum Spanning Trees Shortest Path
Presentation transcript:

Directed Graphs (Part II) CSE 373 Data Structures

Dijkstra’s Shortest Path Algorithm Initialize the cost of source to 0, and all the rest of the nodes to  Initialize set S to be  S is the set of nodes to which we have a shortest path While S is not all vertices Select the node A with the lowest cost that is not in S and identify the node as now being in S for each node B adjacent to A if cost(A)+[A->B] < B’s currently known cost set cost(B) = cost(A)+[A->B] set previous(B) = A so that we can remember the path 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Example: Initialization  Cost(source) = 0 Cost(all vertices but source) = A 2 B  4 1 3 10 C 2 D 2 E    5 8 4 6 F 1 G   Pick vertex not in S with lowest cost. 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Example: Update Cost neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E   1 5 8 4 6 Cost(B) = 2 F 1 G Cost(D) = 1   11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Example: pick vertex with lowest cost and add it to S G F B E C D 4 1 2 10 3 6 8 5  Pick vertex not in S with lowest cost, i.e., D 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Example: update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Cost(C) = 1 + 2 = 3 Cost(E) = 1 + 2 = 3 Cost(F) = 1 + 8 = 9 Cost(G) = 1 + 4 = 5 F 1 G 9 5 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Example (Ct’d) Pick vertex not in S with lowest cost (B) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Note : cost(D) not updated since already in S and cost(E) not updated since it is larger than previously computed F 1 G 9 5 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Example: (ct’d) Pick vertex not in S (E) with lowest cost and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G No updating 9 5 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Example: (ct’d) Pick vertex not in S with lowest cost (C) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G 8 5 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Example: (ct’d) Pick vertex not in S with lowest cost (G) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G Previous cost 6 5 Cost(F) = min (8, 5+1) = 6 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Example (end) 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G 6 5 Pick vertex not in S with lowest cost (F) and update neighbors 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Data Structures Adjacency Lists adj next previous cost priority queue pointers cost P C Q G 1 2 3 4 5 6 7 2 2 4 1  4 3 5 10  1 4 6 5  3 2 5 2  7 6 6 8  7 4  6 1 Priority queue for finding and deleting lowest cost vertex and for decreasing costs (Binary Heap works) 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Object-Oriented Approach Vertex object has additional fields for cost, previous Alternatively, HashMap mapping Vertex to cost & previous Priority queue of vertices comparing on cost, which allows fast decreaseKeys 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Time Complexity |V| vertices and |E| edges Initialize data structures O(|V|+|E|) Find min cost vertices O(|V| log |V|) |V| deleteMins Update costs O(|E| log |V|) Potentially |E| updates Update previous pointers O(|E|) Total time O((|E| + |V|) log |V|) - very fast. (can be reduced to O(|E| log |V|) by fib or relaxed heap) 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Or… using selection-sort pq |V| vertices and |E| edges Initialize data structures O(|V|+|E|) Find min cost vertices O(|V|^2) |V| deleteMins Update costs O(|E|) Potentially |E| updates Update previous pointers O(|E|) Total time O(|V|^2+|E|) = O(|V|^2). 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Correctness Dijkstra’s algorithm is an example of a greedy algorithm Greedy algorithms always make choices that currently seem the best Short-sighted – no consideration of long-term or global issues Locally optimal does not always mean globally optimal In Dijkstra’s case – choose the least cost node, but what if there is another path through other vertices that is cheaper? 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

“Cloudy” Proof: The Idea Next shortest path from inside the known cloud Least cost node G THE KNOWN CLOUD competitor P Source If the path to G is the next shortest path, the path to P must be at least as long. Therefore, any path through P to G cannot be shorter! 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Inside the Cloud (Proof) Everything inside the cloud has the correct shortest path Proof is by induction on the number of nodes in the cloud: Base case: Initial cloud is just the source s with shortest path 0. Inductive hypothesis: Assume that a cloud of k-1 nodes all have shortest paths. Inductive step: choose the least cost node G  has to be the shortest path to G (previous slide). Add k-th node G to the cloud. To polish this off, note that it’s an inductive proof. Here’s the other problem with negative weights. A negative weight could make it actually better to wander outside the cloud for a while instead of going straight to a new unknown node. 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 A-star Search Heuristic of distance to goal Efficient in friendly situations, slow with unexpected obstacles Doesn’t calculate shortest path from source to all other nodes – rather calculates a path between source and destination Instead of using a cost of “distance traveled from source,” use a cost of “distance traveled from source plus estimated distance to goal” 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

All Pairs Shortest Path Given a edge weighted directed graph G = (V,E) find for all u,v in V the length of the shortest path from u to v. Use matrix representation. C 1 2 3 4 5 6 7 1 0 2 : 1 : : : 2 : 0 : 3 10 : : 3 4 : 0 : : 5 : 4 : : 2 0 2 8 4 5 : : : : 0 : 6 6 : : : : : 0 : 7 : : : : : 1 0 : = infinity 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

A (simpler) Related Problem: Transitive Closure Given a digraph G(V,E) the transitive closure is a digraph G’(V’,E’) such that V’ = V (same set of vertices) If (vi, vi+1,…,vk) is a path in G, then (vi, vk) is an edge of E’ 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Unweighted Digraph Boolean Matrix Representation C is called the connectivity matrix 1 = connected 0 = not connected 1 2 C 1 2 3 4 5 6 7 1 0 1 0 1 0 0 0 2 0 0 0 1 1 0 0 3 1 0 0 0 0 1 0 4 0 0 1 0 1 1 1 5 0 0 0 0 0 0 1 6 0 0 0 0 0 0 0 7 0 0 0 0 0 1 0 3 4 5 7 6 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Transitive Closure 1 2 C 1 2 3 4 5 6 7 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 5 0 0 0 0 0 1 1 6 0 0 0 0 0 0 0 7 0 0 0 0 0 1 0 3 4 5 7 6 On the graph, we show only the edges added with 1 as origin. The matrix represents the full transitive closure. 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Finding Paths of Length 2 // First initialize C2 to all zero // Length2 { for k = 1 to n for i = 1 to n do for j = 1 to n do C2[i,j] := C2[i,j]  (C[i,k]  C[k,j]); } where  is Boolean And (&&) and  is Boolean OR (||) This means if there is an edge from i to k AND an edge from k to j, then there is a path of length 2 between i and j. Column k (C[i,k]) represents the predecessors of k Row k (C[k,j]) represents the successors of k path of length 2 i k j 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Paths of Length 2 C 1 2 3 4 5 6 7 1 0 1 0 1 0 0 0 2 0 0 0 1 1 0 0 3 1 0 0 0 0 1 0 4 0 0 1 0 1 1 1 5 0 0 0 0 0 0 1 6 0 0 0 0 0 0 0 7 0 0 0 0 0 1 0 Time O(n3) 1 2 C2 3 1 2 3 4 5 6 7 1 0 0 1 1 1 1 1 2 0 0 1 0 1 1 1 3 0 1 0 1 0 0 0 4 1 0 0 0 0 1 1 5 0 0 0 0 0 1 0 6 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 4 5 7 6 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Transitive Closure Union of paths of length 0, length 1, length 2, …, length n-1. Time complexity n * O(n3) = O(n4) There exists a better (O(n3) ) algorithm: Warshall’s algorithm 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm i k j TransitiveClosure { for k = 1 to n do // k is the step number // for i = 1 to n do for j = 1 to n do C [i,j] := C[i,j]  (C[i,k]  C[k,j]); } where C[i,j] starts as the original connectivity matrix and C[i,j] is updated after step k if a new path from i to j through k is found. or and 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Proof of Correctness Prove: After the k-th time through the loop, C[i,j] =1 if there is a path from i to j that only passes through vertices numbered 1,2,…,k (except for the initial edges) Base case: k = 1. C [i,j] = 1 for the initial connectivity matrix (path of length 0) and C [i,j] = 1 if there is a path (i,1,j) 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Cloud Argument Ck(i,j) k Ck-1(k,j) Ck-1(i,k) Vertices numbered 1,2,…,k-1 j i 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Inductive Step Inductive Hypothesis: Suppose after step k-1 that C[i,j] contains a 1 if there is a path from i to j through vertices 1,…,k-1. Induction: Consider step k, which does C[i,j] := C[i,j]  (C[i,k]  C[k,j]); Either C[i,j] is already 1 or there is a new path through vertex k, which makes it 1. or and 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001000000000 B101000000000 C100110000000 D100000000000 E000100000000 F000000010000 G000001001101 H000000100000 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001000000000 B101000000000 C100110000000 D101000000000 E000100000000 F000000010000 G000001001101 H000000100000 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001000000000 B101000000000 C100110000000 D101000000000 E000100000000 F000000010000 G000001001101 H000000100000 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E000100000000 F000000010000 G000001001101 H000000100000 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000010000 G000001001101 H000000100000 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000010000 G000001001101 H000000100000 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000010000 G000001011101 H000000100000 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000010000 G000001011101 H000001101101 I000000000100 J000000000010 K000000010000 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000111101 G000001011101 H000001101101 I000000000100 J000000000010 K000001111101 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000111101 G000001011101 H000001101101 I000000000100 J000000000010 K000001111101 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000111111 G000001011111 H000001101111 I000000000110 J000000000010 K000001111101 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000111111 G000001011111 H000001101111 I000001110111 J000001111011 K000001111101 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 Warshall Algorithm ABCDEFGHIJKL A001110000000 B101110000000 C100110000000 D101010000000 E101100000000 F000000111111 G000001011111 H000001101111 I000001110111 J000001111011 K000001111101 L000000000000 B F H A G L C D J E K I 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Back to Weighted graphs: Matrix Representation C[i,j] = the cost of the edge (i,j) C[i,i] = 0 because no cost to stay where you are C[i,j] = ∞ if no edge from i to j. C 1 2 3 4 5 6 7 1 0 2 ∞ 1 ∞ ∞ ∞ 2 ∞ 0 ∞ 3 10 ∞ ∞ 3 4 ∞ 0 ∞ ∞ 5 ∞ 4 ∞ ∞ 2 0 2 8 4 5 ∞ ∞ ∞ ∞ 0 ∞ 6 6 ∞ ∞ ∞ ∞ ∞ 0 ∞ 7 ∞ ∞ ∞ ∞ ∞ 1 0 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

Floyd – Warshall Algorithm // Start with the cost matrix C All_Pairs_Shortest_Path { for k = 1 to n do for i = 1 to n do for j = 1 to n do C[i,j] := min(C[i,j], C[i,k] + C[k,j]); } Note x + ∞ = ∞ by definition old cost updated new cost On termination C[i,j] is the length of the shortest path from i to j. 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2

CSE 373 AU 05 - Digraph Algorithms 2 The Computation 1 2 3 4 5 6 7 1 0 2 ∞ 1 ∞ ∞ ∞ 2 ∞ 0 ∞ 3 10 ∞ ∞ 3 4 ∞ 0 ∞ ∞ 5 ∞ 4 ∞ ∞ 2 0 2 8 4 5 ∞ ∞ ∞ ∞ 0 ∞ 1 6 ∞ ∞ ∞ ∞ ∞ 0 ∞ 7 ∞ ∞ ∞ ∞ ∞ 1 0 1 2 3 4 5 6 7 1 0 2 3 1 3 6 5 2 9 0 5 3 5 8 7 3 4 6 0 5 4 5 9 4 6 8 2 0 2 5 4 5 ∞ ∞ ∞ ∞ 0 7 1 6 ∞ ∞ ∞ ∞ ∞ 0 ∞ 7 ∞ ∞ ∞ ∞ ∞ 1 0 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD: AE: AF: AG: BA: BC: BD: BE: BF: BG: CA: CB: CD: CE: CF: CG: DA: DB: DC: DE: DF: DG: EA: EB: EC: ED: EF: EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 ∞ 1 ∞ ∞ ∞ B ∞ ∞ 3 10 ∞ ∞ C 4 ∞ ∞ ∞ 5 ∞ D ∞ ∞ 2 2 8 4 E ∞ ∞ ∞ ∞ ∞ 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD: AE: AF: AG: BA: BC: BD: BE: BF: BG: CA: CB: CA,AB CD: CA,AD CE: CF: CG: DA: DB: DC: DE: DF: DG: EA: EB: EC: ED: EF: EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 ∞ 1 ∞ ∞ ∞ B ∞ ∞ 3 10 ∞ ∞ C 4 6 5 ∞ 5 ∞ D ∞ ∞ 2 2 8 4 E ∞ ∞ ∞ ∞ ∞ 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD: AE: AB,BE AF: AG: BA: BC: BD: BE: BF: BG: CA: CB: CA,AB CD: CA,AD CE: AB,BE CF: CG: DA: DB: DC: DE: DF: DG: EA: EB: EC: ED: EF: EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 ∞ 1 12 ∞ ∞ B ∞ ∞ 3 10 ∞ ∞ C 4 6 5 16 5 ∞ D ∞ ∞ 2 2 8 4 E ∞ ∞ ∞ ∞ ∞ 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD: AE: AB,BE AF: AG: BA: BC: BD: BE: BF: BG: CA: CB: CA,AB CD: CA,AD CE: AB,BE CF: CG: DA: DC,CA DB: DC,CB DC: DE: DF: DC,CF DG: EA: EB: EC: ED: EF: EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 ∞ 1 12 ∞ ∞ B ∞ ∞ 3 10 ∞ ∞ C 4 6 5 16 5 ∞ D 6 8 2 2 7 4 E ∞ ∞ ∞ ∞ ∞ 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD,DC AD: AE: AD,DE AF: AD,DF AG: AD,DG BA: BC,CA BC: BD,DC BD: BE: BF: BD,DF BG: BD,DG CA: CB: CA,AB CD: CA,AD CE: CD,DE CF: CG: CD,DG DA: DC,CA DB: DC,CB DC: DE: DF: DC,CF DG: EA: EB: EC: ED: EF: EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 3 1 3 8 5 B 9 5 3 10 10 7 C 4 6 5 7 5 9 D 6 8 2 2 7 4 E ∞ ∞ ∞ ∞ ∞ 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD,DC AD: AE: AD,DE AF: AD,DF AG: AE,EG BA: BC,CA BC: BD,DC BD: BE: BF: BD,DF BG: BE,EG CA: CB: CA,AB CD: CA,AD CE: CD,DE CF: CG: CE,EG DA: DC,CA DB: DC,CB DC: DE: DF: DC,CF DG: DE,EG EA: EB: EC: ED: EF: EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 3 1 3 8 4 B 9 5 3 10 10 6 C 4 6 5 7 5 8 D 6 8 2 2 7 3 E ∞ ∞ ∞ ∞ ∞ 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD,DC AD: AE: AD,DE AF: AD,DF AG: AE,EG BA: BC,CA BC: BD,DC BD: BE: BF: BD,DF BG: BE,EG CA: CB: CA,AB CD: CA,AD CE: CD,DE CF: CG: CE,EG DA: DC,CA DB: DC,CB DC: DE: DF: DC,CF DG: DE,EG EA: EB: EC: ED: EF: EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 3 1 3 8 4 B 9 5 3 10 10 6 C 4 6 5 7 5 8 D 6 8 2 2 7 3 E ∞ ∞ ∞ ∞ ∞ 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD,DC AD: AE: AD,DE AF: AG,GF AG: AE,EG BA: BC,CA BC: BD,DC BD: BE: BF: BG,GF BG: BE,EG CA: CB: CA,AB CD: CA,AD CE: CD,DE CF: CG: CE,EG DA: DC,CA DB: DC,CB DC: DE: DF: DG,GF DG: DE,EG EA: EB: EC: ED: EF: EG,GF EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 3 1 3 5 4 B 9 5 3 10 7 6 C 4 6 5 7 5 8 D 6 8 2 2 4 3 E ∞ ∞ ∞ ∞ 2 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 C 2 D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

CSE 373 AU 05 - Digraph Algorithms 2 AB: AC: AD,DC AD: AE: AD,DE AF: AG,GF AG: AE,EG BA: BC,CA BC: BD,DC BD: BE: BF: BG,GF BG: BE,EG CA: CB: CA,AB CD: CA,AD CE: CD,DE CF: CG: CE,EG DA: DC,CA DB: DC,CB DC: DE: DF: DG,GF DG: DE,EG EA: EB: EC: ED: EF: EG,GF EG: FA: FB: FC: FD: FE: FG: GA: GB: GC: GD: GE: GF: A B C D E F G A 2 3 1 3 5 4 B 9 5 3 10 7 6 C 4 6 5 7 5 8 D 6 8 2 2 4 3 E ∞ ∞ ∞ ∞ 2 1 F ∞ ∞ ∞ ∞ ∞ ∞ G ∞ ∞ ∞ ∞ ∞ 1 A 2 B 4 1 3 10 2 C D 2 E 5 8 4 1 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2 F 1 G

Time Complexity of All Pairs Shortest Path n is the number of vertices Three nested loops. O(n3) Shortest paths can be found too Repeated Dijkstra’s algorithm O(n(n +m)log n) (= O(n3 log n) for dense graphs). Run Dijkstra starting at each vertex. 11/29/2018 CSE 373 AU 05 - Digraph Algorithms 2