uct algorithm circle: brute force and advanced recursion · recursion, as we’ll soon see in one...

64
university-logo Introduction Brute Force Memoisation Summary up to now Advanced Problems UCT Algorithm Circle: Brute Force and Advanced Recursion Roland Elliott 14 April 2011 Roland Elliott Brute Force and Advanced Recursion

Upload: others

Post on 06-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

UCT Algorithm Circle: Brute Force andAdvanced Recursion

Roland Elliott

14 April 2011

Roland Elliott Brute Force and Advanced Recursion

Page 2: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Outline

1 Introduction

2 Brute Force

3 Memoisation

4 Summary up to now

5 Advanced ProblemsProblem 1: Minimal Path SumProblem 2: Eight Queens

Roland Elliott Brute Force and Advanced Recursion

Page 3: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Lecture plan

In previous classes you have encountered the idea ofrecursion, when a function calls itself.You should understand the following terms:

Function callBase caseInfinite recursion

Today we’re going to look at more advanced examples ofrecursion.Before we do, however, we are going to look at the idea ofBrute Force.

Roland Elliott Brute Force and Advanced Recursion

Page 4: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Outline

1 Introduction

2 Brute Force

3 Memoisation

4 Summary up to now

5 Advanced ProblemsProblem 1: Minimal Path SumProblem 2: Eight Queens

Roland Elliott Brute Force and Advanced Recursion

Page 5: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search

When we say a solution or algorithm is brute force, whatwe mean is that it makes use of a brute force search tocalculate the answer.A brute force search is a problem-solving technique wherethe program loops through all possible solutions (calledsolution candidates) and stops once a good solution isfound.We can improve the speed of a brute force algorithm byreducing the number of candidates we check.

Roland Elliott Brute Force and Advanced Recursion

Page 6: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search

When we say a solution or algorithm is brute force, whatwe mean is that it makes use of a brute force search tocalculate the answer.A brute force search is a problem-solving technique wherethe program loops through all possible solutions (calledsolution candidates) and stops once a good solution isfound.We can improve the speed of a brute force algorithm byreducing the number of candidates we check.

Roland Elliott Brute Force and Advanced Recursion

Page 7: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search

When we say a solution or algorithm is brute force, whatwe mean is that it makes use of a brute force search tocalculate the answer.A brute force search is a problem-solving technique wherethe program loops through all possible solutions (calledsolution candidates) and stops once a good solution isfound.We can improve the speed of a brute force algorithm byreducing the number of candidates we check.

Roland Elliott Brute Force and Advanced Recursion

Page 8: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search: Example

Problem Find the factors for some positive, whole number n.Solution Loop through all the numbers from 1 to n. Everynumber that divides n is a factor of n.

function factors(n):factors = []for i = 1 to n:

if n % i == 0:factors.append(i)

return factors

Note: the speed of this algoritm is O(n).

Roland Elliott Brute Force and Advanced Recursion

Page 9: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search: Example

In this example we can reduce the candidates by noticingthat we only need to check from 1 to the square root of n.This is because factors come in pairs on either “side” of thesqaure root:

function factors(n):factors = []for i=1 to sqrt(n):

if n % i == 0:factors.append(i)factors.append(n/i)

return factors

Note: we have to add two factors because the factor on theother side of

√n will never be reached.

Roland Elliott Brute Force and Advanced Recursion

Page 10: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search: Example

Even though the first one runs in O(n) time and the secondone runs in O(

√n) time both are still considered brute

force solutions because they make use of the brute forcesearch technique.Often a problem will have a brute force and a non-bruteforce solution. Non-brute force solutions are usually betterbut are harder to find. In these cases you have to decidewhether you should try and find the non-brute forcesolution or just optimise the brute force solution.Brute force solutions can usually easily be coded usingrecursion, as we’ll soon see in one of our examples.

Roland Elliott Brute Force and Advanced Recursion

Page 11: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search: Example

Even though the first one runs in O(n) time and the secondone runs in O(

√n) time both are still considered brute

force solutions because they make use of the brute forcesearch technique.Often a problem will have a brute force and a non-bruteforce solution. Non-brute force solutions are usually betterbut are harder to find. In these cases you have to decidewhether you should try and find the non-brute forcesolution or just optimise the brute force solution.Brute force solutions can usually easily be coded usingrecursion, as we’ll soon see in one of our examples.

Roland Elliott Brute Force and Advanced Recursion

Page 12: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force Search: Example

Even though the first one runs in O(n) time and the secondone runs in O(

√n) time both are still considered brute

force solutions because they make use of the brute forcesearch technique.Often a problem will have a brute force and a non-bruteforce solution. Non-brute force solutions are usually betterbut are harder to find. In these cases you have to decidewhether you should try and find the non-brute forcesolution or just optimise the brute force solution.Brute force solutions can usually easily be coded usingrecursion, as we’ll soon see in one of our examples.

Roland Elliott Brute Force and Advanced Recursion

Page 13: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Outline

1 Introduction

2 Brute Force

3 Memoisation

4 Summary up to now

5 Advanced ProblemsProblem 1: Minimal Path SumProblem 2: Eight Queens

Roland Elliott Brute Force and Advanced Recursion

Page 14: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

What is memoisation?

With many recursive functions, there is an overlap ofsubproblems. What this means is that certain subproblemsare used more than once and therefore need to becalculated more than once.For example, consider the fibonacci algorithm we looked atlast time:

function fib(n):if n <= 2:

return 1else:

return fib(n-1) + fib(n-2)

Roland Elliott Brute Force and Advanced Recursion

Page 15: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Fibonacci call trace

If we trace the calls for fib(5) we get the following call-tree:

fib(5)

fib(4) 0 0 0 fib(3)

fib(3) fib(2)

fib(2) fib(1)

fib(2) fib(1)

For one call of fib(5), fib(2) is called 3 times: once in fib(3)and twice in fib(4).

Roland Elliott Brute Force and Advanced Recursion

Page 16: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Fibonacci call trace

For fib(6), fib(2) is calculated 3 times for fib(5) and twice forfib(4) which is 5 times in total.For fib(7), fib(2) is calculated 8 times.For fib(50), fib(2) is calculated 7,778,742,049 times.

This happens because there is an overlap of subproblems.Clearly we need to find some way to prevent such a bigoverlap in subproblems.This is where memoisation comes in.

Roland Elliott Brute Force and Advanced Recursion

Page 17: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Fibonacci call trace

For fib(6), fib(2) is calculated 3 times for fib(5) and twice forfib(4) which is 5 times in total.For fib(7), fib(2) is calculated 8 times.For fib(50), fib(2) is calculated 7,778,742,049 times.

This happens because there is an overlap of subproblems.Clearly we need to find some way to prevent such a bigoverlap in subproblems.This is where memoisation comes in.

Roland Elliott Brute Force and Advanced Recursion

Page 18: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Fibonacci call trace

For fib(6), fib(2) is calculated 3 times for fib(5) and twice forfib(4) which is 5 times in total.For fib(7), fib(2) is calculated 8 times.For fib(50), fib(2) is calculated 7,778,742,049 times.

This happens because there is an overlap of subproblems.Clearly we need to find some way to prevent such a bigoverlap in subproblems.This is where memoisation comes in.

Roland Elliott Brute Force and Advanced Recursion

Page 19: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Fibonacci call trace

For fib(6), fib(2) is calculated 3 times for fib(5) and twice forfib(4) which is 5 times in total.For fib(7), fib(2) is calculated 8 times.For fib(50), fib(2) is calculated 7,778,742,049 times.

This happens because there is an overlap of subproblems.Clearly we need to find some way to prevent such a bigoverlap in subproblems.This is where memoisation comes in.

Roland Elliott Brute Force and Advanced Recursion

Page 20: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Fibonacci call trace

For fib(6), fib(2) is calculated 3 times for fib(5) and twice forfib(4) which is 5 times in total.For fib(7), fib(2) is calculated 8 times.For fib(50), fib(2) is calculated 7,778,742,049 times.

This happens because there is an overlap of subproblems.Clearly we need to find some way to prevent such a bigoverlap in subproblems.This is where memoisation comes in.

Roland Elliott Brute Force and Advanced Recursion

Page 21: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Fibonacci call trace

For fib(6), fib(2) is calculated 3 times for fib(5) and twice forfib(4) which is 5 times in total.For fib(7), fib(2) is calculated 8 times.For fib(50), fib(2) is calculated 7,778,742,049 times.

This happens because there is an overlap of subproblems.Clearly we need to find some way to prevent such a bigoverlap in subproblems.This is where memoisation comes in.

Roland Elliott Brute Force and Advanced Recursion

Page 22: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

What is memoisation?

Memoisation is a technique that helps a recursive function“remember” answers to previously calculated problems.In the case of the fibonacci numbers, we would store theanswers in a dictionary (hash table) so that answers areonly calculated once and later just read from the dictionary.In our example fib(5) will call fib(4) first which will call fib(3)which will call fib(2) and fib(1). Now fib(3) is stored, thenfib(4) is stored.Then we get to fib(5)’s call of fib(3). Now there is no needto recalculate fib(3), since we’ve stored the answer, so wejust read the stored answer.What does the call tree of our memoised function look likenow?

Roland Elliott Brute Force and Advanced Recursion

Page 23: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

What is memoisation?

Memoisation is a technique that helps a recursive function“remember” answers to previously calculated problems.In the case of the fibonacci numbers, we would store theanswers in a dictionary (hash table) so that answers areonly calculated once and later just read from the dictionary.In our example fib(5) will call fib(4) first which will call fib(3)which will call fib(2) and fib(1). Now fib(3) is stored, thenfib(4) is stored.Then we get to fib(5)’s call of fib(3). Now there is no needto recalculate fib(3), since we’ve stored the answer, so wejust read the stored answer.What does the call tree of our memoised function look likenow?

Roland Elliott Brute Force and Advanced Recursion

Page 24: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

What is memoisation?

Memoisation is a technique that helps a recursive function“remember” answers to previously calculated problems.In the case of the fibonacci numbers, we would store theanswers in a dictionary (hash table) so that answers areonly calculated once and later just read from the dictionary.In our example fib(5) will call fib(4) first which will call fib(3)which will call fib(2) and fib(1). Now fib(3) is stored, thenfib(4) is stored.Then we get to fib(5)’s call of fib(3). Now there is no needto recalculate fib(3), since we’ve stored the answer, so wejust read the stored answer.What does the call tree of our memoised function look likenow?

Roland Elliott Brute Force and Advanced Recursion

Page 25: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

What is memoisation?

Memoisation is a technique that helps a recursive function“remember” answers to previously calculated problems.In the case of the fibonacci numbers, we would store theanswers in a dictionary (hash table) so that answers areonly calculated once and later just read from the dictionary.In our example fib(5) will call fib(4) first which will call fib(3)which will call fib(2) and fib(1). Now fib(3) is stored, thenfib(4) is stored.Then we get to fib(5)’s call of fib(3). Now there is no needto recalculate fib(3), since we’ve stored the answer, so wejust read the stored answer.What does the call tree of our memoised function look likenow?

Roland Elliott Brute Force and Advanced Recursion

Page 26: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

What is memoisation?

Memoisation is a technique that helps a recursive function“remember” answers to previously calculated problems.In the case of the fibonacci numbers, we would store theanswers in a dictionary (hash table) so that answers areonly calculated once and later just read from the dictionary.In our example fib(5) will call fib(4) first which will call fib(3)which will call fib(2) and fib(1). Now fib(3) is stored, thenfib(4) is stored.Then we get to fib(5)’s call of fib(3). Now there is no needto recalculate fib(3), since we’ve stored the answer, so wejust read the stored answer.What does the call tree of our memoised function look likenow?

Roland Elliott Brute Force and Advanced Recursion

Page 27: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

What is memoisation?

fib(5)

fib(4) 0 0 0 fib(3)

fib(3) fib(2)

fib(2) fib(1)

fib(2) fib(1)

Notice how fib(2) is not recalculated a third time.In fact, even in the case of fib(50), fib(2) is only calculatedtwice!

Roland Elliott Brute Force and Advanced Recursion

Page 28: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Memoisation: Fibonacci

What does the code for our memoised fibancci functionlook like?We use a dictionary to store the answers to values:

answers = {}function fib(n):

if n <= 2:return 1

else if n in answers:return answers[n]

else:ans = fib(n-1) + fib(n-2)#store the answer for later useanswers[n] = ansreturn ans

Roland Elliott Brute Force and Advanced Recursion

Page 29: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Memoisation: Issues

There are two main issues that come with memoisation:We have to be careful when using memoisation, becausethe dictionary we use to store the answers could get verylarge for large problems. We might need to enforce amaximum dictionary size.Sometimes problems that can be solved with memoisationcan also be solved with Dynamic Programming, which isusually better. When you cover dynamic programming you’llsee what I mean.

Roland Elliott Brute Force and Advanced Recursion

Page 30: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Memoisation: Issues

There are two main issues that come with memoisation:We have to be careful when using memoisation, becausethe dictionary we use to store the answers could get verylarge for large problems. We might need to enforce amaximum dictionary size.Sometimes problems that can be solved with memoisationcan also be solved with Dynamic Programming, which isusually better. When you cover dynamic programming you’llsee what I mean.

Roland Elliott Brute Force and Advanced Recursion

Page 31: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Memoisation: Issues

There are two main issues that come with memoisation:We have to be careful when using memoisation, becausethe dictionary we use to store the answers could get verylarge for large problems. We might need to enforce amaximum dictionary size.Sometimes problems that can be solved with memoisationcan also be solved with Dynamic Programming, which isusually better. When you cover dynamic programming you’llsee what I mean.

Roland Elliott Brute Force and Advanced Recursion

Page 32: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Outline

1 Introduction

2 Brute Force

3 Memoisation

4 Summary up to now

5 Advanced ProblemsProblem 1: Minimal Path SumProblem 2: Eight Queens

Roland Elliott Brute Force and Advanced Recursion

Page 33: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force and Memoisation

Brute force algorithms:An algorithm that makes use of a brute force search iscalled a brute force algorithm.Often a brute force algorithm can be improved if we findsome way to reduce its candidates.

Memoisation:Memoisation is a technique where the function“remembers” previously calculated solutions.When there are overlapping subproblems this improvescomputation speed considerably by reducing the number ofrecalculations needed to be done.There are two issues that need to be considered whenusing memoisation in real life.

Roland Elliott Brute Force and Advanced Recursion

Page 34: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force and Memoisation

Brute force algorithms:An algorithm that makes use of a brute force search iscalled a brute force algorithm.Often a brute force algorithm can be improved if we findsome way to reduce its candidates.

Memoisation:Memoisation is a technique where the function“remembers” previously calculated solutions.When there are overlapping subproblems this improvescomputation speed considerably by reducing the number ofrecalculations needed to be done.There are two issues that need to be considered whenusing memoisation in real life.

Roland Elliott Brute Force and Advanced Recursion

Page 35: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force and Memoisation

Brute force algorithms:An algorithm that makes use of a brute force search iscalled a brute force algorithm.Often a brute force algorithm can be improved if we findsome way to reduce its candidates.

Memoisation:Memoisation is a technique where the function“remembers” previously calculated solutions.When there are overlapping subproblems this improvescomputation speed considerably by reducing the number ofrecalculations needed to be done.There are two issues that need to be considered whenusing memoisation in real life.

Roland Elliott Brute Force and Advanced Recursion

Page 36: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force and Memoisation

Brute force algorithms:An algorithm that makes use of a brute force search iscalled a brute force algorithm.Often a brute force algorithm can be improved if we findsome way to reduce its candidates.

Memoisation:Memoisation is a technique where the function“remembers” previously calculated solutions.When there are overlapping subproblems this improvescomputation speed considerably by reducing the number ofrecalculations needed to be done.There are two issues that need to be considered whenusing memoisation in real life.

Roland Elliott Brute Force and Advanced Recursion

Page 37: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force and Memoisation

Brute force algorithms:An algorithm that makes use of a brute force search iscalled a brute force algorithm.Often a brute force algorithm can be improved if we findsome way to reduce its candidates.

Memoisation:Memoisation is a technique where the function“remembers” previously calculated solutions.When there are overlapping subproblems this improvescomputation speed considerably by reducing the number ofrecalculations needed to be done.There are two issues that need to be considered whenusing memoisation in real life.

Roland Elliott Brute Force and Advanced Recursion

Page 38: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force and Memoisation

Brute force algorithms:An algorithm that makes use of a brute force search iscalled a brute force algorithm.Often a brute force algorithm can be improved if we findsome way to reduce its candidates.

Memoisation:Memoisation is a technique where the function“remembers” previously calculated solutions.When there are overlapping subproblems this improvescomputation speed considerably by reducing the number ofrecalculations needed to be done.There are two issues that need to be considered whenusing memoisation in real life.

Roland Elliott Brute Force and Advanced Recursion

Page 39: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Brute Force and Memoisation

Brute force algorithms:An algorithm that makes use of a brute force search iscalled a brute force algorithm.Often a brute force algorithm can be improved if we findsome way to reduce its candidates.

Memoisation:Memoisation is a technique where the function“remembers” previously calculated solutions.When there are overlapping subproblems this improvescomputation speed considerably by reducing the number ofrecalculations needed to be done.There are two issues that need to be considered whenusing memoisation in real life.

Roland Elliott Brute Force and Advanced Recursion

Page 40: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Outline

1 Introduction

2 Brute Force

3 Memoisation

4 Summary up to now

5 Advanced ProblemsProblem 1: Minimal Path SumProblem 2: Eight Queens

Roland Elliott Brute Force and Advanced Recursion

Page 41: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Problem

Problem Given an nxn grid filled with positive integers findthe minimal path sum from the top left cell to the bottom rightcell.Example The red numbers describe the path with minimalsum in the below grid:

131 673 234 103 18201 96 342 965 150630 803 746 422 111537 699 497 121 956805 732 524 37 331

Problem 81 from Project Euler (www.projecteuler.net)

Roland Elliott Brute Force and Advanced Recursion

Page 42: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Solution

We define mps(x , y) to be the minimal path sum from cells(0,0) to (x,y). Also we let grid [x ][y ] be equal to the value ofcell (x,y).It is very important that we note the restriction onmovement: at each point we can only move right or downIf we consider some arbitrary cell (x,y) then the minimalpath could have only come from the cell above it or the cellto its left:

(x-1, y-1) (x, y-1) or (x-1, y-1) (x, y-1)(x-1, y) (x,y) (x-1, y) (x,y)

To minimise mps(x , y) we need to take the path from thecell with the smallest minimal path sum.

Roland Elliott Brute Force and Advanced Recursion

Page 43: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Solution

So we have the following function for mps(x , y):

mps(x , y) = grid [x ][y ]+ min(mps(x − 1, y),mps(x , y − 1))

This looks a lot like a recursive function. But what is itsbase case?The base case is mps(0,0), because we always know that:

mps(0,0) = grid [0][0]

Also note that if x = 0 then the path can only come fromthe left and it y = 0 the path can only come from above.

Roland Elliott Brute Force and Advanced Recursion

Page 44: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Solution

So we have the following function for mps(x , y):

mps(x , y) = grid [x ][y ]+ min(mps(x − 1, y),mps(x , y − 1))

This looks a lot like a recursive function. But what is itsbase case?The base case is mps(0,0), because we always know that:

mps(0,0) = grid [0][0]

Also note that if x = 0 then the path can only come fromthe left and it y = 0 the path can only come from above.

Roland Elliott Brute Force and Advanced Recursion

Page 45: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Solution

So we have the following function for mps(x , y):

mps(x , y) = grid [x ][y ]+ min(mps(x − 1, y),mps(x , y − 1))

This looks a lot like a recursive function. But what is itsbase case?The base case is mps(0,0), because we always know that:

mps(0,0) = grid [0][0]

Also note that if x = 0 then the path can only come fromthe left and it y = 0 the path can only come from above.

Roland Elliott Brute Force and Advanced Recursion

Page 46: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Solution

So we have the following function for mps(x , y):

mps(x , y) = grid [x ][y ]+ min(mps(x − 1, y),mps(x , y − 1))

This looks a lot like a recursive function. But what is itsbase case?The base case is mps(0,0), because we always know that:

mps(0,0) = grid [0][0]

Also note that if x = 0 then the path can only come fromthe left and it y = 0 the path can only come from above.

Roland Elliott Brute Force and Advanced Recursion

Page 47: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Solution

So we have the following function for mps(x , y):

mps(x , y) = grid [x ][y ]+ min(mps(x − 1, y),mps(x , y − 1))

This looks a lot like a recursive function. But what is itsbase case?The base case is mps(0,0), because we always know that:

mps(0,0) = grid [0][0]

Also note that if x = 0 then the path can only come fromthe left and it y = 0 the path can only come from above.

Roland Elliott Brute Force and Advanced Recursion

Page 48: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Solution Code

The solution code looks like this:

function mps(x,y):if x == 0 and y == 0:

return grid[x][y]else if x == 0:

return grid[x][y] + mps(x,y-1)else if y == 0:

return grid[x][y] + mps(x-1,y)else:

return grid[x][y] + min(mps(x-1, y),mps(x, y-1))

Roland Elliott Brute Force and Advanced Recursion

Page 49: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Improved Solution

What is the problem with this solution?Answer: We have overlapping subproblems like in thefibonacci problem. This means that many of the cells aregoing to recalculated over and over again.How do we fix this?Answer: Memoisation!

Roland Elliott Brute Force and Advanced Recursion

Page 50: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Improved Solution

What is the problem with this solution?Answer: We have overlapping subproblems like in thefibonacci problem. This means that many of the cells aregoing to recalculated over and over again.How do we fix this?Answer: Memoisation!

Roland Elliott Brute Force and Advanced Recursion

Page 51: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Improved Solution

What is the problem with this solution?Answer: We have overlapping subproblems like in thefibonacci problem. This means that many of the cells aregoing to recalculated over and over again.How do we fix this?Answer: Memoisation!

Roland Elliott Brute Force and Advanced Recursion

Page 52: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Improved Solution

What is the problem with this solution?Answer: We have overlapping subproblems like in thefibonacci problem. This means that many of the cells aregoing to recalculated over and over again.How do we fix this?Answer: Memoisation!

Roland Elliott Brute Force and Advanced Recursion

Page 53: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Minimal Path Sum: Improved Solution Code

answers = {}function mps(x,y):

if (x,y) not in answers:ans = -1if x == 0 and y == 0:

ans = grid[x][y]else if x == 0:

ans = grid[x][y] + mps(x, y-1)else if y == 0:

ans = grid[x][y] + mps(x-1, y)else:

ans = grid[x][y] + min(mps(x-1, y),mps(x, y-1))

answers[(x,y)] = ansreturn answers[(x,y)]

Roland Elliott Brute Force and Advanced Recursion

Page 54: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Problem

Problem Find a way to lay out 8 queens on a standard chessboard such that no queen can capture another in a single move.Example In the below board layout the first and thrid queenscan capture each other:

8 q7 q6 q5 q

a b c d e f g h

Roland Elliott Brute Force and Advanced Recursion

Page 55: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

The solution we’re going to use is a recursive brute forcesolution.Before we get there, though, we need a way to check if agiven queen placement is ok:

We take in a list of the positions of the queens and the (x,y)co-ordinate of cell we want to place the queen in.For example the list of the above example is [(1,0), (5,1),(3,2), (6,3)]For each queen we check if it would be able to capture thequeen at the given (x,y)

Roland Elliott Brute Force and Advanced Recursion

Page 56: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

The solution we’re going to use is a recursive brute forcesolution.Before we get there, though, we need a way to check if agiven queen placement is ok:

We take in a list of the positions of the queens and the (x,y)co-ordinate of cell we want to place the queen in.For example the list of the above example is [(1,0), (5,1),(3,2), (6,3)]For each queen we check if it would be able to capture thequeen at the given (x,y)

Roland Elliott Brute Force and Advanced Recursion

Page 57: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

The solution we’re going to use is a recursive brute forcesolution.Before we get there, though, we need a way to check if agiven queen placement is ok:

We take in a list of the positions of the queens and the (x,y)co-ordinate of cell we want to place the queen in.For example the list of the above example is [(1,0), (5,1),(3,2), (6,3)]For each queen we check if it would be able to capture thequeen at the given (x,y)

Roland Elliott Brute Force and Advanced Recursion

Page 58: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

function okToPlaceQueen(queens, x, y):for i = 0 to len(queens)-1:

if queens[i][0] == x or queens[i][1] == y:return False

if queens[i][0]-x == queens[i][1]-y:return False

if queens[i][0]-x == -(queens[i][1]-y):return False

return True

Roland Elliott Brute Force and Advanced Recursion

Page 59: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

So what does the solution look like?Using recursion we could easily try every configuration ofqueens on the board. But this will take very, very, very long.If we realise that no two queens will ever be on the samehorizontal line we can reduce our candidate solutions to avery little amount.Solution idea:

Place queen on first valid square on line n.Check if we can place queens on all lines greater than n.

If we can then this is a valid placementIf not, then move the queen to the next valid square on theline and try again for all lines greater than n.

Roland Elliott Brute Force and Advanced Recursion

Page 60: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

So what does the solution look like?Using recursion we could easily try every configuration ofqueens on the board. But this will take very, very, very long.If we realise that no two queens will ever be on the samehorizontal line we can reduce our candidate solutions to avery little amount.Solution idea:

Place queen on first valid square on line n.Check if we can place queens on all lines greater than n.

If we can then this is a valid placementIf not, then move the queen to the next valid square on theline and try again for all lines greater than n.

Roland Elliott Brute Force and Advanced Recursion

Page 61: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

So what does the solution look like?Using recursion we could easily try every configuration ofqueens on the board. But this will take very, very, very long.If we realise that no two queens will ever be on the samehorizontal line we can reduce our candidate solutions to avery little amount.Solution idea:

Place queen on first valid square on line n.Check if we can place queens on all lines greater than n.

If we can then this is a valid placementIf not, then move the queen to the next valid square on theline and try again for all lines greater than n.

Roland Elliott Brute Force and Advanced Recursion

Page 62: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution

So what does the solution look like?Using recursion we could easily try every configuration ofqueens on the board. But this will take very, very, very long.If we realise that no two queens will ever be on the samehorizontal line we can reduce our candidate solutions to avery little amount.Solution idea:

Place queen on first valid square on line n.Check if we can place queens on all lines greater than n.

If we can then this is a valid placementIf not, then move the queen to the next valid square on theline and try again for all lines greater than n.

Roland Elliott Brute Force and Advanced Recursion

Page 63: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution Code

The following code will return a list of positions for thequeens satifying the problem statement.

function solve(y, queens):if y >= 8:

return queensfor x = 0 to 7:

if okToPlaceQueen(queens, x, y):newQueens = queens + [(x,y)]ret = solve(y+1, newQueens)if ret != []:

return retreturn []

Roland Elliott Brute Force and Advanced Recursion

Page 64: UCT Algorithm Circle: Brute Force and Advanced Recursion · recursion, as we’ll soon see in one of our examples. Roland Elliott Brute Force and Advanced Recursion. university-logo

university-logo

IntroductionBrute Force

MemoisationSummary up to nowAdvanced Problems

Problem 1: Minimal Path SumProblem 2: Eight Queens

Eight Queens: Solution Board

The code above gives this as a solution:

8 q7 q6 q5 q4 q3 q2 q1 q

a b c d e f g h

Roland Elliott Brute Force and Advanced Recursion