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.

Slides:



Advertisements
Similar presentations
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Advertisements

The Singleton Pattern II Recursive Linked Structures.
Lesson Four: More of the Same
CSE 380 – Computer Game Programming Pathfinding AI
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Arrays Chapter 7.
Sets and Maps Chapter 9.
Values vs. References Lecture 13.
4. Java language basics: Function
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Java 4/4/2017 Recursion.
Chapter 7 Part 1 Edited by JJ Shepherd
10.2 Implementation and Execution of Recursive Code
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
Java Software Structures: John Lewis & Joseph Chase
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Algorithm Design and Analysis (ADA)
Data Structures and Algorithms for Information Processing
Stacks.
Stacks.
1D Arrays and Lots of Brackets
Sets and Maps Chapter 9.
Search,Sort,Recursion.
Algorithms: Design and Analysis
CSC 143 Java Linked Lists.
Dr. Sampath Jayarathna Cal Poly Pomona
CSE 143 Lecture 18 More Recursive Backtracking
CS210- Lecture 3 Jun 6, 2005 Announcements
Presentation transcript:

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)