0
0
DSA Typescriptprogramming~10 mins

Minimum Spanning Tree Kruskal's Algorithm in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Minimum Spanning Tree Kruskal's Algorithm
Start with all edges sorted by weight
Pick smallest edge not yet chosen
Check if adding edge forms a cycle?
YesDiscard edge
No
Add edge to MST
Repeat until MST has (V-1) edges
Done
Kruskal's algorithm sorts edges by weight and adds them one by one to the MST if they don't form a cycle, until MST spans all vertices.
Execution Sample
DSA Typescript
edges = [(A,B,1), (B,C,3), (A,C,2)]
sort edges by weight
for edge in edges:
  if no cycle with edge:
    add edge to MST
This code sorts edges by weight and adds them to MST if they don't create a cycle.
Execution Table
StepOperationEdge ConsideredCycle Check ResultMST EdgesVisual State
1Sort edges by weight--[]Edges sorted: (A,B,1), (A,C,2), (B,C,3)
2Consider edge(A,B,1)No cycle[(A,B,1)]MST: A--1--B
3Consider edge(A,C,2)No cycle[(A,B,1), (A,C,2)]MST: A--1--B and A--2--C
4Consider edge(B,C,3)Cycle detected[(A,B,1), (A,C,2)]MST unchanged: A--1--B and A--2--C
5Check MST sizeEdges in MST = 2MST complete[(A,B,1), (A,C,2)]Final MST: A--1--B and A--2--C
💡 MST has (V-1)=2 edges for 3 vertices, so algorithm stops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
edges[(A,B,1), (B,C,3), (A,C,2)][(A,B,1), (A,C,2), (B,C,3)][(A,B,1), (A,C,2), (B,C,3)][(A,B,1), (A,C,2), (B,C,3)][(A,B,1), (A,C,2), (B,C,3)][(A,B,1), (A,C,2), (B,C,3)]
MST[][][(A,B,1)][(A,B,1), (A,C,2)][(A,B,1), (A,C,2)][(A,B,1), (A,C,2)]
Union-Find sets[A],[B],[C][A],[B],[C][A,B],[C][A,B,C][A,B,C][A,B,C]
Key Moments - 3 Insights
Why do we check for cycles before adding an edge?
Because adding an edge that forms a cycle breaks the tree property. See step 4 in execution_table where edge (B,C,3) is discarded due to cycle.
How do we know when to stop adding edges?
We stop when MST has (V-1) edges, where V is number of vertices. Step 5 shows MST has 2 edges for 3 vertices, so algorithm stops.
What data structure helps detect cycles efficiently?
Union-Find (Disjoint Set) tracks connected components. If two vertices are already connected, adding edge creates cycle. See variable_tracker for Union-Find sets.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what edges are in the MST?
A[(A,B,1), (B,C,3)]
B[(A,B,1)]
C[(A,B,1), (A,C,2)]
D[]
💡 Hint
Check the MST Edges column at step 3 in execution_table.
At which step does the algorithm detect a cycle?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the Cycle Check Result column in execution_table.
If the edge (B,C,3) had weight 0.5, how would the MST change?
AEdge (A,B,1) would be discarded
BEdge (B,C,3) would be added first
CMST would have edges (B,C,3) and (A,C,2)
DNo change in MST
💡 Hint
Edges are sorted by weight; check variable_tracker edges after Step 1.
Concept Snapshot
Kruskal's Algorithm for MST:
- Sort all edges by weight ascending
- Use Union-Find to detect cycles
- Add edges if no cycle formed
- Stop when MST has (V-1) edges
- Result is minimum spanning tree connecting all vertices
Full Transcript
Kruskal's algorithm builds a minimum spanning tree by sorting edges from smallest to largest weight. It picks edges one by one, adding them if they don't create a cycle. Cycle detection uses a Union-Find data structure to track connected components. The process stops when the MST contains one less edge than the number of vertices. This ensures the MST connects all vertices with minimum total edge weight.