Depth-First Search and Template Patterns

Similar documents
Depth-First Search A B D E C

Template Method. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 24 11/15/2007. University of Colorado, 2007

Data Structures Lecture 14

Tecniche di Progettazione: Design Patterns

Graphs ORD SFO LAX DFW. Data structures and Algorithms

Graph ADT. Lecture18: Graph II. Adjacency Matrix. Representation of Graphs. Data Vertices and edges. Methods

Breadth-First Search L 1 L Goodrich, Tamassia, Goldwasser Breadth-First Search 1

Graphs ORD SFO LAX DFW. Graphs 1

CHAPTER 13 GRAPH ALGORITHMS

DFS on Directed Graphs BOS. Outline and Reading ( 6.4) Digraphs. Reachability ( 6.4.1) Directed Acyclic Graphs (DAG s) ( 6.4.4)

Design Patterns. State. Oliver Haase

Lecture 14: Graph Representation, DFS and Applications

Design Patterns: Template Method, Strategy, State, Bridge

MORE DESIGN PATTERNS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 24 11/10/2011

Template Method Pa.ern and Iterator Pa.ern

CS 225. April 16 Graph Traversal. Data Structures. Wade Fagen-Ulmschneider

Graphs ORD SFO LAX DFW Goodrich, Tamassia Graphs 1

Design Patterns: Composite, Memento, Template Method, Decorator, Chain of Responsibility, Interpreter

Graph Terminology and Representations

Applications of BFS and DFS

DataStruct 13. Graph Algorithms

Lecture 16: Directed Graphs

Directed Graphs BOS Goodrich, Tamassia Directed Graphs 1 ORD JFK SFO DFW LAX MIA

Template Method, Iterator & Composite

Digraphs ( 12.4) Directed Graphs. Digraph Application. Digraph Properties. A digraph is a graph whose edges are all directed.

Graphs ORD SFO LAX DFW

Tecniche di Progettazione: Design Patterns

CS302 - Data Structures using C++

Lecture 9 Graph Traversal

Definition Example Solution

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

Graph Algorithms Using Depth First Search

Graph Algorithms. Definition

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

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

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Suggested Study Strategy

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Spanning trees. Suppose you have a connected undirected graph

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Minimum Spanning Trees Ch 23 Traversing graphs

Spanning Trees. Lecture 22 CS2110 Spring 2017

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

Copyright 2000, Kevin Wayne 1

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

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


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

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

Graph. Vertex. edge. Directed Graph. Undirected Graph

CS 310 Advanced Data Structures and Algorithms

CSE 421 DFS / Topological Ordering

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

Inf 2B: Graphs, BFS, DFS

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

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

I want an Oompa-Loompa! screamed Veruca. Roald Dahl, Charlie and the Chocolate Factory

Data Structures Brett Bernstein

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Searching in Graphs (cut points)

Homework Assignment #3 Graph

Representations of Graphs

Graph Representation

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

csci 210: Data Structures Graph Traversals

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

Graph Search. Adnan Aziz

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

Outline. Graphs. Divide and Conquer.

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

Graph Traversals. Ric Glassey

Announcements. HW3 is graded. Average is 81%

Graphs & Digraphs Tuesday, November 06, 2007

Basic Graph Algorithms

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Graph: representation and traversal

Chapter 22 Elementary Graph Algorithms

Graphs Weighted Graphs. Reading: 12.5, 12.6, 12.7

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

A loose end: binary search

CS/COE

Prim & Kruskal Algorithm

Some major graph problems

Info 2950, Lecture 16

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

Graph Representations and Traversal

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Graph Algorithms shortest paths, minimum spanning trees, etc.

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

Graph Traversals BFS & DFS 1 CS S-16

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

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

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

Unweighted Graphs & Algorithms

Undirected Graphs. Hwansoo Han

Transcription:

Depth-First Search and Template Patterns A B D E C 1

Outline Template Patterns and Search Depth-first search Algorithm Example Properties Analysis Applications of DFS Path finding Cycle finding CS 4407, Algorithms 2

General Form of DFS Introduce general version of DFS Vertex labels Edge labels Show how general version can be easily applied to multiple graph search problems Paths Cycles Example of template method pattern CS 4407, Algorithms

Template Pattern Encapsulates an algorithm by creating a template for it. Defines the skeleton of an algorithm as a set of steps. Some methods of the algorithm have to be implemented by the subclasses these are abstract methods in the super class. The subclasses can redefine certain steps of the algorithm without changing the algorithm s structure. Some steps of the algorithm are concrete methods defined in the super class.

Trivial Example: Programming a Hot Drink Machine CS 4407, Algorithms Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling water Pour tea in cup Add lemon Suppose you are required to implement a system to maintain this

Problems with the Solution Code is duplicated across the classes code changes would have to be made in more than one place. Adding a new beverage would result in further duplication. Knowledge of the algorithm and implementation is distributed over classes.

More General Approach Both subclasses inherit a general algorithm. Some methods in the algorithm are concrete, i.e. methods that perform the same actions for all subclasses. Other methods in the algorithm are abstract, i.e. methods that perform class-specific actions.

Class Diagram for the New Approach Caffeine Beverage {Abstract} preparerecipe() boilw ater() pourincup() NewCoffee NewTea preparerecipe() brewcoffeegrinds() addsugarandmilk() NewTea NewCoffee preparerecipe() steepteabag() addlemon()

Abstracting Prepare Recipe Caffeine Beverage {Abstract} preparerecipe() boilwater() brew() pourincup() addcondiments() NewTea brew() addcondiments() NewCoffee brew() addcondiments()

Advantages of the New Approach A single class protects and controls the algorithm, namely, CaffeineBeverage. The superclass facilitates reuse of methods. Code changes will occur in only one place. Other beverages can be easily added. CS 4407, Algorithms

Template Pattern Structure AbstractClass templatemethod() primitiveoperation1()... primitiveoperationn() concreteoperations ConcreteClass primitiveoperation1()... primitiveoperationn()

Code for the Template public abstract class AbstractClass { final void templatemethod() { primitiveoperation1(); primitiveoperation2(); concreteoperation(); } abstract void primitiveoperation1(); abstract void primitiveoperation2(); void concreteoperation() { //Implementation } }

DFS Template Primitive operations DFS search procedure: node search order Vertex labeling UNEXPLORED, DISCOVERY, VISITED Edge labeling DISCOVERY, BACK Concrete operation (example) Path analysis (via a stack) Path and cycle discovery CS 4407, Algorithms

Using Hooks We want to minimize the number of abstract methods used. Thus, the steps of the algorithm should not be too granular. However, less granularity means less flexibility. Hooks are methods which can be overridden by subclasses, however this is optional. Example: Suppose the customer is given an option as to whether they would like condiments or not.

Why Hooks The number of abstract methods used must be minimized. Enables a subclass to implement an optional part of an algorithm. Enables a subclass to react to a step in the template method. Enables the subclass to make a decision for the abstract class.

Hollywood Principle The template pattern follows the Hollywood principle Don t call us, we will call you. Low-level components are activated by high-level components. A low-level component never calls a high-level component. In the template pattern the abstract class is the high-level component and the concrete classes the low-level components.

Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph CS 4407, Algorithms 17

18 Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components

19 Trees and Forests A (free) tree is an undirected graph T such that T is connected T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Tree Forest

Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph CS 4407, Algorithms Spanning tree 20

21 Depth-First Search Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G DFS on a graph with n vertices and m edges takes O(n + m ) time DFS can be further extended to solve other graph problems Find and report a path between two given vertices Find a cycle in the graph

22 DFS Algorithm The algorithm uses a mechanism for setting and getting labels of vertices and edges Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u G.vertices() setlabel(u, UNEXPLORED) for all e G.edges() setlabel(e, UNEXPLORED) for all v G.vertices() if getlabel(v) = UNEXPLORED DFS(G, v) Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setlabel(v, VISITED) for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) DFS(G, w) else setlabel(e, BACK)

Example A unexplored vertex A A visited vertex unexplored edge B D E discovery edge back edge C A A B D E B D E C C CS 4407, Algorithms 23

24 Example (cont.) A A B D E B D E C C A A B D E B D E C C

25 Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v B A C D E

Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice once as UNEXPLORED once as VISITED Each edge is labeled twice once as UNEXPLORED once as DISCOVERY or BACK Method incidentedges is called once for each vertex DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that S v deg(v) = 2m CS 4407, Algorithms 26

The algorithm uses a mechanism for setting and getting labels of vertices and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u G.vertices() setlabel(u, UNEXPLORED) for all e G.edges() setlabel(e, UNEXPLORED) for all v G.vertices() if getlabel(v) = UNEXPLORED BFS(G, v) BFS Algorithm CS 4407, Algorithms Algorithm BFS(G, s) L 0 new empty sequence L 0.insertLast(s) setlabel(s, VISITED) i 0 while L i.isempty() L i +1 new empty sequence for all v L i.elements() for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) setlabel(w, VISITED) L i +1.insertLast(w) else setlabel(e, CROSS) i i +1

Analysis Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice once as UNEXPLORED once as VISITED Each edge is labeled twice once as UNEXPLORED once as DISCOVERY or CROSS Each vertex is inserted once into a sequence L i Method incidentedges is called once for each vertex BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that S v deg(v) = 2m

Applications Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n + m) time Compute the connected components of G Compute a spanning forest of G Find a simple cycle in G, or report that G is a forest Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists CS 4407, Algorithms

DFS vs. BFS Applications DFS BFS Spanning forest, connected components, paths, cycles Shortest paths Biconnected components A L 0 A B C D L 1 B C D E F L 2 E F DFS BFS

DFS vs. BFS (cont.) Back edge (v,w) w is an ancestor of v in the tree of discovery edges Cross edge (v,w) w is in the same level as v or in the next level in the tree of discovery edges A L 0 A B C D L 1 B C D E F L 2 E F DFS BFS CS 4407, Algorithms

DFS Generalizations via Template Method Path Finding Cycle Finding Others Maze following Solving puzzles (block stacking, etc.) CS 4407, Algorithms

34 Path Finding We can specialize the DFS algorithm to find a path between two given vertices u and z using the template method pattern We call DFS(G, u) with u as the start vertex We use a stack S to keep track of the path between the start vertex and the current vertex As soon as destination vertex z is encountered, we return the path as the contents of the stack Algorithm pathdfs(g, v, z) setlabel(v, VISITED) S.push(v) if v = z return S.elements() for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) S.push(e) pathdfs(g, w, z) S.pop(e) else setlabel(e, BACK) S.pop(v)

Algorithm DFS(G, v) setlabel(v, VISITED) Path Finding from v to z for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) DFS(G, w) else setlabel(e, BACK) DFS CS 4407, Algorithms Stack S Algorithm pathdfs(g, v, z) setlabel(v, VISITED) S.push(v) if v = z return S.elements() for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) S.push(e) pathdfs(g, w, z) S.pop(e) else setlabel(e, BACK) S.pop(v) DFS for Path-Finding

Cycle Finding We can specialize the DFS algorithm to find a simple cycle using the template method pattern We use a stack S to keep track of the path between the start vertex and the current vertex As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w Algorithm cycledfs(g, v, z) setlabel(v, VISITED) S.push(v) for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) S.push(e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) pathdfs(g, w, z) S.pop(e) else T new empty stack repeat o S.pop() T.push(o) until o = w return T.elements() S.pop(v) CS 4407, Algorithms 36

Cycle Finding from v to z Algorithm DFS(G, v) setlabel(v, VISITED) for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) DFS(G, w) else setlabel(e, BACK) Algorithm cycledfs(g, v, z) setlabel(v, VISITED) S.push(v) for all e G.incidentEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) S.push(e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) pathdfs(g, w, z) S.pop(e) else T new empty stack repeat o S.pop() T.push(o) until o = w return T.elements() S.pop(v)

38 DFS and Maze Traversal The DFS algorithm is similar to a classic strategy for exploring a maze We mark each intersection, corner and dead end (vertex) visited We mark each corridor (edge ) traversed We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

Sorting Using the Template Pattern Java s Array class provides a template method for sorting. The sort is a merge sort and requires a method compareto(). Code: public static void sort(object a[]){ Object aux[] = (Object a[])a.clone); mergesort(aux,a,0,a.length,0); } private static void mergesort(object src[], Object dest[], int low,int high, int off) { for(int i=low; i < high; ++i){ for(int j=i; j < low; && ((Comparable)dest[j-1).compareTo((Comparable)dest[j])>0;j--) { swap(dest, j, j-1); } } return; }