SSSP in DAGs (directed acyclic graphs). DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w);

Slides:



Advertisements
Similar presentations
CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
Advertisements

Single Source Shortest Paths
Lecture 7 March 1, 11 discuss HW 2, Problem 3
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
1 Data Structures DFS, Topological Sort Dana Shapira.
Connected Components, Directed Graphs, Topological Sort COMP171.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 10.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Lecture 13 Algorithm Analysis
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
 2004 SDU 1 Lecture5-Strongly Connected Components.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Introduction to Algorithms
Data Structures and Algorithm Analysis Lecture 5
Chapter 22 Elementary Graph Algorithms
CSE 373 Topological Sort Graph Traversals
CSE 2331/5331 Topic 9: Basic Graph Alg.
Bipartite Matching and Other Graph Algorithms
CS120 Graphs.
More Graph Algorithms.
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
"Learning how to learn is life's most important skill. " - Tony Buzan
Topological Sort CSE 373 Data Structures Lecture 19.
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Richard Anderson Autumn 2016 Lecture 5
Lecture 13 Algorithm Analysis
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
Chapter 24: Single-Source Shortest Paths
Chapter 24: Single-Source Shortest Paths
Richard Anderson Winter 2019 Lecture 6
Chapter 24: Single-Source Shortest-Path
Advanced Algorithms Analysis and Design
Presentation transcript:

SSSP in DAGs (directed acyclic graphs)

DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w); } If G=(V,E) not (strongly) connected then it may have DFS forest

Topological Sort Topological Sort: Ordering of vertices in a DAG based on precedence : if path then v i before v j in ordering – not unique: – V  V : indegree(v): #edges(u,v)  E – Can use DFS to obtain TS order – Given directed graph G, is it a DAG? Can use TS to answer it ( cannot find vertex with indegree 0)

TS algorithm Output (number) v  V with indegree 0 (remove v and its outgoing edges) Repeat until no vertex left

TS algorithm How to find v  V with indgree(v)=0 – Use queue: Each time incoming edge at u  V is removed, decrease indegree(u) If indegree(u) is 0, place u in queue O(|V|+|E|) time – Init: Find v  V with indegree(v)=0 in O(|V|) time Place in queue

DAG and TS Lemma 1: A DAG has a sink(outdegree=0) and a source(indegree=0) – Proof: Let P be the longest path in G, Claim: u is a source – If not,  (w,u). If w ∉ P => P’ = {(w,u)} U P is longer than P. If w  p => cycle: A similar argument shows v is a sink

Theorem 1: A directed G has a TS  G is a DAG Proof: – Assume G has TS and a cycle C. Let V i  C be the vertex with smallest index assigned by TS. There is then an edge (V j,V i ), with V j  C and j>I => contradiction with TS assignment. Now Assume G acyclic. Use induction: – Define P(n): a DAG with n vertices has a TS – P(1) = true Assume P(m) is true, Let G have m+1 vertices => source V 0. G\{V 0 } has TS V 1,V 2,…, V m => G has TS V 0,V 1,…, V m

SSSP in DAG (cont.) Use vertex selection rule: select in TS order – SP well defined, even if negative weight edges ( negative weight cycle cannot exist) DAG-SP(G,w,s) Topologically sort vertices of G Init-Single-Source ( G, s) For each u ∈ V, in TS order do For each v ∈ Adj[u] do Relax(u,v,w) – O(|V|+|E|) time

SSSP in DAG (cont.) Critical path in a DAG: longest path through the DAG – Can find one by: (1) negating edge weights and running DAG-SP, or (2) run DAG-SP with modifications: – Replace “∞” by “- ∞” in init-single-source – Replace “>” by “<“ in RELAX – Note: Longest simple path in unweighted graph is NP-Complete

BFS( breadth first search) Visit all nodes at same level before going to next level; use queue for this Queue_init();id=0;visited[u]=0,  u  V; Bfs(v  V){ enqueue(v); While(!queue_empty()){ u=dequeue();visited[u]=++id; (need 3 values) for each w  adj[u] do if(visited[w]==0){enqueue(w);visited[w]=-1} } How many levels in the queue at a time?

Application:Unweighted SPs δ(s,v) : #edges on the path (all edges have weight 1) Use BFS: need min # edges to reach each u  V from s. – Process vertices by layers: Process those that can be reached with1 edge Process those that can be reached with2 edge, etc. – Use a queue: Check d[v]: if d[v]= ∞, place in queue.