Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Similar documents
Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Graph Representation

Representations of Graphs

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

DFS & STRONGLY CONNECTED COMPONENTS

Chapter 22. Elementary Graph Algorithms

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Elementary Graph Algorithms

Design and Analysis of Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Basic Graph Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graph representation

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph Algorithms. Definition

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graph: representation and traversal

Design and Analysis of Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Minimum Spanning Trees Ch 23 Traversing graphs

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

1 Summation Formulae, Induction and Bounding. 2 How to compare functions: o, ω, O, Ω, Θ. 3 How to count the running time of algorithms

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

CSI 604 Elementary Graph Algorithms

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Strongly Connected Components

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Introduction to Algorithms. Lecture 11

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Graph implementations :

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Announcements. HW3 is graded. Average is 81%

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Search. Adnan Aziz

Elementary Graph Algorithms CSE 6331

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Graph: representation and traversal

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

COT 6405 Introduction to Theory of Algorithms

22.3 Depth First Search

Graph Algorithms Using Depth First Search

Unit 2: Algorithmic Graph Theory

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

W4231: Analysis of Algorithms

Analysis of Algorithms Prof. Karen Daniels

Fundamental Graph Algorithms Part Three

Directed Graphs (II) Hwansoo Han

Review: Graph Theory and Representation

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017

Lecture 10: Strongly Connected Components, Biconnected Graphs

Algorithm Design and Analysis

Elementary Graph Algorithms

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Chapter 22 Elementary Graph Algorithms

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

Quiz 2 Practice Problems

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Algorithm Design and Analysis

6.006 Introduction to Algorithms Spring 2008

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Statements or Basic Blocks (Maximal sequence of code with branching only allowed at end) Possible transfer of control

Outlines: Graphs Part-2

Basic Graph Algorithms (CLRS B.4-B.5, )

Homework Assignment #3 Graph

Data Structures and Algorithms. Chapter 7. Graphs

CS 4407 Algorithms Lecture 5: Graphs an Introduction

Finding Strongly Connected Components

CS 310 Advanced Data Structures and Algorithms

Sample Solutions to Homework #4

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Algorithm Design and Analysis

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

csci 210: Data Structures Graph Traversals

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

Connectivity. Use DFS or BFS. 03/11/04 Lecture 18 1

Topic 12: Elementary Graph Algorithms

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

Suggested Study Strategy

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Depth First Search (DFS)

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

Transcription:

Taking Stock IE170: Algorithms in Systems Engineering: Lecture 17 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University March 2, 2007 Last Time Depth-First Search This Time: Uses of Topological Sort Strongly Connected Components Depth-First Search (Initialize and Go) Input: Graph G = (V, E) No source vertex here. Works for undirected and directed graphs. We focus on directed graphs today... Output: Two timestamps for each node d(v), f(v), Output: π(v), predecessor of v not on shortest path necessarily dfs(v, E) 1 for each u in V 2 do color(u) green 3 π(u) nil 4 time 0 5 for each u in V 6 do if color[u] = green 7 then dfs-visit(u)

(Visit Node Recursive) Parenthesis Theorem dfs-visit(u) 1 color(u) yellow 2 d[u] time ++ 3 for each v in Adj[u] 4 do if color[v] = green 5 then π[v] u 6 dfs-visit(v) 7 8 color(u) red 9 f[u] = time ++ Let s look at the intervals: [d[v], f[v]] for each vertex v V. (Surely, d[v] < f[v]) These tell us about the predecessor relationship in G π 1 If I finish exploring u before first exploring v, (d[u] < f[v]) then v is not a descendant of u. (Or versa vice) 2 If [d[u], f[u]] [d[v], f[v]] then u is a descendent of v in the tree 3 If [d[v], f[v]] [d[u], f[u]] then v is a descendent of u in the tree Analysis of Graph Review... Loop on lines 1-3 O( V ) dfs-visit is called exactly once for each vertex v (Why?) Because the first thing you do is paint the node yellow The Loop on lines 3-6 in calls dfs-visit Adj[v] times for vertex v. Since visit is called exactly once per vertex, the total running time to do loop on lines 3-6 is Adj[v] = Θ( E ). v V Therefore: running time of on G = (V, E) is Θ( V + E ): Linear in the (adjacency list) size of the graph Think back to your thorough reading of Appendix B.4 and B.5... A path in G is a sequence of vertices such that each vertex is adjacent to the vertex preceding it in the sequence. Simple paths do not repeat nodes. A (simple) cycle is a (simple) path except that the first and last vertices are the same. Paths and cycles can either be directed or undirected If I say cycle or path, I will often mean simple, undirected cycle or path

I Can t See the Forest Through the... More Definitions The graph: G π = (V, E π ) forms a forest of subtrees New Definitions: Tree A tree T = (V, E) is a connected graph that does not contain a cycle All pairs of vertices in V are connected by a simple (undirected) path E = V 1 Adding any edge to E forms a cycle in T A (Undirected) acyclic graph is usually called a forest A DAG is a Directed, Acyclic Graph A directed forest A subtree is simply a subgraph that is a tree Classifying Edges in the Tree Modifying to Classify Edges Given a Tree G π, there are four type of edges (u, v) 1 Tree Edges: Edges in E π. These are found by exploring (u, v) in the procedure 2 Back Edges: Connect u to an ancestor v in a tree 3 Forward Edges: Connect u to a descendent v in a tree 4 Cross Edges: All other edges. They can be edges in the same tree, or can cross trees in teh forest G π can be modified to classify edges as it encounters them... Classify e = (u, v) based on the color of v when e is first explored... green: Indicates Tree Edge yellow: Indicates Back Edge red: Indicates Forward or Cross Edge

Undirected Graphs DAG Gum it! Thm In an undirected graph, there may be some ambiguity, as (u, v) and (v, u) are the same edge. The following theorem will help clear things up In a of an undirected graph G = (V, E), every edge is a a tree edge or a back edge. DAGs are good at modeling processes and structures that have a partial order ( ) A B and B C A < C May have neither A B nor B C Think of a partial order as the way in which you must do tasks to ensure successful completion Sometimes it doesn t matter if you do A first or B first... Spring Break on My Mind! Put ice in shaker (A) Pour gin a in shaker (B) Pour vermouth b in shaker (C) Stir c (D) Strain (E) Put ice in glass (F) Remove ice from glass (G) Pour in glass (H) Add olive to glass (I) Enjoy! (J) a Preferably Boodles b Very Little c Never shake Task Relations A B A C B D C D D E F G G H G I E H H J I J Topological Sort We would like to produce a valid order for making a martini A topological sort of a directed acyclic graph (DAG) is a linear ordering of its nodes which is compatible with the partial order induced on the nodes. u v if there s a directed path from u to v in the DAG. An equivalent definition is that each node comes before all nodes to which it has edges. i.e. u must be done before v Every DAG has at least one topological sort, and may have many.

Topological Sort Why Does This Work? (cont.) Topological Sort: The Whole Algorithm 1 search the graph 2 List vertices in order of decreasing finishing time Why Does This Work? Show that (u, v) E f[v] < f[u] When we explore (u, v), u is yellow What color is v? Is v yellow? No, since then DAG would have a cycle Is v green? Then it becomes descendant of u and (by () theorem), d[u] < d[v] < f[v] < f[u] Is v red? If so, then we re finished and f[v] < f[u] since we re still exploring u Therefore if (u, v) E, f[v] < f[u] Quite Enough Done Strongly Connected Components Finding Strongly Connected Components Given a directed graph G = (V, E), a strongly connected component of G is a maximal set of vertices C V such that u, v, C there exists a directed path both from u to b and from v to u The algorithm uses the transpose of a directed graph G = (V, E), where the orientations are flipped: G T = (V, E T ), where E T = {(v, u) (u, v) E} What is running time to create G T? Note: G and G T have the same Strongly Connected Components u and v are both reachable from each other in G if and only if they are both reachable with the orientations flipped (G T ). 1 Call (G) to topologically sort G 2 Compute G T 3 Call (G T ) but consider vertices in topologically sorteded order (from G) 4 Vertices in each tree of depth-first forest for SCC

Component Graph More Lemma Lemma G SCC (G) = (V SCC, E SCC ) V SCC has one vertex for each strongly connected component of G e E SCC is there is an edge between corresponding SCC s in G G SCC is a DAG Lemma Let C and C be distinct SCC in G, if (u, v) E and u C, v C, then f(c) > f(c ) if (u, v) E T and u C, v C, then f(c) < f(c ) If f(c) > f(c ), there is no edge from C to C in G T For C V, f(c) def = max v C {f[v]} Why SCC Works. (Intuition) on G T starts with SCC C such that f(c) is maximum. Since f(c) > f(c ), there are no edges from C to C in G T This means that the will visit only vertices in C The next root has the largest f(c ) for all C C. visits all vertices in C, and any other edges must go to C, which we have already visited.. Next Time Spanning Trees