840 likes | 1.06k Views
CS1020. Week 12: 10 th April 2014. Contents. Part 1: Discussion on Sit-in Lab #4 Part 2: Discussion on Practice Exercises #41 – 43 . Part 1. Discussion on Sit-in Lab #4 Set A: Robot Navigation . Set A. Set A: Robot Navigation. Robot Navigation System
E N D
CS1020 Week 12: 10th April 2014
Contents • Part 1: Discussion on Sit-in Lab #4 • Part 2: Discussion on Practice Exercises #41 – 43 Week 12
Part 1 Discussion on Sit-in Lab #4 Set A: Robot Navigation Week 12
Set A Set A: Robot Navigation • Robot Navigation System • Navigate a robot from a starting point to an ending point in an indoor terrain represented as a 1D array or NxN matrix • Value in each cell represent the cost incurred if the robot occupies it. • 3 parts • Robot moves on a 1D array. • Robot moves on an NxN matrix, according to some navigation strategy • Robot moves on an NxN matrix and must find the value of the minimum cost path to move Week 12
Requirements Set A: Robot Navigation • Ability to use recursion to solve simple problems (parts 1 & 2) • Ability to use recursion to perform exhaustive search (part 3) Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation • Robot can only move right in a straight line from the starting cell to the destination cell. Move right 9 3 2 7 5 1 Destination cell, index 5 Starting cell, index 2 Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation • Index increases by 1each time robot moves right one cell towards the destination Move right 3 2 7 5 9 1 Destination cell, index 5 next cell, index 3 Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation • Recursive nature of 1D robot navigation • At the start, problem consist of entire sub-array from starting index to last index • As robot moves right by 1 cell, problem becomes smaller by 1 cell (recursive step) • When robot is at last index, this is the base case. 3 3 3 3 2 2 2 2 7 7 7 7 5 5 5 5 9 9 9 9 1 1 1 1 Problem gets simpler and smaller as move towards last index Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation • Recursive solution – • Start calling recursive function from starting index • Increment index by 1 for every recursive call. • Add value in current cell to value returned by recursive call. • Base case: index is the last index of the array (array size - 1), simply return value in cell • Input to Recursive function • 1D Array of integers • Size of the array • Index of current cell robot is in (value between 0 and array size – 1) Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } Start function call navArray(6,2,array) 3 2 7 5 9 1 Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } navArray(6,2,array) 3 3 2 2 7 7 5 5 9 9 1 1 7+navArray(6,3,array) recursive call Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } navArray(6,2,array) 3 3 3 2 2 2 7 7 7 5 5 5 9 9 9 1 1 1 7+navArray(6,3,array) 5+navArray(6,4,array) Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } navArray(6,2,array) 3 3 3 3 2 2 2 2 7 7 7 7 5 5 5 5 9 9 9 9 1 1 1 1 7+navArray(6,3,array) 5+navArray(6,4,array) 9+navArray(6,5,array) Base case Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } navArray(6,2,array) 3 3 3 3 2 2 2 2 7 7 7 7 5 5 5 5 9 9 9 9 1 1 1 1 7+navArray(6,3,array) 5+navArray(6,4,array) 9+1 Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } navArray(6,2,array) 3 3 3 3 2 2 2 2 7 7 7 7 5 5 5 5 9 9 9 9 1 1 1 1 7+navArray(6,3,array) 5+10 9+1 Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } navArray(6,2,array) 3 3 3 3 2 2 2 2 7 7 7 7 5 5 5 5 9 9 9 9 1 1 1 1 7+15 5+10 9+1 Week 12
Part 1 – Robot navigation on 1D array Set A: Robot Navigation public int navArray(int dim, int index, int[] array) { if (index == dim-1) // at destination return array[index]; else // cost at index + total cost to move from index+1 to last index return array[index]+navArray(dim,index+1,array); } navArray(6,2,array) Returns 22 3 3 3 3 2 2 2 2 7 7 7 7 5 5 5 5 9 9 9 9 1 1 1 1 7+15 5+10 9+1 Week 12
Part 2 – Robot Navigation in 2D Set A: Robot Navigation • This is a simple extension of the 1D case • Here the robot can move in 3 possible directions • South • East • South-East • Navigation strategy is to choose cell with lowest cost to move to, or if one of the possible cell is the destination simply move to it Week 12
Part 2 – Robot Navigation in 2D Set A: Robot Navigation • Recursive nature of 2D robot navigation • At the start, problem consist of entire sub-matrix from starting coordinate (as top left corner) to bottom right coordinate • As robot moves to the next cell (according to the navigation strategy), problem becomes smaller (recursive case) by • 1 row and column (choose south-east cell) • 1 row (choose south cell) • 1 column (choose east cell) • When robot is at bottom right coordinate, this is the base case. Week 12
Part 2 – Robot Navigation in 2D Set A: Robot Navigation • E.g. start at (1,1) move to (3,3). 3 3 2 2 Robot moves to (2,2) 7 7 1 1 Be careful that if one of the possible cells is the destination cell, then regardless of the value of that cell, robot will move there!! …. Week 12
Part 2 – Robot Navigation in 2D Set A: Robot Navigation • Recursive solution – similar to 1D case except • Out of possible cells to move to choose least cost cell or simply destination cell if it is one of the possible cells. • Input to Recursive function • 2D square matrix of integers • dimension of the matrix • row and column of current cell robot is in (values between 0 and matrix dimension -1) Week 12
Part 2 – Robot navigation on 2D array Set A: Robot Navigation public int navMatrix(int dim, int row, int col, int[][] matrix) { if (row == dim-1 && col == dim-1) // base case: at destination return matrix[row][col]; else { int verVal = matrixVal(dim,row+1,col,matrix); int horVal = matrixVal(dim,row,col+1,matrix); int diaVal = matrixVal(dim,row+1,col+1,matrix); if (row+1 == dim-1 && col == dim-1) return matrix[row][col]+navMatrix(dim,row+1,col,matrix); … else { if (verVal < horVal && verVal < diaVal) return matrix[row][col]+navMatrix(dim,row+1,col,matrix); else if (horVal < verVal && horVal < diaVal) return matrix[row][col]+navMatrix(dim,row,col+1,matrix); else return matrix[row][col]+navMatrix(dim,row+1,col+1,matrix); } } } Cost of possible cells to move to If destination is one of the cells, move to it If not, choose least cost cell to move to Week 12
Part 3 – Optimal Robot Navigation in 2D Set A: Robot Navigation • Similar to part 2 except the robot will take an optimal path from starting coordinate to destination coordinate • An example of the difference between an optimal (minimum cost) path and the greedy navigation strategy of part 2 is given in the question paper. Path from (0,0) to (3,3) using greedy strategy in part 2 ~ total cost = 57 Minimum cost path from (0,0) to (3,3) ~ total cost = 39 Week 12
Part 3 – Optimal Robot Navigation in 2D Set A: Robot Navigation • How to find the cost of the minimum cost path? • Idea – Exhaustively try all paths from starting cell to destination cell! • How to recursively generate all paths from starting cell to destination cell ? • Similar to solution for part 2, except we do not choose 1 cell to move to BUT make a recursive call for each possible cell to move to. Week 12
Part 3 – Optimal Robot Navigation in 2D Set A: Robot Navigation Branch refers to recursive calls S – South E – East SE – South East (0,0) starting cell S E SE (1,1) (1,0) (0,1) S E SE S E S E SE SE (2,0) (1,1) (2,1) (1,1) (0,2) (1,2) (2,1) (1,2) (2,2) … S E SE … … (3,2) (2,3) (3,3) Computation diagram of recursive calls which shows all possible paths from start to destination and each path from (0,0) to a (3,3) is a possible path the robot can take. S E SE E (3,3) (3,3) Invalid movements Week 12
Part 3 – Optimal Robot Navigation in 2D Set A: Robot Navigation • How to know which path to choose ? • Out of the three recursive calls made (south, east, south-east), choose the one which returns the smallest value. • This ensures that at the end the cost returned is the minimum cost (starting from (3,3) back to (0,0), only the minimum is chosen, thus at (0,0) we have the minimum cost). Week 12
Part 3 – Optimal Robot Navigation in 2D Set A: Robot Navigation (0,0) starting cell Min{S,E,SE} Recursive calls return S E SE (1,1) (1,0) (0,1) Min{S,E,SE} S E SE S E S E SE SE (2,0) (1,1) (2,1) (1,1) (0,2) (1,2) (2,1) (1,2) (2,2) Min{S,E,SE} … S E SE … … (3,2) (2,3) (3,3) Min{S} Min{E} S E (3,3) (3,3) Week 12
Part 3 – Optimal Robot Navigation in 2D Set A: Robot Navigation public int minNavMatrix(int dim, int row, int col, int[][] matrix) { if (row == dim-1 && col == dim-1) // base case: at destination return matrix[row][col]; else if (row < 0 || row >= dim || col < 0 || col >= dim) return MAX_VALUE; else { int optVerVal = minNavMatrix(dim,row+1,col,matrix); int optHorVal = minNavMatrix(dim,row,col+1,matrix); int optDiaVal = minNavMatrix(dim,row+1,col+1,matrix); if (optVerVal < optHorVal && optVerVal < optDiaVal) return matrix[row][col]+optVerVal; else if (optHorVal < optVerVal && optHorVal < optDiaVal) return matrix[row][col]+optHorVal; else return matrix[row][col]+optDiaVal; } } Invalid coordinate (outside of boundary) so return MAX_VALUE to ensure this path is not chosen Recursive call to all three directions Choose the minimum among the 3 possible directions taken Week 12
Part 1 Discussion on Sit-in Lab #4 Set B: String Replacement Week 12
Set B: String Replacement Set B: String Replacement • Perform string replacement • Replace matching character • Given a string A and character C, replace each occurrence of C in A by a white space. • Replace matching substring • Given a string A and string B, replace each occurrence of B in A by a white space. • Replace matching LCS – • Given a string A and string B, find the longest common substring D between A and B, and replace each occurrence of D in A by a white space. Week 12
Requirements Set B: String Replacement • Ability to use recursion to solve simple problems (parts 1 & 2) • Ability to use recursion to perform exhaustive search (part 3) Week 12
Part 1 – Replace matching character Set B: String Replacement • Idea – Go through string A and replace each character matching C. • Can do this from front to back or back to front. (Will show front to back version) • An Iterative solution is simple, how to make it recursive? Week 12
Part 1 – Replace matching character Set B: String Replacement • Iterative solution • String A = abcbe • Character C = b a a a a a b b b b c c c c c b b b b e e e e e move from index 0 to index 4 checking and replacing characters Week 12
Part 1 – Replace matching character Set B: String Replacement • This can be converted into a recursive solution • Recursive function input • String A • Character C • Recursive function output • String A with all occurrences of C replaced by “ ” public String replaceChar(String A, char C) Week 12
Part 1 – Replace matching character Set B: String Replacement • Recurrence Append 1st character or white space to string returned by replaceChar, depending on whether 1st character matches C Recursive step: replaceChar called on smaller subproblem which is substring of A without 1st character (similar to moving left to right for iterative solution) Base case: A is empty string Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b + replaceChar( , ) c b e Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b + replaceChar( , ) b c + replaceChar( , ) c b e b e Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b + replaceChar( , ) b c + replaceChar( , ) c b e e b + replaceChar( , ) b e Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b + replaceChar( , ) b c + replaceChar( , ) c b e e b + replaceChar( , ) b e b e + replaceChar(“”, ) Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b b c b e + replaceChar( , ) b c + replaceChar( , ) c b e e b + replaceChar( , ) b e b e + replaceChar(“”, ) “” Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b + replaceChar( , ) b c + replaceChar( , ) c b e e b + replaceChar( , ) b “e” e + replaceChar(“”, ) b e “” Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b + replaceChar( , ) b c + replaceChar( , ) c b e e b “ e” + replaceChar( , ) b “e” e + replaceChar(“”, ) b e “” Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b + replaceChar( , ) b “c e” c + replaceChar( , ) c b e e b “ e” + replaceChar( , ) b “e” e + replaceChar(“”, ) b e “” Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e a b + replaceChar( , ) b c b e b “ c e” + replaceChar( , ) b “c e” c + replaceChar( , ) c b e e b “ e” + replaceChar( , ) b “e” e b e + replaceChar(“”, ) “” Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e “a c e” a b + replaceChar( , ) b c b e b “ c e” + replaceChar( , ) b “c e” c + replaceChar( , ) c b e e b “ e” + replaceChar( , ) b “e” e b e + replaceChar(“”, ) “” Week 12
Part 1 – Replace matching character Set B: String Replacement b replaceChar( , ) a b c b e “a c e” a b + replaceChar( , ) b c b e “ c e” b + replaceChar( , ) “c e” c b e b c + replaceChar( , ) “ e” e b + replaceChar( , ) “e” b e b e + replaceChar(“”, ) Week 12 “”
Part 1 – Replace matching character Set B: String Replacement public String replaceChar(String str, char c) { if (str.length() == 0) return ""; else { if (str.charAt(0) == c) return " "+replaceChar(str.substring(1,str.length()),c); else return str.charAt(0)+ replaceChar(str.substring(1,str.length()), c); } } Week 12
Part 2 – Replace matching substring Set B: String Replacement • The idea is almost exactly the same as part 1! Week 12