Graphs ORD SFO LAX DFW

Similar documents
Graph Terminology and Representations

Graphs ORD SFO LAX DFW. Lecture notes adapted from Goodrich and Tomassia. 3/14/18 10:28 AM Graphs 1

Graphs 3/25/14 15:37 SFO LAX Goodrich, Tamassia, Goldwasser Graphs 2

Graphs ORD SFO LAX DFW. Data structures and Algorithms

Minimum Spanning Trees

Minimum Spanning Trees

Data Structures Lecture 14

Minimum Spanning Trees. Application: Connecting a Network

Graph Terminology and Representations

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

CHAPTER 13 GRAPH ALGORITHMS

Graphs ORD SFO LAX DFW. Graphs 1

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

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

CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

Depth-First Search A B D E C

Graphs ADTs and Implementations

On (K t e)-saturated Graphs

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

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

Depth-First Search and Template Patterns

The isoperimetric problem on the hypercube

Lecture 16: Directed Graphs

Combination Labelings Of Graphs

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

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

Graphs ORD SFO LAX DFW Goodrich, Tamassia Graphs 1

CIS 121. Introduction to Trees

Name of the Student: Unit I (Logic and Proofs) 1) Truth Table: Conjunction Disjunction Conditional Biconditional

INTERSECTION CORDIAL LABELING OF GRAPHS

Lecture 2: Spectra of Graphs

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Perhaps the method will give that for every e > U f() > p - 3/+e There is o o-trivial upper boud for f() ad ot eve f() < Z - e. seems to be kow, where

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

6.854J / J Advanced Algorithms Fall 2008

Strong Complementary Acyclic Domination of a Graph

Ones Assignment Method for Solving Traveling Salesman Problem

CS 111: Program Design I Lecture 21: Network Analysis. Robert H. Sloan & Richard Warner University of Illinois at Chicago April 10, 2018

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling

Examples and Applications of Binary Search

Lecture 14: Graph Representation, DFS and Applications

Computers and Scientific Thinking

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

condition w i B i S maximum u i

Some non-existence results on Leech trees

Biconnected Components

Graphs. Shortest Path and Topological Sort

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem

Module 8-7: Pascal s Triangle and the Binomial Theorem

Computational Geometry

c-dominating Sets for Families of Graphs

Random Graphs and Complex Networks T

1.2 Binomial Coefficients and Subsets

1 Graph Sparsfication

CS 683: Advanced Design and Analysis of Algorithms

Some cycle and path related strongly -graphs

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

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

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

Graphs. CS16: Introduction to Data Structures & Algorithms Spring 2018

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 16, March 10, 2016

Design and Analysis of Algorithms Notes

MAXIMUM MATCHINGS IN COMPLETE MULTIPARTITE GRAPHS

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Spanning Maximal Planar Subgraphs of Random Graphs

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

. Written in factored form it is easy to see that the roots are 2, 2, i,

Data Structures Week #5. Trees (Ağaçlar)

Lecture 5. Counting Sort / Radix Sort

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Analysis of Algorithms

Alpha Individual Solutions MAΘ National Convention 2013

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes

A study on Interior Domination in Graphs

Graphs Weighted Graphs. Reading: 12.5, 12.6, 12.7

DataStruct 13. Graph Algorithms

Graph Algorithms shortest paths, minimum spanning trees, etc.

Super Vertex Magic and E-Super Vertex Magic. Total Labelling

Big-O Analysis. Asymptotics

Graph Algorithms shortest paths, minimum spanning trees, etc.

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

Big-O Analysis. Asymptotics

Average Connectivity and Average Edge-connectivity in Graphs

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

Thompson s Group F (p + 1) is not Minimally Almost Convex

Journal of Mathematical Nanoscience. Sanskruti Index of Bridge Graph and Some Nanocones

Message Integrity and Hash Functions. TELE3119: Week4

Parabolic Path to a Best Best-Fit Line:

A Note on Chromatic Transversal Weak Domination in Graphs

Data Structures and Algorithms. Analysis of Algorithms

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

5.3 Recursive definitions and structural induction

Algorithm Design Techniques. Divide and conquer Problem

CS 111: Program Design I Lecture 19: Networks, the Web, and getting text from the Web in Python

Counting the Number of Minimum Roman Dominating Functions of a Graph

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Transcription:

Graphs SFO 337 1843 802 ORD LAX 1233 DFW

Graphs A graph is a pair (V, E), where V is a set of odes, called vertices E is a collectio of pairs of vertices, called edges Vertices ad edges are positios ad store elemets Example: A vertex represets a airport ad stores the three-letter airport code A edge represets a flight route betwee two airports ad stores the mileage of the route HNL 2555 SFO LAX 2014 Goodrich, Tamassia, Goldwasser 337 1843 1233 Graphs 802 ORD DFW 849 1120 LGA PVD MIA 2

Edge Types Directed edge ordered pair of vertices (u,v) first vertex u is the origi secod vertex v is the destiatio e.g., a flight Directed graph all the edges are directed e.g., route etwork Udirected edge uordered pair of vertices (u,v) e.g., a flight route Udirected graph all the edges are udirected e.g., flight etwork SFO LAX Soeyik Goodrich UA 120 AA 49 SW 45 ORD UA 877 DL 335 DFW AA 411 AA 523 AA 1387 Garg Goldwasser Tamassia Tollis NW 35 JFK AA 903 MIA BOS DL 247 Vitter Preparata Chiag Graphs 3

Applicatios Electroic circuits Prited circuit board Itegrated circuit Trasportatio etworks Highway etwork Flight etwork Computer etworks Local area etwork Iteret Web Databases Etity-relatioship diagram cslab1a cslab1b math.brow.edu cs.brow.edu brow.edu west.et att.et cox.et Joh Paul David 2014 Goodrich, Tamassia, Goldwasser Graphs 4

Termiology Ed vertices (or edpoits) of a edge U ad V are the edpoits of a Edges icidet o a vertex a a, d, ad b are icidet o V outgoig edges U icomig edges Adjacet vertices c U ad V are adjacet Degree of a vertex w X has degree 5 i-degree out-degree Parallel edges h ad i are parallel edges Self-loop j is a self-loop V d W f b e X Y g h i Z j Graphs 5

Questio What is the sum of degrees of all vertices? Graphs 6

Termiology (2) Path seuece of alteratig vertices ad edges begis with a vertex eds with a vertex each edge is preceded ad followed by its edpoits Simple path path such that all its vertices ad edges are distict Examples P 1 =(V,b,X,h,Z) is a simple path P 2 =(U,c,W,e,X,g,Y,f,W,d,V) is a path that is ot simple SFO U a c V d P 2 W b e f ORD P 1 X Y g h JFK BOS Z LAX DFW MIA 2014 Goodrich, Tamassia, Goldwasser Graphs 7

Termiology (3) Cycle circular seuece of alteratig vertices ad edges each edge is preceded ad followed by its edpoits Simple cycle cycle such that all its vertices ad edges are distict Examples C 1 =(V,b,X,g,Y,f,W,c,U,a, ) is a simple cycle C 2 =(U,c,W,e,X,g,Y,f,W,d,V,a, ) is a cycle that is ot simple U a c V d C 2 W f e b X Y C 1 g h Z 2014 Goodrich, Tamassia, Goldwasser Graphs 8

Termiology (4) BOS Reachable V is reachable from U if there is a path from U to V. Udirected graph reachability is symmetric Coected a graph is coected if, for ay two vertices, there is a path betwee them some path subgraph subset of vertices ad edges formig a graph SFO LAX DFW ORD JFK MIA coected Graphs ot coected 9

Termiology (5) Coected Compoet: maximal coected subgraph, e.g., the graph below has 3 coected compoets. Graphs 10

Termiology (6) Tree coected graph without cycles free tree Forest collectio of trees tree tree forest tree tree Graphs 11

Coectivity Let = # of vertices m = # of edges = 5 m = (5 4)/2 = 10 Complete graph all pairs of vertices are adjacet There (-1)/2 pairs of vertices ad so m=(-1)/2 if a graph is ot complete, m<(-1)/2 Graphs 12

Coectivity (2) = #vertices m= #edges For a tree m=-1 = 5 m = 4 Prove! Tree coected graph without cycles Graphs 13

Coectivity (3) = #vertices m= #edges For a tree m=-1 If m<-1, G is ot coected = 5 m = 3 Prove Graphs 14

Spaig Tree A spaig tree of G is a subgraph which is a tree cotais all vertices of G G spaig tree of G Failure of ay edge discoects system Graphs 15

Euler ad the Koeigsberg Bridges Gilliga s Isle? C Pregal River A D B Ca oe walk across each bridge exactly oce ad retur at the startig poit? I 1736, Euler proved that this is ot possible. Graphs 16

Graph Model (with parallel edges) Euleria Tour- path that traverses every edge exactly oce ad returs to the first vertex Euler s Theorem A graph has a Euleria tour if ad oly if all vertices have eve degree C A D Graphs B 17

Vertices ad Edges A graph is a collectio of vertices ad edges. We model the abstractio as a combiatio of three data types: Vertex, Edge, ad Graph. A Vertex is a lightweight object that stores a arbitrary elemet provided by the user (e.g., a airport code) We assume it supports a method, elemet(), to retrieve the stored elemet. A Edge stores a associated object (e.g., a flight umber, travel distace, cost), retrieved with the elemet( ) method. 2014 Goodrich, Tamassia, Goldwasser Graphs 18

Edge List Structure Vertex object elemet referece to positio i vertex seuece Edge object elemet origi vertex object destiatio vertex object referece to positio i edge seuece Vertex seuece seuece of vertex objects Edge seuece seuece of edge objects 2014 Goodrich, Tamassia, Goldwasser Graphs 19

Performace vertices, m edges o parallel edges o self-loops Edge List Adjacecy List Adjacecy Matrix Space + m + m 2 icidetedges(v) m deg(v) areadjacet (v, w) m mi(deg(v), deg(w)) 1 isertvertex(o) 1 1 2 isertedge(v, w, o) 1 1 1 removevertex(v) m deg(v) 2 removeedge(e) 1 1 1 2014 Goodrich, Tamassia, Goldwasser Graphs 20

Adjacecy List Structure Icidece seuece for each vertex seuece of refereces to edge objects of icidet edges Augmeted edge objects refereces to associated positios i icidece seueces of ed vertices 2014 Goodrich, Tamassia, Goldwasser Graphs 21

Performace vertices, m edges o parallel edges o self-loops Edge List Adjacecy List Adjacecy Matrix Space + m + m 2 icidetedges(v) m deg(v) areadjacet (v, w) m mi(deg(v), deg(w)) 1 isertvertex(o) 1 1 2 isertedge(v, w, o) 1 1 1 removevertex(v) m deg(v) 2 removeedge(e) 1 1 1 2014 Goodrich, Tamassia, Goldwasser Graphs 22

Adjacecy Matrix Structure Edge list structure Augmeted vertex objects Iteger key (idex) associated with vertex 2D-array adjacecy array Referece to edge object for adjacet vertices Null for o oadjacet vertices The old fashioed versio just has 0 for o edge ad 1 for edge 2014 Goodrich, Tamassia, Goldwasser Graphs 23

Performace vertices, m edges o parallel edges o self-loops Edge List Adjacecy List Adjacecy Matrix Space + m + m 2 icidetedges(v) m deg(v) areadjacet (v, w) m mi(deg(v), deg(w)) 1 isertvertex(o) 1 1 2 isertedge(v, w, o) 1 1 1 removevertex(v) m deg(v) 2 removeedge(e) 1 1 1 2014 Goodrich, Tamassia, Goldwasser Graphs 24

Graph Search Algorithms Systematic search of every edge ad vertex of the graph Graph G = (V, E) is either directed or udirected Applicatios Compilers Networks Graphics Gamig 25

Breadth-First Search L 0 A L 1 B C D L 2 E F

Breadth-First Search Breadth-first search (BFS) is a geeral techiue for traversig a graph A BFS traversal of a graph G Visits all the vertices ad edges of G Determies whether G is coected Computes the coected compoets of G Computes a spaig forest of G BFS o a graph with vertices ad m edges takes O( + m ) time BFS ca be further exteded to solve other graph problems Fid ad report a path with the miimum umber of edges betwee two give vertices 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 27

BFS Algorithm The algorithm uses a mechaism for settig ad gettig labels of vertices ad edges Algorithm BFS(G) Iput graph G Output labelig of the edges ad partitio 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) Algorithm BFS(G, s) L 0 ew empty seuece L 0.addLast(s) setlabel(s, VISITED) i 0 while L i.isempty() L i +1 ew empty seuece for all v L i.elemets() for all e G.icidetEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) setlabel(w, VISITED) L i +1.addLast(w) else setlabel(e, CROSS) i i +1 2014 Goodrich, Tamassia, Goldwasser 28 Breadth First Search

Example A uexplored vertex L 0 A A visited vertex uexplored edge L 1 B C D discovery edge cross edge E F L 0 A L 0 A L 1 B C D L 1 B C D E F E F 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 29

Example (cot.) L 0 A L 0 A L 1 B C D L 1 B C D E F L 2 E F L 0 A L 0 A L 1 B C D L 1 B C D L 2 E F L 2 E F 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 30

Example (cot.) L 0 A L 0 A L 1 B C D L 1 B C D L 2 E F L 2 E F L 0 A L 1 B C D L 2 E F 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 31

BFS - Example 0 A B C D E F G H I J K L M N O P Breadth First Search 32

BFS - Example 0 1 A B C D E F G H I J K L M N O P Breadth First Search 33

BFS - Example 0 1 2 A B C D E F G H I J K L M N O P Breadth First Search 34

BFS - Example 0 1 2 3 A B C D E F G H I J K L M N O P Breadth First Search 35

BFS - Example 0 1 2 3 A B C D E F G H 4 I J K L M N O P Breadth First Search 36

BFS - Example 0 1 2 3 A B C D E F G H 4 I J K L M N O P 5 Breadth First Search 37

Aalysis Settig/gettig a vertex/edge label takes O(1) time Each vertex is labeled twice oce as UNEXPLORED oce as VISITED Each edge is labeled twice oce as UNEXPLORED oce as DISCOVERY or CROSS Each vertex is iserted oce ito a seuece L i Method icidetedges is called oce for each vertex BFS rus i O( + m) time provided the graph is represeted by the adjacecy list structure Recall that Σ v deg(v) = 2m 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 38

Properties Notatio G s : coected compoet of s G s is a breadth first tree V s cosists of the vertices reachable from s, ad for all v i V s there is a uiue simple path from s to v i G that is also a shortest path from s to v i G The edges i G s are called tree edges For every vertex v reachable from s, the path i the breadth first tree from s to v, correspods to a shortest path i G L 1 B L 0 L 2 B A E A E C C F F D D Breadth First Search 39

Properties (2) Notatio G s : coected compoet of s Property 1 BFS(G, s) visits all the vertices ad edges of G s Property 2 The discovery edges labeled by BFS(G, s) form a spaig tree T s of G s Property 3 B L 0 A E A C F D For each vertex v i L i The path of T s from s to v has i edges Every path from s to v i G s has at least i edges L 1 L 2 B E C F D 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 40

Applicatios We ca specialize the BFS traversal of a graph G to solve the followig problems i O( + m) time Compute the coected compoets of G Compute a spaig forest of G Give two vertices of G, fid a path i G betwee them with the miimum umber of edges, or report that o such path exists Check if a coected graph G is bipartite. Breadth First Search 41

Applicatios Computig Coected Compoets a b c d f g e h k j l i Breadth First Search 42

BFS Algorithm The algorithm uses a mechaism for settig ad gettig labels of vertices ad edges Algorithm BFS(G) Iput graph G Output labelig of the edges ad partitio 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) Algorithm BFS(G, s) L 0 ew empty seuece L 0.addLast(s) setlabel(s, VISITED) i 0 while L i.isempty() L i +1 ew empty seuece for all v L i.elemets() for all e G.icidetEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) setlabel(w, VISITED) L i +1.addLast(w) else setlabel(e, CROSS) i i +1 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 43

Applicatios Computig Spaig Forest a b c d f g e h k j l i Breadth First Search 44

Applicatios Shortest Path betwee two odes I a BFS startig from a vertex v, the level umber of vertex u is the legth of the shortest path from v to u. Proof show there is a path from u to v. a path of smaller legth will jump a level w violatig the BFS. Breadth First Search 45

Applicatios Check if a graph is bipartite A udirected graph G = (V, E) is bipartite if the odes ca be colored red or blue such that every edge has oe red ad oe blue ed. Breadth First Search 46

Testig Bipartiteess Give a graph G, is it bipartite? Before attemptig to desig a algorithm, we eed to uderstad structure of bipartite graphs. v 2 v 3 v 2 v 1 v 4 v 6 v 5 v 4 v 3 v 5 v 6 v 7 v 1 v 7 a bipartite graph G aother drawig of G Breadth First Search 47

Prelimiary Lemma. If G has a odd cycle, the it caot be bipartite. Proof: Not possible to 2-color the odd cycle, let aloe G. bipartite (2-colorable) ot bipartite (ot 2-colorable) Breadth First Search 48

Bipartiteess ad BFS Lemma. Let G be a coected graph, ad let L 0,, L k be the layers produced by BFS startig at ode s. Exactly oe of the followig holds. case 1: No edge of G jois two odes of the same layer, ad G is bipartite. L 1 L 1 L 2 L 2 L 3 L 3 Breadth First Search 49

Bipartiteess ad BFS Lemma. Let G be a coected graph, ad let L 0,, L k be the layers produced by BFS startig at ode s. Exactly oe of the followig holds. case 1: No edge of G jois two odes of the same layer, ad G is bipartite. case 2: A edge of G jois two odes of the same layer, ad G cotais a odd-legth cycle (ad hece is ot bipartite). L 1 L 2 L 3 Breadth First Search 50

Bipartiteess ad BFS Lemma. Let G be a coected graph, ad let L 0,, L k be the layers produced by BFS startig at ode s. Exactly oe of the followig holds. case 1: No edge of G jois two odes of the same layer, ad G is bipartite. case 2: A edge of G jois two odes of the same layer, ad G cotais a odd-legth cycle (ad hece is ot bipartite). Proof case 2: Suppose (x,y) is a edge with x, y i the same level L j. Let z = LCA(x, y) lowest commo acestor Let L i be the level cotaiig z Cosider the cycle that takes edge from x to y, the the path from y to x, the z to x The legth of this path is 1+(j-i)+(j-i), which is odd Breadth First Search 51

Questio If G has oly eve legth cycles, the G is bipartite. Breadth First Search 52

Depth-First Search A B D E C

Depth-First Search Depth-first search (DFS) is a geeral techiue for traversig a graph A DFS traversal of a graph G Visits all the vertices ad edges of G Determies whether G is coected Computes the coected compoets of G Computes a spaig forest of G DFS o a graph with vertices ad m edges takes O( + m ) time DFS ca be further exteded to solve other graph problems Fid ad report a path betwee two give vertices Fid a cycle i the graph Depth-first search is to graphs what Euler tour is to biary trees 2014 Goodrich, Tamassia, Goldwasser Depth First Search 54

Example A uexplored vertex A A visited vertex uexplored edge B D E discovery edge back edge C A A B D E B D E C C 2014 Goodrich, Tamassia, Goldwasser Depth First Search 55

Example (cot.) A A B D E B D E C C A A B D E B D E C C 2014 Goodrich, Tamassia, Goldwasser Depth First Search 56

DFS ad Maze Traversal The DFS algorithm is similar to a classic strategy for explorig a maze We mark each itersectio, corer ad dead ed (vertex) visited We mark each corridor (edge ) traversed We keep track of the path back to the etrace (start vertex) by meas of a rope (recursio stack) 2014 Goodrich, Tamassia, Goldwasser Depth First Search 57

DFS for a Etire Graph The algorithm uses a mechaism for settig ad gettig labels of vertices ad edges Algorithm DFS(G) Iput graph G Output labelig of the edges of G as discovery edges ad 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) Iput graph G ad a start vertex v of G Output labelig of the edges of G i the coected compoet of v as discovery edges ad back edges setlabel(v, VISITED) for all e G.icidetEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) DFS(G, w) else setlabel(e, BACK) 2014 Goodrich, Tamassia, Goldwasser Depth First Search 58

Example A B C D E F G H I J K L M N O P Depth First Search 59

Example A B C D E F G H I J K L M N O P Depth First Search 60

Example A B C D E F G H I J K L M N O P Depth First Search 61

Example A B C D E F G H I J K L M N O P Depth First Search 62

Example A B C D E F G H I J K L M N O P Depth First Search 63

Example A B C D E F G H I J K L M N O P Depth First Search 64

Aalysis of DFS Settig/gettig a vertex/edge label takes O(1) time Each vertex is labeled twice oce as UNEXPLORED oce as VISITED Each edge is labeled twice oce as UNEXPLORED oce as DISCOVERY or BACK Method icidetedges is called oce for each vertex DFS rus i O( + m) time provided the graph is represeted by the adjacecy list structure Recall that Σ v deg(v) = 2m 2014 Goodrich, Tamassia, Goldwasser Depth First Search 65

Properties of DFS Property 1 DFS(G, v) visits all the vertices ad edges i the coected compoet of v Property 2 The discovery edges labeled by DFS(G, v) form a spaig tree of the coected compoet of v B A C D E 2014 Goodrich, Tamassia, Goldwasser Depth First Search 66