Optimize Joe's transportation network by finding the MST
Help Joe find the Minimum Spanning Tree (MST) of a transportation network to minimize the total cost of connecting all cities using Kruskal's algorithm.
Cost Matrix:
0 10 20
10 0 15
20 15 0
Output:
1 -> 2
2 -> 3
Cost Matrix:
0 10 20 30
10 0 15 25
20 15 0 35
30 25 35 0
Output:
1 -> 2
2 -> 3
2 -> 4
| Edge | Weight | Action |
|---|---|---|
| 1 -> 2 | 10 | Added to MST |
| 2 -> 3 | 15 | Added to MST |
| 1 -> 3 | 20 | Skipped (forms cycle) |
Output: 1 -> 2\n2 -> 3
Sorting edges dominates, where E is number of edges
For Union-Find and edge storage
| Edge | Weight | Action |
|---|
Example 1: N=3, cost=[[0,10,20],[10,0,15],[20,15,0]] → 1 -> 2\n2 -> 3
Example 2: N=4, cost=[[0,10,20,30],[10,0,15,25],[20,15,0,35],[30,25,35,0]] → 1 -> 2\n2 -> 3\n2 -> 4
Sorting edges dominates, where E is number of edges
For Union-Find and edge storage