1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.

Slides:



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

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Chapter 25: All-Pairs Shortest-Paths
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Graphs David Kauchak cs302 Spring DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
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.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
Properties and Applications of Depth-First Search Trees and Forests
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Introduction to Algorithms
Topological Sorting.
Chapter 22 Elementary Graph Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
Topological Sort (an application of DFS)
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Topological Sort (an application of DFS)
Trevor Brown DC 2338, Office hour M3-4pm
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

1 Chapter 22: Elementary Graph Algorithms III

2 About this lecture Topological Sort

3 Cycles in Directed Graph Theorem: For any DFS on a directed graph G, there is a back edge  G has a cycle Proof: (  ) If there is a back edge (u,v), it implies there is a path from v to u. Thus, this back edge completes a cycle B u v

4 Proof (  ) If G has a cycle C, let v = first vertex discovered in C (u,v) = v’s preceding edge in C Thus, when v is discovered, all nodes in C are still undiscovered (white)  v is ancestor of u in DFS forest (why?)  (u,v) becomes a back edge

5 Topological Sort Directed graph can be used to indicate precedence among a set of events E.g., a possible precedence is dressing under-shorts pants belt shirt tie socks shoes watch jacket

6 Topological Sort The previous directed graph is also called a precedence graph Question: Given a precedence graph G, can we order the events such that if (u,v) is in G (i.e. u should complete before v ) then u appears before v in the ordering ? We call this problem topological sorting of G

7 Topological Sort Fact: If G contains a cycle, then it is impossible to find a desired ordering (Prove by contradiction) However, if G is acyclic (not contains any cycle) we show that the algorithm in next slide always find one of the desired ordering

8 Topological Sort Topological-Sort(G) { 1. Call DFS on G 2. If G contains a back edge, abort ; 3. Else, output vertices in decreasing order of their finishing times ; } Why is the algorithm correct?

9 Topological Sort Theorem: If G is acyclic, the previous algorithm produces a topological sort of G Proof: Let (u,v) be an edge in G. We shall show that f(u)  f(v) so that u appears before v in the output ordering Recall G is acyclic, there is no back edges. There are two main cases …

10 Proof Case 1: (u,v) is a tree or forward edge  u is an ancestor of v  d(u)  d(v)  f(v)  f(u) (why??) Case 2: (u,v) is a cross edge  d(v)  d(u) (otherwise, by white-path, u must be an ancestor of v, so that (u,v) cannot be a cross edge)  Since G is acyclic, v cannot reach u, so d(v)  f(v)  d(u)  f(u) (why??) Both cases show f(u)  f(v)  Done !

11 under-shorts pants belt shirt tie socks shoes watch jacket Discovery and Finishing Times after a possible DFS 1/8 2/5 3/4 6/7 11/14 12/13 9/10 15/18 16/17 Topological Sort (Example)

12 under-shorts pants belt shirt tie socks shoes watch jacket Ordering Finishing Times (in descending order) If we order the events from left to right, anything special about the edge directions ?

13 Performance Let G = (V,E) be the input directed graph Running time for Topological-Sort : 1. Perform DFS : O(|V|+|E|) time 2. Sort finishing times Naïve method: O(|V| log |V|) time Clever method: (use an extra stack S) During DFS, push a node into stack S once finished  no need to sort !! Total time: O(|V|+|E|)

14 Homework Exercises: ,

15 Quiz What is an optimal Huffman code for the following set of frequencies: a:1, b:2, c:4, d:5, e:6, f:9, g:12