Directed Graph Algorithms

Slides:



Advertisements
Similar presentations
Problem solving with graph search
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Topological Sort and Hashing
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
Topological Sort: Definition
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Topological Sorting.
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Cse 373 May 8th – Dijkstras.
C.Eng 213 Data Structures Graphs Fall Section 3.
Introduction to Graphs
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
More Graph Algorithms.
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Spring 2016.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Topological Sort CSE 373 Data Structures Lecture 19.
Graphs.
Based on slides of Dan Suciu
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Richard Anderson Autumn 2016 Lecture 5
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 373: Data Structures and Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Graphs.
CSE 417: Algorithms and Computational Complexity
Graphs.
Graphs.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Graphs.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Spring 2016
Graphs.
Graphs: Shortest path and mst
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Directed Graph Algorithms CSE 373

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): Stack (after): A unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): A Stack (after): C unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): C Stack (after): D, E unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): D (from C), E Stack (after): D (from C), D (from E) unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): D (from C), D (from E) Stack (after): D (from C) unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): D (from C) Stack (after): unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): Stack (after): B unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): B Stack (after): unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): Stack (after): F unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): F Stack (after): H unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): H Stack (after): G unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): G Stack (after): I, J, L unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): I, J, L Stack (after): I, J unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): I, J Stack (after): I, K unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): I, K Stack (after): I unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Depth-First Search Stack (before): I Stack (after): unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): Queue (after): A unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): A Queue (after): C unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): C Queue (after): D, E unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): D, E Queue (after): E unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): E Queue (after): unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): Queue (after): B unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): B Queue (after): unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): B Queue (after): unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): Queue (after): F unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): F Queue (after): H unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): H Queue (after): G unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): G Queue (after): I, J, L unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): I, J, L Queue (after): J (from G), L, J (from I) unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): J (from G), L, J (from I) Queue (after): L, J (from I), K unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): L, J (from I), K Queue (after): J (from I), K unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): J (from I), K Queue (after): K unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Breadth-First Search Queue (before): K Queue (after): unexplored edge B discovery edge A F H cross edge back edge L G forward edge C D J E I K 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topological Sort 142 322 143 321 Problem: Find an order in which all these courses can be taken. Example: 142  143  378 370  321  341  322 326  421  401 326 370 341 378 421 In order to take a course, you must take all of its prerequisites first 401 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topological Sort Given a digraph G = (V, E), find a total ordering of its vertices such that: for any edge (v, w) in E, v precedes w in the ordering B C A F D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Topo sort - good example B C Any total ordering in which all the arrows go to the right is a valid solution A F D E A B F C D E Note that F can go anywhere in this list because it is not connected. Also the solution is not unique. 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo sort - bad example B C Any ordering in which an arrow goes to the left is not a valid solution A F D E A B F C E D NO! 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Paths and Cycles Given a digraph G = (V,E), a path is a sequence of vertices v1,v2, …,vk such that: (vi,vi+1) in E for 1 < i < k path length = number of edges in the path path cost = sum of costs of each edge A path is a cycle if : k > 1; v1 = vk G is acyclic if it has no cycles. 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Only acyclic graphs can be topologically sorted A directed graph with a cycle cannot be topologically sorted. B C A F D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Topological sort algorithm: 1 Step 1: Identify vertices that have no incoming edges The “in-degree” of these vertices is zero B C A F D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo sort algorithm: 1a Step 1: Identify vertices that have no incoming edges If no such vertices, graph has only cycle(s) (cyclic graph) Topological sort not possible – Halt. B C A Example of a cyclic graph D 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo sort algorithm:1b Step 1: Identify vertices that have no incoming edges Select one such vertex Select B C A F D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo sort algorithm: 2 Step 2: Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it in the output. B C A A F D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Continue until done Repeat Step 1 and Step 2 until graph is empty Select B C A F D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms B Select B. Copy to sorted list. Delete B and its edges. B C F A B D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Select C. Copy to sorted list. Delete C and its edges. C F A B C D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Select D. Copy to sorted list. Delete D and its edges. F A B C D D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms E, F Select E. Copy to sorted list. Delete E and its edges. Select F. Copy to sorted list. Delete F and its edges. F A B C D E F E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Done B C A F D E A B C D E F 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Implementation Assume adjacency list representation B 1 2 3 4 5 6 2 C 4 A 3 F 4 5 D E 5 Translation array 1 2 3 4 5 6 value next A B C D E F 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Calculate In-degrees D A 1 2 3 4 5 6 2 4 1 3 1 4 5 In-Degree array; or add a field to array A 2 5 2 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Calculate In-degrees for i = 1 to n do D[i] := 0; endfor for i = 1 to n do x := A[i]; while x  null do D[x.value] := D[x.value] + 1; x := x.next; endwhile endfor 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Maintaining Degree 0 Vertices Key idea: Initialize and maintain a queue (or stack) of vertices with In-Degree 0 D A 1 2 3 4 5 6 2 4 Queue 1 6 1 3 1 4 5 2 3 6 2 5 1 2 4 5 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Topo Sort using a Queue (breadth-first) After each vertex is output, when updating In-Degree array, enqueue any vertex whose In-Degree becomes zero D A Queue 6 2 1 2 3 4 5 6 2 4 dequeue enqueue Output 1 3 1 4 5 2 3 1 5 6 1 2 4 5 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Topo Sort using a Stack (depth-first) After each vertex is output, when updating In-Degree array, push any vertex whose In-Degree becomes zero D A Stack 2 6 1 2 3 4 5 6 2 4 pop push Output 1 3 1 4 5 2 3 1 5 6 1 2 4 5 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Topological Sort Algorithm Store each vertex’s In-Degree in an array D Initialize queue with all “in-degree=0” vertices While there are vertices remaining in the queue: (a) Dequeue and output a vertex (b) Reduce In-Degree of all vertices adjacent to it by 1 (c) Enqueue any of these vertices whose In-Degree became zero If all vertices are output then success, otherwise there is a cycle. Why use a queue? Some sense of stability as Zasha discussed it while I was out partying. 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Some Detail Main Loop while notEmpty(Q) do x := Dequeue(Q) Output(x) y := A[x]; while y  null do D[y.value] := D[y.value] – 1; if D[y.value] = 0 then Enqueue(Q,y.value); y := y.next; endwhile 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ queue Queue (before): Queue (after): 1, 6 1 1 2 3 6 1 2 2 4 5 Answer: 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ queue Queue (before): 1, 6 Queue (after): 6, 2 1 2 3 6 1 1 2 4 5 Answer: 1 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ queue Queue (before): 6, 2 Queue (after): 2 1 2 3 6 1 1 2 4 5 Answer: 1, 6 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ queue Queue (before): 2 Queue (after): 3 2 3 6 1 1 2 4 5 Answer: 1, 6, 2 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ queue Queue (before): 3 Queue (after): 4 2 3 6 1 1 4 5 Answer: 1, 6, 2, 3 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ queue Queue (before): 4 Queue (after): 5 2 3 6 1 4 5 Answer: 1, 6, 2, 3, 4 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ queue Queue (before): 5 Queue (after): 2 3 6 1 4 5 Answer: 1, 6, 2, 3, 4, 5 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): Stack (after): 1, 6 1 2 2 3 1 6 8 1 3 2 1 4 5 7 Answer: 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 1, 6 Stack (after): 1, 7, 8 1 2 2 3 6 8 1 3 2 4 5 7 Answer: 6 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 1, 7, 8 Stack (after): 1, 7 1 2 2 3 6 8 1 3 2 4 5 7 Answer: 6, 8 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 1, 7 Stack (after): 1 1 2 2 3 6 8 1 3 2 4 5 7 Answer: 6, 8, 7 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 1 Stack (after): 2 1 2 3 6 8 1 2 2 4 5 7 Answer: 6, 8, 7, 1 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 2 Stack (after): 3 2 3 6 8 1 1 2 4 5 7 Answer: 6, 8, 7, 1, 2 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 3 Stack (after): 4 2 3 6 8 1 1 4 5 7 Answer: 6, 8, 7, 1, 2, 3 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 4 Stack (after): 5 2 3 6 8 1 4 5 7 Answer: 6, 8, 7, 1, 2, 3, 4 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Topo Sort w/ stack Stack (before): 5 Stack (after): 2 3 6 8 1 4 5 7 Answer: 6, 8, 7, 1, 2, 3, 4, 5 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

TopoSort Fails (cycle) Queue (before): Queue (after): 1 1 2 2 3 1 2 1 4 5 Answer: 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

TopoSort Fails (cycle) Queue (before): 1 Queue (after): 2 2 2 3 1 1 1 4 5 Answer: 1 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

TopoSort Fails (cycle) Queue (before): 2 Queue (after): 1 2 3 1 1 1 4 5 Answer: 1, 2 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Topological Sort Analysis Initialize In-Degree array: O(|V| + |E|) Initialize Queue with In-Degree 0 vertices: O(|V|) Dequeue and output vertex: |V| vertices, each takes only O(1) to dequeue and output: O(|V|) Reduce In-Degree of all vertices adjacent to a vertex and Enqueue any In-Degree 0 vertices: O(|E|) For input graph G=(V,E) run time = O(|V| + |E|) Linear time! 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Recall Path cost ,Path length Path cost: the sum of the costs of each edge Path length: the number of edges in the path Path length is the unweighted path cost 4 Chicago Seattle 2 2 Salt Lake City 2 2 3 2 length(p) = 5 3 San Francisco cost(p) = 11 Dallas 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Shortest Path Problems Given a graph G = (V, E) and a “source” vertex s in V, find the minimum cost paths from s to every vertex in V Many variations: unweighted vs. weighted cyclic vs. acyclic pos. weights only vs. pos. and neg. weights etc The problem that Dijkstra’s algorithm addresses is the single source, shortest path problem. Given a graph and a source vertex, find the shortest path from the source to every vertex. We can put a variety of limitations or spins on the problem. We’ll focus on weighted graphs with no negative weights. This is used in all sorts of optimization problems: minimum delay in a network, minimum cost flights for airplane routes, etc. 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Why study shortest path problems? Traveling on a budget: What is the cheapest airline schedule from Seattle to city X? Optimizing routing of packets on the internet: Vertices are routers and edges are network links with different delays. What is the routing path with smallest total delay? Shipping: Find which highways and roads to take to minimize total delay due to traffic etc. 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Unweighted Shortest Path Problem: Given a “source” vertex s in an unweighted directed graph G = (V,E), find the shortest path from s to all vertices in G B A F H Only interested in path lengths Now, can we handle unweighted graphs? How might we implement this? Breadth first search! Will it work for weighted graphs? No. It won’t. What about Depth First search? Clearly not that either. What about the third search we studied? Best first search? That won’t quite work, but it might if we counted the path cost in with the edge cost. Let’s come back to that. G Source C D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Breadth-First Search Solution Basic Idea: Starting at node s, find vertices that can be reached using 0, 1, 2, 3, …, N-1 edges (works even for cyclic graphs!) B A F H G C D E 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Breadth-First Search Alg. Uses a queue to track vertices that are “nearby” source vertex is s Distance[s] := 0 Enqueue(Q,s); Mark(s)//After a vertex is marked once // it won’t be enqueued again while queue is not empty do X := Dequeue(Q); for each vertex Y adjacent to X do if Y is unmarked then Distance[Y] := Distance[X] + 1; Previous[Y] := X;//if we want to record paths Enqueue(Q,Y); Mark(Y); Running time = O(|V| + |E|) 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Example: Shortest Path length B A F H G C D E Queue Q = C 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Example (ct’d) B A F H G C D E 1 1 1 Previous Queue Q = A D E pointer G C 1 D E 1 Previous pointer Queue Q = A D E Indicates the vertex is marked 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Example (ct’d) 2 1 B A F H G C 1 D E 1 Q = D E B 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Example (ct’d) 2 1 B A F H G 2 C 1 D E 1 Q = B G 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Example (ct’d) 3 2 4 1 B A F H G 2 C 1 D E 1 Q = F 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

CSE 373 AU 05 - Digraph Algorithms Example (ct’d) 3 2 1 B A F H G 2 C 1 D E 1 Q = H 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

What if edges have weights? Breadth First Search does not work anymore minimum cost path may have more edges than minimum length path Shortest path (length) from C to A: CA (cost = 9) Minimum Cost Path = CEDA (cost = 8) 2 3 B A F H 1 1 2 1 3 9 4 G 8 C 2 D 1 E 3 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Dijkstra’s Algorithm for Weighted Shortest Path Classic algorithm for solving shortest path in weighted graphs (without negative weights) A greedy algorithm (irrevocably makes decisions without considering future consequences) Each vertex has a cost for path from initial vertex Among the wide variety of things he has done, he created Dijkstra’s algorithm more than 30 years ago. Dijkstra’s algorithm is a greedy algorithm (like Huffman encoding), so it just makes the best local choice at each step. The choice it makes is which shortest path to declare known next. It starts by declaring the start node known to have a shortest path of length 0. Then, it updates neighboring node’s path costs according to the start node’s cost. Then, it just keeps picking the next shortest path and fixing that one until it has all the vertices. 1/18/2019 CSE 373 AU 05 - Digraph Algorithms

Basic Idea of Dijkstra’s Algorithm Find the vertex with smallest cost that has not been “marked” yet. Mark it and compute the cost of its neighbors. Do this until all vertices are marked. Note that each step of the algorithm we are marking one vertex and we won’t change our decision: hence the term “greedy” algorithm 1/18/2019 CSE 373 AU 05 - Digraph Algorithms