Lecture 12: Depth-First Search Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Similar documents
Data Structures and Algorithms Written Examination

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Exam study sheet for CS2711. List of topics

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Ordered Lists and Binary Trees

Algorithms and Data Structures

Analysis of Algorithms, I

Data Structure with C

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

CMPSCI611: Approximating MAX-CUT Lecture 20

Classification/Decision Trees (II)

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

GRAPH THEORY LECTURE 4: TREES

Data Structures. Chapter 8

From Last Time: Remove (Delete) Operation

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Data Structures Fibonacci Heaps, Amortized Analysis

Problem Set 7 Solutions

Binary Search Trees CMPSC 122

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Lecture 2: Data Structures Steven Skiena. skiena

Data Structures and Algorithms

Chapter 7: Termination Detection

Any two nodes which are connected by an edge in a graph are called adjacent node.

Priority Based Enhancement of Online Power-Aware Routing in Wireless Sensor Network. Ronit Nossenson Jerusalem College of Technology

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

Subgraph Patterns: Network Motifs and Graphlets. Pedro Ribeiro

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Part 2: Community Detection

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

Graph Theory Problems and Solutions

Approximation Algorithms

Analysis of Algorithms I: Binary Search Trees

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CSC 505, Fall 2000: Week 8

Data Structures. Level 6 C Module Descriptor

Rotation Operation for Binary Search Trees Idea:

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Network (Tree) Topology Inference Based on Prüfer Sequence


Lecture 19: Introduction to NP-Completeness Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Parallelization: Binary Tree Traversal

Converting a Number from Decimal to Binary

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

A Fast Algorithm For Finding Hamilton Cycles

CompSci-61B, Data Structures Final Exam

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

5.1 Bipartite Matching

Lecture 1: Course overview, circuits, and formulas

International Journal of Software and Web Sciences (IJSWS)

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Binary Trees and Huffman Encoding Binary Search Trees

The LCA Problem Revisited

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

International Journal of Advanced Research in Computer Science and Software Engineering

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Lecture Notes on Binary Search Trees

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Outline 2.1 Graph Isomorphism 2.2 Automorphisms and Symmetry 2.3 Subgraphs, part 1

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

INDISTINGUISHABILITY OF ABSOLUTELY CONTINUOUS AND SINGULAR DISTRIBUTIONS

Topology-based network security

2) Write in detail the issues in the design of code generator.

Adding New Level in KDD to Make the Web Usage Mining More Efficient. Abstract. 1. Introduction [1]. 1/10

Data Structures Using C++

Simplified External memory Algorithms for Planar DAGs. July 2004

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

PES Institute of Technology-BSC QUESTION BANK

TU e. Advanced Algorithms: experimentation project. The problem: load balancing with bounded look-ahead. Input: integer m 2: number of machines

The Graphical Method: An Example

Introduction Solvability Rules Computer Solution Implementation. Connect Four. March 9, Connect Four

Lecture 10: Regression Trees

Vector storage and access; algorithms in GIS. This is lecture 6

A New Marketing Channel Management Strategy Based on Frequent Subtree Mining

Algorithm Design and Analysis

Scheduling Shop Scheduling. Tim Nieberg

The Basics of Graphical Models

5 Directed acyclic graphs

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

Dynamic programming. Doctoral course Optimization on graphs - Lecture 4.1. Giovanni Righini. January 17 th, 2013

TREE BASIC TERMINOLOGIES

Lecture 12 Doubly Linked Lists (with Recursion)

Sample Questions Csci 1112 A. Bellaachia

Lecture Notes on Binary Search Trees

The Goldberg Rao Algorithm for the Maximum Flow Problem

TORA : Temporally Ordered Routing Algorithm

DATA STRUCTURES USING C

Symbol Tables. Introduction

Network Flow I. Lecture Overview The Network Flow Problem

Data Structure [Question Bank]

Satisfiability Checking

Transcription:

Lecture 12: Depth-First Search Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena

Problem of the Day Prove that in a breadth-first search on a undirected graph G, every edge in G is either a tree edge or a cross edge, where a cross edge (x, y) is an edge where x is neither is an ancestor or descendent of y.

Depth-First Search DFS has a neat recursive implementation which eliminates the need to explicitly use a stack. Discovery and final times are a convenience to maintain. dfs(graph *g, int v) { edgenode *p; (* temporary pointer *) int y; (* successor vertex *) if (finished) return; (* allow for search termination *) discovered[v] = TRUE; time = time + 1; entry time[v] = time; process vertex early(v); p = g >edges[v]; while (p! = NULL) { y = p >y;

if (discovered[y] == FALSE) { parent[y] = v; process edge(v,y); dfs(g,y); } else if ((!processed[y]) (g >directed)) process edge(v,y); if (finished) return; } p = p >next; process vertex late(v); time = time + 1; exit time[v] = time; } processed[v] = TRUE;

The Key Idea with DFS A depth-first search of a graph organizes the edges of the graph in a precise way. In a DFS of an undirected graph, we assign a direction to each edge, from the vertex which discover it: 1 2 3 2 6 1 4 3 5 4 6 5

Edge Classification for DFS Every edge is either: 1. A Tree Edge 3. A Forward Edge to a decendant 2. A Back Edge to an ancestor 4. A Cross Edge to a different node On any particular DFS or BFS of a directed or undirected graph, each edge gets classified as one of the above.

Edge Classification Implementation int edge classification(int x, int y) { if (parent[y] == x) return(tree); if (discovered[y] &&!processed[y]) return(back); if (processed[y] && (entry time[y] entry time[x])) return(forward); if (processed[y] && (entry time[y] entry time[x])) return(cross); } printf( Warning: unclassified edge (%d,%d),x,y);

DFS: Tree Edges and Back Edges Only The reason DFS is so important is that it defines a very nice ordering to the edges of the graph. In a DFS of an undirected graph, every edge is either a tree edge or a back edge. Why? Suppose we have a forward edge. We would have encountered (4, 1) when expanding 4, so this is a back edge. 1 2 3 4

No Cross Edges in DFS Suppose we have a cross-edge 1 2 5 When expanding 2, we would discover 5, so the tree would look like: 3 4 6 1 2 3 4 5 6

DFS Application: Finding Cycles Back edges are the key to finding a cycle in an undirected graph. Any back edge going from x to an ancestor y creates a cycle with the path in the tree from y to x. process edge(int x, int y) { if (parent[x]! = y) { (* found back edge! *) printf( Cycle from %d to %d:,y,x); find path(y,x,parent); finished = TRUE; } }

Articulation Vertices Suppose you are a terrorist, seeking to disrupt the telephone network. Which station do you blow up? An articulation vertex is a vertex of a connected graph whose deletion disconnects the graph. Clearly connectivity is an important concern in the design of any network. Articulation vertices can be found in O(n(m+n)) just delete each vertex to do a DFS on the remaining graph to see if it is connected.

A Faster O(n + m) DFS Algorithm In a DFS tree, a vertex v (other than the root) is an articulation vertex iff v is not a leaf and some subtree of v has no back edge incident until a proper ancestor of v. X The root is a special case since it has no ancestors. X is an articulation vertex since the right subtree does not have a back edge to a proper ancestor. Leaves cannot be articulation vertices

Topological Sorting A directed, acyclic graph has no directed cycles. B D A C E G F A topological sort of a graph is an ordering on the vertices so that all edges go from left to right. DAGs (and only DAGs) has at least one topological sort (here G, A, B, C, F, E, D).

Applications of Topological Sorting Topological sorting is often useful in scheduling jobs in their proper sequence. In general, we can use it to order things given precidence constraints. Example: Dressing schedule from CLR.

Example: Identifying errors in DNA fragment assembly Certain fragments are constrained to be to the left or right of other fragments, unless there are errors. A B R A C A C A D A A D A B R D A B R A R A C A D A B R A C A D A B R A A B R A C R A C A D A C A D A A D A B R D A B R A Solution build a DAG representing all the left-right constraints. Any topological sort of this DAG is a consistant ordering. If there are cycles, there must be errors.

Topological Sorting via DFS A directed graph is a DAG if and only if no back edges are encountered during a depth-first search. Labeling each of the vertices in the reverse order that they are marked processed finds a topological sort of a DAG. Why? Consider what happens to each directed edge {x, y} as we encounter it during the exploration of vertex x:

Case Analysis If y is currently undiscovered, then we then start a DFS of y before we can continue with x. Thus y is marked completed before x is, and x appears before y in the topological order, as it must. If y is discovered but not completed, then {x, y} is a back edge, which is forbidden in a DAG. If y is completed, then it will have been so labeled before x. Therefore, x appears before y in the topological order, as it must.

Topological Sorting Implementation process vertex late(int v) { push(&sorted,v); } process edge(int x, int y) { int class; } class = edge classification(x,y); if (class == BACK) printf( Warning: directed cycle found, not a DAG ); We push each vertex on a stack soon as we have evaluated all outgoing edges. The top vertex on the stack always has no incoming edges from any vertex on the stack, repeatedly popping them off yields a topological ordering.

Strongly Connected Components A directed graph is strongly connected iff there is a directed path between any two vertices. The strongly connected components of a graph is a partition of the vertices into subsets (maximal) such that each subset is strongly connected. a b e f c d g h Observe that no vertex can be in two maximal components, so it is a partition.

There is an elegant, linear time algorithm to find the strongly connected components of a directed graph using DFS which is similar to the algorithm for biconnected components.

Backtracking and Depth-First Search Depth-first search uses essentially the same idea as backtracking. Both involve exhaustively searching all possibilities by advancing if it is possible, and backing up as soon as there is no unexplored possibility for further advancement. Both are most easily understood as recursive algorithms.