Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
COSC 2006 Chapter 7 Stacks III
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Queues and Priority Queues
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.
Recursion Gordon College CPS212
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.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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)
Sorting Algorithms and their Efficiency Chapter 11 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Structures Using C++ 2E Chapter 6 Recursion.
Queues and Priority Queues
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)
Implementations of the ADT Stack Chapter 7 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
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.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
C++ Classes C++ Interlude 1 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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Link Based Implementations
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
Revised based on textbook author’s notes.
Recursion: The Mirrors
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.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 6 Stacks.
List Implementations Chapter 9
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Ch. 6 Recursion as a Problem Solving Technique
Recursion as a Problem-Solving Technique
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Figure 6.1 Stack of cafeteria dishes. Figure 6.1 Stack of cafeteria dishes.
Chapters 6 and 7 Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Contents Defining Languages Algebraic Expressions Backtracking The Relationship Between Recursion and Mathematical Induction Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Defining Languages A language is  A set of strings of symbols  From a finite alphabet. C++Programs = {string s : s is a syntactically correct C++ program} AlgebraicExpressions = {string s : s is an algebraic expression} Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Basics of Grammars Special symbols  x | y means x or y  xy (and sometimes x y ) means x followed by y  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, © 2013

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

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

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

Two Simple Languages Palindromes = {string s : s reads the same left to right as right to left} Grammar for the language of palindromes: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

Algebraic Expressions Compiler must recognize and evaluate algebraic expressions Example y = x + z * (w / k + z * (7 * 6)); Kinds of algebraic expressions  infix  prefix  postfix Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Algebraic Expressions infix  Binary operator appears between its operands prefix  Operator appears before its operands postfix  Operator appears after its operands Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Prefix Expressions Grammar that defines language of all prefix expressions Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

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

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

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

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

Postfix Expressions Grammar that defines language of postfix expressions Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Fully Parenthesized Expressions Grammar that defines language of fully parenthesized infix expression Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Backtracking Consider searching for an airline route Input text files that specify all of the flight information for HPAir Company  Names of cities HPAir serves  Pairs of city names, each pair representing origin and destination of one of HPAir’s flights  Pairs of city names, each pair representing a request to fly from some origin to some destination Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Backtracking FIGURE 5-4 Flight map for HPAir Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

Backtracking Possible outcomes of applying the previous strategy 1. Eventually reach destination city and can conclude that it is possible to fly from origin to destination. 2. Reach a city C from which there are no departing flights. 3. Go around in circles. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

Backtracking Note possible operations for ADT flight map, Listing 5-AListing 5-A View source code for C++ implementation of searchR, Listing 5-B Listing 5-B Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 FIGURE 5-6 Flight map for Checkpoint Question 6.htm code listing files must be in the same folder as the.ppt files for these links to work

The Eight Queens Problem FIGURE 5-7 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, © 2013

The Eight Queens Problem FIGURE 5-7 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, © 2013

The Eight Queens Problem View pseudocode of algorithm for placing queens in columns, Listing 5-CListing 5-C FIGURE 5-8 A solution to the Eight Queens problem Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Eight Queens Problem Note header file for the Board class, Listing 5-1 Listing 5-1 View source code for class Queen, Listing 5-2 Listing 5-2 And inspect an implementation of placeQueen, Listing 5-D Listing 5-D Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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, © 2013

Correctness of the Recursive Factorial Function Assume property true for k = n Now show Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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, © 2013

The Cost of Towers of Hanoi Consider … begin with N disks, how many moves does solveTowers make to solve problem? We conjecture Make assumption for N = k Must show Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

End Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013