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
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
§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.
8.3 Representing Relations Connection Matrices Let R be a relation from A = {a 1, a 2,..., a m } to B = {b 1, b 2,..., b n }. Definition: A n m  n connection.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
All-Pairs Shortest Paths
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Dijkstra's algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
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.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
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 Paths.
CSC317 Shortest path algorithms
Minimum Spanning Trees
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Graphs
Shortest Path Problems
Shortest Path Graph represents highway system Edges have weights
All-Pairs Shortest Paths (26.0/25)
CSE 421: Introduction to Algorithms
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Shortest Paths.
Directed Graphs (Part II)
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
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Shortest Path Algorithms
Minimum Spanning Tree Algorithms
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
Algorithms (2IL15) – Lecture 7
Minimum Spanning Trees
Shortest Path Problems
CSE332: Data Abstractions Lecture 17: Shortest Paths
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Shortest Path Problems
Shortest Paths.
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
Lecture 12 Shortest Path.
Richard Anderson Spring 2016
All-Pairs Shortest Paths
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Directed Graphs (Part II) CSE 373 Data Structures

Dijkstra’s Shortest Path Algorithm Initialize the cost of s 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)+cost(A,B) < B’s currently known cost set cost(B) = cost(A)+cost(A,B) set previous(B) = A so that we can remember the path 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

Example: Initialization  Cost(source) = 0 Cost(all vertices but source) = v1 2 v2  4 1 3 10 v3 2 v4 2 v5    5 8 4 6 v6 1 v7   Pick vertex not in S with lowest cost. 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

Example: Update Cost neighbors 2 v1 2 v2 4 1 3 10 v3 2 v4 2 v5   1 5 8 4 6 Cost(v2) = 2 v6 1 v7 Cost(v4) = 1   8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

Example: pick vertex with lowest cost and add it to S 4 1 2 10 3 6 8 5  Pick vertex not in S with lowest cost, i.e., v4 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

Example: update neighbors 2 v1 2 v2 4 1 3 10 v3 2 v4 2 v5 3 3 1 5 8 4 6 Cost(v3) = 1 + 2 = 3 Cost(v5) = 1 + 2 = 3 Cost(v6) = 1 + 8 = 9 Cost(v7) = 1 + 4 = 5 v6 1 v7 9 5 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 Example (Ct’d) Pick vertex not in S with lowest cost (v2) and update neighbors 2 v1 2 v2 4 1 3 10 v3 2 v4 2 v5 3 3 1 5 8 4 6 Note : cost(v4) not updated since already in S and cost(v5) not updated since it is larger than previously computed v6 1 v7 9 5 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 Example: (ct’d) Pick vertex not in S (v5) with lowest cost and update neighbors 2 v1 2 v2 4 1 3 10 v3 2 v4 2 v5 3 3 1 5 8 4 6 v6 1 v7 No updating 9 5 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 Example: (ct’d) Pick vertex not in S with lowest cost (v3) and update neighbors 2 v1 2 v2 4 1 3 10 v3 2 v4 2 v5 3 3 1 5 8 4 6 v6 1 v7 8 5 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 Example: (ct’d) Pick vertex not in S with lowest cost (v7) and update neighbors 2 v1 2 v2 4 1 3 10 v3 2 v4 2 v5 3 3 1 5 8 4 6 v6 1 v7 Previous cost 6 5 Cost(v6) = min (8, 5+1) = 6 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 Example (end) 2 v1 2 v2 4 1 3 10 v3 2 v4 2 v5 3 3 1 5 8 4 6 v6 1 v7 6 5 Pick vertex not in S with lowest cost (v6) and update neighbors 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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) 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 Time Complexity n vertices and m edges Initialize data structures O(n+m) Find min cost vertices O(n log n) n delete mins Update costs O(m log n) Potentially m updates Update previous pointers O(m) Total time O((n + m) log n) - very fast. 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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? 8/22/2019 CSE 373 AU 04 - 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! 8/22/2019 CSE 373 AU 04 - 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. 8/22/2019 CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - 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’ 8/22/2019 CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 1 2 3 4 5 6 7 8/22/2019 CSE 373 AU 04 - 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. 8/22/2019 CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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) 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - 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 8/22/2019 CSE 373 AU 04 - 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] = infinity (:) 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 8/22/2019 CSE 373 AU 04 - 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 (: is infinity) old cost updated new cost On termination C[i,j] is the length of the shortest path from i to j. 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

CSE 373 AU 04 - Digraph Algorithms 2 The Computation C 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 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 6 4 6 8 2 0 2 5 4 5 : : : : 0 7 6 6 : : : : : 0 : 7 : : : : : 1 0 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2

Time Complexity of All Pairs Shortest Path n is the number of vertices Three nested loops. O(n3) Shortest paths can be found too (see the book). Repeated Dijkstra’s algorithm O(n(n +m)log n) (= O(n3 log n) for dense graphs). Run Dijkstra starting at each vertex. But, Dijkstra also gives the shortest paths not just their lengths. 8/22/2019 CSE 373 AU 04 - Digraph Algorithms 2