recursion as a problem-solving technique

29
Recursion as a Recursion as a Problem-Solving Problem-Solving Technique Technique Chapter 5 Chapter 5

Upload: shilah

Post on 08-Jan-2016

36 views

Category:

Documents


2 download

DESCRIPTION

Recursion as a Problem-Solving Technique. Chapter 5. Chapter 2 presented the basic concepts of recursion. This chapter introduces you to two new concepts, backtracking and formal grammars. Backtracking is a problem-solving technique that involves guesses at a solution. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursion as a Problem-Solving Technique

Recursion as a Problem-Recursion as a Problem-Solving TechniqueSolving Technique

Chapter 5Chapter 5

Page 2: Recursion as a Problem-Solving Technique

CS 308 2Chapter 5 -- Recursion as a Problem-Solving Technique

Chapter 2 presented the basic concepts of Chapter 2 presented the basic concepts of recursion.recursion.

This chapter introduces you to two new This chapter introduces you to two new concepts, backtracking and formal grammars.concepts, backtracking and formal grammars. Backtracking is a problem-solving technique that Backtracking is a problem-solving technique that

involves guesses at a solution.involves guesses at a solution. Formal grammars enable you to define, for Formal grammars enable you to define, for

example, syntactically correct algebraic example, syntactically correct algebraic expressions.expressions.

This chapter concludes with a discussion of the This chapter concludes with a discussion of the close relationship between recursion and close relationship between recursion and mathematical induction.mathematical induction.

Page 3: Recursion as a Problem-Solving Technique

CS 308 3Chapter 5 -- Recursion as a Problem-Solving Technique

BacktrackingBacktracking

This section considers an organized way to This section considers an organized way to make successive guesses at a solution.make successive guesses at a solution. If a particular guess leads to a dead end, you back If a particular guess leads to a dead end, you back

up to that guess and replace it with a different up to that guess and replace it with a different guess.guess.

You can combine recursion and backtracking to You can combine recursion and backtracking to solve the following problemsolve the following problem

Page 4: Recursion as a Problem-Solving Technique

CS 308 4Chapter 5 -- Recursion as a Problem-Solving Technique

The Eight Queens ProblemThe Eight Queens Problem Given: Given:

a chessboard (8x8 grid)a chessboard (8x8 grid) Problem: Problem:

Place 8 queens on the board in a non attacking fashion.Place 8 queens on the board in a non attacking fashion. Remember: a Queen can attack in her row, column, or Remember: a Queen can attack in her row, column, or

along her diagonal.along her diagonal.

Page 5: Recursion as a Problem-Solving Technique

CS 308 5Chapter 5 -- Recursion as a Problem-Solving Technique

One strategy is to guess at a solution.One strategy is to guess at a solution. However there are 4,426,165,368 ways to arrange 8 However there are 4,426,165,368 ways to arrange 8

queens on a chessboard.queens on a chessboard. I think you might get tired trying all of these!I think you might get tired trying all of these!

A simple observation can eliminate many A simple observation can eliminate many arrangements:arrangements:

No queen can reside on a row or column that contains No queen can reside on a row or column that contains another queen, another queen,

Or each row and column can contain exactly one queen.Or each row and column can contain exactly one queen. This leaves 8! Arrangements to evaluate (8!=40,320)This leaves 8! Arrangements to evaluate (8!=40,320)

Let’s try something elseLet’s try something else

Page 6: Recursion as a Problem-Solving Technique

CS 308 6Chapter 5 -- Recursion as a Problem-Solving Technique

Let’s provide some organization for the guessing Let’s provide some organization for the guessing strategystrategy

Place 1 Queen per column Place 1 Queen per column (beginning with the first square of column 1)(beginning with the first square of column 1)

Next you consider column 2, Next you consider column 2, you eliminate its first square (because column 1 has a Queen you eliminate its first square (because column 1 has a Queen

on row 1)on row 1) you eliminate its second square (because of a diagonal attack)you eliminate its second square (because of a diagonal attack) you finally place the queen on the third squareyou finally place the queen on the third square

Now consider column 3Now consider column 3 ………………

Page 7: Recursion as a Problem-Solving Technique

CS 308 7Chapter 5 -- Recursion as a Problem-Solving Technique

After placing 5 Queens you end up at this configuration:After placing 5 Queens you end up at this configuration: And you are stuckAnd you are stuck

So, what should you do?So, what should you do? You can’t place a queen in column 6, so back up to column 5 You can’t place a queen in column 6, so back up to column 5

and go to the next possible solution.and go to the next possible solution.

Page 8: Recursion as a Problem-Solving Technique

CS 308 8Chapter 5 -- Recursion as a Problem-Solving Technique

The next possible location in The next possible location in Column 5 is the last row.Column 5 is the last row.

When you consider column 6, When you consider column 6, there are still no solutions there are still no solutions available, so you back up… 5 has available, so you back up… 5 has no more options, so you back up to no more options, so you back up to column 4.column 4.

Page 9: Recursion as a Problem-Solving Technique

CS 308 9Chapter 5 -- Recursion as a Problem-Solving Technique

So, let’s write the code forSo, let’s write the code for bool placeQueens(int currColumn)bool placeQueens(int currColumn)

Page 10: Recursion as a Problem-Solving Technique

CS 308 10Chapter 5 -- Recursion as a Problem-Solving Technique

bool placeQueens(int currColumn)bool placeQueens(int currColumn){{

if (currColumn > BOARD_SIZE)if (currColumn > BOARD_SIZE)return true;return true;

else{else{bool queenPlaced = false;bool queenPlaced = false;int row = 1;int row = 1;while (!queenPlaced && ( row < BOARD_SIZE){while (!queenPlaced && ( row < BOARD_SIZE){

if (isUnderAttack(row,currColumn)if (isUnderAttack(row,currColumn)row++;row++;

else{else{setQueen(row,currColumn)setQueen(row,currColumn)queenPlaced=placeQueens(currColumn+1)queenPlaced=placeQueens(currColumn+1)if(!queenPlaced){if(!queenPlaced){ removeQueen(row,currColumn);removeQueen(row,currColumn); row++row++}}

}}}}return queenPlaced;return queenPlaced;

}}}}

Page 11: Recursion as a Problem-Solving Technique

CS 308 11Chapter 5 -- Recursion as a Problem-Solving Technique

Here is one solutionHere is one solution

Page 12: Recursion as a Problem-Solving Technique

CS 308 12Chapter 5 -- Recursion as a Problem-Solving Technique

Defining LanguagesDefining Languages

What is a language?What is a language? A language is nothing more than a set of strings of A language is nothing more than a set of strings of

symbols.symbols. C++Programs = {strings w: w is a syntactically C++Programs = {strings w: w is a syntactically

correct C++ program}correct C++ program} Note: all programs a re strings, BUT not all strings are Note: all programs a re strings, BUT not all strings are

programs.programs. Now we need to discuss the rules for forming the Now we need to discuss the rules for forming the

strings in a language strings in a language This is a Grammar.This is a Grammar.

Page 13: Recursion as a Problem-Solving Technique

CS 308 13Chapter 5 -- Recursion as a Problem-Solving Technique

The Basics of GrammarsThe Basics of Grammars A grammar uses several special symbolsA grammar uses several special symbols

x | y means x or yx | y means x or y xy means x followed by yxy means x followed by y <word> means any instance of word that definition <word> means any instance of word that definition

defines.defines. Example: C++IdsExample: C++Ids

C++Ids = {w: w is a legal C++ Identifier}C++Ids = {w: w is a legal C++ Identifier} So, what makes a legal identifier?So, what makes a legal identifier?

A letter, followed by 0 or more letters or digits.A letter, followed by 0 or more letters or digits. Remember that _ is a letter.Remember that _ is a letter.

Page 14: Recursion as a Problem-Solving Technique

CS 308 14Chapter 5 -- Recursion as a Problem-Solving Technique

Example: C++Ids (cont)Example: C++Ids (cont) There are many ways to represent this. One is a There are many ways to represent this. One is a

Syntax DiagramSyntax Diagram

A syntax Diagram is easy for people to use, but a grammar is A syntax Diagram is easy for people to use, but a grammar is a better starting point if you want to write a function that will a better starting point if you want to write a function that will recognize an identifierrecognize an identifier

Page 15: Recursion as a Problem-Solving Technique

CS 308 15Chapter 5 -- Recursion as a Problem-Solving Technique

Example: C++Ids (cont)Example: C++Ids (cont) A grammar for the language C++Ids isA grammar for the language C++Ids is

<identifier> = <letter> | <identifier> <letter> | <identifier> = <letter> | <identifier> <letter> | <identifier><digit><identifier><digit>

<letter> = a|b|...|z|A|B|...|Z|_<letter> = a|b|...|z|A|B|...|Z|_ <digit> = 0|1|...|9<digit> = 0|1|...|9

This definition reads as follows:This definition reads as follows: An identifier is a letter, or an identifier followed by a letter, An identifier is a letter, or an identifier followed by a letter,

or an identifier followed by a digit.or an identifier followed by a digit.

Note that this grammar is recursive, as are many Note that this grammar is recursive, as are many grammars.grammars.

Page 16: Recursion as a Problem-Solving Technique

CS 308 16Chapter 5 -- Recursion as a Problem-Solving Technique

So, now we need to write code to recognize strings in So, now we need to write code to recognize strings in this language.this language. bool isId(string w)bool isId(string w)

This function is recursive, This function is recursive, so what is the base case.so what is the base case. How is the recursive call done?How is the recursive call done?

Page 17: Recursion as a Problem-Solving Technique

CS 308 17Chapter 5 -- Recursion as a Problem-Solving Technique

Here is a trace of this function for the string A2BHere is a trace of this function for the string A2B

Page 18: Recursion as a Problem-Solving Technique

CS 308 18Chapter 5 -- Recursion as a Problem-Solving Technique

Two Simple LanguagesTwo Simple Languages Now we want to consider two more simple Now we want to consider two more simple

examples of languages, their grammars, and the examples of languages, their grammars, and the resulting recognition algorithmsresulting recognition algorithms

The first one is PalindromesThe first one is Palindromes First, what is a palindrome?First, what is a palindrome? Palindromes = {w : w reads the same left to right as Palindromes = {w : w reads the same left to right as

right to left}right to left} Specifically, w is a palindrome if and only if:Specifically, w is a palindrome if and only if:

The first and last characters of w are the same ANDThe first and last characters of w are the same AND w minus its first and last characters is a palindromew minus its first and last characters is a palindrome

Page 19: Recursion as a Problem-Solving Technique

CS 308 19Chapter 5 -- Recursion as a Problem-Solving Technique

Palindromes (cont)Palindromes (cont) So the grammar is....So the grammar is....

<pal> = empty_string | <ch> | a<pal>a | b<pal>b | ... | <pal> = empty_string | <ch> | a<pal>a | b<pal>b | ... | Z<pal>ZZ<pal>Z

<ch> = a|b|...|z|A|B|...|Z<ch> = a|b|...|z|A|B|...|Z

Now, we need to write the code:Now, we need to write the code: bool isPal(string w)bool isPal(string w)

Page 20: Recursion as a Problem-Solving Technique

CS 308 20Chapter 5 -- Recursion as a Problem-Solving Technique

The second example is strings of the form AThe second example is strings of the form AnnBBnn

This is the set of strings of n consecutive A’s followed This is the set of strings of n consecutive A’s followed by n consecutive B’sby n consecutive B’s

This grammar is actually very similar to the grammar This grammar is actually very similar to the grammar for palindromes. for palindromes. You strip away both the first and last characters and check to You strip away both the first and last characters and check to

see that the first is an A and the last is a Bsee that the first is an A and the last is a B <legal-word> = empty_string | A<legal-word>B<legal-word> = empty_string | A<legal-word>B

Now we need to write the code:Now we need to write the code: bool isAnBn(string w)bool isAnBn(string w)

Page 21: Recursion as a Problem-Solving Technique

CS 308 21Chapter 5 -- Recursion as a Problem-Solving Technique

Algebraic ExpressionsAlgebraic Expressions One of the tasks a compiler must perform is to recognize One of the tasks a compiler must perform is to recognize

and evaluate algebraic expressionsand evaluate algebraic expressions

Consider the following:Consider the following: y = x+z*(w/k+z*(7*6));y = x+z*(w/k+z*(7*6)); First: is the RHS a syntactically legal algebraic expression?First: is the RHS a syntactically legal algebraic expression? If so, the compiler must then indicate how to compute the If so, the compiler must then indicate how to compute the

expressions value.expressions value.

There are several common definitions for “syntactically There are several common definitions for “syntactically legal” legal”

some definitions require an expression to be fully parenthesized: some definitions require an expression to be fully parenthesized: ((a*b)*c)((a*b)*c)

This section presents 3 different languages for algebraic This section presents 3 different languages for algebraic expressions.expressions.

Page 22: Recursion as a Problem-Solving Technique

CS 308 22Chapter 5 -- Recursion as a Problem-Solving Technique

Infix, Prefix, and postfix notationInfix, Prefix, and postfix notation Definitions:Definitions:

Infix: operator appears between its operandsInfix: operator appears between its operands 3 + 23 + 2 We need associativity rules (precedence) and the use of We need associativity rules (precedence) and the use of

parentheses to avoid ambiguity: a + b * cparentheses to avoid ambiguity: a + b * c ten there is a / b * cten there is a / b * c

Prefix: operator precedes its operandsPrefix: operator precedes its operands + 3 2+ 3 2

Postfix: operator follows its operandsPostfix: operator follows its operands 3 2 +3 2 +

We will come back to more Infix discussions later We will come back to more Infix discussions later (Chapter 6)(Chapter 6)

Page 23: Recursion as a Problem-Solving Technique

CS 308 23Chapter 5 -- Recursion as a Problem-Solving Technique

Prefix expressionsPrefix expressions A grammar that defines the language of all prefix expressions A grammar that defines the language of all prefix expressions

is:is: <prefix> = <identifier> | <operator><prefix><prefix><prefix> = <identifier> | <operator><prefix><prefix> <operator> = + | - | * | /<operator> = + | - | * | / <identifier> = a | b | ... | z<identifier> = a | b | ... | z

Now you can write a recursive algorithm that recognizes Now you can write a recursive algorithm that recognizes whether a string is a prefix expressionwhether a string is a prefix expression What is the base case?What is the base case? What is the recursive call?What is the recursive call?

bool isPre( string w)bool isPre( string w)

Page 24: Recursion as a Problem-Solving Technique

CS 308 24Chapter 5 -- Recursion as a Problem-Solving Technique

Postfix expressionsPostfix expressions A grammar that defines the language of all postfix A grammar that defines the language of all postfix

expressions is:expressions is: <postfix> = <identifier> | <postfix><postfix><operator> <postfix> = <identifier> | <postfix><postfix><operator> <operator> = + | - | * | /<operator> = + | - | * | / <identifier> = a | b | ... | z<identifier> = a | b | ... | z

Now you can write a recursive algorithm that converts from Now you can write a recursive algorithm that converts from postfix to prefix.postfix to prefix. What is the base case?What is the base case? What is the recursive call?What is the recursive call?

string convertPost2Pre(string w)string convertPost2Pre(string w)

Page 25: Recursion as a Problem-Solving Technique

CS 308 25Chapter 5 -- Recursion as a Problem-Solving Technique

The Relationship between Recursion The Relationship between Recursion and Mathematical Inductionand Mathematical Induction

A very strong relationship exists between A very strong relationship exists between recursion and mathematical induction. recursion and mathematical induction.

Given the similarities, it should not be Given the similarities, it should not be surprising that induction is often employed to surprising that induction is often employed to prove properties about recursive algorithmsprove properties about recursive algorithms

Page 26: Recursion as a Problem-Solving Technique

CS 308 26Chapter 5 -- Recursion as a Problem-Solving Technique

The Correctness of the Recursive Factorial The Correctness of the Recursive Factorial FunctionFunction

The proof is by induction on nThe proof is by induction on n BasisBasis

show the property is true for n=0show the property is true for n=0 fact(0)= 1;fact(0)= 1;

Inductive HypothesisInductive Hypothesis assume that the property is true for an arbitrary kassume that the property is true for an arbitrary k

Inductive ConclusionInductive Conclusion show it is true for n=k+1show it is true for n=k+1

fact(k+1) = (k+1) * fact(k)fact(k+1) = (k+1) * fact(k)

Page 27: Recursion as a Problem-Solving Technique

CS 308 27Chapter 5 -- Recursion as a Problem-Solving Technique

The Cost of Towers of HanoiThe Cost of Towers of Hanoi If you begin with N disks, how many moves does If you begin with N disks, how many moves does

it take?it take? When N = 1 this is easy --- 1 moveWhen N = 1 this is easy --- 1 move When N > 1 the number is not so apparentWhen N > 1 the number is not so apparent

Moves(N) = Moves(N-1) + Moves(1) + Moves(N-1)Moves(N) = Moves(N-1) + Moves(1) + Moves(N-1) Moves(N) = 2*Moves(N-1) + Moves(1)Moves(N) = 2*Moves(N-1) + Moves(1) This is a recurrence relationship.This is a recurrence relationship.

You can compute a closed form algebraic formula for You can compute a closed form algebraic formula for this, but the techniques are not relevant to this course this, but the techniques are not relevant to this course (they are covered in CS 465-Algorithms)(they are covered in CS 465-Algorithms) The solution is moves(N)=2The solution is moves(N)=2NN-1-1 The proof of this is by induction on NThe proof of this is by induction on N

Page 28: Recursion as a Problem-Solving Technique

CS 308 28Chapter 5 -- Recursion as a Problem-Solving Technique

BasisBasis show true for N=1, here 2show true for N=1, here 211-2=1-2=1

Inductive hypothesisInductive hypothesis assume it is true for N=Kassume it is true for N=K

Inductive conclusionInductive conclusion show that it is true for N=K+1show that it is true for N=K+1 moves(k+1) = 2*moves(k)+1moves(k+1) = 2*moves(k)+1 = 2*(2= 2*(2kk-1)+1-1)+1 = 2= 2k+1k+1-1-1

Page 29: Recursion as a Problem-Solving Technique

CS 308 29Chapter 5 -- Recursion as a Problem-Solving Technique