Skip to content

Commit 8a529be

Browse files
committed
Dijkstra min path
1 parent 7111618 commit 8a529be

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

PuzzleCoding/src/MinPathSum.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
4+
5+
/**
6+
* Given a m x n grid filled with non-negative numbers,
7+
* find a path from top left to bottom right which minimizes the sum of all numbers along its path.
8+
* <p/>
9+
* Note: You can only move either down or right at any point in time.
10+
* <p/>
11+
* If we could move to any direction in the path, then we kind of use
12+
* Dijkstra's algorithm to solve this problem. The difference with the
13+
* shotest path problem in graph is that in the graph, different node
14+
* to one node may have path with different weight. But int the grid,
15+
* each neighbor to this cell would sum with the same value. So if we
16+
* would also get minimun sum in fisrt calculate for this cell.
17+
* <p/>
18+
* Since the question ask to move either right or down only, then the
19+
* question merely become Robort move question. With dynamic programming
20+
* we could solve it in O(mn) time complexity.
21+
*/
22+
public class MinPathSum {
23+
public static void main(String[] args) {
24+
int[][] grid = new int[][]{
25+
{1, 3, 1},
26+
{1, 5, 1},
27+
{4, 2, 1}
28+
};
29+
// result 7
30+
DijkstraPathSum(grid);
31+
minPahtSum(grid);
32+
33+
34+
int[][] a2 = new int[][]{{3}};
35+
//3
36+
DijkstraPathSum(a2);
37+
minPahtSum(a2);
38+
39+
40+
int[][] a3 = new int[][]{
41+
{1, 0, 4, 9, 6, 0, 9, 1, 8, 9, 5},
42+
{1, 2, 8, 9, 2, 4, 8, 1, 7, 3, 2},
43+
{5, 0, 7, 9, 3, 5, 1, 3, 8, 2, 3},
44+
{3, 2, 2, 5, 3, 3, 3, 2, 0, 5, 6},
45+
{9, 6, 8, 3, 6, 2, 0, 1, 4, 6, 1},
46+
{1, 7, 4, 8, 8, 9, 7, 1, 3, 2, 5},
47+
{7, 7, 8, 0, 3, 0, 0, 0, 8, 1, 8},
48+
{8, 7, 4, 0, 9, 5, 4, 7, 9, 8, 5},
49+
{5, 6, 3, 5, 5, 6, 0, 7, 1, 7, 7},
50+
{9, 9, 2, 1, 1, 2, 1, 5, 0, 0, 4}
51+
};
52+
//40
53+
DijkstraPathSum(a3);
54+
minPahtSum(a3);
55+
56+
57+
}
58+
59+
// only left or down, Dynamic programming to solve this.
60+
public static void minPahtSum(int[][] grid) {
61+
if (grid.length == 0) {
62+
System.out.println("sum path for zero grid " + 0);
63+
return;
64+
}
65+
66+
int row = grid.length;
67+
int col = grid[0].length;
68+
69+
// calculate left edge
70+
for (int i = row - 1; i > 0; --i)
71+
grid[i - 1][col - 1] += grid[i][col - 1];
72+
73+
// calculate bottom edge
74+
for (int j = col - 1; j > 0; --j)
75+
grid[row - 1][j - 1] += grid[row - 1][j];
76+
77+
for (int i = row - 2; i >= 0; --i) {
78+
for (int j = col - 2; j >= 0; --j) {
79+
//sum with the less one
80+
grid[i][j] +=
81+
(grid[i + 1][j] < grid[i][j + 1]) ?
82+
grid[i + 1][j] : grid[i][j + 1];
83+
}
84+
}
85+
86+
System.out.println(grid[0][0]);
87+
88+
}
89+
90+
// Dijkstra's algorithm
91+
public static void DijkstraPathSum(int[][] grid){
92+
if(grid.length == 0){
93+
System.out.println("sum path for zero grid " + 0);
94+
return;
95+
}
96+
97+
int row = grid.length;
98+
int col = grid[0].length;
99+
boolean[][] visit = new boolean[row][col];
100+
int[][] cost = new int[row][col];
101+
102+
for(int i = 0; i<row; i++){
103+
for(int j = 0; j<col;j++){
104+
cost[i][j] = Integer.MAX_VALUE;
105+
}
106+
}
107+
108+
String[][] path = new String[row][col];
109+
110+
cost[0][0] = grid[0][0];
111+
path[0][0] = "0-0";
112+
visit[0][0] = true;
113+
114+
for(int i = 0; i< row ; i++){
115+
for(int j = 0; j< col && visit[i][j] ;j++){
116+
117+
//left
118+
int r = i+1, c = j;
119+
120+
if( r < row ){
121+
if(cost[r][c] > cost[i][j] + grid[r][c]){
122+
cost[r][c] = cost[i][j] + grid[r][c];
123+
visit[r][c]= true;
124+
path[r][c] = String.valueOf(i)+"-"+String.valueOf(j);
125+
}
126+
127+
}
128+
// down
129+
r = i;
130+
c = j+1;
131+
if(c < col ){
132+
if(cost[r][c] > cost[i][j] + grid[r][c]){
133+
cost[r][c] = cost[i][j] + grid[r][c];
134+
visit[r][c] = true;
135+
path[r][c] = String.valueOf(i)+"-"+String.valueOf(j);
136+
}
137+
138+
}
139+
}
140+
}
141+
142+
System.out.println(cost[row-1][col-1]);
143+
// System.out.println(Arrays.deepToString(cost));
144+
}
145+
}

0 commit comments

Comments
 (0)