# Algorithms and Data Structures Lecture 9. Agenda: Graph traverse Breadth-first search algorithm Depth-first search algorithm Topological sort.

## Presentation on theme: "Algorithms and Data Structures Lecture 9. Agenda: Graph traverse Breadth-first search algorithm Depth-first search algorithm Topological sort."— Presentation transcript:

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: 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 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 breadth- first forest

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 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: topological sort Let G= is a directed acyclic graph Let’s define comparison operation on V Vertices u and v are comparable and u { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4318474/slides/slide_35.jpg", "name": "Graphs: topological sort Let G= is a directed acyclic graph Let’s define comparison operation on V Vertices u and v are comparable and u

Graphs: topological sort The task of topological sort assumes rearrangement of all graph vertices to a linear sequence Input: set of vertices V={v 1,v 2,…,v n } Output: sequence of vertices S=(v 1,v 2,…,v n ), where v i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4318474/slides/slide_36.jpg", "name": "Graphs: topological sort The task of topological sort assumes rearrangement of all graph vertices to a linear sequence Input: set of vertices V={v 1,v 2,…,v n } Output: sequence of vertices S=(v 1,v 2,…,v n ), where v i

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 on usage

Q & A

Similar presentations