Search code examples
typescriptdepth-first-search

What should be consider as a correct DFS (Depth First Search) algorithm output for 2D or 3D array?


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

enter image description here

  • 2nd output: -1, 2, 3, 8, 9, 0, 1, 0, 1

enter image description here

  • 3rd output: -1, 0, 1, 0, 1, 8, 9, 2, 3

enter image description here


Solution

  • 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.