You are on page 1of 61

CSC-221 DATA STRUCTURES AND ALGORITHMS

Instructor: Maryam Aslam


Lecture : Graphs Searching & Traversal
Graphs
Searching & Traversal
GRAPH TRAVERSAL
 Traversal is the facility to move through a structure visiting each of the
vertices once.
 Two traversal methods for a graph are breadth-first and depth-first.
GRAPH TRAVERSAL/SEARCHING
DFS Traversal
DEPTH-FIRST SEARCH (DFS)

 Start from a given vertex v


 Visit first adjacent vertex w, of v
 Then visit first adjacent vertex of w which has not already been visited
…… continue until
 all nodes of graph have been examined
 If dead-end reached
 Back up to last visited node
 Examine remaining adjacent vertices
6
DEPTH-FIRST SEARCH
 Start from node A
 What is a sequence of nodes which would be visited in DFS?

A,
A,B,
B,E,
E,F,F,H,
H,C,
C,D,
D,GG
7
2
DEPTH-FIRST SEARCH EXAMPLE 3
4
1
6
7
5
0

 If we start at vertex 1 then a valid depth-first order would be:

1, 2, 3, 6, 4, 5, 7, 0
8
DEPTH-FIRST SEARCH
B F
ABFIH
I ABFIHGCD
C G
ABFIHGCDE
A
Starting depth first search at some vertices, we may
D not be able to visit all the vertices.
H

E
A depth-first search, starting at vertex B will visit:
B F I H G C D 9
DEPTH FIRST SEARCH TRAVERSAL

 This method visits all the vertices, beginning with a specified start vertex.
 This strategy proceeds along a path from vertex V as deeply into the graph
as possible.
 This means that after visiting V, the algorithm tries to visit any unvisited
vertex adjacent to V.
 When the traversal reaches a vertex which has no adjacent vertex, it back
tracks and visits an unvisited adjacent vertex.
 Depth-first traversal makes use of a Stack data structure.
DFS Visited
Array
F C A
A B
C
B D D
H
E
G E F
G
H

Task: Conduct a depth-first search of the


graph starting with node D
DFS
Visited Array
F C A
A B
B C
D
H D √
E
G E
F
G
D
H
The order nodes are visited:
D Visit D
DFS
Visited Array
F C A
A B
B C
D
H D √
E
G E
F
G
D
H
The order nodes are visited:
Consider nodes adjacent to D,
D
decide to visit C first.
DFS
Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
C
G
D
H
The order nodes are visited:
Visit C
D, C
DFS
Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
C
G
D
H
The order nodes are visited:
No nodes adjacent to C; cannot continue
D, C  backtrack, i.e., pop stack and restore
previous state
DFS
Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
G
D
H
The order nodes are visited:
Back to D – C has been visited, decide to
D, C visit E next
DFS
Visited Array
F C A
A B
B C √
D
H D √
E √
G E
F
E
G
D
H
The order nodes are visited:
Back to D – C has been visited, decide to
D, C, E visit E next
DFS
Visited Array
F C A
A B
B C √
D
H D √
E √
G E
F
E
G
D
H
The order nodes are visited:
Only G is adjacent to E
D, C, E
DFS
Visited Array
F C A
A B
B C √
D
H D √
E √
G E G
F
E
G √
D
H
The order nodes are visited:
Visit G
D, C, E, G
DFS
Visited Array
F C A
A B
B C √
D
H D √
E √
G E
G
F
E
G √
D
H
The order nodes are visited:
Nodes D and H are adjacent to G. D has
D, C, E, G already been visited. Decide to visit H.
DFS
Visited Array
F C A
A B
B C √
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Visit H
D, C, E, G, H
DFS
Visited Array
F C A
A B
B C √
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Nodes A and B are adjacent to H. Decide
D, C, E, G, H to visit A next.
DFS
Visited Array
F C A √
A B
B C √ A
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Visit A
D, C, E, G, H, A
DFS
Visited Array
F C A √
A B
B C √ A
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Only Node B is adjacent to A. Decide to
D, C, E, G, H, A visit B next.
DFS
Visited Array
F C A √
A B √ B
B C √ A
D
H D √
H
E √
G E G
F
E
G √
D
H √
The order nodes are visited:
Visit B
D, C, E, G, H, A, B
DFS
Visited Array
F C A √
A B √
B C √ A
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to B.
D, C, E, G, H, A, B Backtrack (pop the stack).
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
H
E √
G E G
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to A.
D, C, E, G, H, A, B Backtrack (pop the stack).
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E G
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to H.
D, C, E, G, H, A, B Backtrack (pop the stack).
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to G.
D, C, E, G, H, A, B Backtrack (pop the stack).
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to E.
D, C, E, G, H, A, B Backtrack (pop the stack).
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
G √
D
H √
The order nodes are visited:
F is unvisited and is adjacent to D. Decide
D, C, E, G, H, A, B to visit F next.
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
F
G √
D
H √
The order nodes are visited:
Visit F
D, C, E, G, H, A, B, F
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to F.
D, C, E, G, H, A, B, F Backtrack.
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
H √
The order nodes are visited:
No unvisited nodes adjacent to D.
D, C, E, G, H, A, B, F Backtrack.
DFS
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
H √
The order nodes are visited:
Stack is empty. Depth-first traversal is
D, C, E, G, H, A, B, F done.
ANOTHER DFS EXAMPLE

A B C

D E F

ABCFED
BFS Traversal
BREADTH-FIRST SEARCH
 BFS visits nodes level by level
 Start from a given vertex v
 Visit all adjacent vertices of v
 Visit all adjacent vertices of first adjacent vertex w of v
 Then visit all adjacent vertices of second adjacent vertex x of v … etc.

38
BREADTH-FIRST SEARCH2 EXAMPLE
3
4
1
6
7
5
0

 If we start at vertex 1 then a valid breadth-first order would be:

1,2,3,7,6,0,5,4
39
BREADTH-FIRST SEARCH
B F
A B D E
I A B D E F
C G A B D E F C H
A A B D E F C H G I
D As was true with depth-first search, a breadth-first
H search from some vertices may fail to locate all the
vertices.
E A breadth-first search from B:
B F G I H C D
40
BREADTH-FIRST GRAPH TRAVERSAL

 Breadth-first traversal makes use of a queue data structure.


 The queue holds a list of vertices which have not been visited yet but which
should be visited soon.
 Since a queue is a first-in first-out structure, vertices are visited in the order
in which they are added to the queue.
 Visiting a vertex involves, for example, outputting the data stored in that
vertex, and also adding its neighbors to the queue.
 Neighbors are not added to the queue if they are already in the queue, or
have already been visited.
BFS
Breadth-first search starts with given
F C node
A

B D
H
0
G E

Task: Conduct a breadth-first search of the graph


starting with node D
BFS
Breadth-first search starts with given
F C node
A Then visits nodes adjacent in some
specified order (e.g., alphabetical)
B D Like ripples in a pond
H
0
G E

Nodes visited: D
BFS
Breadth-first search starts with given
F C node
A Then visits nodes adjacent in some
specified order (e.g., alphabetical)
B D Like ripples in a pond
H
0
G E

Nodes visited: D, C
BFS
Breadth-first search starts with given
F C node
A Then visits nodes adjacent in some
specified order (e.g., alphabetical)
B D Like ripples in a pond
H
0
G E

Nodes visited: D, C, E
BFS
Breadth-first search starts with given
F C node
A Then visits nodes adjacent in some
specified order (e.g., alphabetical)
B D Like ripples in a pond
H
0
G E

Nodes visited: D, C, E, F
BFS
When all nodes in ripple are visited,
F C visit nodes in next ripples
A

B D
H
0
G E

2 1

Nodes visited: D, C, E, F, G
BFS
When all nodes in ripple are visited,
F C visit nodes in next ripples
A

B D
H
0
3 G E

2 1

Nodes visited: D, C, E, F, G, H
BFS
4 When all nodes in ripple are visited,
F C visit nodes in next ripples
A

B D
H
0
3
G E

2 1

Nodes visited: D, C, E, F, G, H, A
BFS
4 When all nodes in ripple are visited,
F C visit nodes in next ripples
A

B D
H
0
3
G E

2 1

Nodes visited: D, C, E, F, G, H, A, B
BFS
F C A
A B
Q
C
B D
D
H
E
G E F
G
H

How is this accomplished? Simply replace the stack with a queue!


Rules: (1) Maintain an enqueued array. (2) Visit node when
dequeued.
BFS
F C A
A B
QD
C
B D
D √
H
E
G E F
G
H
Nodes visited:

Enqueue D. Notice, D not yet visited.


BFS
F C A
A B
QCEF
C √
B D
D √
H
E √
G E F √
G
H
Nodes visited: D

Dequeue D. Visit D. Enqueue unenqueued nodes adjacent to D.


BFS
F C A
A B
QEF
C √
B D
D √
H
E √
G E F √
G
H
Nodes visited: D, C

Dequeue C. Visit C. Enqueue unenqueued nodes adjacent to C.


BFS
F C A
A B
QFG
C √
B D
D √
H
E √
G E F √
G
H
Nodes visited: D, C, E

Dequeue E. Visit E. Enqueue unenqueued nodes adjacent to E.


BFS
F C A
A B
QG
C √
B D
D √
H
E √
G E F √
G √
H
Nodes visited: D, C, E, F

Dequeue F. Visit F. Enqueue unenqueued nodes adjacent to F.


BFS
F C A
A B
QH
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G

Dequeue G. Visit G. Enqueue unenqueued nodes adjacent to G.


BFS
F C A √
A B √
Q AB
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H

Dequeue H. Visit H. Enqueue unenqueued nodes adjacent to H.


BFS
F C A √
A B √
QB
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H, A

Dequeue A. Visit A. Enqueue unenqueued nodes adjacent to A.


BFS
F C A √
A B √
Q empty
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H, A, B

Dequeue B. Visit B. Enqueue unenqueued nodes adjacent to B.


BFS
F C A √
A B √
Q empty
C √
B D
D √
H
E √
G E F √
G √
H √
Nodes visited: D, C, E, F, G, H, A, B

Q empty. Algorithm done.

You might also like