Graph

Quick Tips

Warm up

Visited Set - when to insert

Try to insert into visited when also inserting into queue (or something similar) so that you always act on "valid" things.
e.g. - Shortest Path code

const shortestPath = (edges, nodeA, nodeB) => {
  const graph = buildGraph(edges);
  const visited = new Set([ nodeA ]);
  const queue = [[ nodeA, 0 ]];
  
  while (queue.length > 0) {
    const [ node, distance ] = queue.shift();
    
    if (node === nodeB) return distance;
    
    for (let neighbor of graph[node]) {
      if (!visited.has(neighbor)) {
        visited.add(neighbor);
        queue.push([ neighbor, distance + 1 ]);
      }
    }
  }
  
  return -1;
};

const buildGraph = (edges) => {
  const graph = {};
  
  for (let edge of edges) {
    const [a, b] = edge;
    if (!(a in graph)) graph[a] = [];
    if (!(b in graph)) graph[b] = [];
    
    graph[a].push(b);
    graph[b].push(a);
  }
  
  return graph;
};

Grid Graph Probs

Island Count


const islandCount = (grid) => {
  const ROWS = grid.length;
  const COLS = grid[0].length;
  let islands = 0;
  let visited = new Set();

  let getKey = (r, c) => r + "," + c;

  const explore = (r, c) => {
    if(r <0 || r >= ROWS) return false;
    if(c <0 || c >= COLS) return false;
    if(visited.has(getKey(r,c))) return false;
    if(grid[r][c] === 'W') return false;

    visited.add(getKey(r,c));

    explore(r -1, c);
    explore(r+1, c);
    explore(r, c-1);
    explore(r, c+1);
    return true;
  }

  for(let r=0; r < ROWS; r++){
    for(let c=0; c < COLS; c++){
      if(explore(r ,c)) islands++;
    }
  }

  return islands;
};

module.exports = {
  islandCount,
};

Min Island

const minimumIsland = (grid) => {
  let minIsland = Infinity;
  const ROWS = grid.length;
  const COLS = grid[0].length;
  let visited = new Set();

  let getKey = (r, c) => r + "," + c;

  const explore = (r, c) => {
    if(r <0 || r >= ROWS) return 0;
    if(c <0 || c >= COLS) return 0;
    if(visited.has(getKey(r,c))) return 0;
    if(grid[r][c] === 'W') return 0;

    visited.add(getKey(r,c));

    return 1 + explore(r -1, c) + explore(r+1, c) + explore(r, c-1) + explore(r, c+1);
    
  }

  for(let r=0; r < ROWS; r++){
    for(let c=0; c < COLS; c++){
      const thisIsland = explore(r ,c);
      if(thisIsland > 0)
        minIsland = Math.min(thisIsland, minIsland);
    }
  }

  return minIsland;
};

module.exports = {
  minimumIsland,
};

White-grey-black cycle detection algo

(visited vs visiting)
only for DAG
white - unexplored
grey - visiting
black - already visited prev

Others