Download presentation

Presentation is loading. Please wait.

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
Graphs: traverse

7
Graphs: traverse

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
**Graphs: breadth-first search, sample**

13
**Graphs: breadth-first search, sample**

14
**Graphs: breadth-first search, sample**

15
**Graphs: breadth-first search, sample**

16
**Graphs: breadth-first search, sample**

17
**Graphs: breadth-first search, sample on usage**

18
**Graphs: breadth-first search, sample on usage**

19
**Graphs: breadth-first search, sample on usage**

20
**Graphs: breadth-first search, sample on usage**

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
**Graphs: breadth-first search**

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
**Graphs: depth-first search, sample**

29
**Graphs: depth-first search, sample on usage**

30
**Graphs: depth-first search, sample on usage**

31
**Graphs: depth-first search, sample on usage**

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
**Graphs: depth-first search**

35
**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

36
**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

37
**Graphs: topological sort**

38
**Graphs: topological sort**

39
**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

40
**Graphs: topological sort, sample**

41
**Graphs: topological sort, sample**

42
**Graphs: topological sort, sample**

43
**Graphs: topological sort, sample**

44
**Graphs: topological sort, sample**

45
**Graphs: topological sort, sample**

46
**Graphs: topological sort, sample on usage**

47
**Graphs: topological sort, sample on usage**

48
**Graphs: topological sort, sample on usage**

49
Q & A

Similar presentations

OK

Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.

Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on windows 7 features English 8 unit 13 read ppt on iphone Ppt on mapping space around us Ppt on self development goals Download ppt on phase controlled rectifiers Ppt on social contract theory of john Ppt on order of operations Ppt on 2d and 3d figures Ppt on precautions of tsunami warning Ppt on c language basics pdf