I am trying to understand/learn DFS
(using TypeScript
) with 2D array
. I can see that it can have multiple output, and I am not sure what should be considered as valid output.
Questions:
Approach:
Example: This code will produce 1st output. If you change left, right, down and up then different output can be produced.
var grid = [
[-1, 2, 3],
[0, 9, 8],
[1, 0, 1]
];
function DFSMatrix(grid){
var h = grid.length;
if (h == 0)
return;
var l = grid[0].length;
var visited = Array.from(Array(h), () => Array(l).fill(false));
var stack = [];
stack.push(0 + "," + 0);
while (stack.length != 0) {
var x = stack.pop();
var row = Number.parseInt(x.split(",")[0])
var col = Number.parseInt(x.split(",")[1]);
if (row < 0 || col < 0 || row >= h || col >= l || visited[row][col])
continue;
visited[row][col] = true;
var element = grid[row][col] + " ";
console.log(element);
// console.log(grid[row][col] + " ");
// result.push(element);
stack.push(row + "," + (col - 1)); //go left
stack.push(row + "," + (col + 1)); //go right
stack.push((row - 1) + "," + col); //go up
stack.push((row + 1) + "," + col); //go down
}
}
Output:
If the purpose of the BFS is to transverse through the entire graph (array)
the output is correct.
A valid answer is one that outlines a shortest path. There are multiple equivalent shortest paths in the example presented so there are multiple valid answers.
In fact, by randomizing the direction of selecting elements (left, right, down, up) you'll get more valid outputs.
On the other hand, if the purpose is to find a shortest path from a source (x1,y,1) to a target (x2,y2) the algorithm need to be changed: Once the target is reached the search stops.
Side note: It would be more efficient to "check if indexes are within the range of given matrix" before adding an element to the stack.