Workspace Path With Minimum Effort
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Path With Minimum Effort visualizer

Learn Path With Minimum Effort 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
{
  "a": [
    [
      1,
      2,
      2
    ],
    [
      3,
      8,
      2
    ],
    [
      5,
      3,
      5
    ]
  ],
  "mode": "effort"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,mode
},s)=>{
let d=a.map(r=>r.map(()=>Infinity)),q=[[mode==="water"?a[0][0]:0,0,0]];
d[0][0]=q[0][0];
while(q.length){
q.sort((a,b)=>a[0]-b[0]);
let [v,i,j]=q.shift();
if(v!==d[i][j])continue;
s("Settle lowest-cost frontier cell",{
Grid:a,Cost:d.map(r=>r.map(x=>Number.isFinite(x)?x:null)),Cell:[i,j]
});
if(i===a.length-1&&j===a[i].length-1)return v;
for(let [x,y] of dirs){
let r=i+x,c=j+y;
if(a[r]?.[c]===undefined)continue;
let k=Math.max(v,mode==="water"?a[r][c]:Math.abs(a[r][c]-a[i][j]));
if(k<d[r][c]){
d[r][c]=k;
q.push([k,r,c])
}
}
}
return -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

Settle lowest-cost frontier cell

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

Grid3 rows
Cost3 rows
Cell2 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

{
  "a": [
    [
      1,
      2,
      2
    ],
    [
      3,
      8,
      2
    ],
    [
      5,
      3,
      5
    ]
  ],
  "mode": "effort"
}

Execution steps

  1. Settle lowest-cost frontier cell

Computed output

2
JavaScript reference implementation
const solve = ({
  a,mode
},s)=>{
  let d=a.map(r=>r.map(()=>Infinity)),q=[[mode==="water"?a[0][0]:0,0,0]];
  d[0][0]=q[0][0];
  while(q.length){
    q.sort((a,b)=>a[0]-b[0]);
    let [v,i,j]=q.shift();
    if(v!==d[i][j])continue;
    s("Settle lowest-cost frontier cell",{
      Grid:a,Cost:d.map(r=>r.map(x=>Number.isFinite(x)?x:null)),Cell:[i,j]
    });
    if(i===a.length-1&&j===a[i].length-1)return v;
    for(let [x,y] of dirs){
      let r=i+x,c=j+y;
      if(a[r]?.[c]===undefined)continue;
      let k=Math.max(v,mode==="water"?a[r][c]:Math.abs(a[r][c]-a[i][j]));
      if(k<d[r][c]){
        d[r][c]=k;
        q.push([k,r,c])
      }
    }
  }
  return -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.