Graph Part02
Graph Part02
F C
A
B D
H
G E
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0]
A [1] B [1]
B [2] C [2]
D
H [3] D [3]
[4] E [4]
G E
[5] F [5]
[6] G [6]
Visited nodes:
[7] H [7]
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0]
A [1] B [1]
B [2] C [2]
D
H [3] D [3]
[4] E [4]
G E
[5] F [5]
[6] G [6]
Visited nodes:
[7] H [7]
Clear the marks (set to false). Push D onto the stack. Set found to false.
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 0
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes:
[7] H [7] 0 D
Clear the marks (set to false). Push D onto the stack. Set found to false.
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 0
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes:
[7] H [7] 0 D
Pop from stack (D is popped). D is not visited yet (unmarked). So, visit D (set D as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes:
D [7] H [7] 0
Pop from stack (D is popped). D is not visited yet (unmarked). So, visit D (set D as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0
Visited nodes:
D [7] H [7] 0
Push all the vertices that are adjacent to D and unvisited (unmarked) onto the
stack (C, E and F are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0 F
[6] G [6] 0 E
Visited nodes:
D [7] H [7] 0 C
Push all the vertices that are adjacent to D and unvisited (unmarked) onto the
stack (C, E and F are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0 F
[6] G [6] 0 E
Visited nodes:
D [7] H [7] 0 C
Pop from stack (F is popped). F is not visited yet (unmarked). So, visit F (set F as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
[6] G [6] 0 E
Visited nodes:
D F [7] H [7] 0 C
Pop from stack (F is popped). F is not visited yet (unmarked). So, visit F (set F as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
[6] G [6] 0 E
Visited nodes:
D F [7] H [7] 0 C
Push all the vertices that are adjacent to F and unvisited (unmarked) onto the
stack (C is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1 C
[6] G [6] 0 E
Visited nodes:
D F [7] H [7] 0 C
Push all the vertices that are adjacent to F and unvisited (unmarked) onto the
stack (C is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1 C
[6] G [6] 0 E
Visited nodes:
D F [7] H [7] 0 C
Pop from stack (C is popped). C is not visited yet (unmarked). So, visit C (set C as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
[6] G [6] 0 E
Visited nodes:
D F C [7] H [7] 0 C
Pop from stack (C is popped). C is not visited yet (unmarked). So, visit C (set C as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
[6] G [6] 0 E
Visited nodes:
D F C [7] H [7] 0 C
Push all the vertices that are adjacent to C and unvisited (unmarked) onto the
stack (nothing is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 1
[6] G [6] 0 E
Visited nodes:
D F C [7] H [7] 0 C
Pop from stack (E is popped). E is not visited yet (unmarked). So, visit E (set E as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0
Visited nodes:
D F C E [7] H [7] 0 C
Pop from stack (E is popped). E is not visited yet (unmarked). So, visit E (set E as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0
Visited nodes:
D F C E [7] H [7] 0 C
Push all the vertices that are adjacent to E and unvisited (unmarked) onto the
stack (G is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0 G
Visited nodes:
D F C E [7] H [7] 0 C
Push all the vertices that are adjacent to E and unvisited (unmarked) onto the
stack (G is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0 G
Visited nodes:
D F C E [7] H [7] 0 C
Pop from stack (G is popped). G is not visited yet (unmarked). So, visit G (set G as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes:
D F C E G [7] H [7] 0 C
Pop from stack (G is popped). G is not visited yet (unmarked). So, visit G (set G as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes:
D F C E G [7] H [7] 0 C
Push all the vertices that are adjacent to G and unvisited (unmarked) onto the
stack (H is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 H
Visited nodes:
D F C E G [7] H [7] 0 C
Push all the vertices that are adjacent to G and unvisited (unmarked) onto the
stack (H is pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 H
Visited nodes:
D F C E G [7] H [7] 0 C
Pop from stack (H is popped). H is not visited yet (unmarked). So, visit H (set H as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes:
D F C E G H [7] H [7] 1 C
Pop from stack (H is popped). H is not visited yet (unmarked). So, visit H (set H as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes:
D F C E G H [7] H [7] 1 C
Push all the vertices that are adjacent to H and unvisited (unmarked) onto the
stack (A and B are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1 B
[6] G [6] 1 A
Visited nodes:
D F C E G H [7] H [7] 1 C
Push all the vertices that are adjacent to H and unvisited (unmarked) onto the
stack (A and B are pushed).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1 B
[6] G [6] 1 A
Visited nodes:
D F C E G H [7] H [7] 1 C
Pop from stack (B is popped). B is not visited yet (unmarked). So, visit B (set B as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 1
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 A
Visited nodes:
D F C E G H B [7] H [7] 1 C
Pop from stack (B is popped). B is not visited yet (unmarked). So, visit B (set B as
marked).
Depth-First Search
Example: Conduct a depth-first search in the graph and find if
there is a path from D to B
vertices marks stack found
F C [0] A [0] 0 false
A [1] B [1] 1
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 A
Visited nodes:
D F C E G H B [7] H [7] 1 C
F C
A
B D
H
G E
Breadth-First Search
• Breadth-first search is a strategy for exploring a graph and find
path between two vertices
• Explore “level by level” in the graph
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
B D
H
0
G E
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D, C
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D, C, E
Breadth-First Search
F
Breadth-first search starts
C
with given node
A
Then visits nodes adjacent in
B D some specified order (e.g.,
H
0 alphabetical)
G E Like ripples in a pond
1
Nodes visited: D, C, E, F
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
G E
2 1
Nodes visited: D, C, E, F
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
G E
2 1
Nodes visited: D, C, E, F, G
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3 G E
2 1
Nodes visited: D, C, E, F, G
Breadth-First Search
F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3 G E
2 1
Nodes visited: D, C, E, F, G, H
Breadth-First Search
4 F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H
Breadth-First Search
4 F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A
Breadth-First Search
4 F
When all nodes in ripple are
C
visited, visit nodes in next
A
ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A, B
Breadth-First Search
Set found to false
queue.Enqueue(startVertex)
do
queue.Dequeue (vertex)
if vertex = endVertex
Write final vertex
Set found to true
else if vertex is unvisited
Write this vertex
Enqueue all unvisited adjacent vertices onto queue
while !queue.IsEmpty() AND !found
if(!found)
Write "Path does not exist"
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
F C
A
B D
H
G E
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0]
A [1] B [1]
B [2] C [2]
D
H [3] D [3]
[4] E [4]
G E
[5] F [5]
[6] G [6]
Visited nodes:
[7] H [7]
Enqueue all the vertices that are adjacent to D and unvisited (unmarked) (C, E and
F are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0 F
[6] G [6] 0 E
Visited nodes:
D [7] H [7] 0 C
Enqueue all the vertices that are adjacent to D and unvisited (unmarked) (C, E and
F are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 0
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0 F
[6] G [6] 0 E
Visited nodes:
D [7] H [7] 0 C
Enqueue all the vertices that are adjacent to C and unvisited (unmarked) (nothing
is enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 0
G E
[5] F [5] 0
[6] G [6] 0 F
Visited nodes:
D C [7] H [7] 0 E
Enqueue all the vertices that are adjacent to E and unvisited (unmarked) (G is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 0
[6] G [6] 0 G
Visited nodes:
D C E [7] H [7] 0 F
Enqueue all the vertices that are adjacent to E and unvisited (unmarked) (G is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 0
[6] G [6] 0 G
Visited nodes:
D C E [7] H [7] 0 F
Enqueue all the vertices that are adjacent to F and unvisited (unmarked) (nothing
is enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 0
Visited nodes:
D C E F [7] H [7] 0 G
Enqueue all the vertices that are adjacent to G and unvisited (unmarked) (H is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes:
D C E F G [7] H [7] 0 H
Enqueue all the vertices that are adjacent to G and unvisited (unmarked) (H is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1
Visited nodes:
D C E F G [7] H [7] 0 H
Enqueue all the vertices that are adjacent to H and unvisited (unmarked) (A and B
are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 B
Visited nodes:
D C E F G H [7] H [7] 1 A
Enqueue all the vertices that are adjacent to H and unvisited (unmarked) (A and B
are enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 0 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 B
Visited nodes:
D C E F G H [7] H [7] 1 A
Enqueue all the vertices that are adjacent to A and unvisited (unmarked) (B is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 1 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 B
Visited nodes:
D C E F G H A [7] H [7] 1 B
Enqueue all the vertices that are adjacent to A and unvisited (unmarked) (B is
enqueued).
Breadth-First Search
Example: Conduct a breadth-first search in the graph and find if
there is a path from D to B
vertices marks queue found
F C [0] A [0] 1 false
A [1] B [1] 0
B [2] C [2] 1
D
H [3] D [3] 1
[4] E [4] 1
G E
[5] F [5] 1
[6] G [6] 1 B
Visited nodes:
D C E F G H A [7] H [7] 1 B
F C
A
B D
H
G E
Concluding Remarks
❑There can be multiple paths from source vertex to destination
vertex.
❑Both Breadth First Search and Depth First Search ensures that
the path will be found if there is any.
❑Breadth First Search ensures that, if there are multiple paths
from source vertex to destination vertex, the destination
vertex is reached using minimum number of edges, i.e. it takes
the shortest among all the paths, given that,
❑The edges do not have weights, OR
❑All the edges have equal weights
❑Depth First Search does not ensure that the shortest path is
taken.