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.