Clue Paths
Memory Alias Review Remember that it’s possible for two unique variables to point to the same memory location myList MyClass mc 0x100 someFn (in another class) { localVar = mc.getMyList(); } localVar 0x100 If I modify localVar, it also changes myList!
Memory Alias Continued In C++, what was the purpose of the copy constructor? In Java, we also use copy constructor, to avoid shallow copy. myList MyClass mc 0x100 myList MyClass mc3 (created using copy, e.g., mc3 = new LinkedList<>(mc); ) 0x500 myList MyClass mc2 (created by assignment, e.g., mc2 =mc; ) 0x100 0x2000x3000x0 0x200 0x3000x100 0x6000x7000x0 0x600 0x7000x
Recursion Review* Base case – causes recursion to end Recursive call – to same function Progress toward base case * the algorithm I describe uses recursion. Recursion is not absolutely required, but this is a good opportunity for review.
Simple Example public static int factorial(int n) { if(n==1) return 1; return factorial(n-1) * n; } public static void main(String[] args) { System.out.println(factorial(3)); } Very Quick Ex: How would factorial(3) be calculated? (trace the code)
Sometimes need “setup” first Example: drawing a fractal – drawFractal: setup center x, center y, x0 and y0 Call drawFractalLine with length, theta, order – drawFractalLine – draws a line, does a recursive call with order-1 What’s the point? – User calls drawFractal – recursive function is drawFractalLine. Our algorithm has similar structure from: Thinking Recursively in Java, Eric Roberts
The Challenge Room Walk Room DoorWalk Room Walk Room WalkPlayerWalk Room Walk Room DoorWalk Room Walk Room WalkPlayerWalk Roll a 1 Highlight all the squares that can be reached in # of steps shown on one die Move only up/down/left/right (no diagonal) Do not touch any location more than one time Highlight only the final squares, not the paths
The Challenge Room Walk Room DoorWalk Room Walk Room WalkPlayerWalk Room Walk Room DoorWalk Room Walk Room WalkPlayerWalk Roll a 2
A clarification Room Walk Room DoorWalk Room Walk Room WalkPlayerWalk Room Walk Room DoorWalk Room Walk Room WalkPlayerWalk Roll a 4. (this is just one target) Note that the person CANNOT backtrack… that is, visit the same cell twice. But the person is NOT required to go in a straight line. So you CANNOT just include all squares that are n away from the initial point.
Simplify for now Forget about doors and rooms. Let’s do a simple 4x4 grid Number the rows and columns. Assume a 2D grid, each containing a board cell. Create an adjacency list. NOTE: adjacency lists will be even more handy in the clue layout, because a square is not necessarily attached to each of its neighbors. By creating the adjacency list when the board is loaded, we can simplify the code for the path-finding algorithm. With your partner: What is this? How would you write a program to create? Adjacency List (sample entries) [0][0] => { [0][1], [1][0] } [1][1] => { [0][1], [1][0], [1][2], [2][1] }
Calculating the Adjacency Lists Notice that for each cell, you have at most 4 neighbors. To calculate the adjacency list: for each cell look at each neighbor if it is a valid index add it to the adjacency list What data structure would you use for the adjacency list? (we’ll use a common structure – covered in a minute)
Calculating Neighbors
Choose Data Structures We want to be able to look up all the adjacencies for a given cell. This is a good application of a map: private Map > adjMtx; What is a BoardCell? For now, a simple class with a row and column. More will be added in the Clue game. Why is the adjacency stored as a linked list? I wanted to add/remove easily at the end of the list. LinkedList supports easily. But there’s no insert/remove in the middle of the list, so LinkedList choice is somewhat arbitrary. What are some other alternatives? To facilitate code swap, we will use the same data types – but others might be equally valid. Adjacency List (sample entries) [0][0] => { [0][1]->[1][0] } [1][1] => { [0][1]->[1][0]->[1][2]->[2][1] }
Goal: Targets with paths of length 2 from cell 4 Answer: Shown in green below adjacency matrix should have been calculated prior to calling calcTargets calcTargets: Set up for recursive call visited list is used to avoid backtracking. Set to empty list. targets will ultimately contain the list of cells (e.g., [0][1], [1][2], [2][1], [3][0]). Initially set to empty list. add the start location to the visited list (so no cycle through this cell) call recursive function what will you name this function? I did findAllTargets what parameters does it need? Why can’t these steps be in recursive fn?
=> [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] adjacencies (in condensed form, 0 is [0][0] etc.) findAllTargets pseudocode Parameters: thisCell and numSteps Set adjacentCells to all cells adjacent to thisCell that have not been visited NOTE: do not modify adjacencies, create a new list! Suggest: helper function, keep code cleaner for each adjCell in adjacentCells -- add adjCell to visited list -- if numSteps == 1, add adjCell to Targets -- else call findAllTargets with adjCell, numSteps-1 -- remove adjCell from visited list findAllTargets recursive call! visited: [1][0]
visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies Targets: (none yet) findAllTargets call: thisCell =[1][0], numSteps=2 Set adjacentCells to all cells adjacent to [1][0] that have not been visited… [[2][0], [1][1], [0][0]] (for each cell in adjacentCells) -- add adjCell to visited -- (if number of steps == 1 … ) -- else call findAllTargets with adjCell, numStep-1 -- (when we return….remove adjCell from visited) findAllTargets
=> [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies Targets: (none yet) findAllTargets call: thisCell =[1][0], numSteps=2 Set adjacentCells to all cells adjacent to 4 that have not been visited… [8, 5, 0] (for each cell in adjacentCells… current is [2][0]) -- add [2][0] to visited list -- (if number of steps == 1 … ) -- else call findAllTargets with adjCell, numStep-1 -- (when we return….remove from visited) visited: [1][0] [2][0] findAllTargets
Targets: (none yet) findAllTargets call: thisCell =[1][0], numSteps=2 Set adjacentCells to all cells adjacent to 4 that have not been visited… [8, 5, 0] (for each cell in adjacentCells… current is [2][0]) -- add adjCell to visited -- (if number of steps == 1 … it’s not) -- else call findAllTargets with adjCell ([2][0]), numStep-1 -- (when we return….set visited to False) recursive call! next slide visited: [1][0] [2][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: none yet Set adjacentCells to all cells adjacent to [2][0] that have not been visited… [[3][0], [2][1]] not [1][0] (but remember, don’t modify list of all adjacencies!) (for each cell in adjacentCells) -- set visited[12] to True -- if number of steps == 1… yes! update Targets -- (else call calcTargets with adjCell, numSteps--) -- set visited to False findAllTargets call: thisCell =[2][0], numSteps=1 visited: [1][0] [2][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: none yet Set adjacentCells to all cells adjacent to 8 that have not been visited… [12, 9] not 4 (for each cell in adjacentCells, current is [3][0]) -- add [3][0] to visited -- if number of steps == 1… yes! update Targets -- (else call calcTargets with adjCell, numSteps--) -- set visited to False findAllTargets call: thisCell = =[2][0], numSteps=1 visited: [1][0] [2][0] [3][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0] Set adjacentCells to all cells adjacent to 8 that have not been visited… [12, 9] not 4 (for each cell in adjacentCells, current is [3][0]) -- set visited[12] to True -- if number of steps == 1… yes! update Targets -- (else call calcTargets with adjCell, numSteps--) -- set visited to False findAllTargets call: thisCell = =[2][0], numSteps=1 visited: [1][0] [2][0] [3][0 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0] Set adjacentCells to all cells adjacent to 8 that have not been visited… [12, 9] not 4 (for each cell in adjacentCells, current is [3][0]) -- set visited[12] to True -- if number of steps == 1… yes! update Targets -- (else call findAllTargets with adjCell, numSteps-1) -- remove [3][0] from visited then move to next cell in for loop, next slide findAllTargets call: thisCell = =[2][0], numSteps=1 visited: [1][0] [2][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
With your partner Continue to trace this code You may want to start tracing from the beginning From past time surveys, people who understand algorithm tend to take <4 hours for CluePaths Some students may be >8 hours
Targets: [3][0] Set adjacentCells to all cells adjacent to 8 that have not been visited… [12, 9] not 4 (for each cell in adjacentCells, current is [2][1]) -- add [2][1] to visited -- if number of steps == 1… yes! update Targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited to False findAllTargets call: thisCell = =[2][0], numSteps=1 visited: [1][0] [2][0] [2][1] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to 8 that have not been visited… [12, 9] not 4 (for each cell in adjacentCells, current is 9) -- set visited[9] to True -- if number of steps == 1… yes! update Targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited to False findAllTargets call: thisCell =[2][0], numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to 8 that have not been visited… [12, 9] not 4 (for each cell in adjacentCells, current is 9) -- set visited[9] to True -- if number of steps == 1… yes! update Targets -- (else call findAllTargets with adjCell, numSteps-1) -- remove [2][1] from visited findAllTargets call: thisCell =[2][0], numSteps=1 then move to next cell in for loop visited: [1][0] [2][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to 8 that have not been visited… [12, 9] not 4 (for each cell in adjacentCells, current is END) -- set visited[9] to True -- if number of steps == 1… yes! update Targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited to False findAllTargets call: thisCell =[2][0], numSteps=1 end of for loop, return visited: [1][0] [2][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to 8 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is 8) -- set visited[9] to True -- if number of steps == 1… yes! update Targets -- (else call findAllTargets with adjCell, numSteps-1) -- when we return, remove [2][0] from visited findAllTargets call: thisCell =[1][0], numSteps=2 end of for loop, return from recursive call visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to 8 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is 5) -- add [1][1] to visited -- if number of steps == 1… it’s not -- (else call findAllTargets with adjCell, numSteps-1) -- when we return, set visited to False findAllTargets call: thisCell =[1][0], numSteps=2 next item in for loop visited: [1][0] [1][1] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to 8 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is [1][1]) -- set visited[5] to True -- if number of steps == 1… it’s not -- (else call FindAllTargets with adjCell ([1][1]), numSteps-1) -- when we return, set visited to False findAllTargets call: thisCell =[1][0], numSteps=2 recursive call visited: [1][0] [1][1] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to [1][1] that have not been visited… [[0][1], [1][2], [2][1]]… not [1][0]! (for each cell in adjacentCells) -- set visited[1] to True -- if number of steps == 1… it’s not -- (else call findAllTargets with adjCell (5), numSteps-1) -- when we return, set visited to False findAllTargets call: thisCell =[1][1], numSteps=1 visited: [1][0] [1][1] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1] Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9]… not 4! (for each cell in adjacentCells, current is 1) -- add [0][1] to visited -- if number of steps == 1… it’s not -- (else call findAllTargets with adjCell (5), numSteps-1) -- when we return, set visited to False findAllTargets call: thisCell =5, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: [3][0], [2][1], [0][1] Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is 1) -- set visited[1] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (5), numSteps-1) -- when we return, set visited to False findAllTargets call: thisCell =5, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets NOTE: rest of slides will use single digits for cells. Set visited to true/false means to add/remove from visited list
Targets: 12, 9, 1 Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is 1) -- set visited[1] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (5), numSteps-1) -- set visited[1] to False findAllTargets call: thisCell =5, numSteps=1 then move to next cell in for loop visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1 Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 3] (for each cell in adjacentCells, current is 6) -- set visited[6] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (5), numSteps-1) -- set visited to False findAllTargets call: thisCell =5, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is 6) -- set visited[6] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (5), numSteps-1) -- set visited to False findAllTargets call: thisCell =5, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is 6) -- set visited[6] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (5), numSteps-1) -- set visited[6] to False findAllTargets call: thisCell =5, numSteps=1 then move to next cell in for loop visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is 9) -- set visited[9] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited to False findAllTargets call: thisCell =5, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6… no need to add 9 again Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is 9) -- set visited[9] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited to False calcTargets call: thisCell =5, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is 9) -- set visited[9] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited[9] to False calcTargets call: thisCell =5, numSteps=1 then move to next cell in for loop visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 5 that have not been visited… [1, 6, 9] (for each cell in adjacentCells, current is END) -- set visited[9] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited[9] to False calcTargets call: thisCell =5, numSteps=1 end of for loop, return from recursive call visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 4 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is 5) -- set visited[5] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell, numSteps-1) -- when return from call, set visited[5] to False findAllTargets call: thisCell =4, numSteps=2 move to next cell in for loop visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 4 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is 0) -- set visited[0] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell, numSteps-1) -- set visited to False findAllTargets call: thisCell =4, numSteps=2 move to next cell in for loop visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 4 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is 0) -- set visited[0] to True -- if number of steps == 1… it’s not -- (else call findAllTargets with adjCell (0), numSteps-1) -- set visited to False findAllTargets call: thisCell =4, numSteps=2 another recursive call visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 0 that have not been visited… [1] … not 4! (for each cell in adjacentCells) -- set visited[0] to True -- if number of steps == 1… it’s not -- (else call findAllTargets with adjCell (0), numSteps-1) -- set visited to False findAllTargets call: thisCell =0, numSteps=1 another recursive call visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 0 that have not been visited… [1] … not 4! (for each cell in adjacentCells, current is 1) -- set visited[1] to True -- if number of steps == 1… it’s not -- (else call findAllTargets with adjCell (0), numSteps-1) -- set visited to False findAllTargets call: thisCell =0, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 … 1 is already in the set Set adjacentCells to all cells adjacent to 0 that have not been visited… [1] … not 4! (for each cell in adjacentCells, current is 1) -- set visited[1] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (0), numSteps-1) -- set visited to False findAllTargets call: thisCell =0, numSteps=1 visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 0 that have not been visited… [1] … not 4! (for each cell in adjacentCells, current is 1) -- set visited[1] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (0), numSteps-1) -- set visited[1] to False findAllTargets call: thisCell =0, numSteps=1 move to next cell in for loop visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 0 that have not been visited… [1] … not 4! (for each cell in adjacentCells, current is END) -- set visited[1] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (0), numSteps-1) -- set visited to False findAllTargets call: thisCell =0, numSteps=1 end of for loop, return from call visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 0 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is 0) -- set visited[1] to True -- if number of steps == 1… yes! update targets -- (else call FindAllTargets with adjCell (0), numSteps-1) -- set visited[0] to False findAllTargets call: thisCell =4, numSteps=2 move to next item in loop visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
Targets: 12, 9, 1, 6 Set adjacentCells to all cells adjacent to 0 that have not been visited… [8, 5, 0] (for each cell in adjacentCells, current is END) -- set visited[1] to True -- if number of steps == 1… yes! update targets -- (else call findAllTargets with adjCell (0), numSteps-1) -- set visited to False findAllTargets call: thisCell =4, numSteps=2 end of loop, return from call visited: [1][0] 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] All adjacencies findAllTargets
F F F F T F T F F F F F F F F F visited 0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14] adjacencies Targets: 12, 9, 1, 6 We have now returned to the calcTargets instance variable targets contains answer calcTargets calcTargets call: thisCell =4, numSteps=2 end of loop, return from call
HINT You can use clone or a copy constructor to create a list of adjacent cells that have not been visited. Or create a new list containing just the unvisted cells. If you just modify the existing list, your code will fail x100 0x200 0x300 0x400 0x500 0x > 4 get(0) gets 0x100 if I remove an element, I update the list in my hash map
HINT 2 Take a look at the javadocs for collections, maybe you’ll find some useful functions (e.g., one student use the Array.fill method)