Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Algorithms and Data Structures Lecture 9

2 Agenda: Graph traverse Breadth-first search algorithm Depth-first search algorithm Topological sort

3 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

4 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

5 Graphs: traverse

6

7

8 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

9 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

10 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

11 Graphs: breadth-first search, sample

12

13

14

15

16

17 Graphs: breadth-first search, sample on usage

18

19

20

21 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

22 Graphs: breadth-first search

23

24 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

25 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

26 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

27 Graphs: depth-first search, sample

28

29 Graphs: depth-first search, sample on usage

30

31

32 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

33 Graphs: depth-first search

34

35 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

36 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

37 Graphs: topological sort

38

39 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

40 Graphs: topological sort, sample

41

42

43

44

45

46 Graphs: topological sort, sample on usage

47

48

49 Q & A


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

Similar presentations


Ads by Google