📖 Scenario: You are working as a network engineer. You want to connect several computers with cables. Each cable has a cost. You want to connect all computers with the least total cost without any loops.
🎯 Goal: Build a program that finds the minimum total cost to connect all computers using Kruskal's algorithm.
📋 What You'll Learn
Create an array of edges with exact weights and nodes
Create a parent array for union-find structure
Implement the union-find functions: find and union
Sort edges by weight
Use Kruskal's algorithm to select edges without cycles
Print the total cost of the minimum spanning tree
💡 Why This Matters
🌍 Real World
Network design, connecting computers or cities with minimum cable or road cost.
💼 Career
Useful for software engineers working on network optimization, infrastructure planning, and graph algorithms.
Progress0 / 4 steps
1
Create the graph edges
Create an array called edges with these exact entries: {from: 0, to: 1, weight: 4}, {from: 0, to: 2, weight: 3}, {from: 1, to: 2, weight: 1}, {from: 1, to: 3, weight: 2}, {from: 2, to: 3, weight: 4}, {from: 3, to: 4, weight: 2}, {from: 4, to: 5, weight: 6}
DSA Typescript
Hint
Use an array of objects with keys from, to, and weight.
2
Create the parent array for union-find
Create an array called parent with length 6 where each element is its own index (0 to 5).
DSA Typescript
Hint
Each node is its own parent initially.
3
Implement union-find and Kruskal's algorithm
Write the find function that returns the root parent of a node using path compression. Write the union function that connects two nodes if they have different parents. Sort the edges array by weight. Use a for loop to iterate over edges and add the edge's weight to totalCost if union returns true.
DSA Typescript
Hint
Use path compression in find. In union, connect roots if different. Sort edges by weight ascending. Add edge weight to totalCost only if union succeeds.
4
Print the total cost of the minimum spanning tree
Write console.log(totalCost) to display the total cost of the minimum spanning tree.
DSA Typescript
Hint
The minimum total cost to connect all computers is 14.
Practice
(1/5)
1. What is the main goal of Kruskal's Algorithm in graph theory?
easy
A. To find the minimum spanning tree of a connected weighted graph
B. To find the shortest path between two nodes
C. To detect cycles in a graph
D. To sort the vertices of a graph
Solution
Step 1: Understand the purpose of Kruskal's Algorithm
Kruskal's Algorithm is designed to find a subset of edges that connects all vertices with the minimum total edge weight and no cycles.
Step 2: Compare with other graph algorithms
Unlike shortest path or cycle detection algorithms, Kruskal's focuses on building a minimum spanning tree.
Final Answer:
To find the minimum spanning tree of a connected weighted graph -> Option A
Quick Check:
Minimum Spanning Tree = Find minimum edges connecting all nodes [OK]
Hint: Kruskal connects all nodes with least total edge weight [OK]
Common Mistakes:
Confusing MST with shortest path
Thinking it detects cycles only
Assuming it sorts vertices
2. Which of the following TypeScript code snippets correctly initializes the edges array for Kruskal's Algorithm?
Step 1: Check the correct TypeScript tuple array syntax
Edges should be an array of tuples, each tuple having three numbers: [node1, node2, weight]. const edges: [number, number, number][] = [[0, 1, 10], [1, 2, 5], [0, 2, 15]]; uses this syntax correctly.
Step 2: Identify incorrect syntax in other options
const edges = {0:1:10, 1:2:5, 0:2:15}; uses invalid object syntax. const edges: number[][] = [0, 1, 10, 1, 2, 5, 0, 2, 15]; is a flat array, not an array of tuples. const edges: Array<number> = [[0, 1, 10], [1, 2, 5], [0, 2, 15]]; uses wrong type annotation (Array<number> instead of tuple array).
Step 2: Pick edges without cycles until all vertices connected
Pick [2,3,4], [0,3,5], [0,2,6] connect nodes 0,2,3. Then pick [0,1,10] to connect node 1. Total weight = 4+5+6+10 = 25. But this forms a cycle because 0,2,3 are connected. Instead, pick [2,3,4], [0,3,5], [0,1,10]. Then pick [0,2,6] creates cycle. So skip [0,2,6]. Next pick [1,3,15] is heavier and unnecessary. So MST edges: [2,3,4], [0,3,5], [0,1,10]. Total weight = 4+5+10=19.
Final Answer:
19 -> Option A
Quick Check:
Sum of MST edges weights = 19 [OK]
Hint: Sort edges, pick smallest without cycles, sum weights [OK]
Common Mistakes:
Including edges that form cycles
Adding all edges without checking
Misordering edges by weight
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.