3
Matrix:
6 9 3
4 8 2
3 5 10

You can start at any integer in the first row, you can only go down the matrix and add the left,right or directly below the previous number. For example, you start at 9 you can either go to 4,8, or 2. I figured out how to get the result matrix which is

Matrix:
6 9 3
10 11 5
13 10 15

The shortest path is clearly 3>2>5 which corresponds to the 3>5>10 on the result matrix. I want to store the coordinates of the cheapest path in an ArrayList.In this case it would be [0,2,1,2,2,1]. This is what I have so far.My question is where do you add the values into the ArrayList?

     private static ArrayList<Integer> minCost(int cost[][])
    {
    ArrayList<Integer> minValues = new ArrayList<>();
    int n = cost.length;
    int m = cost[0].length;
    int i, j;
    int tc[][] = new int[m][n];

    for (i = 0; i <= n - 1; i++) {
        tc[0][i] = cost[0][i];
    }


    for (i = 1;i <= n - 1; i++) {
        for (j = 0; j <= m - 1; j++) {
            if(j ==0){
                tc[i][j] = Math.min(tc[i - 1][j], 
                        tc[i-1][j + 1]) 
                        + cost[i][j];
            }
            else if(j == m-1){
                tc[i][j] = Math.min(tc[i - 1][j - 1], 
                        tc[i - 1][j]) 
                        + cost[i][j];
            }
            else{
                tc[i][j] = min(tc[i - 1][j - 1], 
                        tc[i - 1][j], 
                        tc[i-1][j + 1]) 
                        + cost[i][j];
            }

        }
    }
            return minValues;
    }
Andy9673
  • 47
  • 5

1 Answers1

2

The values should be added in the array list after the entire total cost matrix is generated, as you have done already.

The path will be reconstructed backwards, starting from the position in the bottom row which has minimum total cost. This will be the last pair of coordinates in the resulting array list.

Afterwards, its predecessor should be identified. This can be done by checking which of the adjacent cells in the previous row has a total cost that, when added to the cost of the current cell, generates the desired total cost.

In the provided example, the optimal path must end up in the bottom row at cell (2, 1), because this is the cell with minimum total cost from the bottom row (its total cost is 10). The previous cell needs to be one that has total_cost = 10 - cost(2, 1) = 5. There is a single candidate with this property among the adjacent cells from row 1, that is cell (1, 2).

The process should go on like this, finding cells of the path one by one, in reverse order, until the path is complete (i.e. it reaches the first row).


Some remarks:

  • if, at some point, there are multiple optimal predecessor candidates (both with the same total cost), then any of them can be chosen. Each of them would generate an optimal path, with the same total cost.

  • to create the final path in the desired order, the position found at each step can be added in the front of the array (to avoid needing to perform a reverse at the end).

danbanica
  • 3,018
  • 9
  • 22
  • 1
    When constructing the list you only have to look at the 2-3 adjacent values in the row above, not at all of them, it's the min of those values. – maraca Apr 20 '17 at 20:50
  • Yes, thank you, I added the keyword "adjacent" to make it clear. – danbanica Apr 20 '17 at 20:51