0% found this document useful (0 votes)
310 views

BFS and DFS

Graph search methods are used to solve many graph problems by starting at a given vertex and visiting every reachable vertex. Breadth-first search visits the start vertex and puts it in a queue, then repeatedly removes vertices from the queue and visits their unvisited neighbors, adding them to the queue. This process continues until the queue is empty, ensuring that closer vertices are always explored before farther ones.

Uploaded by

Leela Pallava
Copyright
© Attribution Non-Commercial (BY-NC)
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)
310 views

BFS and DFS

Graph search methods are used to solve many graph problems by starting at a given vertex and visiting every reachable vertex. Breadth-first search visits the start vertex and puts it in a queue, then repeatedly removes vertices from the queue and visits their unvisited neighbors, adding them to the queue. This process continues until the queue is empty, ensuring that closer vertices are always explored before farther ones.

Uploaded by

Leela Pallava
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 9

Graph Search Methods Graph Search Methods

• A vertex u is reachable from vertex v iff there is a • A search method starts at a given vertex v and
path from v to u. visits/labels/marks every vertex that is reachable
from v.
2 2
3 3
8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Graph Search Methods Breadth-First Search


• Many graph problems solved using a search
method. • Visit start vertex and put into a FIFO queue.
ƒ Path from one vertex to another.
• Repeatedly remove a vertex from the queue, visit
ƒ Is the graph connected?
its unvisited adjacent vertices, put newly visited
ƒ Find a spanning tree. vertices into the queue.
ƒ Etc.
• Commonly used search methods:
ƒ Breadth-first search.
ƒ Depth-first search.

Breadth-First Search Example Breadth-First Search Example

2
3
2
3
FIFO Queue
8 8
1 1 1
4 4
5 5
9 9
10 10

6 6
7 11 7 11

Start search at vertex 1. Visit/mark/label start vertex and put in a FIFO queue.
Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8
1 8 2 4
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 1 from Q; visit adjacent unvisited vertices; Remove 1 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.

Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 2 4 8 4 5 3 6
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 2 from Q; visit adjacent unvisited vertices; Remove 2 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.

Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 4 5 3 6 8 5 3 6
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 4 from Q; visit adjacent unvisited vertices; Remove 4 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.
Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 5 3 6 8 3 6 9 7
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 5 from Q; visit adjacent unvisited vertices; Remove 5 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.

Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 3 6 9 7 8 6 9 7
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 3 from Q; visit adjacent unvisited vertices; Remove 3 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.

Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 6 9 7 8 9 7
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 6 from Q; visit adjacent unvisited vertices; Remove 6 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.
Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 9 7 8 7 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 9 from Q; visit adjacent unvisited vertices; Remove 9 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.

Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 7 8 8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 7 from Q; visit adjacent unvisited vertices; Remove 7 from Q; visit adjacent unvisited vertices;
put in Q. put in Q.

Breadth-First Search Example Breadth-First Search Example

2
3
FIFO Queue 2
3
FIFO Queue
8 8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Remove 8 from Q; visit adjacent unvisited vertices; Queue is empty. Search terminates.
put in Q.
Breadth-First Search Property Time Complexity
• Each visited vertex is put on (and so
• All vertices reachable from the start vertex removed from) the queue exactly once.
(including the start vertex) are visited. • When a vertex is removed from the queue,
we examine its adjacent vertices.
ƒ O(n) if adjacency matrix used
ƒ O(vertex degree) if adjacency lists used
• Total time
ƒ O(mn), where m is number of vertices in the
component that is searched (adjacency matrix)

Time Complexity Path From Vertex v To Vertex u

ƒ O(n + sum of component vertex degrees) (adj.


lists) • Start a breadth-first search at vertex v.
= O(n + number of edges in component) • Terminate when vertex u is visited or when
Q becomes empty (whichever occurs first).
• Time
ƒ O(n2) when adjacency matrix used
ƒ O(n+e) when adjacency lists used (e is number
of edges)

Is The Graph Connected? Connected Components


• Start a breadth-first search at any as yet
unvisited vertex of the graph.
• Start a breadth-first search at any vertex of
the graph. • Newly visited vertices (plus edges between
them) define a component.
• Graph is connected iff all n vertices get
visited. • Repeat until all vertices are visited.
• Time
ƒ O(n2) when adjacency matrix used
ƒ O(n+e) when adjacency lists used (e is number
of edges)
Connected Components Time Complexity

2
ƒ O(n2) when adjacency matrix used
ƒ O(n+e) when adjacency lists used (e
3
8
1
is number of edges)
4
5
9
10

6
7 11

Spanning Tree Spanning Tree

2
• Start a breadth-first search at any vertex of
3 the graph.
8
1 • If graph is connected, the n-1 edges used to
get to unvisited vertices define a spanning
4
5
9
tree (breadth-first spanning tree).
• Time
ƒ O(n2) when adjacency matrix used
6
7 ƒ O(n+e) when adjacency lists used (e is number
of edges)
Breadth-first search from vertex 1.
Breadth-first spanning tree.

Depth-First Search Depth-First Search Example


2
3
8
1
depthFirstSearch(v)
{ 4
5
9
Label vertex v as reached. 10

for (each unreached vertex u


6
adjacenct from v) 7 11

depthFirstSearch(u); Start search at vertex 1.


Label vertex 1 and do a depth first search
} from either 2 or 4.
Suppose that vertex 2 is selected.
Depth-First Search Example Depth-First Search Example
2 2
3 3
8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Label vertex 2 and do a depth first search Label vertex 5 and do a depth first search
from either 3, 5, or 6. from either 3, 7, or 9.
Suppose that vertex 5 is selected. Suppose that vertex 9 is selected.

Depth-First Search Example Depth-First Search Example


2 2
3 3
8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Label vertex 9 and do a depth first search


Label vertex 8 and return to vertex 9.
from either 6 or 8.
Suppose that vertex 8 is selected. From vertex 9 do a dfs(6).

Depth-First Search Example Depth-First Search Example


2 2
3 3
8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Label vertex 6 and do a depth first search from


either 4 or 7. Label vertex 4 and return to 6.
Suppose that vertex 4 is selected. From vertex 6 do a dfs(7).
Depth-First Search Example Depth-First Search Example
2 2
3 3
8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Label vertex 7 and return to 6.


Return to 9. Return to 5.

Depth-First Search Example Depth-First Search Example


2 2
3 3
8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Label 3 and return to 5.


Return to 2.
Do a dfs(3).

Depth-First Search Example Depth-First Search Example


2 2
3 3
8 8
1 1

4 4
5 5
9 9
10 10

6 6
7 11 7 11

Return to 1. Return to invoking method.


Depth-First Search Properties
• Same complexity as BFS.
• Same properties with respect to path
finding, connected components, and
spanning trees.
• Edges used to reach unlabeled vertices
define a depth-first spanning tree when the
graph is connected.
• There are problems for which bfs is better
than dfs and vice versa.

You might also like