You are on page 1of 42

Analysis, Design of Algorithms

CS3610

Dr. Islam Hegazy


Associate Professor

F2023 Analysis, Design of Algorithms 1


Objectives

By the end of this lecture, you will:

Differentiate between the graph traversal techniques DFS and BFS

Know the complexity of each technique

F2023 Analysis, Design of Algorithms 2


Agenda

01 Depth-First Search 02 Breadth-First Search

F2023 Analysis, Design of Algorithms 3


Depth-First Search

Search as deep as possible first.

Choose a vertex v (source, usually root of tree)

Explore edges out of v


1. Depth-First Search

When all edges of v have been explored, backtrack to predecessor of v and


explore all edges from predecessor
Continue until all vertices reachable from the original source are discovered

F2023 Analysis, Design of Algorithms 4


Depth-First Search

Many problems in computer science can be solved using DFS

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

Traveling Salesman problem


F2023 Analysis, Design of Algorithms 5
Depth-First Search

It is convenient to use a stack to trace the operation of depth-first search.

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).

F2023 Analysis, Design of Algorithms 6


Depth-First Search

0 3
Visited
1. Depth-First Search

2
Stack
1 4

F2023 Analysis, Design of Algorithms 7


Depth-First Search

0 3
0 Visited
1. Depth-First Search

2
1 2 3 Stack
1 4

F2023 Analysis, Design of Algorithms 8


Depth-First Search

0 3
0 1 Visited
1. Depth-First Search

2
2 3 Stack
1 4

F2023 Analysis, Design of Algorithms 9


Depth-First Search

0 3
0 1 2 Visited
1. Depth-First Search

2
4 3 Stack
1 4

F2023 Analysis, Design of Algorithms 10


Depth-First Search

0 3
0 1 2 4 Visited
1. Depth-First Search

2
3 Stack
1 4

F2023 Analysis, Design of Algorithms 11


Depth-First Search

0 3
0 1 2 4 3 Visited
1. Depth-First Search

2
Stack
1 4

F2023 Analysis, Design of Algorithms 12


Depth-First Search

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.

F2023 Analysis, Design of Algorithms 13


Depth-First Search

The DFS algorithm works as follows:

Start by putting any one of the graph's vertices on top of a stack.

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.

F2023 Analysis, Design of Algorithms 14


Depth-First Search

Implements a depth-first search traversal of a given graph

Input: Graph G = (V, E)

Output: Graph G with its vertices marked with consecutive integers in the order
1. Depth-First Search

they are first encountered by the DFS traversal

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)

F2023 Analysis, Design of Algorithms 15


Depth-First Search

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)

F2023 Analysis, Design of Algorithms 16


Depth-First Search

Time complexity of DFS

If represented in adjacency matrix = O(V2)

If represented in adjacency list = O(V + E)


1. Depth-First Search

F2023 Analysis, Design of Algorithms 17


Depth-First Search

Applications of DFS

Cycle Detection

Maze Solver
1. Depth-First Search

Resolve Dependencies (Topological Sort)


Job Scheduling
Courses Ordering
Find Strongly Connected Components (SCC) in Directed G

F2023 Analysis, Design of Algorithms 18


Agenda

01 Depth-First Search 02 Breadth-First Search

F2023 Analysis, Design of Algorithms 19


Breadth-First Search

Explore a graph, turning it into a tree


One vertex at a time
Expand frontier of explored vertices across its breadth
2. BreadthFirst Search

Discovers all vertices at distance k from s before those at distance k+1


A level-by-level order traversal
Builds a tree over the graph
Pick a source vertex to be the root
Find (“discover”) all of its children, then all of their children, etc.

F2023 Analysis, Design of Algorithms 20


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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

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

vertices are visited.


0

1 2 3

4 5 6 7

8
F2023 Analysis, Design of Algorithms 30
Breadth-First Search

It is convenient to use a queue to trace the operation of 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

Add neighbours of the front vertex to the queue


Remove front vertex and mark it as visited

F2023 Analysis, Design of Algorithms 31


Breadth-First Search

0 3
Visited
2. BreadthFirst Search

2
Queue
1 4

F2023 Analysis, Design of Algorithms 32


Breadth-First Search

0 3
0 Visited
2. BreadthFirst Search

2
1 2 3 Queue
1 4

F2023 Analysis, Design of Algorithms 33


Breadth-First Search

0 3
0 1 Visited
2. BreadthFirst Search

2
2 3 Queue
1 4

F2023 Analysis, Design of Algorithms 34


Breadth-First Search

0 3
0 1 2 Visited
2. BreadthFirst Search

2
3 4 Queue
1 4

F2023 Analysis, Design of Algorithms 35


Breadth-First Search

0 3
0 1 2 3 Visited
2. BreadthFirst Search

2
4 Queue
1 4

F2023 Analysis, Design of Algorithms 36


Breadth-First Search

0 3
0 1 2 3 4 Visited
2. BreadthFirst Search

2
Queue
1 4

F2023 Analysis, Design of Algorithms 37


Breadth-First Search

Implements a breadth-first search traversal of a given graph

Input: Graph G = V, E

Output: Graph G with its vertices marked with consecutive integers in the order
2. BreadthFirst Search

they are visited by the BFS traversal

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)

F2023 Analysis, Design of Algorithms 38


Breadth-First Search

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

Shortest path (un-weighted G)

Find Connected Components in Undirected G


2. BreadthFirst Search

Web Crawling
Social N/W: Friend Finder
Pocket Cube (2x2x2 Rubic Cube)
Garbage Collector

F2023 Analysis, Design of Algorithms 40


Breadth-First Search

DFS BFS
2. BreadthFirst Search

Traversal order Depth Level


Data structure Stack Queue
Time complexity O(V+E) || O(V2) O(V+E) || O(V2)
Space complexity O(V) O(V)

F2023 Analysis, Design of Algorithms 41


F2023 Analysis, Design of Algorithms 42

You might also like