Chapter 22: Elementary Graph Algorithms III

Slides:



Advertisements
Similar presentations
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Advertisements

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
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
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.
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 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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
Properties and Applications of Depth-First Search Trees and Forests
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
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
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
Topological Sort (an application of DFS)
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
Data Structures and Algorithms CMPSC 465
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
Elementary graph algorithms Chapter 22
Algorithms and Data Structures Lecture XII
Applications of DFS Topological sort (for directed acyclic graph)
Graph Representation (23.1/22.1)
Topological Sort (an application of DFS)
Trevor Brown DC 2338, Office hour M3-4pm
Elementary graph algorithms Chapter 22
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Chapter 24: Single-Source Shortest-Path
Assignment #2 (Assignment due: Nov. 06, 2018) v1 v2 v3 v4 v5
Presentation transcript:

Chapter 22: Elementary Graph Algorithms III

About this lecture Topological Sort

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 v B u

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

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

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

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

Why is the algorithm correct? 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?

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 …

d(v)  f(v)  d(u)  f(u) (why??) 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 !

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

Ordering Finishing Times (in descending order) 3 1 under-shorts pants belt shirt tie socks shoes watch jacket 5 2 4 6 7 8 9 If we order the events from left to right, anything special about the edge directions ?

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|)

Homework Exercises: 22.4-2, 22.4-4

Quiz 4 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