Graphs 1.7

# Graphs 1.7 - Properties of DFS Property 1 DFS-Visit(u)...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Properties of DFS Property 1 DFS-Visit(u) visits all the vertices and edges in the connected component of u A Property 2 The discovery edges labeled by DFS-Visit(u) form a spanning tree of the connected component of u CSE 2011 Prof. J. Elder B D E C - 31 - Last Updated: 4/1/10 10:16 AM DFS Algorithm Pattern DFS(G) Precondition: G is a graph Postcondition: all vertices in G have been visited for each vertex u V [G] color[u] = BLACK //initialize vertex for each vertex u V [G] if color[u] = BLACK //as yet unexplored total work = (V ) DFS-Visit(u) CSE 2011 Prof. J. Elder - 32 - Last Updated: 4/1/10 10:16 AM DFS Algorithm Pattern DFS-Visit (u) Precondition: vertex u is undiscovered Postcondition: all vertices reachable from u have been processed colour[u ] RED for each v Adj[u ] //explore edge (u,v ) if color[v ] = BLACK colour [u ] DFS-Visit(v ) GRAY total work = | Adj[v ] | = (E ) vV Thus running time = (V + E ) (assuming adjacency list structure) CSE 2011 Prof. J. Elder - 33 - Last Updated: 4/1/10 10:16 AM Variants of Depth-First Search In addition to, or instead labeling vertices with colours, they can be labeled with discovery and finishing times. ‘Time’ is an integer that is incremented whenever a vertex changes state from unexplored to discovered from discovered to finished These discovery and finishing times can then be used to solve other graph problems (e.g., computing strongly-connected components) CSE 2011 Prof. J. Elder - 34 - Last Updated: 4/1/10 10:16 AM DFS Algorithm with Discovery and Finish Times DFS(G) Precondition: G is a graph Postcondition: all vertices in G have been visited for each vertex u V [G] color[u] = BLACK //initialize vertex time 0 for each vertex u V [G] if color[u] = BLACK //as yet unexplored DFS-Visit(u) CSE 2011 Prof. J. Elder - 35 - Last Updated: 4/1/10 10:16 AM ...
View Full Document

Ask a homework question - tutors are online