Workspace Number Of Ways To Arrive At Destination
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Number of Ways to Arrive at Destination visualizer

Learn Number of Ways to Arrive at Destination with a step-by-step Graphs visualizer, JavaScript source code and a worked example. Practice topological sorting and shortest paths.

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 / 9Execution step
Reference example · JavaScript
Inspect reference input
{
  "n": 4,
  "edges": [
    [
      0,
      1,
      2
    ],
    [
      0,
      2,
      3
    ],
    [
      1,
      3,
      2
    ],
    [
      2,
      3,
      1
    ]
  ],
  "mode": "ways"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
n,edges,mode
},s)=>{
let a=adj(n,edges,mode!=="ways"),d=Array(n).fill(Infinity),ways=Array(n).fill(0),q=[[0,0]];
d[0]=0;
ways[0]=1;
while(q.length){
q.sort((a,b)=>a[0]-b[0]);
let [du,u]=q.shift();
if(du!==d[u])continue;
for(let [v,w] of a[u]){
let nd=du+w;
if(nd<d[v]){
d[v]=nd;
ways[v]=ways[u];
q.push([nd,v])
}else if(nd===d[v])ways[v]+=ways[u];
s("Relax edge "+u+" → "+v,{
Edges:edges,Distance:d.map(x=>Number.isFinite(x)?x:null),Ways:ways,Frontier:q,Current:[u,v]
})
}
}
return mode==="dist"?d:mode==="max"?Math.max(...d):ways[n-1]
};
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

Relax edge 0 → 1

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

Edges4 rows
23210123Select a node to inspect its connections.
Distance4 items
Ways4 items
Frontier1 rows
Current2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Graphs · Topological sorting and shortest paths

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,
      2
    ],
    [
      0,
      2,
      3
    ],
    [
      1,
      3,
      2
    ],
    [
      2,
      3,
      1
    ]
  ],
  "mode": "ways"
}

Execution steps

  1. Relax edge 0 → 1
  2. Relax edge 0 → 2
  3. Relax edge 1 → 0
  4. Relax edge 1 → 3
  5. Relax edge 2 → 0
  6. Relax edge 2 → 3
  7. Relax edge 3 → 1
  8. Relax edge 3 → 2

Computed output

2
JavaScript reference implementation
const solve = ({
  n,edges,mode
},s)=>{
  let a=adj(n,edges,mode!=="ways"),d=Array(n).fill(Infinity),ways=Array(n).fill(0),q=[[0,0]];
  d[0]=0;
  ways[0]=1;
  while(q.length){
    q.sort((a,b)=>a[0]-b[0]);
    let [du,u]=q.shift();
    if(du!==d[u])continue;
    for(let [v,w] of a[u]){
      let nd=du+w;
      if(nd<d[v]){
        d[v]=nd;
        ways[v]=ways[u];
        q.push([nd,v])
      }else if(nd===d[v])ways[v]+=ways[u];
      s("Relax edge "+u+" → "+v,{
        Edges:edges,Distance:d.map(x=>Number.isFinite(x)?x:null),Ways:ways,Frontier:q,Current:[u,v]
      })
    }
  }
  return mode==="dist"?d:mode==="max"?Math.max(...d):ways[n-1]
};
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.