© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
COSC 2006 Chapter 7 Stacks III
Recursion B.Ramamurthy Chapter 4. Ramamurthy Introduction  Recursion is one of most powerful methods of solution available to computer scientists. 
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
CMPT 225 Stacks-part2.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Section 5.2 Defining Languages. © 2005 Pearson Addison-Wesley. All rights reserved5-2 Defining Languages A language –A set of strings of symbols –Examples:
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Chapter 5 Recursion as a Problem-Solving Technique.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Data Structures Using C++ 2E Chapter 6 Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Data Structures Using C++ 2E Chapter 6 Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
COSC2007 Data Structures II
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
EXAMPLES OF RECURSION Towers of Hanoi Writing Linked Lists Backwards Recursive Insert 8 Queens Recognizing Simple Languages Prefix Expressions Conversion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Recursion as a Problem-Solving Technique. What is recursion? There are two types of recursive functions: Direct and Indirect Explain… There are two basic.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
1 In this puzzle, the player begins with n disks of decreasing diameter placed one on top of the other on one of three pegs of the game board. The player.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
Figure 5.1 a) Five queens that cannot attack each other, but that can attack all of column 6; b) backtracking to column 5 to try another square for the.
Principles of Programming and Software Engineering
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Java Software Structures: John Lewis & Joseph Chase
Chapter 6 Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Ch. 6 Recursion as a Problem Solving Technique
Recursion as a Problem-Solving Technique
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Chapters 6 and 7 Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique

© 2006 Pearson Addison-Wesley. All rights reserved6-2 Backtracking –A strategy for guessing at a solution and backing up when an impasse is reached Recursion and backtracking can be combined to solve problems

© 2006 Pearson Addison-Wesley. All rights reserved6-3 The Eight Queens Problem Problem –Place eight queens on the chessboard so that no queen can attack any other queen Strategy: guess at a solution –There are 4,426,165,368 ways to arrange 8 queens on a chessboard of 64 squares

© 2006 Pearson Addison-Wesley. All rights reserved6-4 The Eight Queens Problem An observation that eliminates many arrangements from consideration –No queen can reside in a row or a column that contains another queen Now: only 40,320 arrangements of queens to be checked for attacks along diagonals

© 2006 Pearson Addison-Wesley. All rights reserved6-5 The Eight Queens Problem Providing organization for the guessing strategy –Place queens one column at a time –If you reach an impasse, backtrack to the previous column

© 2006 Pearson Addison-Wesley. All rights reserved6-6 The Eight Queens Problem Figure 6-1 a) Five queens that cannot attack each other, but that can attack all of column 6; b) backtracking to column 5 to try another square for the queen; c) backtracking to column 4 to try another square for the queen and then considering column 5 again

© 2006 Pearson Addison-Wesley. All rights reserved6-7 The Eight Queens Problem A recursive algorithm that places a queen in a column –Base case If there are no more columns to consider –You are finished –Recursive step If you successfully place a queen in the current column –Consider the next column If you cannot place a queen in the current column –You need to backtrack

© 2006 Pearson Addison-Wesley. All rights reserved6-8 The Eight Queens Problem Figure 6-2 A solution to the Eight Queens problem

© 2006 Pearson Addison-Wesley. All rights reserved6-9 Defining Languages A language –A set of strings of symbols –Examples: English, Java –If a Java program is one long string of characters, the language JavaPrograms is defined as JavaPrograms = {strings w : w is a syntactically correct Java program}

© 2006 Pearson Addison-Wesley. All rights reserved6-10 Defining Languages A language does not have to be a programming or a communication language –Example The set of algebraic expressions AlgebraicExpressions = {w : w is an algebraic expression}

© 2006 Pearson Addison-Wesley. All rights reserved6-11 Defining Languages Grammar –States the rules for forming the strings in a language Benefit of recursive grammars –Ease of writing a recognition algorithm for the language A recognition algorithm determines whether a given string is in the language

© 2006 Pearson Addison-Wesley. All rights reserved6-12 The Basics of Grammars Symbols used in grammars –x | y means x or y –x y means x followed by y (In x y, the symbol means concatenate, or append) – means any instance of word that the definition defines

© 2006 Pearson Addison-Wesley. All rights reserved6-13 The Basics of Grammars Java identifiers –A Java identifier begins with a letter and is followed by zero or more letters and digits Figure 6-3 A syntax diagram for Java identifiers

© 2006 Pearson Addison-Wesley. All rights reserved6-14 The Basics of Grammars Java identifiers –Language JavaIds = {w : w is a legal Java identifier} –Grammar = | | = a | b | … | z | A | B | …| Z | _ | $ = 0 | 1 | … | 9

© 2006 Pearson Addison-Wesley. All rights reserved6-15 The Basics of Grammars Recognition algorithm isId(w) if (w is of length 1) { if (w is a letter) { return true } else { return false } else if (the last character of w is a letter or a digit) { return isId(w minus its last character) } else { return false }

© 2006 Pearson Addison-Wesley. All rights reserved6-16 Two Simple Languages: Palindromes A string that reads the same from left to right as it does from right to left Examples: radar, deed Language Palindromes = {w : w reads the same left to right as right to left}

© 2006 Pearson Addison-Wesley. All rights reserved6-17 Palindromes Grammar = empty string | | a a | b b | … | Z Z = a | b | … | z | A | B | … | Z

© 2006 Pearson Addison-Wesley. All rights reserved6-18 Palindromes Recognition algorithm isPal(w) if (w is the empty string or w is of length 1) { return true } else if (w’s first and last characters are the same letter ) { return isPal(w minus its first and last characters) } else { return false }

© 2006 Pearson Addison-Wesley. All rights reserved6-19 Strings of the form A n B n A n B n –The string that consists of n consecutive A’s followed by n consecutive B’s Language L = {w : w is of the form A n B n for some n ≥ 0} Grammar = empty string | A B

© 2006 Pearson Addison-Wesley. All rights reserved6-20 Strings of the form A n B n Recognition algorithm isAnBn(w) if (the length of w is zero) { return true } else if (w begins with the character A and ends with the character B) { return isAnBn(w minus its first and last characters) } else { return false }

© 2006 Pearson Addison-Wesley. All rights reserved6-21 Algebraic Expressions Three languages for algebraic expressions –Infix expressions An operator appears between its operands Example: a + b –Prefix expressions An operator appears before its operands Example: + a b –Postfix expressions An operator appears after its operands Example: a b +

© 2006 Pearson Addison-Wesley. All rights reserved6-22 Algebraic Expressions To convert a fully parenthesized infix expression to a prefix form –Move each operator to the position marked by its corresponding open parenthesis –Remove the parentheses –Example Infix expression: ((a + b) * c Prefix expression: * + a b c

© 2006 Pearson Addison-Wesley. All rights reserved6-23 Algebraic Expressions To convert a fully parenthesized infix expression to a postfix form –Move each operator to the position marked by its corresponding closing parenthesis –Remove the parentheses –Example Infix form: ((a + b) * c) Postfix form: a b + c *

© 2006 Pearson Addison-Wesley. All rights reserved6-24 Algebraic Expressions Prefix and postfix expressions –Never need Precedence rules Association rules Parentheses – Have Simple grammar expressions Straightforward recognition and evaluation algorithms

© 2006 Pearson Addison-Wesley. All rights reserved6-25 Prefix Expressions Grammar = | = + | - | * | / = a | b | … | z A recognition algorithm isPre() size = length of expression strExp lastChar = endPre(0, size – 1) if (lastChar >= 0 and lastChar == size-1 { return true } else { return false }

© 2006 Pearson Addison-Wesley. All rights reserved6-26 Prefix Expressions An algorithm that evaluates a prefix expression evaluatePrefix(strExp) ch = first character of expression strExp Delete first character from strExp if (ch is an identifier) { return value of the identifier } else if (ch is an operator named op) { operand1 = evaluatePrefix(strExp) operand2 = evaluatePrefix(strExp) return operand1 op operand2 }

© 2006 Pearson Addison-Wesley. All rights reserved6-27 Postfix Expressions Grammar = | = + | - | * | / = a | b | … | z At high-level, an algorithm that converts a prefix expression to postfix form if (exp is a single letter) { return exp } else { return postfix(prefix1) + postfix(prefix2) + operator }

© 2006 Pearson Addison-Wesley. All rights reserved6-28 Postfix Expressions A recursive algorithm that converts a prefix expression to postfix form convert(pre) ch = first character of pre Delete first character of pre if (ch is a lowercase letter) { return ch as a string } else { postfix1 = convert(pre) postfix2 = convert(pre) return postfix1 + postfix2 + ch }

© 2006 Pearson Addison-Wesley. All rights reserved6-29 Fully Parenthesized Expressions To avoid ambiguity, infix notation normally requires –Precedence rules –Rules for association –Parentheses Fully parenthesized expressions do not require –Precedence rules –Rules for association

© 2006 Pearson Addison-Wesley. All rights reserved6-30 Fully Parenthesized Expressions Fully parenthesized expressions –A simple grammar = | ( ) = + | - | * | / = a | b | … | z –Inconvenient for programmers

© 2006 Pearson Addison-Wesley. All rights reserved6-31 The Relationship Between Recursion and Mathematical Induction A strong relationship exists between recursion and mathematical induction Induction can be used to –Prove properties about recursive algorithms –Prove that a recursive algorithm performs a certain amount of work

© 2006 Pearson Addison-Wesley. All rights reserved6-32 The Correctness of the Recursive Factorial Method Pseudocode for a recursive method that computes the factorial of a nonnegative integer n fact(n) if (n is 0) { return 1 } else { return n * fact(n – 1) }

© 2006 Pearson Addison-Wesley. All rights reserved6-33 The Correctness of the Recursive Factorial Method Induction on n can prove that the method fact returns the values fact(0) = 0! = 1 fact(n) = n! = n * (n – 1) * (n – 2) * …* 1 if n > 0

© 2006 Pearson Addison-Wesley. All rights reserved6-34 The Cost of Towers of Hanoi Solution to the Towers of Hanoi problem solveTowers(count, source, destination, spare) if (count is 1) { Move a disk directly from source to destination } else { solveTowers(count-1, source, spare, destination) solveTowers(1, source, destination, spare) solveTowers(count-1, spare, destination, source) }

© 2006 Pearson Addison-Wesley. All rights reserved6-35 The Cost of Towers of Hanoi Question –If you begin with N disks, how many moves does solveTowers make to solve the problem? Let –moves(N) be the number of moves made starting with N disks When N = 1 –moves(1) = 1

© 2006 Pearson Addison-Wesley. All rights reserved6-36 The Cost of Towers of Hanoi When N > 1 moves(N) = moves(N – 1) + moves(1) + moves(N – 1) Recurrence relation for the number of moves that solveTowers requires for N disks moves(1) = 1 moves(N) = 2 * moves(N – 1) + 1 if N > 1

© 2006 Pearson Addison-Wesley. All rights reserved6-37 The Cost of Towers of Hanoi A closed-form formula for the number of moves that solveTowers requires for N disks moves(N) = 2 N – 1, for all N ≥ 1 Induction on N can provide the proof that moves(N) = 2 N – 1

© 2006 Pearson Addison-Wesley. All rights reserved6-38 Summary Backtracking is a solution strategy that involves both recursion and a sequence of guesses that ultimately lead to a solution A grammar is a device for defining a language –A language is a set of strings of symbols –A recognition algorithm for a language can often be based directly on the grammar of the language –Grammars are frequently recursive

© 2006 Pearson Addison-Wesley. All rights reserved6-39 Summary Different languages of algebraic expressions have their relative advantages and disadvantages –Prefix expressions –Postfix expressions –Infix expressions A close relationship exists between mathematical induction and recursion –Induction can be used to prove properties about a recursive algorithm