Lecture 6: Recursion RECURSION

Similar documents
Lecture 7 CS2110 Fall 2014 RECURSION

9/16/14. Overview references to sections in text RECURSION. What does generic mean? A little about generics used in A3

Lecture 6 CS2110 Spring 2013 RECURSION

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

RECURSION. Many Slides from Ken Birman, Cornell University

Recursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly).

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

csci 210: Data Structures Stacks and Queues in Solution Searching

APCS-AB: Java. Recursion in Java December 12, week14 1

RECURSION (CONTINUED)

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

RECURSION (CONTINUED)

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Reduction & Recursion Overview

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Recursive Definitions

Lecture Notes on Dynamic Programming

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

Practice Problems for the Final

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

Lecture 10: Recursion vs Iteration

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Recursion. Chapter Simple Recursion. Goals Trace recursive algorithms Implement recursive algorithms

Backtracking. See Section 7.7 p of Weiss

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

Recursion. Recursion is: Recursion splits a problem:

CSE 143 SAMPLE MIDTERM

Recursion Revisited; Recursive Backtracking

Algorithm Design and Recursion. Search and Sort Algorithms

C22a: Problem Solving using Recursion

CS201 Discussion 7 MARKOV AND RECURSION

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Recursion. Recursion [Bono] 1

Unit #2: Recursion, Induction, and Loop Invariants

Problem solving paradigms

Recursion. Fundamentals of Computer Science

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Catie Baker Spring 2015

STUDENT LESSON A9 Recursion

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Recursion. James Brucker


CS 151. Recursion (Review!) Wednesday, September 19, 12

CSE 143 Lecture 18. More Recursive Backtracking. slides created by Marty Stepp

Finite Math - J-term Homework. Section Inverse of a Square Matrix

CSC 427: Data Structures and Algorithm Analysis. Fall 2006

Unit #3: Recursion, Induction, and Loop Invariants

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

CS 3410 Ch 7 Recursion

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Chapter 5: Recursion. Objectives

Lecture P6: Recursion

Algorithms COMBINATORIAL SEARCH. permutations backtracking counting subsets paths in a graph

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Lecture 2: List algorithms using recursion and list comprehensions

Combinatorial Search. permutations backtracking counting subsets paths in a graph. Overview

Data Structures And Algorithms

CITS1001 exam 2013 SAMPLE SOLUTIONS O cial cover page to be substituted here. May 10, 2013

Definitions. You can represent a point by a dot and name it by a capital letter.

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

15-780: Graduate AI Homework Assignment #2 Solutions

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Recursion Problems. Warm Ups. Enumeration 1 / 7

Using Recursion Goodrich, Tamassia. Using Recursion 1

A function that invokes itself is said to

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

Fun facts about recursion

Pace University. Fundamental Concepts of CS121 1

Fall Lecture 13 Thursday, October 11

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Dartmouth College Computer Science 10, Fall 2014 Solution to Final Exam A

Complexity of Algorithms

CS 211: Recursion. Chris Kauffman. Week 13-1

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

Discrete Mathematics Introduction

Recursion Solution. Counting Things. Searching an Array. Organizing Data. Backtracking. Defining Languages

CMSC 150 LECTURE 7 RECURSION

Computer Science Foundation Exam

15. Pointers, Algorithms, Iterators and Containers II

CSE 143 SAMPLE MIDTERM SOLUTION

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

CS 61B Discussion 5: Inheritance II Fall 2014

5.1 The String reconstruction problem

3137 Data Structures and Algorithms in C++

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CSC 222: Computer Programming II. Spring 2005

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

We cover recursion in 150. Why do it again in 151?

Transcription:

Lecture 6: Recursion RECURSION

You are now Java experts! 2 This was almost all the Java that we will teach you in this course Will see a few last things in the remainder of class Now will begin focusing on data-structures

Question 10 on Homework How can you access a private field from a subclass? -> I got it wrong. You can only use super if they are inner classes as well as extending each other -> You can use reflection Ignore that question. I ll post a note on Piazza with the details.

This Lecture Recursion in the wild How is recursion implemented Recursion Structure How to design a recursive method Examples of recursive problems Backtracking Recursion Examples of backtracking recursive problems

5 To Understand Recursion

To Understand Recursion 6 Definition: defining a property/functionality in terms of itself

To Understand Recursion 7 Break up problem into one or more smaller subproblems of similar structure. Solve subproblems using same method. Reach a stage where you know the answer to the sub-problem Combine results to produce solution to original problem.

Why recursion? 8 Useful programming paradigm Especially useful to manipulate trees, lists, collections Introduces the divide-and-conquer principle When one problem is too hard, break it down into smaller subproblems, and keep doing that until you know how to solve the subproblem

Roadmap We ll first look at examples of recursion in real world, in maths, in Java We ll then derive from them how to write recursive methods We ll look at some more examples

Recursion is real! 10 My ancestors are: My parents, and the ancestors of my parents. Parents

Recursion is real! 11 My ancestors are: My parents, and the ancestors of my parents. What are the ancestors of my parents? Their parents, and the ancestors of their parents Parents + Parents

Recursion is real! 12 My ancestors are: My parents, and the ancestors of my parents. What are the ancestors of my parents? Their parents, and the ancestors of their parents What are the ancestors of their parents? Their parents, and the ancestors of their parents... Parents + Parents + Parents

Recursion is real! 13 Factorials are defined recursively: 0! = 1 n! = n x (n-1)! Power of a number is defined recursive b^0 = 1 b^c = b * b ^(c-1)

Recursion is real! 14 Factorials are defined recursively: 0! = 1 n! = n x (n-1)! 5! = 5 x 4! 4! = 4 x 3! 3! = 3 x 2! 2! = 2x 1! 1! = 1 x 0! 5 x 4 x 3 x 2 x 1 = 120 1 Power of a number is defined recursive b^0 = 1 b^c = b * b ^(c-1)

Recursion is real! 15 Factorials are defined recursively: 0! = 1 n! = n x (n-1)! 5! = 5 x 4! 4! = 4 x 3! 3! = 3 x 2! 2! = 2x 1! 1! = 1 x 0! 5 x 4 x 3 x 2 x 1 = 120 1 Power of a number is defined recursive b^0 = 1 b^c = b * b ^(c-1) 2^3 = 2 * 2^2 2^3 = 2 * 2^2 2^2 = 2 * 2^1 2^1 = 2 * 2^0 2 * 2 * 2 * 1 = 8 1

Two ways to understand recursion 16 1. How is it executed? (or, why does this even work?) 2. How do we understand recursive methods? (or, how do we write/develop recursive methods?)

Recursion in Java 17 How to compute the sum of all the digits in an integer Ex: sum(1) = 1 sum(13) = 4 sum(852) = 15 /** = sum of digits in n. * Precondition: n >= 0 */ public static int sum(int n) { if (n < 10) return n; sum calls itself! // { n has at least two digits // return first digit + sum of rest return n%10 + sum(n/10);

18 An implementation detour: Stacks and Queues stack grows top element 2nd element... bottom element Stack: list with (at least) two basic ops: * Push an element onto its top * Pop (remove) top element Last-In-First-Out (LIFO) Like a stack of trays in a cafeteria

19 An Implementation Detour: Stack Frame A frame contains information about a method call: a frame local variables parameters return info

20 An Implementation Detour: Stack Frame A frame contains information about a method call: At runtime Java maintains a a frame stack that contains frames for all method calls that are being executed but have not completed. local variables parameters return info

21 An Implementation Detour: Stack Frame A frame contains information about a method call: At runtime Java maintains a a frame stack that contains frames for all method calls that are being executed but have not completed. local variables parameters return info Start of method call: push a frame for call on stack. Use the frame for the call to reference local variables and parameters.

22 An Implementation Detour: Stack Frame A frame contains information about a method call: At runtime Java maintains a a frame stack that contains frames for all method calls that are being executed but have not completed. local variables parameters return info Start of method call: push a frame for call on stack. Use the frame for the call to reference local variables and parameters. End of method call: pop its frame from the stack; if it is a function leave the return value on top of stack.

23 An implementation detour: Memorise method call execution! A frame for a call contains parameters, local variables, and other information needed to properly execute a method call. To execute a method call: 1. push a frame for the call on the stack, 2. assign argument values to parameters, 3. execute method body, 4. pop frame for call from stack, and (for a function) push returned value on stack When executing method body look in frame for call for parameters and local variables.

An implementation detour: implementation of sum 24 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); main is called. Frame placed on stack. main r args return info

An implementation detour: implementation of sum 25 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); main calls sum with args 824 main n 824 return info r args return info

An implementation detour: implementation of sum 26 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); 824>=10, sum calls sum main n 82 return info n 824 return info r args return info

An implementation detour: implementation of sum 27 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); 82>=10, sum calls sum again main n 8 return info n 82 return info n 824 return info r args return info

An implementation detour: implementation of sum 28 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); 8<10, sum stops: frame is popped and n is put on stack: main n 8 8 return info n 82 return info n 824 return info r args return info

An implementation detour: implementation of sum 29 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); Using return value 8 stack computes 2 + 8 = 10 pops frame from stack puts return value 10 on stack: main n 82 8 + 2 = 10 return info n 824 return info r args return info

An implementation detour: implementation of sum 30 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); Using return value 10 stack computes 4 + 10 = 14 pops frame from stack puts return value 14 on stack main n 824 10 + 4 =14 return info r args return info

An implementation detour: implementation of sum 31 public static int sum(int n) { if (n < 10) return n; return n%10 + sum(n/10); public static void main(string[] args) { int r= sum(824); System.out.println(r); Using return value 14 main stores 14 in r and removes 14 from stack main r _14_ args return info

What do these methods have in common? 32 List my ancestors ancestor = parents U ancestor(parents) Factorials 0! = 1 n! = n x (n-1)! Power of a number b^0 = 1 ; b^c = b * b ^ (c-1)

What do these methods have in common? 33 List my ancestors ancestor = parents U ancestor(parents) Factorials 0! = 1 n! = n x (n-1)! Power of a number b^0 = 1 ; b^c = b * b ^ (c-1)

What do these methods have in common? 34 List my ancestors ancestor = parents U ancestor(parents) Factorials 0! = 1 n! = n x (n-1)! Power of a number b^0 = 1 ; b^c = b * b ^ (c-1)

What do these methods have in common? 35 List my ancestors ancestor = parents U ancestor(parents) Factorials 0! = 1 n! = n x (n-1)! Power of a number b^0 = 1 ; b^c = b * b ^ (c-1)

What do these methods have in common? 36 List my ancestors ancestor = parents U ancestor(parents) Factorials 0! = 1 n! = n x (n-1)! Power of a number b^0 = 1 ; b^c = b * b ^ (c-1)

What do these methods have in common? 37 List my ancestors ancestor = parents U ancestor(parents) Factorials 0! = 1 n! = n x (n-1)! Power of a number b^0 = 1 ; b^c = b * b ^ (c-1)

What do these methods have in common? 38 List my ancestors ancestor = parents U ancestor(parents) Factorials 0! = 1 n! = n x (n-1)! Power of a number b^0 = 1 ; b^c = b * b ^ (c-1)

What do these methods have in common? 39 Every recursive method has one (or more) base cases Scenarios that we can solve easily without recursion 0! = 1.. b^0 Every recursive method has one (or more) recursive cases The function calls itself on inputs that are closer to the base case n! = n x (n-1)!.. b^c = b * b ^ (c-1) Every recursive method merges the result of each recursive call n! = n x (n-1)!.. b^c = b * b ^ (c-1)

To design a recursive method 40 Write a precise spec Spec of sum(n) says the value of a call equals the sum of the digits of n Identify a base case, and check, with concrete numbers that the method returns correct values in the base case Ex: sum(8) = 8 Look at the recursive case(s). In your mind replace each recursive call by what it does acc. to the spec and verify correctness. Ex: sum(82) = 2 + sum(8) (No infinite recursion) Make sure that the args of recursive calls are in some sense smaller than the args of the method.

Powers and Factorials 41 Factorials 0! = 1 n! = n x (n-1)! /** Computes n! Precondition n>=0 **/ public static int fact(int n) { if (n == 0) return 1; return n * fact(n-1); Power of a number b^0 = 1 ; b^c = b * b ^ (c-1) public static int exp(int b, int c) { if (c == 0) return 1; return b * exp(b, c-1);

Counting all the letters e in a string 42 Return the number of times that the letter e appears in a string using recursion. ex: counte( natacha ) = 0 counte( e ) = 1 counte( elephant ) = 2 What do you think is the base case? Think of the smallest possible string What about the recursive call? How do we merge multiple calls?

Counting all the letters e in a string 43 Return the number of times that the letter e appears in a string using recursion. ex: counte( natacha ) = 0 counte( e ) = 1 counte( elephant ) = 2 What do you think is the base case? Think of the smallest possible string What about the recursive call? How do we merge multiple calls? /** Number of times e occurs in str **/ public static int countx(string str) { if (str.equals("")) return 0; else { char c = str.charat(0); if (c == 'e') { return 1 + countx(str.substring(1)); else return 0 + countx(str.substring(1));

Fibonacci Numbers 44 Mathematical definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n 1) + fib(n 2) n 2 Fibonacci sequence: 0 1 1 2 3 5 8 13 Fruit sprouts of a pineapple Flowering of an artichoke Fibonacci Heaps Fibonacci Cubes (ways to organise distributed systems together) Applications in chemical graph theory...

Fibonacci Numbers 45 Mathematical definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n 1) + fib(n 2) n 2 two base cases! Fibonacci sequence: 0 1 1 2 3 5 8 13 /** = fibonacci(n). Pre: n >= 0 */ static int fib(int n) { // fib(0) = 1, fib(1) = 2 if (n <= 1) return n; return fib(n-1) + fib(n-2); Fruit sprouts of a pineapple Flowering of an artichoke Fibonacci Heaps Fibonacci Cubes (ways to organise distributed systems together) Applications in chemical graph theory...

Palindromes 46 A palindrome is a string that reads the same backward and forwards ispal(racecar) = true ispal(pumpkin) = false ispal(n) = true

Palindromes 47 A palindrome is a string that reads the same backward and forwards ispal(racecar) = true ispal(pumpkin) = false ispal(n) = true How do we go about implementing this? Let s try to rephrase it as a recursive definition

Palindromes 48 A palindrome is a string that reads the same backward and forwards ispal(racecar) = true ispal(pumpkin) = false ispal(n) = true How do we go about implementing this? Let s try to rephrase it as a recursive definition A String with at least two characters is a palindrome if its first and last characters are equal and chars between first & last characters are also palindrome:

Palindromes 49 A palindrome is a string that reads the same backward and forwards ispal(racecar) = true ispal(pumpkin) = false ispal(n) = true How do we go about implementing this? Let s try to rephrase it as a recursive definition A String with at least two characters is a palindrome if its first and last characters are equal and chars between first & last characters are also palindrome: What are we missing here?

Palindromes 50 A palindrome is a string that reads the same backward and forwards ispal(racecar) = true ispal(pumpkin) = false ispal(n) = true How do we go about implementing this? Let s try to rephrase it as a recursive definition A String with at least two characters is a palindrome if It is of length 0/1 or its first and last characters are equal and chars between first & last characters are also palindrome: Basecase!

Palindromes 51 /** = "s is a palindrome" */ public static boolean ispal(string s) { if (s.length() <= 1) return true; Substring from s[1] to s[n-1] // { s has at least 2 chars int n= s.length()-1; return s.charat(0) == s.charat(n) && ispal(s.substring(1,n));

Recursion with backtracking Some recursion problems require you to enumerate all solutions (Type 1) Find solutions subject to a set of constraints (Type 2) Follow a similar format: Explore one solution until the end: If Type 1: add that solution to a set, and backtrack to the last recursive call to explore other solutions If Type 2: if solution satisfies constraint, return true, otherwise, backtrack to the last recursive call to explore other solutions

Recursion with backtracking Option 1 + function(x-1) Option 1 + function(x-2) Option 2 + function(x-2) Reach base case Reach base case function(x) Option 2 + function(x-1) Option 1 + function(x-2) Option 2 + function(x-2) Option 3 + function(x-1) Option 1 + function(x-2) I am writing x-1 to mean smaller input and + to mean merge

Finding permutations of a string 54 Compute all permutations of a string (assuming the string has distinct characters) perms(abc) = abc, acb, bac, bca, cab, cba Recursive Definition Each possible first letter, followed by all permutations of the remaining characters

Finding permutations of a string 55 perm(abc)

Finding permutations of a string 56 a + perm( bc ) perm(abc) b + perm( ac ) c + perm( ab )

Finding permutations of a string 57 a + perm( bc ) b + perm( c ) c + perm( b ) perm(abc) b + perm( ac ) a + perm( c ) c + perm( a ) c + perm( ab ) a + perm( b ) b + perm( a )

Finding permutations of a string 58 b + perm( c ) c + perm( ) a + perm( bc ) c + perm( b ) b + perm( ) perm(abc) b + perm( ac ) a + perm( c ) c + perm( ) c + perm( a ) a + perm( ) c + perm( ab ) a + perm( b ) b + perm( ) b + perm( a ) a + perm( )

Finding permutations of a string 59 b + perm( c ) c + perm( ) a + perm( bc ) c + perm( b ) b + perm( ) perm(abc) b + perm( ac ) a + perm( c ) c + perm( ) c + perm( a ) a + perm( ) c + perm( ab ) a + perm( b ) b + perm( ) b + perm( a ) a + perm( )

Finding permutations of a string 60 b + perm( c ) c + { a + perm( bc ) c + perm( b ) b + { perm(abc) b + perm( ac ) a + perm( c ) c + { c + perm( a ) a + { c + perm( ab ) a + perm( b ) b + { b + perm( a ) a + {

Finding permutations of a string 61 b + perm( c ) c + { c a + perm( bc ) c + perm( b ) b + { b perm(abc) b + perm( ac ) a + perm( c ) c + { c c + perm( a ) a + { c c + perm( ab ) a + perm( b ) b + { b b + perm( a ) a + { a

Finding permutations of a string 62 a + perm( bc ) b + { c c + { b perm(abc) b + perm( ac ) a + { c c + { a c + perm( ab ) a + { b b + { a

Finding permutations of a string 63 a + perm( bc ) { bc { cb perm(abc) b + perm( ac ) { ac { ca c + perm( ab ) { ab { ba

Finding permutations of a string 64 a + [{ bc, { cb ] perm(abc) b + [{ ac, { ca ] c + [{ ab,{ ba ]

Finding permutations of a string 65 [{ abc, { acb ] perm(abc) [{ bac, { bca ] [{ cab,{ cba ]

Finding permutations of a string 66 [ abc, acb, bac, bca, cab, cba ]

67 Finding permutations of a string

Remember: Recursion with backtracking (Type 1) Option 1 + function(x-1) Option 1 + function(x-2) Option 2 + function(x-2) Reach base case Reach base case function(x) = Set<Solutions> set; If base case { add x to set else { // Recursive Step for every option: Set<Solutions> recursive = function(x-1) for every solution in recursive: merge (option,recursive) Add x to set Return set;

8-queens problem 69 Find the position on an 8 x 8 chessboard such that no queen is attacking the other A queen is attacking another queen if they are in the same row, column, or diagonal

8-queens problem 70 Find the position on an 8 x 8 chessboard such that no queen is attacking the other A queen is attacking another queen if they are in the same row, column, or diagonal Recursive Formulation: Queen 1 is not attacking anyone, and the other n-1 queens solve the n-1-queens problem

8-queens problem 71 Step 1: solve queen(4, chessboard)

8-queens problem 72 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { currentarray[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Try placing Queen 1 in (0,0)

8-queens problem 73 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { currentarray[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Is queen Safe at (0,0)? Yes!

8-queens problem 74 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Update chessboard

8-queens problem 75 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(3,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Call solvequeen(3,chess)

8-queens problem 76 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (0,0)

8-queens problem 77 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Is she safe? No

8-queens problem 78 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (1,0)

8-queens problem 79 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (0,0)

8-queens problem 80 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (2,0)

8-queens problem 81 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Is she safe? No

8-queens problem 82 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (3,0)

8-queens problem 83 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Is she safe? No

8-queens problem 84 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (0,1)

8-queens problem 85 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Is she safe? No

8-queens problem 86 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (1,1)

8-queens problem 87 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Is she safe? No

8-queens problem 88 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try placing Queen 2 in (2,1)

8-queens problem 89 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Is she safe? Yes!

8-queens problem 90 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Update Chessboard

8-queens problem 91 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Call queen(2,currentarray)

8-queens problem 92 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try Queen 3 (0,0)

8-queens problem 93 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Try Queen 3 (0,1)

8-queens problem 94 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Try Queen 3 until (1,3)

8-queens problem 95 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Is she safe? Yes!

8-queens problem 96 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Update chessboard

8-queens problem 97 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Call queen(1,currentarray);

8-queens problem 98 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) Try Queen 4 (0,0)

8-queens problem 99 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) Try Queen 4 up to (3,3)

8-queens problem 100 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) Queen cannot be placed!

8-queens problem 101 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Must backtrack. Return with success = false Frame solvequeen(1, chess)

8-queens problem 102 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Remove Queen 3. Continue trying to place it Frame solvequeen(2, chess)

8-queens problem 103 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Try Queen 3 (3,3)

8-queens problem 104 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Is Queen Safe? Yes

8-queens problem 105 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Update chessboard

8-queens problem 106 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Call queen(1,currentarray);

8-queens problem 107 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) Try all possibilities

8-queens problem 108 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) Queen is never safe

8-queens problem 109 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Backtrack with return success = false Frame solvequeen(1, chess)

8-queens problem 110 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Undo third queen

8-queens problem 111 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; No more options for third queen. Backtrack success = false Frame solvequeen(2, chess)

8-queens problem 112 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Undo Second Queen

8-queens problem 113 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Try Queen 2 (1,3)

8-queens problem 114 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Is Safe? Yes

8-queens problem 115 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Update Chessboard

8-queens problem 116 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(3, chess) Call queen(2,currentarray)

8-queens problem 117 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Try to place Queen 3

8-queens problem 118 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(2, chess) Try to place Queen 3

8-queens problem 119 Fast forward a little bit: explore all solutions until reach last position of Queen 2. That solution also fails, so backtrack all the way back to Queen 1

8-queens problem 120 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(3,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Try to place Queen 1 (1,0)

8-queens problem 121 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(3,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Is it Safe? Yes

8-queens problem 122 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(3,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Update chessboard

8-queens problem 123 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(3,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(4, chess) Call queen(3,currentarray)

8-queens problem 124 Frame solvequeen(1, chess) Fast forward a little, go through every solution, backtracking as necessary, until are in the following state Place Queen 4

8-queens problem 125 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) It is safe!

8-queens problem 126 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) Update chessboard

8-queens problem 127 for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(1, chess) Call queen(0,currentarray)

8-queens problem 128 if (n ==0) return true for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(-1,currentarray); If (!success) currentarray[row][col] = 0 Return success; Frame solvequeen(0, chess) Hit base case

8-queens problem 129 if (n ==0) return true for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(0,currentarray); If (!success) currentarray[row][col] = 0 else return true; Return success; Return true in previous frame Frame solvequeen(1, chess)

8-queens problem 130 if (n ==0) return true for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(1,currentarray); If (!success) currentarray[row][col] = 0 else return true; Return success; Return true in previous frame Frame solvequeen(2, chess)

8-queens problem 131 if (n ==0) return true for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(2,currentarray); If (!success) currentarray[row][col] = 0 else return true; Return success; Return true in previous frame Frame solvequeen(3, chess)

8-queens problem 132 if (n ==0) return true for (int row = 0 ; row < chessboard.length ; row++) { for (int col = 0 ; col < chessboard.length ; col++) { bool success = issafe(currentarray[row][col]) If (success) { chess[row][col] = 1; success = queen(3,currentarray); If (!success) currentarray[row][col] = 0 else return true; Return success; Return true in previous frame Frame solvequeen(4, chess)

8-queens problem 133 Frame solvequeen(4, chess) Chess = { {0,0,1,0, {1,0,0,0, {0,0,0,1, {0,1,0,0 Return true in function!

8-queens problem 134 1) Start in the leftmost column 2) If all queens are placed return true 3) Try all rows in the current column. For every row do: a) If the queen can be placed safely in this row then mark this [row, column] as part of the solution and recursively check if placing queen here leads to a solution. b) If placing the queen in [row, column] leads to a solution then return true. c) If placing queen doesn't lead to a solution then umark this [row, column] (Backtrack) and go to step (a) to try other rows. 3) If all rows have been tried and nothing worked, return false to trigger backtracking.

8-queens problem 135 Using the algorithm described, try implementing it in Java for HW Hint 1: First define a method: issafe(int[][] board) that determines whether a queen can be placed here. You need to check that there is no queen in the current row, column, diagonal Hint 2: if placing a queen in a particular location fails (because other queens can t be placed there in the recursive calls), remember to remove the queen from that location public static boolean solve(int nbqueens, int[][] board) // empty entries marked as zero. Entries with queens marked as 1

Sudoku (Solve it for a bonus in HW!) 136 Recursive Backtracking Solution public static boolean solve(int[][] s) // empty entries marked as zero.

Map Colouring Problem 137 Cool theorem: given any separation of a plane into contiguous regions, producing a figure called a map, no more than four colors are required to color the regions of the map so that no two adjacent regions have the same color But how do you find the assignment? You can use recursive backtracking, just like we did for the other solutions Tons of cool applications: AI, register assignment in computer architecture, etc.

Remember: Recursion with backtracking (Type 2) Option 1 + function(x-1) Option 1 + function(x-2) Option 2 + function(x-2) Reach base case Reach base case function(x) = If base case { if (isvalid(x)) return true ; else false else { // Recursive Step for every option: if (function(x-1) == true) { // Solution found return true Stops as soon as has found a solution => it is called greedy

Will recursion save the world? 139 Recursion makes it easy to solve complex algorithms and explore problems with many different parameters/constraints Easy to solve 8-Queens, Sudoku, etc. But: it can be a very expensive solution! Think about a sudoku solution: for each square, I try 9 solutions. Then another 9, then another 9. 9 x 9 x 9 gets expensive pretty quickly! Next lecture, we will formalise what expensive actually means!