Ch. 6 Recursion as a Problem Solving Technique

Slides:



Advertisements
Similar presentations
Chapter 20 Recursion.
Advertisements

COSC2007 Data Structures II
PROLOG 8 QUEENS PROBLEM.
Recursion B.Ramamurthy Chapter 4. Ramamurthy Introduction  Recursion is one of most powerful methods of solution available to computer scientists. 
CMPT 225 Stacks-part2.
CMPT 225 Stacks.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
© 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 5 Recursion as a Problem-Solving Technique.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Data Structures Using C++ 2E Chapter 6 Recursion.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CARRANO CH ; CUSACK CH 8: OPTIONAL.
Discrete Maths Objective to introduce mathematical induction through examples , Semester 2, Mathematical Induction 1.
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.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Two Dimensional Arrays
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Sudoku Jordi Cortadella Department of Computer Science.
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.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Stack Applications Qamar Rehman.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
TOWERS OF HANOI. : If n = 1, move disk 1 from pole 'A' to pole 'B'. else: 1.First, move n-1 disks from pole 'A' to pole 'C', using pole 'B' as.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Recursion – some examples. Sum of Squares Write Vertical.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Recursion Powerful Tool
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
Languages.
Intro to Computer Science II
Data Structures and Algorithms
Sit-In Lab 1 Ob-CHESS-ion
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Hubert Chan (Chapters 1.6, 1.7, 4.1)
CSE 143 Lecture 19 More Recursive Backtracking
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Jordi Cortadella Department of Computer Science
CSE 143 Lecture 18 More Recursive Backtracking
CSC 143 Recursion.
Stack.
B.Ramamurthy Chapter 9 CSE116A,B
Recursion as a Problem-Solving Technique
CSE 143 Lecture 18 More Recursive Backtracking
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSC 205 – Java Programming II
Presentation transcript:

Ch. 6 Recursion as a Problem Solving Technique Chapter 4 Basics on recursion Chapter 6 More on recursion Two useful concepts Backtracking & formal grammars

Backtracking A search strategy by a seq. of guesses Guesses, retraces in reverse order, and tries a new sequence of steps Has a strong relationship with recursion and stack

Eight-Queens Problem: An Example Chessboard with 64 squares 8 rows and 8 columns A queen can attack other pieces within its row within its column along its diagonal Want to place eight queens on the chessboard so that no queen can attack any other queen

1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 • • • • X • • X • • • • • • •

col row row col row Public boolean placeQueens(int col) { // Situation: Queens are placed correctly in columns 1 thru col – 1 // Return true if a solution is found; return false if there is no solution; if (col > BOARD_SIZE) { return true; } else { boolean queenPlaced = false; int row = 1; // square id in column while (!queenPlaced && (row <= BOARD_SIZE)) { if (isUnderAttack(row, col)) { ++row; // consider next square } else { // found valid square setQueen(row, col); queenPlaced = placeQueens(col+1); if (!queenPlaced) { // failed removeQueen(row, col); ++row; } } // end while return queenPlaced; col row row col row

1 2 3 4 5 6 7 8 X public boolean placeQueens(int col) { // Situation: Queens are placed correctly in columns 1 thru col – 1 // Return true if a solution is found; return false if there is no solution; if (col > BOARD_SIZE) { return true; } else { boolean queenPlaced = false; int row = 1; // square id in column while (!queenPlaced && (row <= BOARD_SIZE)) { if (isUnderAttack(row, col)) { ++row; // consider next square } else { // found valid square setQueen(row, col); queenPlaced = placeQueens(col+1); if (!queenPlaced) { // failed removeQueen(row, col); ++row; } } // end while return queenPlaced; 1 2 3 4 5 6 7 8 • X • • •

Formal Grammars Basics x|y means x or y xy means x followed by y <word> means any instance of word that the definition defines

Example – JAVA Identifier <identifier> = <letter> | <identifier> <letter> | <identifier> <digit> <letter> = a | b | · · · | z | A | B | · · · | Z | _ | $ <digit> = 0 | 1 | · · · | 9 letter ~3/21/2018(제6강) letter digit Valid identifiers b, tmp3_off, $toy, _33, Zippul, …

Recursive Routine for <identifier> Determination isId(w) // pseudo code { if (w is of length 1) { // base case 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} } <identifier> <letter> | <identifier> <digit>

Example - Palindrome Palindromes = {w | w reads the same left to right as right to left} <palindrome> = empty string | <ch> | a <palindrome> a | b <palindrome> b | … | Z <palindrome> Z <ch> = a | b | · · · | z | A | B | · · · | Z e.g. abcba, chainniahc, lioninoil, …

Recursive Routine for Palindrome Determination isPalindrome(w) // pseudo code { if (w is empty string or w is of length 1) return true; else if (w’s first and last characters are the same) return isPalindrome(w minus its first and last characters); else return false; }

Example - AnBn L = {w | w is the form AnBn for some n >= 0 } <L> = empty string | A <L> B e.g. AB, AAABBB, AAAAAABBBBBB, …

Recursive Routine for AnBn Determination isAnBn(w) // pseudo code { if (w is empty string) return true; else if (w begins w/ the character A and ends w/ the character B) return isAnBn(w minus its first and last characters); else return false; }

Example – Infix, Prefix, Postfix Expression Infix expression The operator locates in the middle of the operands The most popular A + B * C – 2 Prefix expression The operator proceeds the operands – + A * B C 2 Postfix expression The opeartor follows the operands A B C * + 2 –

<infix> = <identifier> | <infix> <operator> <infix> <identifier> = a | b | … | z <prefix> = <identifier> | <operator> <prefix> <prefix> <operator> = + | – | * | / <identifier> = a | b | … | z <postfix> = <identifier> | <postfix> <postfix> <operator> <operator> = + | – | * | / <identifier> = a | b | … | z

Recursive Routine for Determination of Prefix isPre(A, 1, n) { // return true if the string A[1…n] is in prefix form // otherwise return false lastChar = endPre(A, 1, n); if (lastChar == n) return true; else return false; } endPre(A, first, …): A[first]에서 시작하는 prefix expression이 여기서 끝난다는 걸 알아내기 first last A[ ]

{ // input: A[first…last], e.g., +*ab+cd, b, *bc endPre(A, first, last) { // input: A[first…last], e.g., +*ab+cd, b, *bc // return the position of the end of the prefix expression // beginning at A[first], if one exists if (first > last) return –1; if (A[first] is an identifier) return first ; else if (A[first] is an operator) { firstEnd = endPre(A, first+1, last); if (firstEnd = –1) return –1; else return endPre(A, firstEnd+1, last); } else return –1; } base case + <prefix> <prefix> 끝

핵심부 (에러가 안난다 가정) a endPre(A, first, last) { base case if (A[first] is an identifier) return first ; else { // A[first] is an operator firstEnd = endPre(A, first+1, last); else return endPre(A, firstEnd+1, last); } base case + <prefix> <prefix> last index first + . . .

생각 훈련: Conversion from Prefix to Postfix convert(pre) { // pre: a valid prefix expression // return the equivalent postfix expression ch = the 1st character of pre; Delete the 1st character from pre; if (ch is an identifier) return ch; else { // ch is an operator postfix1 = convert(pre); postfix2 = convert(pre); return postfix1•postfix2•ch; // concatenation } <postfix> <postfix> + pre[ ] + <prefix> <prefix> pre[ ] <prefix> <prefix>

Recursion & Math.cal Induction Cost of Hanoi Tower: An example Recursive relation for the # of moves moves(N) = 2 • moves(N–1) + 1 N : # of discs move(N, A, B, C) { …. move(N –1, A, C, B) move(1, A, B, C) move(N –1, C, B, A); }

Fact: moves(N) = 2N – 1 <Proof> Basis: moves(1) = 1 = 21 – 1 Inductive hypothesis: Assume moves(k) = 2k –1. Inductive conclusion: moves(k+1) = 2•moves(k) + 1 = 2(2k – 1) + 1 = 2k+1 – 1