Workspace Kruskal Algorithm
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Kruskal Algorithm visualizer

Learn Kruskal Algorithm with a step-by-step Graphs visualizer, JavaScript source code and a worked example. Practice mst, dsu and advanced.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 6Execution step
Reference example · JavaScript
Inspect reference input
{
  "n": 4,
  "edges": [
    [
      0,
      1,
      1
    ],
    [
      0,
      2,
      4
    ],
    [
      1,
      2,
      2
    ],
    [
      1,
      3,
      5
    ],
    [
      2,
      3,
      3
    ]
  ],
  "prim": false
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
n,edges,prim
},s)=>{
let o=[],cost=0;
if(prim){
let a=adj(n,edges),q=[[0,-1,0]],seen=new Set;
while(q.length){
q.sort((a,b)=>a[0]-b[0]);
let [w,p,u]=q.shift();
if(seen.has(u))continue;
seen.add(u);
cost+=w;
if(p>=0)o.push([p,u,w]);
for(let [v,c] of a[u])if(!seen.has(v))q.push([c,u,v]);
s("Add cheapest edge crossing visited boundary",{
Edges:edges,Chosen:o,Frontier:q,Cost:[cost]
})
}
}else{
let d=dsu(n);
for(let [u,v,w] of edges.slice().sort((a,b)=>a[2]-b[2])){
if(d.union(u,v)){
cost+=w;
o.push([u,v,w])
}
s("Accept edge only when roots differ",{
Edges:edges,Chosen:o,Parents:d.p,Cost:[cost]
})
}
}
return cost
};
function adj(n,edges,directed=false){
let a=Array.from({
length:n
},()=>[]);
for(let [u,v,w=1] of edges){
a[u].push([v,w]);
if(!directed)a[v].push([u,w])
}
return a
}
function dsu(n){
let p=Array.from({
length:n
},(_,i)=>i),z=p.map(()=>1);
function find(x){
while(p[x]!==x){
p[x]=p[p[x]];
x=p[x]
}
return x
}
function union(a,b){
a=find(a);
b=find(b);
if(a===b)return false;
if(z[a]<z[b])[a,b]=[b,a];
p[b]=a;
z[a]+=z[b];
return true
}
return {
p,z,find,union
}
}
const dirs=[[1,0],[-1,0],[0,1],[0,-1]];

Execution canvas

Active Changed
STEP 1

Accept edge only when roots differ

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Edges5 rows
142530123Select a node to inspect its connections.
Chosen1 rows
Parents4 items
Cost1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Graphs · MST, DSU and advanced

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "n": 4,
  "edges": [
    [
      0,
      1,
      1
    ],
    [
      0,
      2,
      4
    ],
    [
      1,
      2,
      2
    ],
    [
      1,
      3,
      5
    ],
    [
      2,
      3,
      3
    ]
  ],
  "prim": false
}

Execution steps

  1. Accept edge only when roots differ

Computed output

6
JavaScript reference implementation
const solve = ({
  n,edges,prim
},s)=>{
  let o=[],cost=0;
  if(prim){
    let a=adj(n,edges),q=[[0,-1,0]],seen=new Set;
    while(q.length){
      q.sort((a,b)=>a[0]-b[0]);
      let [w,p,u]=q.shift();
      if(seen.has(u))continue;
      seen.add(u);
      cost+=w;
      if(p>=0)o.push([p,u,w]);
      for(let [v,c] of a[u])if(!seen.has(v))q.push([c,u,v]);
      s("Add cheapest edge crossing visited boundary",{
        Edges:edges,Chosen:o,Frontier:q,Cost:[cost]
      })
    }
  }else{
    let d=dsu(n);
    for(let [u,v,w] of edges.slice().sort((a,b)=>a[2]-b[2])){
      if(d.union(u,v)){
        cost+=w;
        o.push([u,v,w])
      }
      s("Accept edge only when roots differ",{
        Edges:edges,Chosen:o,Parents:d.p,Cost:[cost]
      })
    }
  }
  return cost
};
function adj(n,edges,directed=false){
  let a=Array.from({
    length:n
  },()=>[]);
  for(let [u,v,w=1] of edges){
    a[u].push([v,w]);
    if(!directed)a[v].push([u,w])
  }
  return a
}
function dsu(n){
  let p=Array.from({
    length:n
  },(_,i)=>i),z=p.map(()=>1);
  function find(x){
    while(p[x]!==x){
      p[x]=p[p[x]];
      x=p[x]
    }
    return x
  }
  function union(a,b){
    a=find(a);
    b=find(b);
    if(a===b)return false;
    if(z[a]<z[b])[a,b]=[b,a];
    p[b]=a;
    z[a]+=z[b];
    return true
  }
  return {
    p,z,find,union
  }
}
const dirs=[[1,0],[-1,0],[0,1],[0,-1]];

Continue learning

Practice related DSA questions in the same topic.