A Dynamic Programing Based Solution to the Two-Dimensional Jump-It Problem Jamil Saquer and Razib Iqbal Computer Science Department Missouri State University Springfield, MO
Introducing the problem 2D board with a cost to visit each cell A player starts at the top-left cell and wants to reach the bottom-right (exit) cell with the lowest cost Allowed moves: one cell to the right one cell below jump over one cell to the right Jump over one cell below 5 7 2 4 8 1 6 3 9
Possible Paths 5 7 2 4 8 1 6 3 9 5 7 2 4 8 1 6 3 9 cost = 5 + 1 + 4 + 7 + 3 = 20 cost = 5 + 7 + 1 + 5 + 8 + 3 = 29 5 7 2 4 8 1 6 3 9 cost = 5 + 2 + 4 + 1 + 3 + 3 = 18
Goal Find the cheapest cost of playing the game Find path that leads to playing the game with the cheapest cost
Review – 1D Jump-It 5 15 75 7 43 11
Review – 1D Jump-It solution: if board length == 1, visit that cell 5 start solution: if board length == 1, visit that cell 5 15 75 7 43 11
Review – 1D Jump-It solution: if board length == 1, visit that cell start solution: if board length == 1, visit that cell if board length == 2, visit both cells 5 15 75 7 43 11
Review – 1D Jump-It solution: if board length == 1, visit that cell start solution: if board length == 1, visit that cell if board length == 2, visit both cells if board length == 3, cheaper to jump over 5 15 75 7 43 11
Review – 1D Jump-It solution: if board length == 1, visit that cell start solution: if board length == 1, visit that cell if board length == 2, visit both cells if board length == 3, cheaper to jump over else min_cost = board[i] + min{jumpIt(i+1), jumpIt(i+2)} 5 15 75 7 43 11
2D Jump-It Finding the Cheapest Cost Let jumpIt(r, c) be the cheapest cost of playing the game starting at any cell (r, c) Many cases 5 7 2 4 8 1 6 3 9
Finding the Cheapest Cost 5 7 2 4 8 1 6 3 9
Finding the Cheapest Cost 5 7 2 4 8 1 6 3 9
Dynamic Programming Solution 2D jump-It is a good candidate for DP Overlapping sub-problems Optimal sub-structure
Top-Down DP Solution board[i][j] contains cost of visiting cell (i, j) costs – a cache table for storing solutions costs[i][j] minimum cost starting game at cell (i, j) fill cache table starting with basic cases 5 7 2 4 8 1 6 3 9 costs board
Top-Down DP Solution costs board fill last row starting fill in last column fill the row before last fill the column before last fill rest of cache table 18 18 13 11 14 5 7 2 4 8 1 6 3 9 14 13 14 7 6 21 12 16 16 11 14 11 10 12 3 costs board
Finding Optimal path board costs path Use another cache table, path, to remember moves path[i][j] - coordinates of cell visited after cell (i ,j) 1 2 3 4 5 7 2 4 8 1 6 3 9 18 18 13 11 14 14 13 14 7 6 1 21 12 16 16 11 2 14 11 10 12 3 3 board costs (0, 2) (0, 3) (0, 3) (1, 3) (1, 4) 1 (1,1) (1, 3) (1, 4) (1, 4) (3, 4) 2 (2, 1) (3, 1) (3, 2) (2, 4) (3, 4) 3 (3, 2) (3, 2) (3,4) (3, 4) (-1,-1) 1 2 3 4 path
2D Jump-It in The CS Curriculum Good problem to use when teaching DP
Questions