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

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

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)

Representations of Graphs

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

Chapter 22. Elementary Graph Algorithms

Graph Representation

DFS & STRONGLY CONNECTED COMPONENTS

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

Graph representation

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph: representation and traversal

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

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

Basic Graph Algorithms

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

Graph Algorithms. Definition

CSI 604 Elementary Graph Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Graph implementations :

Introduction to Algorithms. Lecture 11

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

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

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

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

Graph Search. Adnan Aziz

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

Unit 2: Algorithmic Graph Theory

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

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

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Announcements. HW3 is graded. Average is 81%

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

csci 210: Data Structures Graph Traversals

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Graph: representation and traversal

W4231: Analysis of Algorithms

COT 6405 Introduction to Theory of Algorithms

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

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

Outlines: Graphs Part-2

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

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

22.3 Depth First Search

Review: Graph Theory and Representation

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

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

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

Quiz 2 Practice Problems

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

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

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

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

csci 210: Data Structures Graph Traversals

Design and Analysis of Algorithms

Suggested Study Strategy

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

6.006 Introduction to Algorithms Spring 2008

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

Elementary Graph Algorithms

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Data Structures and Algorithms. Chapter 7. Graphs

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

Figure 1: A directed graph.

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

Algorithm Design and Analysis

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

Algorithm Design and Analysis

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

Chapter 22 Elementary Graph Algorithms

Data Structures and Algorithms. Werner Nutt

Elementary Graph Algorithms CSE 6331

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

Sample Solutions to Homework #4

Lecture 11: Analysis of Algorithms (CS ) 1

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

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

Minimum Spanning Trees Ch 23 Traversing graphs

TIE Graph algorithms

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

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Dijkstra s Shortest Path Algorithm

Fundamental Graph Algorithms Part Three

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

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

Graph Algorithms Using Depth First Search

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Introduction to Algorithms November 28, 2007 Massachusetts Institute of Technology Fall 2007 Professors Ron Rivest and Srini Devadas Quiz 2

Homework Assignment #3 Graph

811312A Data Structures and Algorithms, , Exercise 6, solution

CS 310 Advanced Data Structures and Algorithms

Strongly Connected Components

Transcription:

Taking Stock IE170: Algorithms in Systems Engineering: Lecture 16 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University February 28, 2007 Last Time The Wonderful World of This Time Finish Breadth-First Search Depth-First Search Graph Search Algorithms Recall BFS There are two workhorse algorithms for searching graphs that form the basis for many more complicated algorithms. Breadth-First Search (BFS): Search broadly Depth-First Search (DFS): Search deeply BFS: Last Time DFS: Today BFS Input: Graph G = (V, E), source node s V Output: d(v), distance (smallest # of edges) from s to v v V Output: π(v), predecessor of v on the shortest path from s to v Oh no! DP again δ(s, v): shortest path from s to v Lemma: If (u, v) E, then δ(s, v) δ(s, u) + 1

BFS BFS(V, E, s) 1 for each u in V \ {s} 2 do d(u) 3 π(u) nil 4 d[s] 0 5 Q 6 add(q, s) 7 while Q 8 do u poll(q) 9 for each v in Adj[u] 10 do if d[v] = 11 then d[v] d[u] + 1 12 π[v] = u 13 add(q, v) Analysis How many times is each vertex added? Answer: Once. So V for add operation How many times is adjaceny list of vertex v scanned? Answer: Once. Since v V size(adj[v]) = 2 E (for undirected), we have E here. Running time: O( V + E ): Linear in the input size of the graph (for adjacency list implementation) Depth-First Search Compare and Contrast DFS 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 BFS: Discovers all nodes at distance k from starting node s before discovering any node at distance k + 1 DFS: As soon as we discover a vertex, we explore from it. Here we are after creating a different predecessor subgraph G π = (V, E π ) with E π = {(π[v], v) v V, π[v] nil} Not shortest edge-path lengths

DFS Colors DFS (Initialize and Go) In this implementation, we will use colors: green: vertex is undiscovered yellow: vertex is discovered, but not finished red: vertex is finished. (i.e., we have completely explored everything from this node) Discovery and Finish Times Unique integers from 1 to 2 V denoting when you first discover a vertex and when you are done with it d[v] < f[v] v V 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) DFS (Visit Node Recursive) Example 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 ++ Here I will show Java code and an example

Analysis of DFS 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 DFS 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 DFS 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 DFS graph: G π = (V, E π ) forms a forest of subtrees New Definitions 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

Parenthesis Theorem Classifying Edges in the DFS Tree 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 DFS tree 3 If [d[v], f[v]] [d[u], f[u]] then v is a descendent of u in the DFS tree Given a DFS 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 DFS procedure 2 Back Edges: Connect u to an ancestor v in a DFS tree 3 Forward Edges: Connect u to a descendent v in a DFS tree 4 Cross Edges: All other edges. They can be edges in the same DFS tree, or can cross trees in teh DFS forest G π Modifying DFS to Classify Edges DFS Undirected Graphs DFS 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 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 DFS of an undirected graph G = (V, E), every edge is a a tree edge or a back edge.

Next Time Graphs, Graphs, and more Graphs. Additional Hmwk: Problems: 22.2-5, 22.2-6, 22.3-8, 22.4-3