0% found this document useful (0 votes)
10 views32 pages

DFS and BFS

The document provides an overview of graph theory, defining graphs as structures consisting of vertices and edges, and distinguishing between directed and undirected graphs. It discusses various types of graphs, such as trees and weighted graphs, and explains different implementations like adjacency matrices and lists. Additionally, it covers search algorithms like Depth-First Search (DFS) and Breadth-First Search (BFS), detailing their operations and time complexities.

Uploaded by

tachbirdewan.bd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

DFS and BFS

The document provides an overview of graph theory, defining graphs as structures consisting of vertices and edges, and distinguishing between directed and undirected graphs. It discusses various types of graphs, such as trees and weighted graphs, and explains different implementations like adjacency matrices and lists. Additionally, it covers search algorithms like Depth-First Search (DFS) and Breadth-First Search (BFS), detailing their operations and time complexities.

Uploaded by

tachbirdewan.bd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

 A data structure that consists of a set of

nodes (vertices) and a set of edges that


relate the nodes to each other
 The set of edges describes relationships
among the vertices
 A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of
vertices
E(G): a set of edges (pairs of
vertices)
 When the edges in a graph have no
direction, the graph is called undirected
 When the edges in a graph have a
direction, the graph is called directed
(or digraph)

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !!

E(Graph2) = {(1,3) (3,1) (5,9) (9,11)


(5,7)
 Trees are special cases of graphs!!
 Adjacent nodes: two nodes are adjacent if
they are connected by an edge

5 is adjacent to 7
7 is adjacent from 5

 Path: a sequence of vertices that connect


two nodes in a graph
 Complete graph: a graph in which every
vertex is directly connected to every other
vertex
 What is the number of edges in a
complete directed graph with N
vertices?
N * (N-1)

2
O( N )
 What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2

2
O( N )
 Weighted graph: a graph in which each
edge carries a value
 Array-based implementation
› A 2D array (adjacency matrix) is used to
represent the edges
 Linked-list implementation
› A 1D array is used to represent the
vertices
› A list is used for each vertex v which
contains the vertices which are adjacent
from v (adjacency list)
 Adjacency matrix
› Good for dense graphs --|E|~O(|V|2)
› Memory requirements: O(|V| + |E| ) = O(|
V|2 )
› Connectivity between two vertices can be
tested quickly

 Adjacency list
› Good for sparse graphs -- |E|~O(|V|)
› Memory requirements: O(|V| + |E|)=O(|V|)
› Vertices adjacent to another vertex can be
found quickly
 Problem: find a path between two
nodes of the graph (e.g., Austin and
Washington)
 Methods: Depth-First-Search (DFS) or
Breadth-First-Search (BFS)
 What is the idea behind DFS?
› Travel as far as you can down a path
› Back up as little as possible when you reach a
"dead end" (i.e., next vertex has been
"marked" or there is no next vertex)
 DFS can be implemented efficiently using
a stack
 Each vertex is visited and adjacency list of
each vertex is also scanned.
 So total time is O(V + E)
DFS(G) DFS-VISIT(G, u)
1 for each vertex u ∈ 1 u.color ← GRAY
V[G] 2 time ← time+1
2 u.color ← WHITE 3 u.d ← time
3 u.π← NIL 4 for each v ∈ Adj[u]
4 time ← 0 5 if v.color == WHITE
5 for each vertex u ∈ 6 v.π ← u
V[G] 7 DFS-VISIT(G, v)
6 if u.color == WHITE 8 u.color← BLACK
7 DFS-VISIT(G, u) 9 time = time +1
10 u.f = time
Adjacency List
A->B,E,F
B->A,C,F
A B C D C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E F G H G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
A->B,E,F
B->A,C,F
A B C D
C->B,D,G
D->C,G,H
E->A,F,I
E F G H F->A,B,E,I
Dead G->C,D,J,K,L
H->D,L
end I->E,F,J,M,N
Backtrac J->G,I,K
I J
kK L
K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
A->B,E,F
B->A,C,F
A B C D
C->B,D,G
D->C,G,H
E->A,F,I
E F G H F->A,B,E,I
G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L
K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
A->B,E,F
B->A,C,F
A B C D
C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E FEnd of G H
G->C,D,J,K,L
Traversa H->D,L
l I->E,F,J,M,N
J->G,I,K
I J K L
K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
 What is the idea behind BFS?
› Look at all possible paths at the same depth
before you go at a deeper level
› Back up as far as possible when you reach a
"dead end" (i.e., next vertex has been
"marked" or there is no next vertex)
BFS(G, s)
1 for each vertex u ∈ V[G] 10 while Q != ∅
− {s} 11 u ← DEQUEUE(Q)
2 u.color ← WHITE 12 for each v ∈
3 u.d←∞ Adj[u]
4 u.π← NIL 13 if v.color=
5 s.color ← GRAY WHITE
6 s.d ← 0 14 v.color ←
7 s.π ← NIL GRAY
8Q←∅ 15 v.d← u.d + 1
9 ENQUEUE(Q, s) 16 v.π← u
17 ENQUEUE(Q,
v)
18 u.color← BLACK
Adjacency List
Level 1 A->B,E,F
B->A,C,F
A B C D C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E F G H G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
Level 2 A->B,E,F
B->A,C,F
A B C D C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E F G H G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
Level 3 A->B,E,F
B->A,C,F
A B C D C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E F G H G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
Level 4 A->B,E,F
B->A,C,F
A B C D C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E F G H G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
Level 5 A->B,E,F
B->A,C,F
A B C D C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E F G H G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
Adjacency List
Finally A->B,E,F
B->A,C,F
A B C D C->B,D,G
D->C,G,H
E->A,F,I
F->A,B,E,I
E F G H G->C,D,J,K,L
H->D,L
I->E,F,J,M,N
J->G,I,K
I J K L K->G,J,N,O
L->G,H,P
M->I,N
N->I,M,K
M N O P O->K,P
P->L,O
 The operations of enqueuing and
dequeuing takes O(1).
 So total queue operation is O(V)
 For each vertex adjacency list is scanned,
so total time spent in scanning adjacencies
is O(E)
 Total time is O(V + E)

You might also like