0% found this document useful (0 votes)
26 views41 pages

FAL (2022-23) CSE3002 ETH AP2022232000601 Reference Material I 10-Sep-2022 DFS BFS

This document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of how BFS and DFS traverse graphs and explains the key differences between the two algorithms. BFS searches level-by-level from the starting node, while DFS explores as far as possible along each branch before backtracking. The runtime of both algorithms on graphs represented by adjacency matrices is O(V^2) and by adjacency lists is O(V+E). BFS and DFS have applications in problems involving connectivity, shortest paths, and cycle detection.

Uploaded by

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

FAL (2022-23) CSE3002 ETH AP2022232000601 Reference Material I 10-Sep-2022 DFS BFS

This document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of how BFS and DFS traverse graphs and explains the key differences between the two algorithms. BFS searches level-by-level from the starting node, while DFS explores as far as possible along each branch before backtracking. The runtime of both algorithms on graphs represented by adjacency matrices is O(V^2) and by adjacency lists is O(V+E). BFS and DFS have applications in problems involving connectivity, shortest paths, and cycle detection.

Uploaded by

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

GRAPH SEARCH

BFS & DFS


By: Parminder Benipal
Usage
2

!  Transportation networks (airline carrier, airports as


node and direct flights as edges (direct edge).
!  Communication networks (a collection of computers as
nodes and the physical link between them as edges).
!  Information networks (World Wide Web can be viewed
as directed graph, the Web pages are nodes and the
hyperlink between the pages are directed edges).
!  Social Network (People are nodes and friendship is an
edge).
Breadth-First Search (BFS)
3

!  BFS begins at a root node and inspects all the


neighboring nodes. Then for each of those neighbor
nodes in turn, it inspects their neighbor nodes which
were unvisited, and so on
Breadth-First Search (BFS)
4

B D

A C E

S F

G H
Breadth-First Search (BFS)
5

B D

A C E

S F

G H
Breadth-First Search (BFS)
6

B D

A C E

S F

G H
Breadth-First Search (BFS)
7

B D

A C E

S F

G H
Breadth-First Search (BFS)
8

B D

A C E

S F

G H
Breadth-First Search (BFS)
9

B D

A C E

S F

G H
Breadth-First Search (BFS)
10

B D

A C E

S F

G H
Breadth-First Search (BFS)
11

B D

A C E

S F

G H
Breadth-First Search (BFS)
12

B D

A C E

S F

G H
Breadth-First Search (BFS)
13

B D

A C E

S F

G H
Breadth-First Search (BFS)
14

B D

A C E

S F

G H
Breadth-First Search (BFS)
15

B D

A C E

S F

G H
Breadth-First Search (BFS)
16

B D

A C E

S F

G H

{A B S C G D E F H}
Tree after BFS run
17

B S

C G

D E F H
BFS Algorithm
18
BFS running time
19

1)  If we represent the graph G by adjacency matrix


2
then the running time of BFS algorithm is O(n ),
where n is the number of nodes.
2)  If we represent the graph G by link lists then the
running time of BFS algorithm is O(m + n), where
m is the number of edges and n is the number of
nodes.
BFS Applications
20

!  Breadth-first search can be used to solve many


problems in graph theory, for example:
¤  Finding all nodes within one connected component
¤  Finding the shortest path between two nodes u and v
¤  Finding the diameter of a graph (seen in assignment).

¤  Testing a graph for bipartiteness (how ?). If there are


two vertices x,y in the same level (layer) L_i that are
adjacent then the graph is not bipartite.
¤  More…
Depth-First Search (DFS)
21

!  We don’t visit the nodes level by level! As long as


there is an unvisited node adjacent to the current
visited node we continue! Once we are stuck, trace
back and go to a different branch!
Depth-First Search (DFS)
22

B D

A C E

S F

G H
Depth-First Search (DFS)
23

B D

A C E

S F

G H
Depth-First Search (DFS)
24

B D

A C E

S F

G H
Depth-First Search (DFS)
25

B D

A C E

S F

G H
Depth-First Search (DFS)
26

B D

A C E

S F

G H
Depth-First Search (DFS)
27

B D

A C E

S F

G H
Depth-First Search (DFS)
28

B D

A C E

S F

G H
Depth-First Search (DFS)
29

B D

A C E

S F

G H
Depth-First Search (DFS)
30

B D

A C E

S F

G H
Depth-First Search (DFS)
31

B D

A C E

S F

G H

{A B S C D E H G F}
Edges of G that are not in DFS
32

B D

A C E

S F

G H
Tree after DFS run and edges in G
33
A

B S

D E

H
G
F
Tree after DFS run
34
A

B S

C
No edge of G
goes from a left subtree
to a right subtree.
D E
The edges of G outside tree
are called back edge.
No cross edge !
H
G
F
Depth-First Algorithm using Stack
35
DFS running time
36

1)  If we represent the graph G by adjacency matrix


2
then the running time of DFS algorithm is O(n ),
where n is the number of nodes.
2)  If we represent the graph G by link lists then the
running time of DFS algorithm is O(m + n), where
m is the number of edges and n is the number of
nodes.
DFS Applications
37

!  We use DFS to find a cycle or shortest cycle. We


also use DFS to find strong components in digraph
(used in Tarjan’s algorithm)
BFS & DFS Example
38

V(G) = {a,b,c,d,e,f,g}
E(G) = {ab,bc,bd,de,ef,fc,fg}
BFS & DFS Example
39

BFS: ab, bd, bc, de, cf, fg DFS: ab, bd, de, ef, fg, fc
BFS & DFS Example
40

!  Previous example shows that if there is a cycle in


graph G then the BFS tree and DFS tree are
different.
41

You might also like