Copyright 2000, Kevin Wayne 1

Similar documents
3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

3.1 Basic Definitions and Applications

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

3.1 Basic Definitions and Applications

Copyright 2000, Kevin Wayne 1

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2019

Algorithm Design and Analysis

Algorithms: Lecture 10. Chalmers University of Technology

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

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

3.4 Testing Bipartiteness

Algorithms: Lecture 7. Chalmers University of Technology

Algorithm Design and Analysis

Goals! CSE 417: Algorithms and Computational Complexity!

Computer Science Approach to problem solving

Lecture 3: Graphs and flows

CS 781 Advanced Algorithms 1 Winter 2011

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211

CSE 421 Applications of DFS(?) Topological sort

CSE 421 DFS / Topological Ordering

CS521 \ Notes for the Final Exam

Graphs. Ouline. Graphs 10/3/2018. An Introduction. A graph G = (V,E) is composed of: An edge e = (u,v) is a pair of vertices

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

Objec&ves. Wrap up: Implemen&ng BFS and DFS Graph Applica&on: Bipar&te Graphs. Get out your BFS implementa&on handouts. Feb 2, 2018 CSCI211 - Sprenkle

Graphs. Ouline. Graphs. An Introduction. Agraph G = (V,E) is composed of: Anedge e = (u,v) is a pair of vertices

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Fundamental Graph Algorithms Part Four

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

Adjacent: Two distinct vertices u, v are adjacent if there is an edge with ends u, v. In this case we let uv denote such an edge.

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Graph. Vertex. edge. Directed Graph. Undirected Graph

10. EXTENDING TRACTABILITY

11. APPROXIMATION ALGORITHMS

Graph: representation and traversal

W4231: Analysis of Algorithms

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

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

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Chapter 11: Graphs and Trees. March 23, 2008

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Algorithm Design and Analysis

Reference Sheet for CO142.2 Discrete Mathematics II

Lecture 22 Tuesday, April 10

II (Sorting and) Order Statistics

Depth First Search (DFS)

10. EXTENDING TRACTABILITY

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

Graph Algorithms. Definition

Algorithms: Graphs. Amotz Bar-Noy. Spring 2012 CUNY. Amotz Bar-Noy (CUNY) Graphs Spring / 95

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

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

CS4800: Algorithms & Data Jonathan Ullman

Solutions to relevant spring 2000 exam problems

CS 310 Advanced Data Structures and Algorithms

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Outline. Graphs. Divide and Conquer.

Graph Algorithms Using Depth First Search

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Graph Representations and Traversal

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Chapter 3. Graphs. 3.1 Basic Definitions and Applications

Copyright 2000, Kevin Wayne 1

Greedy algorithms is another useful way for solving optimization problems.

Matching Theory. Figure 1: Is this graph bipartite?

Keys to Success: CAR Theorem CHAPTER 3 GRAPHS. Outline. Basics. Definitions and applications. Iris Hui-Ru Jiang Fall 2014.

Algorithm Design and Analysis

Algorithms. Graphs. Algorithms

CS 6783 (Applied Algorithms) Lecture 5

Lecture Summary CSC 263H. August 5, 2016

Induction Review. Graphs. EECS 310: Discrete Math Lecture 5 Graph Theory, Matching. Common Graphs. a set of edges or collection of two-elt subsets

Announcements. HW3 is graded. Average is 81%

CS 580: Algorithm Design and Analysis

3/7/2018. CS 580: Algorithm Design and Analysis. 8.1 Polynomial-Time Reductions. Chapter 8. NP and Computational Intractability

11. APPROXIMATION ALGORITHMS

Approximation Algorithms

Depth First Search (DFS)

Chapter 8. NP and Computational Intractability. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Lecture 10 Graph algorithms: testing graph properties

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

Number Theory and Graph Theory

Lecture 20 : Trees DRAFT

Graph Algorithms (part 3 of CSC 282),

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

Discrete mathematics II. - Graphs

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

! Greed. O(n log n) interval scheduling. ! Divide-and-conquer. O(n log n) FFT. ! Dynamic programming. O(n 2 ) edit distance.

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

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

Graph Theory CS/Math231 Discrete Mathematics Spring2015

Transcription:

Linear Time: O(n) CS 580: Algorithm Design and Analysis 2.4 A Survey of Common Running Times Merge. Combine two sorted lists A = a 1,a 2,,a n with B = b 1,b 2,,b n into sorted whole. Jeremiah Blocki Purdue University Spring 2018 i = 1, j = 1 while (both lists are nonempty) { if (a i b j ) append a i to output list and increment i else(a i b j )append b j to output list and increment j append remainder of nonempty list to output list Claim. Merging two lists of size n takes O(n) time. Pf. After each comparison, the length of output list increases by 1. 5 Recap: Asymptotic Analysis Linear Time: O(n) O(n log n) Time Five Representative Problems Algorithmic Techniques: Greedy, Dynamic Programming, Network Flow, Computationally Intractable Problems: Unlikely that polynomial time algorithm exists. Formal Definition of Big O,Ω, Θ notation T f ---- upper bound Means we can find constants c,n > 0 s.t. whenever n > N T f Intuition: f upperbounds T for large enough inputs T Ω f ---- lower bound T Θ f ---- lower bound and upper bound Linear time. Running time is proportional to input size. Computing the maximum. Compute maximum of n numbers a 1,, a n. max a 1 for i = 2 to n { if (a i > max) max a i O(n log n) time. Arises in divide-and-conquer algorithms. also referred to as linearithmic time Sorting. Mergesort and heapsort are sorting algorithms that perform O(n log n) comparisons. Largest empty interval. Given n time-stamps x 1,, x n on which copies of a file arrive at a server, what is largest interval of time when no copies of the file arrive? O(n log n) solution. Sort the time-stamps. Scan the sorted list in order, identifying the maximum gap between successive time-stamps. Polynomial Time function. T for some constant d (d is independent of the input size). 2 4 Copyright 2000, Kevin Wayne 1

Quadratic Time: O(n 2 ) Polynomial Time: O(n k ) Time Quadratic time. Enumerate all pairs of elements. Closest pair of points. Given a list of n points in the plane (x 1, y 1 ),, (x n, y n ), find the pair that is closest. O(n 2 ) solution. Try all pairs of points. min (x 1 - x 2 ) 2 + (y 1 - y 2 ) 2 for i = 1 to n { for j = i+1 to n { d (x i - x j ) 2 + (y i - y j ) 2 if (d < min) min d Remark. (n 2 ) seems inevitable, but this is just an illusion. don't need to take square roots Independent set of size k. Given a graph, are there k nodes such that no two are joined by an edge? k is a constant O(n k ) solution. Enumerate all subsets of k nodes. foreach subset S of k nodes { check whether S in an independent set if (S is an independent set) report S is an independent set Check whether S is an independent set = O(k 2 ). Number of k element subsets = n O(k 2 n k / k!) = O(n k ). n (n 1) (n 2) (n k 1) k k (k 1) (k 2) (2) (1) poly-time for k=17, but not practical nk k! Chapter 3 Graphs Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. see chapter 5 7 9 11 Cubic Time: O(n 3 ) Exponential Time Cubic time. Enumerate all triples of elements. Set disjointness. Given n sets S 1,, S n each of which is a subset of 1, 2,, n, is there some pair of these which are disjoint? Independent set. Given a graph, what is maximum size of an independent set? O(n 2 2 n ) solution. Enumerate all subsets. 3.1 Basic Definitions and Applications O(n 3 ) solution. For each pairs of sets, determine if they are disjoint. foreach set S i { foreach other set S j { foreach element p of S i { determine whether p also belongs to S j if (no element of S i belongs to S j ) report that S i and S j are disjoint S* foreach subset S of nodes { check whether S in an independent set if (S is largest independent set seen so far) update S* S 8 10 Copyright 2000, Kevin Wayne 2

Undirected Graphs World Wide Web Ecological Food Web Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. Web graph. Node: web page. Edge: hyperlink from one page to another. cnn.com Food web graph. Node = species. Edge = from prey to predator. V = { 1, 2, 3, 4, 5,, 7, 8 E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5- netscape.com novell.com cnnsi.com timewarner.com n = 8 m = 11 hbo.com sorpranos.com Reference: http://www.twingroves.district9.k12.il.us/wetlands/salamander/salgraphics/salfoodweb.giff 13 15 17 Some Graph Applications 9-11 Terrorist Network Graph Representation: Adjacency Matrix Social network graph. Node: people. Edge: relationship between two people. Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. Two representations of each edge. Space proportional to n 2. Graph transportation Nodes street intersections Edges highways Checking if (u, v) is an edge takes (1) time. Identifying all edges takes (n 2 ) time. communication computers fiber optic cables World Wide Web web pages hyperlinks social people relationships food web species predator-prey software systems functions function calls scheduling tasks precedence constraints circuits gates wires 1 2 3 4 5 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 Reference: Valdis Krebs, http://www.firstmonday.org/issues/issue7_4/krebs 14 1 18 Copyright 2000, Kevin Wayne 3

Graph Representation: Adjacency List Cycles Rooted Trees Adjacency list. Node indexed array of lists. Two representations of each edge. degree = number of neighbors of u Space proportional to m + n. Checking if (u, v) is an edge takes O(deg(u)) time. Identifying all edges takes (m + n) time. Def. A cycle is a path v 1, v 2,, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct. 1 7 Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. Importance. Models hierarchical structure. 1 2 3 2 1 3 4 5 3 7 1 2 5 8 2 3 4 5 8 root r parent of v v 4 2 5 5 2 3 4 5 7 3 8 8 3 7 cycle C = 1-2-4-5-3-1 child of v (leaf node) a tree the same tree, rooted at 1 19 21 23 Paths and Connectivity Trees Phylogeny Trees Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1, v 2,, v k-1, v k with the property that each consecutive pair v i, v i+1 is joined by an edge in E. Def. A path is simple if all nodes are distinct. Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v. Def. An undirected graph is a tree if it is connected and does not contain a cycle. Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. G is connected. G does not contain a cycle. G has n-1 edges. Phylogeny trees. Describe evolutionary history of species. 20 22 24 Copyright 2000, Kevin Wayne 4

Binary Tree Breadth First Search Def. A rooted tree in which each node has at most 2 children Def. Height of a tree is the number of edges in the longest path from root to leaf. root r 1 8 2 3 4 5 7 Thm. Number of nodes in binary tree of height h is 2 1. Balanced Binary Tree. Height log Height: 3.2 Graph Traversal BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time. BFS algorithm. L = { s. s L 1 L 2 L n-1 0 L = all neighbors of L. 1 0 L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge to a node in L 1. L i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L i. 1 2 4 5 7 8 Theorem. For each i, L i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer. 3 25 29 GUI Containment Hierarchy Connectivity Breadth First Search GUI containment hierarchy. Describe organization of GUI widgets. s-t connectivity problem. Given two node s and t, is there a path between s and t? Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. s-t shortest path problem. Given two node s and t, what is the length of the shortest path between s and t? 1 2 3 7 Applications. Navigation (Google Maps). 4 5 8 Maze traversal. Kevin Bacon number. Fewest number of hops in a communication network. 1 L 0 2 3 L 1 4 5 7 8 L 2 Reference: http://java.sun.com/docs/books/tutorial/uiswing/overview/anatomy.html 2 28 30 L 3 Copyright 2000, Kevin Wayne 5

Breadth First Search Connected Component Flood Fill Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. Connected component. Find all nodes reachable from s. Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. Node: pixel. Edge: two neighboring lime pixels. Blob: connected component of lime pixels. recolor lime green blob to blue L 0 L 1 L 2 Connected component containing node 1 = { 1, 2, 3, 4, 5,, 7, 8. L 3 31 33 35 Breadth First Search: Analysis Flood Fill Connected Component Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency representation. Pf. Easy to prove O(n 2 ) running time: at most n lists L[i] each node occurs on at most one list; for loop runs n times when we consider node u, there are n incident edges (u, v), and we spend O(1) processing each edge Actually runs in O(m + n) time: when we consider node u, there are deg(u) incident edges (u,v) total time processing edges is u V deg(u) = 2m Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. Node: pixel. Edge: two neighboring lime pixels. Blob: connected component of lime pixels. recolor lime green blob to blue Connected component. Find all nodes reachable from s. Theorem. Upon termination, R is the connected component containing s. BFS = explore in order of distance from s. DFS = explore in a different way. s R u it's safe to add v v each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v) 32 34 3 Copyright 2000, Kevin Wayne

v 2 v 3 v 2 v 4 v 5 v 1 Testing Bipartiteness Bipartite Graphs 3.4 Testing Bipartiteness Testing bipartiteness. Given a graph G, is it bipartite? Many graph problems become: easier if the underlying graph is bipartite (matching) tractable if the underlying graph is bipartite (independent set) Before attempting to design an algorithm, we need to understand structure of bipartite graphs. Lemma. Let G be a connected graph, and let L 0,, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). v v 5 v 4 v 3 L 1 L 2 L 3 L 1 L 2 L 3 v Case (i) Case (ii) v 7 v 1 v 7 a bipartite graph G another drawing of G 39 41 Bipartite Graphs An Obstruction to Bipartiteness Bipartite Graphs Def. An undirected graph G = (V, E) is bipartite if the nodes can be colored red or blue such that every edge has one red and one blue end. Applications. Stable marriage: men = red, women = blue. Scheduling: machines = red, jobs = blue. Lemma. If a graph G is bipartite, it cannot contain an odd length cycle. Pf. Not possible to 2-color the odd cycle, let alone G. Lemma. Let G be a connected graph, and let L 0,, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). Pf. (i) Suppose no edge joins two nodes in adjacent layers. By previous lemma, this implies all edges join nodes on same level. Bipartition: red = nodes on odd levels, blue = nodes on even levels. bipartite (2-colorable) not bipartite (not 2-colorable) a bipartite graph L 1 L 2 L 3 38 40 Case (i) 42 Copyright 2000, Kevin Wayne 7

Bipartite Graphs Graph Search Lemma. Let G be a connected graph, and let L 0,, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). Pf. (ii) Suppose (x, y) is an edge with x, y in same level L j. Let z = lca(x, y) = lowest common ancestor. Let L i be level containing z. Consider cycle that takes edge from x to y, then path from y to z, then path from z to x. Its length is 1 + (j-i) + (j-i), which is odd. z = lca(x, y) 3.5 Connectivity in Directed Graphs Directed reachability. Given a node s, find all nodes reachable from s. Directed s-t shortest path problem. Given two node s and t, what is the length of the shortest path between s and t? Graph search. BFS extends naturally to directed graphs. Web crawler. Start from web page s. Find all web pages linked from s, either directly or indirectly. (x, y) path from path from y to z z to x 47 43 Obstruction to Bipartiteness Directed Graphs Strong Connectivity Corollary. A graph G is bipartite iff it contain no odd length cycle. Directed graph. G = (V, E) Edge (u, v) goes from node u to node v. Def. Node u and v are mutually reachable if there is a path from u to v and also a path from v to u. Def. A graph is strongly connected if every pair of nodes is mutually reachable. 5-cycle C Lemma. Let s be any node. G is strongly connected iff every node is reachable from s, and s is reachable from every node. bipartite (2-colorable) not bipartite (not 2-colorable) Ex. Web graph - hyperlink points from one web page to another. Directedness of graph is crucial. Modern web search engines exploit hyperlink structure to rank web pages by importance. Pf. Follows from definition. Pf. Path from u to v: concatenate u-s path with s-v path. Path from v to u: concatenate v-s path with s-u path. s u ok if paths overlap v 44 4 48 Copyright 2000, Kevin Wayne 8

Strong Connectivity: Algorithm Directed Acyclic Graphs Directed Acyclic Graphs Theorem. Can determine if G is strongly connected in O(m + n) time. Pf. Pick any node s. Run BFS from s in G. reverse orientation of every edge in G Run BFS from s in G rev. Return true iff all nodes reached in both BFS executions. Correctness follows immediately from previous lemma. Def. An DAG is a directed graph that contains no directed cycles. Ex. Precedence constraints: edge (v i, v j ) means v i must precede v j. Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v 1, v 2,, v n so that for every edge (v i, v j ) we have i < j. v 2 v 3 v 1 v 2 v 3 v 4 v 5 v v 7 Lemma. If G has a topological order, then G is a DAG. Pf. (by contradiction) Suppose that G has a topological order v 1,, v n and that G also has a directed cycle C. Let's see what happens. Let v i be the lowest-indexed node in C, and let v j be the node just before v i ; thus (v j, v i ) is an edge. By our choice of i, we have i < j. On the other hand, since (v j, v i ) is an edge and v 1,, v n is a topological order, we must have j < i, a contradiction. v v 5 v 4 the directed cycle C strongly connected not strongly connected v 7 v 1 a topological ordering v 1 v i v j v n a DAG the supposed topological order: v 1,, v n 49 51 53 Precedence Constraints Directed Acyclic Graphs 3. DAGs and Topological Ordering Precedence constraints. Edge (v i, v j ) means task v i must occur before v j. Applications. Course prerequisite graph: course v i must be taken before v j. Compilation: module v i must be compiled before v j. Pipeline of computing jobs: output of job v i needed to determine input of job v j. Lemma. If G has a topological order, then G is a DAG. Q. Does every DAG have a topological ordering? Q. If so, how do we compute one? 52 54 Copyright 2000, Kevin Wayne 9

Directed Acyclic Graphs Topological Sorting Algorithm: Running Time Lemma. If G is a DAG, then G has a node with no incoming edges. Pf. (by contradiction) Suppose that G is a DAG and every node has at least one incoming edge. Let's see what happens. Pick any node v, and begin following edges backward from v. Since v has at least one incoming edge (u, v) we can walk backward to u. Then, since u has at least one incoming edge (x, u), we can walk backward to x. Repeat until we visit a node, say w, twice. Let C denote the sequence of nodes encountered between successive visits to w. C is a cycle. Theorem. Algorithm finds a topological order in O(m + n) time. Pf. Maintain the following information: count[w] = remaining number of incoming edges S = set of remaining nodes with no incoming edges Initialization: O(m + n) via single scan through graph. Update: to delete v remove v from S decrement count[w] for all edges from v to w, and add w to S if c count[w] hits 0 this is O(1) per edge w x u v 55 57 Directed Acyclic Graphs Lemma. If G is a DAG, then G has a topological ordering. Pf. (by induction on n) Base case: true if n = 1. Given DAG on n > 1 nodes, find a node v with no incoming edges. G - { v is a DAG, since deleting v cannot create cycles. By inductive hypothesis, G - { v has a topological ordering. Place v first in topological ordering; then append nodes of G - {v in topological order. This is valid since v has no incoming edges. DAG v 5 Copyright 2000, Kevin Wayne 10