1

So I have a gird that can be any given size (i.e. matrix or 2d array). Each element contains a value and simply I need to find the shortest path. However, the problem I am having is trying to represent this grid as a graph or adj matrix or what ever you are meant to do. For example this is my code:

public int shortestPath (int[][] matrix, int sr, int sc, int dr, int dc)
{
    int p = matrix.length * matrix[0].length;
    int[] distances = new int[p];
    boolean[] visited = new boolean[p]; //I think all are set to false by default

    for (int i = 0; i < p; i++)
    {
        distances[i] = Integer.MAX_VALUE;
    }

    PriorityQueue<Node> pq = new Priority<>(); // a Node holds the int row, int col and the distance from source cell. It also orders by distances, so therefore a comparable was created to change natural order.
    pq.add(new Node(sr,sc,0);
    distances[(sr * matrix[0].length) + sc] = 0;
    visited[(sr * matrix[0].length) + sc] = true;

    while(!pq.isEmpty())
    {
        Node n = pq.poll();
        int row = n.getRow();
        int col = n.getColumn();
        int dist = n.getDistance();

     //Now here with a normal graph I would search neighbours via a for loop and find in the adj list where an element = 1 and is not visited. This is where I am stuck
    }
}

So obviously with a grid, I will need to find neighbours of left/right/up/down (not doing diagonals). Thus, I need to take into account boundaries. How can a create an Adj matrix or what is the correct way to start searching neighbours for a grid like this?

I am having bad luck with this today because most examples show in graph form to adj matrix and not from grid form to adj matrix.

Willdomybest18
  • 127
  • 3
  • 12
  • Dijkstra has nothing to do with how the graph is actually represented. It seems that you did not fully understand the algorithm yet, I suggest studying it more. All you have to do is to relax all outgoing edges per step, it is completely up to you how to get hands on those. Take a look at its pseudo-code rather than actual implementations and watch some animations, maybe then you will understand the algorithm better. – Zabuzard May 01 '20 at 11:06
  • @Zabuzard I am new so I will make mistakes. But how do I search for the neighbours? Normally its done one neighbour at a time (BFS) on an Adj matrix. But a grid is hard to perform a loop to find neighborus because I know that i + 1, i -1, j + 1 an j - 1 are possible neighbours. Also I know it compares distance of stored path with the distance of potential new path. – Willdomybest18 May 01 '20 at 11:09
  • https://stackoverflow.com/questions/61444628/does-every-matrix-correspond-to-a-graph-conceptually/61448978#61448978 – Matt Timmermans May 01 '20 at 11:54
  • You know the coordinate of the node. So, as you shared, you also know all the coordinates of all neighbors. Now simply check with `if` whether they are still in-bounds. If so, proceed. If not, skip that non-existing-neighbor. – Zabuzard May 01 '20 at 14:26

1 Answers1

3

There's a trick for grid graphs:

  1. lets say {x,y} denotes difference between 2 neighbour cells
  2. You know neighbours will be in {0,-1}, {0,1}, {1,0} or {-1,0} (assuming no diagonal moves), or those cells will be out of bounds
  3. Save arrays of those differences:
int differenceX[] = {0,0,1,-1};
int differenceY[] = {-1,1,0,0};
  1. Now you can use for loop for neighbours:
    for(int i=0; i<4; i++)
    {
        int neighRow = row + differenceY[i];
        int neighCol = col + differenceX[i];
        if(min(neighRow, neighCol) >= 0 && neighRow < matrix.length && neighCol < matrix[0].length){
            //process node
        }
    }
Photon
  • 2,717
  • 1
  • 18
  • 22
  • 1
    The important key aspect which OP was not aware of is `if(notOutOfBound(...))`. It would help OP to show him exactly that. I.e. a coordinate-bounds-check. – Zabuzard May 01 '20 at 14:27
  • @Photon - Thanks that makes sense now. I would just like to ask, if the gird is not symmetrical i.e. k * k where k is an integer. How do you check boundaries efficiently there? For example a 3 * 4 grid. – Willdomybest18 May 02 '20 at 02:00
  • @Willdomybest18 updated if statement for general matrix case – Photon May 02 '20 at 07:55