Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Connecting Cities With Minimum Cost
Bookmark
Kruskal
Prim
Input
5 Cities
4 Cities
3 Cities
Custom
n
=
5
,
connections
=
[[1,2,3],[1,3,6],[2,3,4],[2,4,2],[3,5,7],[4,5,5],[1,4,8]]
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
(not initialized)
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
(not initialized)
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
2
2-4
3
1-2
4
2-3
5
4-5
6
1-3
7
3-5
8
1-4
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
3
1-2
4
2-3
5
4-5
6
1-3
7
3-5
8
1-4
Current
City 4
Cities Connected: 1/5 | Total Cost: 0
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
3
1-2
4
2-3
5
4-5
6
1-3
7
3-5
8
1-4
Current
City 4
Cities Connected: 2/5 | Total Cost: 2
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
4
2-3
5
4-5
6
1-3
7
3-5
8
1-4
Current
City 2
Cities Connected: 2/5 | Total Cost: 2
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
5
4-5
6
1-3
7
3-5
8
1-4
Current
City 3
Cities Connected: 3/5 | Total Cost: 5
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
5
4-5
6
1-3
7
3-5
8
1-4
Current
City 3
Cities Connected: 4/5 | Total Cost: 9
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
6
1-3
7
3-5
8
1-4
Current
City 5
Cities Connected: 4/5 | Total Cost: 9
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
6
1-3
7
3-5
8
1-4
Current
City 5
Cities Connected: 5/5 | Total Cost: 14
Legend:
Unvisited
Considering
In MST
Skipped
Graph
3
6
4
2
7
5
8
1
2
3
4
5
Sorted Edges
Cost
Edge
6
1-3
7
3-5
8
1-4
Cities Connected: 5/5 | Total Cost: 14
Legend:
Unvisited
Considering
In MST
Skipped
algo
master
.
io
Step:
Given 5 cities and 7 connections. Find the minimum cost to connect all cities using Kruskal's algorithm.
0 / 16
Input
5 Cities
4 Cities
3 Cities
Custom
n
=
5
,
connections
=
[[1,2,3],[1,3,6],[2,3,4],[2,4,2],[3,5,7],[4,5,5],[1,4,8]]
0 / 16
algo
master
.
io
Step:
Given 5 cities and 7 connections. Find the minimum cost to connect all cities using Kruskal's algorithm.