Chapter 5 Recursion as a Problem-Solving Technique

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Recursion B.Ramamurthy Chapter 4. Ramamurthy Introduction  Recursion is one of most powerful methods of solution available to computer scientists. 
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
CMPT 225 Stacks-part2.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
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.
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.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Induction and recursion
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 4: Induction and Recursion
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.
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
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.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
ALGORITHMS.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Chapter 4: Induction and Recursion
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Recursion Topic 5.
Stacks Chapter 6.
Intro to Computer Science II
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.
Induction and Recursion
CSE 311 Foundations of Computing I
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Chapter 6 Stacks.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
This Lecture Substitution model
Ch. 6 Recursion as a Problem Solving Technique
This Lecture Substitution model
B.Ramamurthy Chapter 9 CSE116A,B
Recursion as a Problem-Solving Technique
Copyright © Cengage Learning. All rights reserved.
This Lecture Substitution model
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Figure 6.1 Stack of cafeteria dishes. Figure 6.1 Stack of cafeteria dishes.
Recursion: The Mirrors
Presentation transcript:

Chapter 5 Recursion as a Problem-Solving Technique CS 302 - Data Structures Modified from authors’ slides

Defining Languages A language is Consider the C++ language A set of strings of symbols From a finite alphabet. Consider the C++ language The set of algebraic expressions forms a language A grammar states the rules of a language. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Basics of Grammars Special symbols x | y means x or y xy (and sometimes x • y ) means x followed by y < word > means any instance of word, where word is a symbol that must be defined elsewhere in the grammar. C++ Identifiers = {string s : s is a legal C++ identifier} Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Basics of Grammars A syntax diagram for C++ identifiers Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Recognition Algorithm for Identifiers Trace of isId("A2B") Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Recognition Algorithm for Identifiers Trace of isId("A2B") Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Two Simple Languages Palindromes Recursive definition of palindrome The first and last characters of s are the same s minus its first and last characters is a palindrome Grammar for the language of palindromes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Two Simple Languages A recognition algorithm for palindromes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Two Simple Languages Strings of the form AnBn Grammar for the language AnBn is Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Algebraic Expressions Compiler must recognize and evaluate algebraic expressions Determine if legal expression If legal, evaluate expression

Kinds of Algebraic Expressions Infix expressions Every binary operator appears between its operands This convention necessitates … Associativity rules Precedence rules Use of parentheses Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Kinds of Algebraic Expressions Prefix expression Operator appears before its operands Postfix expressions Operator appears after its operands Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions Grammar that defines language of all prefix expressions Recursive algorithm that recognizes whether string is a prefix expression Check if first character is an operator Remainder of string consists of two consecutive prefix expressions Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions endPre determines the end of a prefix expression Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Prefix Expressions endPre determines the end of a prefix expression Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Prefix Expressions Trace of endPre( "+/ab-cd",0) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions Trace of endPre( "+/ab-cd",0) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions Trace of endPre( "+/ab-cd",0) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions Trace of endPre( "+/ab-cd",0) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions Trace of endPre( "+/ab-cd",0) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions A recognition algorithm for prefix expressions Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions An algorithm to evaluate a prefix expression Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Prefix Expressions An algorithm to evaluate a prefix expression Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Grammar that defines the language of all postfix expressions An algorithm that converts a prefix expression to postfix form Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Postfix Expressions Recursive algorithm that converts a prefix expression to postfix form Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Fully Parenthesized Expressions Grammar for language of fully parenthesized algebraic expressions Most programming languages support definition of algebraic expressions Includes both precedence rules for operators and rules of association Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Backtracking Strategy for guessing at a solution and … Backing up when an impasse is reached Retracing steps in reverse order Trying a new sequence of steps Combine recursion and backtracking to solve problems Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Searching for an Airline Route Must find a path from some point of origin to some destination point Program to process customer requests to fly From some origin city To some destination city Use three input text files names of cities served Pairs of city names, flight origins and destinations Pairs of names, request origins, destinations Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Backtracking Flight map for HPAir Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Backtracking A recursive strategy Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Searching for an Airline Route Possible outcomes of applying the previous strategy Eventually reach destination city and can conclude that it is possible to fly from origin to destination. Reach a city C from which there are no departing flights. Go around in circles. Use backtracking to recover from a wrong choice (2 or 3) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Backtracking A piece of a flight map Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Searching for an Airline Route ADT flight map operations Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Searching for an Airline Route ADT flight map operations Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Searching for an Airline Route C++ implementation of searchR Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Searching for an Airline Route C++ implementation of searchR Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Searching for an Airline Route Flight map for Checkpoint Question Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Eight Queens Problem Chessboard contains 64 squares Form eight rows and eight column Problem asks you to place eight queens on the chessboard … So that no queen can attack any other queen. Note there are 4,426,165,368 ways to arrange eight queens on a chessboard Exhausting to check all possible ways Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Eight Queens Problem However, each row and column can contain exactly one queen. Attacks along rows or columns are eliminated, leaving only 8! = 40,320 arrangements Solution now more feasible Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Eight Queens Problem Placing one queen at a time in each column, and the placed queens’ range of attack: (a) the first queen in column 1; (b) the second queen in column 2; (c) the third queen in column 3; (d) the fourth queen in column 4; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Eight Queens Problem Placing one queen at a time in each column, and the placed queens’ range of attack: (e) five queens can attack all of column 6; (f) backtracking to column 5 to try another square for queen; (g) backtracking to column 4 to try another square for the queen; (h) considering column 5 again Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Eight Queens Problem Pseudocode describes the algorithm for placing queens Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Eight Queens Problem Pseudocode describes the algorithm for placing queens Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Eight Queens Problem A solution to the Eight Queens problem Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Relationship Between Recursion and Mathematical Induction Recursion solves a problem by Specifying a solution to one or more base cases Then demonstrating how to derive solution to problem of arbitrary size From solutions to smaller problems of same type. Can use induction to prove Recursive algorithm either is correct Or performs certain amount of work Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Correctness of the Recursive Factorial Function A recursive function that computes the factorial of a nonnegative integer n Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

Correctness of Recursive Factorial Function Inductive hypothesis Inductive conclusion Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Cost of Towers of Hanoi Recall solution to the Towers of Hanoi problem Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017

The Cost of Towers of Hanoi Claim Basis Show that the property is true for N = 1 Inductive hypothesis Assume that property is true for N = k Inductive conclusion Show that property is true for N = k + 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017