Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
Finding neighbouring nodes on a graph for the BFS algorithm

I'm trying to make an adjacency list for the breath first search algorithm and want find neighbouring nodes. I'm currently trying to look at each node on all rows and then either + 1 or - 1 for left and right neighbouring nodes.

const getNeighbours = (row, col) => {
  let neighbours = [];
  let left;
  let right;
  if(row > 0 ){
    neighbours[left] = [row - 1];
  }
  else if(row === 0){
    neighbours[right] = [row + 1]; 
  }
  console.log(neighbours[left])
} 

The graph is generated in a 2D array with rows and columns.

const createGrid = () => {
  let neighbours = [];
  let grid = [];
  for (let row = 1; row < 20; row++) {
    grid[row]= [];
    
    for (let col = 1; col < 47; col++) {
      grid = createNode(grid, row, col);
    }
    grid.push(grid[row]);
  }
  return grid;
};

The values I get for the getNeighbour is just 20 arrays with values of NaN. The graph has no edge cost and is undirected.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda