Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Number of Connected Components in an Undirected Graph
Bookmark
DFS
Union Find
Input
2 Components
Path (1 Component)
Fully Connected
Mostly Disconnected
Custom
n
=
5
,
edges
=
[[0,1],[1,2],[3,4]]
Input Graph
0
1
2
3
4
Traversal State
visited
F
0
F
1
F
2
F
3
F
4
call stack
empty
Legend:
Standard
Processing
Root
Connected
Input Graph
0
1
2
3
4
Traversal State
visited
F
0
F
1
F
2
F
3
F
4
call stack
empty
Legend:
Standard
Processing
Root
Connected
Components: 1
Input Graph
0
1
2
3
4
Traversal State
visited
F
0
F
1
F
2
F
3
F
4
call stack
empty
Legend:
Standard
Processing
Root
Connected
Components: 1
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
F
1
F
2
F
3
F
4
call stack
0
Legend:
Standard
Processing
Root
Connected
Components: 1
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
T
1
F
2
F
3
F
4
call stack
0
1
Legend:
Standard
Processing
Root
Connected
Components: 1
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
T
1
T
2
F
3
F
4
call stack
0
1
Legend:
Standard
Processing
Root
Connected
Components: 1
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
T
1
T
2
F
3
F
4
call stack
empty
Legend:
Standard
Processing
Root
Connected
Components: 2
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
T
1
T
2
F
3
F
4
call stack
empty
Legend:
Standard
Processing
Root
Connected
Components: 2
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
T
1
T
2
T
3
T
4
call stack
3
4
Legend:
Standard
Processing
Root
Connected
Components: 2
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
T
1
T
2
T
3
T
4
call stack
3
Legend:
Standard
Processing
Root
Connected
Components: 2
Input Graph
0
1
2
3
4
Traversal State
visited
T
0
T
1
T
2
T
3
T
4
call stack
empty
Legend:
Standard
Processing
Root
Connected
algo
master
.
io
Step:
Given an undirected graph with 5 nodes and 3 edges. Find the number of connected components using DFS.
0 / 24
Input
2 Components
Path (1 Component)
Fully Connected
Mostly Disconnected
Custom
n
=
5
,
edges
=
[[0,1],[1,2],[3,4]]
0 / 24
algo
master
.
io
Step:
Given an undirected graph with 5 nodes and 3 edges. Find the number of connected components using DFS.