0% found this document useful (0 votes)
25 views57 pages

Graph Traversal Depth First Search: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms

Depth first search (DFS) is a graph traversal algorithm that visits all nodes of a graph by exploring as far as possible along each branch before backtracking. It uses a stack to keep track of vertices to visit later. DFS starts at a source node, visits its neighbors, and explores their unexplored neighbors before moving to the next vertex on the stack. This process continues until all vertices are visited or the target is found.

Uploaded by

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

Graph Traversal Depth First Search: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms

Depth first search (DFS) is a graph traversal algorithm that visits all nodes of a graph by exploring as far as possible along each branch before backtracking. It uses a stack to keep track of vertices to visit later. DFS starts at a source node, visits its neighbors, and explores their unexplored neighbors before moving to the next vertex on the stack. This process continues until all vertices are visited or the target is found.

Uploaded by

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

Graph Traversal

Depth First Search

B.Tech (CSE), Semester 5,


Analysis and Design of Algorithms
Objectives
This session will enable students to:

1. Understand the need and basics of graph traversal.

2. Traverse the graph using depth first search technique.

3. Differentiate between connected and strongly connected components.


Outcomes
After completing this session, students will be able to:

1. Apply depth first search technique for various applications areas.

2. Analyze the complexity of depth first search technique.

3. Find the strongly connected components in the graph.


Graph Traversal

• Graph traversal refers to the process of visiting each vertex in a graph.

• Graph Traversal vs Graph Search


 "Traversal": visit every node in the graph.
 "Search": visit nodes until find what we are looking for. E.g.:
 A node labeled "New York".
 A node containing integer 2014

• Using graph traversal, we can:

 Find if there’s a connection from one node to another?


 Determine if we can start from our node and touch every other node?
 Find the shortest path between two nodes?
Finding a Route
• Graph traversal refers to the process of visiting each vertex in a graph.

v
Find a sequence of vertices
0
v0, v1, …, vk such that

• v0 is source

• Each (vi,vi+1) is an edge


v
in E v
1
3
• vk is target

v v
2 4

v
Graph Traversal: Depth First Search
• Start from i, visit a neighbour j

• Suspend the exploration of i and explore j instead

• Continue till you reach a vertex with no unexplored


neighbours

• Backtrack to nearest suspended vertex that still has an


unexplored neighbour

• Suspended vertices are stored in a stack

• Last in, first out: most recently suspended is checked


first
Depth First Search
1
2
1 3
4
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9

1
Depth First Search
1
Start at 4 2
1 3
4
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9

1
Depth First Search
1
Start at 4 2
1 3
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9

1
Depth First Search
1 1
Start at 4 2
1 3
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
4 1
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
4 1 2
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
5 d
2 7
8
4 9
6 7 10

StackofSuspended Vertices
8 9
4 1
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
5 d
2 7
8
4 9
6 7 10

StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
5 d
2 7
8
4
9
6 7 10
StackofSuspended Vertices
8 9

1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6
5 d
2 7
8
4 9
6 7 10

StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7
8
4 9
6 7 10

StackofSuspended Vertices
8 9
4 5
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8
4 9
6 7 10

StackofSuspended Vertices
8 9
4 5 6
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8
4 9
6 7 10

StackofSuspended Vertices
8 9
4 5
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9
6 7 10

StackofSuspended Vertices
8 9
4 5 6
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9 1
6 7 10

StackofSuspended Vertices
8 9
4 5 6 8
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9
4 5 6 8 9
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9
4 5 6 8
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9
4 5 6
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9 1
6 7 10 1
StackofSuspended Vertices
8 9
4 5
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9 1
6 7 10 1
StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9

1
Depth First Search
//Initialization
for j = 1..n {visited[j] = 0; parent [j] =-1}

function DFS(i) // DFS starting from vertex i

//Mark I as visited
visited[i] = 1;

//Explore each neighbor of I recursively


For each (i,j) in E
if visited[j] == 0
parent[j] = I
DFS(j)
Complexity of Depth First Search

• Each vertex marked and explored exactly once


• DFS(j) need to examine all neighbours of j
• In adjacency matrix, scan row j: n entries
• Overall O(n2)
• With adjacency list, scanning takes O(m) time across
all vertices
• Total time is O(m+n), like BFS
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.
Depth First Search: Another Example

Not been there


yet
Been there,
haven’t explored
all the paths out.

Been there, have


start explored all the
paths out.

re d !
explo
Depth First Search
DFS finds all the nodes reachable from the starting point

In an undirected graph, this is called


a connected component.

start

One application: finding


connected components.
Depth First Search
Why is it called depth-first?
• We are implicitly building a tree:

D E A

F
A B E

C
B

Call this the


G “DFS tree” G
C
• And first we go as deep as we can.
F D
Depth First Search: Applications
• DFS has many applications:

• For undirected graphs:


 Connected components
 Connectivity properties

• For directed graphs:


 Finding cycles
 Topological sorting
 Connectivity properties: Strongly connected components
Strong Connectivity
Def. Nodes u and v are mutually reachable if there is both a path from u to v
and also a path from v to u.

Def. A graph is strongly connected if every pair of nodes is mutually reachable.

Lemma. Let s be any node. G is strongly connected iff every node is reachable
from s, and s is reachable from every node.

Pf.  Follows from definition.


Pf.  Path from u to v: concatenate u↝s path with s↝v path.
Path from v to u: concatenate v↝s path with s↝u path.

ok if paths overlap
s u

v
Strong Connectivity
Theorem. Can determine if G is strongly connected in O(m + n) time. Pf.
・ Pick any node s.

reverse orientation of every edge in G


・ Run
Run BFS
BFS from
from ss in
in G.
G reverse.
・ Return true if all nodes reached in both BFS executions.

s tro ng ly c o n n e c t e d n o t st ro ng ly c o n ne c t e d
Strong Components

Def. A strong component is a maximal subset of mutually reachable nodes.


Strongly Connected Components
• A strongly connected component of a directed graph
G=(V,E) is a maximal set of vertices C such that for every
pair of vertices u and v in C, both vertices u and v are
reachable from each other.

Example
Strongly Connected Components

cd
abe
h
fg

The Component Graph results by collapsing each


strong component into a single vertex.

The Component Graph is a DAG (directed acyclic graph)


Connected vs Strongly
Connected Components

v and w are connected if there v and w are strongly connected if there


is a path between v and w is both a directed path from v to w and
a directed path from w to v
Strongly Connected Components:
Applications
Food web graph. Vertex = species; edge = from producer to consumer.

Strong component. Subset of species with common energy flow.


Strongly Connected Components:
Applications
Software module dependency graph.
・ Vertex = software module. ・ Edge: from module to dependency.

Strong component. Subset of mutually interacting modules.


Approach 1. Package strong components together.
Approach 2. Use to improve design!
Summary

• Graph Traversal- Need and Basics


• Depth First Search Algorithm
• Complexity Analysis of Depth First Search Algorithm
• Depth First Search Algorithm: Applications
• Strongly Connected Components

You might also like