Lecture 5
Lecture 5
CS3610
Analyzing networks
Mapping routes
1. Depth-First Search
Scheduling
Finding spanning trees
DFS can also be used as a subroutine to solve complex problems
Matching algorithm
Hopcroft–Karp
Push a vertex onto the stack when the vertex is reached for the first time (i.e
the visit of the vertex starts)
1. Depth-First Search
Pop a vertex off the stack when it becomes a dead end (i.e., the visit of the
vertex ends).
0 3
Visited
1. Depth-First Search
2
Stack
1 4
0 3
0 Visited
1. Depth-First Search
2
1 2 3 Stack
1 4
0 3
0 1 Visited
1. Depth-First Search
2
2 3 Stack
1 4
0 3
0 1 2 Visited
1. Depth-First Search
2
4 3 Stack
1 4
0 3
0 1 2 4 Visited
1. Depth-First Search
2
3 Stack
1 4
0 3
0 1 2 4 3 Visited
1. Depth-First Search
2
Stack
1 4
A standard DFS implementation puts each vertex of the graph into one of two
categories:
Visited
1. Depth-First Search
Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding
cycles.
Take the top item of the stack and add it to the visited list.
1. Depth-First Search
Create a list of that vertex's adjacent nodes. Add the ones which aren't in the
visited list to the top of the stack.
Keep repeating steps 2 and 3 until the stack is empty.
Output: Graph G with its vertices marked with consecutive integers in the order
1. Depth-First Search
DFS(G)
mark each vertex in V with 0 as a mark of being “unvisited”
count ←0
for each vertex v in V do
if v is marked with 0
dfs(v)
Visit recursively all the unvisited vertices connected to vertex v by a path and
numbers them in the order they are encountered via global variable count
dfs(v)
1. Depth-First Search
count ←count + 1;
mark v with count
for each vertex w in V adjacent to v do
if w is marked with 0
dfs(w)
Applications of DFS
Cycle Detection
Maze Solver
1. Depth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 21
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 22
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 23
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 24
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 25
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 26
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 27
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 28
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 29
Breadth-First Search
Proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex,
Then visit unvisited vertices two edges apart from it, and so on, until all the
2. BreadthFirst Search
1 2 3
4 5 6 7
8
F2023 Analysis, Design of Algorithms 30
Breadth-First Search
Add a vertex onto the queue when the vertex is reached for the first time (i.e
the visit of the vertex starts)
2. BreadthFirst Search
0 3
Visited
2. BreadthFirst Search
2
Queue
1 4
0 3
0 Visited
2. BreadthFirst Search
2
1 2 3 Queue
1 4
0 3
0 1 Visited
2. BreadthFirst Search
2
2 3 Queue
1 4
0 3
0 1 2 Visited
2. BreadthFirst Search
2
3 4 Queue
1 4
0 3
0 1 2 3 Visited
2. BreadthFirst Search
2
4 Queue
1 4
0 3
0 1 2 3 4 Visited
2. BreadthFirst Search
2
Queue
1 4
Input: Graph G = V, E
Output: Graph G with its vertices marked with consecutive integers in the order
2. BreadthFirst Search
BFS(G)
mark each vertex in V with 0 as a mark of being “unvisited”
count ←0
for each vertex v in V do
if v is marked with 0
bfs(v)
Visits all the unvisited vertices connected to vertex v by a path and numbers
them in the order they are visited via global variable count
2. BreadthFirst Search
bfs(v)
count ←count + 1;
mark v with count and initialize a queue with v
while the queue is not empty do
for each vertex w in V adjacent to the front vertex do
if w is marked with 0
count ←count + 1;
mark w with count
add w to the queue
remove the front vertex from the queue
F2023 Analysis, Design of Algorithms 39
Breadth-First Search
Applications of BFS
Web Crawling
Social N/W: Friend Finder
Pocket Cube (2x2x2 Rubic Cube)
Garbage Collector
…
DFS BFS
2. BreadthFirst Search