Workspace Strongly Connected Components
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Strongly Connected Components visualizer

Learn Strongly Connected Components 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 / 8Execution step
Reference example · JavaScript
Inspect reference input
{
  "n": 5,
  "edges": [
    [
      0,
      1
    ],
    [
      1,
      2
    ],
    [
      2,
      0
    ],
    [
      1,
      3
    ],
    [
      3,
      4
    ],
    [
      4,
      3
    ]
  ]
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
n,edges
},s)=>{
let a=adj(n,edges,true),b=adj(n,edges.map(([u,v])=>[v,u]),true),seen=new Set,order=[],o=[];
function f(u){
seen.add(u);
for(let [v] of a[u])if(!seen.has(v))f(v);
order.push(u);
s("Record DFS finish order",{
Edges:edges,Visited:[...seen],Order:order
})
}
for(let i=0;
i<n;
i++)if(!seen.has(i))f(i);
seen.clear();
function g(u,c){
seen.add(u);
c.push(u);
for(let [v] of b[u])if(!seen.has(v))g(v,c)
}
while(order.length){
let u=order.pop();
if(!seen.has(u)){
let c=[];
g(u,c);
o.push(c.sort((a,b)=>a-b));
s("Traverse reversed edges from next finish vertex",{
Edges:edges,Components:o,Remaining:order
})
}
}
return o.sort((a,b)=>a[0]-b[0])
};
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

Record DFS finish order

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

Edges6 rows
01234Select a node to inspect its connections.
Visited3 items
Order1 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": 5,
  "edges": [
    [
      0,
      1
    ],
    [
      1,
      2
    ],
    [
      2,
      0
    ],
    [
      1,
      3
    ],
    [
      3,
      4
    ],
    [
      4,
      3
    ]
  ]
}

Execution steps

  1. Record DFS finish order
  2. Traverse reversed edges from next finish vertex

Computed output

[
  [
    0,
    1,
    2
  ],
  [
    3,
    4
  ]
]
JavaScript reference implementation
const solve = ({
  n,edges
},s)=>{
  let a=adj(n,edges,true),b=adj(n,edges.map(([u,v])=>[v,u]),true),seen=new Set,order=[],o=[];
  function f(u){
    seen.add(u);
    for(let [v] of a[u])if(!seen.has(v))f(v);
    order.push(u);
    s("Record DFS finish order",{
      Edges:edges,Visited:[...seen],Order:order
    })
  }
  for(let i=0;
  i<n;
  i++)if(!seen.has(i))f(i);
  seen.clear();
  function g(u,c){
    seen.add(u);
    c.push(u);
    for(let [v] of b[u])if(!seen.has(v))g(v,c)
  }
  while(order.length){
    let u=order.pop();
    if(!seen.has(u)){
      let c=[];
      g(u,c);
      o.push(c.sort((a,b)=>a-b));
      s("Traverse reversed edges from next finish vertex",{
        Edges:edges,Components:o,Remaining:order
      })
    }
  }
  return o.sort((a,b)=>a[0]-b[0])
};
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.