Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!