Algorithms and Data Structures Lecture 9 Agenda: Graph traverse Breadth-first search algorithm Depth-first search algorithm Topological sort Graphs: traverse Sometimes we need to perform some procedure on all vertices of a graph Task can be accomplished visiting each vertex of a graph (from vertex from vertex) and performing required procedure Such task is known as graph traverse Let’s examine two well-known graph traverse algorithms: breadth-first search and depth-first search algorithms Graphs: traverse The ideas behind graph traverse algorithms are the following: For given graph G and some vertex s, algorithm builds paths from vertex s to all vertices, attainable from s Paths built from a given vertex constitute a tree Some vertices of a graph (unattainable from vertex s) may remain out of a tree; procedure is repeated until all the vertices of a graph will be covered by some tree Trees (of a given graph) constitute a forest Graphs: traverse Graphs: traverse Graphs: traverse Graphs: breadth-first search Breadth-first search is one of graph traverse algorithms Algorithm is often used by other algorithms Algorithm is applicable for both directed and undirected graphs Graphs: breadth-first search Input: graph G Output: breadth-first forest Any vertex of a G may be colored in white, gray or black color White vertices are unvisited, gray – discovered vertices, black – already processed vertices (finished) Q – auxiliary storage (queue) of FIFO type; holds discovered vertices (gray) Initially, G consists of white vertices only; finally, all the vertices of a G are black Graphs: breadth-first search Step 1: all the vertices are marked as white Step 2: start with any white vertex s; it is marked as gray and added to the Q Step 3: get first vertex x from the Q Step 4: each adjacent to x white vertex is added to the Q and marked as gray Step 5: vertex x is removed from the Q, marked as black and “some” vertex processing procedure is performed (e.g. printing) Step 6: if Q is non-empty go to step 3 Step 7: continue from step 2 until G has no more white vertices Graphs: breadth-first search, sample Graphs: breadth-first search, sample Graphs: breadth-first search, sample Graphs: breadth-first search, sample Graphs: breadth-first search, sample Graphs: breadth-first search, sample Graphs: breadth-first search, sample on usage Graphs: breadth-first search, sample on usage Graphs: breadth-first search, sample on usage Graphs: breadth-first search, sample on usage Graphs: breadth-first search For given graph G and some white vertex s, algorithm builds paths from vertex s to all vertices, attainable from s Paths are shortest Paths built from a given vertex constitute a breadth-first tree Breadth-first trees (constructed while considering distinct white vertices) constitute a breadth-first forest Changing order of considered white vertices may produce another breadthfirst forest Graphs: breadth-first search Graphs: breadth-first search Graphs: depth-first search Depth-first search is one of graph traverse algorithms Algorithm is often used by other algorithms Algorithm is applicable for both directed and undirected graphs Graphs: depth-first search Input: graph G Output: depth-first forest Any vertex of a G may be colored in white, gray or black color White vertices are unvisited, gray – discovered vertices, black – already processed vertices (finished) Initially, G consists of white vertices only; finally, all the vertices of G are black Graphs: depth-first search Step 1: all the vertices are marked as white Step 2: start with any white vertex s Step 3: vertex s is marked as gray Step 4: for each adjacent to s white vertex algorithm performs recursive reentrance (steps 3-5) Step 5: if there are no more adjacent to s white vertices in G, algorithm marks vertex s as black and performs some procedure under the vertex (e.g. prints data) Step 7: continue from step 2 until G has no more white vertices Graphs: depth-first search, sample Graphs: depth-first search, sample Graphs: depth-first search, sample on usage Graphs: depth-first search, sample on usage Graphs: depth-first search, sample on usage Graphs: depth-first search For given graph G and some white vertex s, algorithm builds paths from vertex s to all vertices, attainable from s Paths built from a given vertex constitute a depth-first tree Breadth-first trees (constructed while considering distinct white vertices) constitute a depth-first forest Changing order of considered white vertices may produce another depth-first forest Graphs: depth-first search Graphs: depth-first search Graphs: topological sort Let G=<V,E> is a directed acyclic graph Let’s define comparison operation on V Vertices u and v are comparable and u<v if exists directed edge (u,v) Vertices u and w are comparable and u<w if exist some vertex v and edges (u,v) and (v,w); u<v & v<w => u<w Otherwise two vertices are incomparable Graph may not have cycles as relation u<u is invalid Graphs: topological sort The task of topological sort assumes rearrangement of all graph vertices to a linear sequence Input: set of vertices V={v1,v2,…,vn} Output: sequence of vertices S=(v1,v2,…,vn), where vi<vk while i<k, or vi and vk are incomparable at all Graphs: topological sort Graphs: topological sort Graphs: topological sort Algorithm: Step 1: Allocate storage S for sorted vertices, |S|=|V| Step 2: Perform Depth-First Search under the graph G; each finished (black) vertex is added to the end of the sequence S Step 3: Sequence S is topologically sorted set of vertices Graphs: topological sort, sample Graphs: topological sort, sample Graphs: topological sort, sample Graphs: topological sort, sample Graphs: topological sort, sample Graphs: topological sort, sample Graphs: topological sort, sample on usage Graphs: topological sort, sample on usage Graphs: topological sort, sample on usage Q&A