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:
- Is this a valid approach? If yes, are all these outputs are valid?
Approach:
- Initialize stack.
- Initialize 2d boolean array, the same size as the original array. To avoid traversal, to go in loops.
- Push the first element position (element at (0,0), row=0, column=0) to stack
- Now until the stack is not empty
- pop the position from the stack. Split it by “,” to get the row index and column index. And check if indexes are within the range of given matrix and marked false in the visited[] array, if not then ignore it and pop the next position from the stack. If indexes are valid and not visited, then print the element.
- Mark the element in the visited array.
- Add the element positions from left, right, down and up from the current element into the stack.
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:
- 1st output: -1, 0, 1, 0, 9, 2, 3, 8, 1
- 2nd output: -1, 2, 3, 8, 9, 0, 1, 0, 1
- 3rd output: -1, 0, 1, 0, 1, 8, 9, 2, 3


