Lecture 12.2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Graph Traversing and Searching

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecture No: 12.2 Week No: 12 Semester: Fall 2020-2021


Lecturer: MAHFUJUR RAHMAN, mahfuj@aiub.edu
Lecture Outline

1. Graph Search Methods


2. Depth-First-Search (DFS)
3. DFS Example
Graph Search Methods

Given: a graph G = (V, E), directed or undirected

Goal: methodically explore every vertex and edge

Ultimately: build a tree on the graph


 Pick a vertex as the root
 Choose certain edges to produce a tree
 Note: might also build a forest if graph is not connected
Graph Search Methods
 Many graph problems solved using a search method.
 Path from one vertex to another.
 Is the graph connected?
 Find a spanning tree.
 Etc.
 Commonly used search methods:
 Depth-first search.
 Breadth-first search.

 Other variants: best-first, iterated deepening search, etc.


Depth-First Search

depthFirstSearch(v)
{
Label vertex v as reached.
for (each unreached vertex u
adjacenct from v)
depthFirstSearch(u);
}
Depth-First Search Example

Start search at vertex 1. 2


3
8
1

4
5
9

1
stack
Label vertex 1 and do a depth first search
from either 2 or 4. 6
7
Suppose that vertex 2 is selected.
Depth-First Search Example
2
3
8
1

4
5
9
stack

6
7

Label vertex 2 and do a depth first search


from either 3, 5, or 6.
Suppose that vertex 5 is selectd.
Depth-First Search Example
2
3
8
1

4
5

5
12
9
stack

6
7

Label vertex 5 and do a depth first search


from either 3, 7, or 9.
Suppose that vertex 9 is selected.
Depth-First Search Example
2
3
8
1

4
5

59
12
9
stack

6
7

Label vertex 9 and do a depth first search


from either 6 or 8.
Suppose that vertex 8 is selected.
Depth-First Search Example
2
3
8
1

4
5
9

8
9
5
2
1
stack

6
7

Label vertex 8 and return to vertex 9.


Depth-First Search Example
2
3
8
1

4
5
9

9
5
2
1
stack

6
7

Label vertex 8 and return to vertex 9.


From vertex 9 do a dfs(6).
Depth-First Search Example
2
3
8
1

4
5
9

6
9
5
2
1
stack

6
7

Label vertex 6 and do a depth first search from


either 4 or 7.
Suppose that vertex 4 is selected.
Depth-First Search Example
2
3
8
1

4
5
9

4
6
9
5
2
1
stack

6
77

Label vertex 4 and return to 6.


From vertex 6 do a dfs(7).
Depth-First Search Example
2
3
8
1

4
5

967
125
9
stack

6
7

Label vertex 7 and return to 6.


Depth-First Search Example
2
3
8
1

6
9
5
2
1
4
5 stack
9

6
7

Label vertex 7 and return to 6.


Return to 9.
Depth-First Search Example
2
3
8
1

59
12
4
5 stack
9

6
7

Label vertex 7 and return to 6.


Return to 9.
Depth-First Search Example
2
3
8
1

4
5
9

5
2
1
stack

6
7

Return to 5.
Depth-First Search Example
2
3
8
1

4
5
9

5
2
1
stack

6
7

Do a dfs(3).
Depth-First Search Example
2
3
8
1

4
5

5
12
9
stack

6
7

Label 3 and return to 5.


Depth-First Search Example

2
3
8
1

4
5
9

2
1
stack

6
7

Return to 2.
Depth-First Search Example
2
3
8
1

4
5
9

1
stack

6
7

Return to 1
Depth-First Search Example
2
3
8
1

4
5
9
stack

6
7

Return to invoking method.


Depth-First Search Example

OUTPUT:
2
3
8
1

4
5
9

6
7
Depth-First Search
Explore “deeper” in the graph whenever possible - (Follows LIFO mechanism)
 Edges are explored/visited out of the most recently discovered vertex v that still has
unexplored/unvisited edges.
 When all of v’s edges have been explored, backtrack to the vertex from which v was
discovered.
 computes 2 timestamps: (discovered) and (finished)
 builds one or more depth-first tree(s) (depth-first forest)

 Algorithm colors each vertex


 WHITE: undiscovered
 GRAY: discovered, in process
 BLACK: finished, all adjacent vertices have been discovered
DFS: Classification of Edges

 DFS can be used to classify edges of G:


 Tree edges: Edges in the depth-first forest.
 Back edges: Edges (u, v) connecting a vertex u to an ancestor v in a
depth-first tree (where v is not the parent of u). It also applies for
self loops.
 Forward edges: Non-tree edges (u, v) connecting a vertex u to a
descendant v in a depth-first tree.
 Cross edges: All other edges.
 DFS yields valuable information about the structure of a graph.
 In DFS of an undirected graph we get only tree and back edges; no
forward or back-edges.
DFS Example: Classification of Edges

u v w
Undiscovered

Discovered,
On Process

Finished, all
adjacent vertices x y z
have been
discovered
starting
Discover time/ Finish time
the
traversal
with
node u
DFS Example: Classification of Edges

u v w
Undiscovered 1/

Discovered,
On Process

Finished, all
adjacent vertices x y z
have been
discovered

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/ 2/

Discovered,
On Process

Finished, all
adjacent vertices x y z
have been
Tree edge
discovered

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/ 2/

Discovered,
On Process
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/
4/5 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/5
4/ 3/
3/6
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/ 2/7
2/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
DFS Example: Classification of Edges

u v w
Undiscovered 1/8
1/ 2/7
2/ 9/12
9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
Applications of Depth First Search

 For a weighted graph, DFS traversal of the graph produces the


minimum spanning tree and all pair shortest path tree
 Detecting cycle in a graph
 Path Finding
 Topological Sorting
 To test if a graph is bipartite
 Finding Strongly Connected Components of a graph
 Solving puzzles with only one solution, such as mazes.
Books

 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard (Can be


found in university Library)
 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,
References

1. https://en.wikipedia.org/wiki/Data_structure
2. https://visualgo.net/en/dfsbfs?slide=1

You might also like