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.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Chapter 7. Binary Search Trees
PROLOG 8 QUEENS PROBLEM.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
ICS-271:Notes 5: 1 Lecture 5: Constraint Satisfaction Problems ICS 271 Fall 2008.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Lecture 4 1) RECURSION 2)BACKTRACKING 3)LOOK AHEAD.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Recursion Chapter 5.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
COSC2007 Data Structures II
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm.
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
Recursion: Backtracking
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
Two Dimensional Arrays
Sudoku Jordi Cortadella Department of Computer Science.
Please snarf the code for today’s class. Then think about the famous 8-Queen’s Puzzle. The question is: is there a way to arrange 8 queens on a (8x8) chessboard.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
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.
Data Structures Using Java1 Chapter 5 Recursion. Data Structures Using Java2 Chapter Objectives Learn about recursive definitions Explore the base case.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Analysis & Design of Algorithms (CSCE 321)
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.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
CSG3F3/ Desain dan Analisis Algoritma
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Intro to Computer Science II
Data Structures and Algorithms
Instructor: Vincent Conitzer
Trees.
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"?
Data Structures Using Java
Chapter 12 Recursion (methods calling themselves)
Back Tracking.
Analysis and design of algorithm
Jordi Cortadella Department of Computer Science
CS Software Studio Assignment 1
Sudoku.
The N-Queens Problem Search The N-Queens Problem Most slides from Milos Hauskrecht.
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"?
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

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 generalization of DFS

Data Structures Using C++ 2E2 Recursion and Backtracking: 8-Queens Puzzle 8-queens puzzle –Place 8 queens on a chess-board No two queens can attack each other –Nonattacking queens Cannot be in same row, same column, same diagonals FIGURE 6-11 A solution to the 8-queens puzzle

Data Structures Using C++ 2E3 Recursion and Backtracking: 8-Queens Puzzle (cont’d.) Backtracking algorithm –Find problem solutions by constructing partial solutions –Ensures partial solution does not violate requirements –Extends partial solution toward completion –If partial solution does not lead to a solution (dead end) Algorithm backs up Removes most recently added part Tries other possibilities

Data Structures Using C++ 2E4 Recursion and Backtracking: 8-Queens Puzzle (cont’d.) n-Queens Puzzle –In backtracking, solution represented as n-tuple (x 1, x 2,..., x n ) Where x i is an integer such that 1 <= x i <= n x i specifies column number, where to place the ith queen in the ith row –Solution example for Figure 6-11 (4,6,8,2,7,1,3,5) Number of 8-tuple representing a solution: 8!

Data Structures Using C++ 2E5 Recursion and Backtracking: 8-Queens Puzzle (cont’d.) n-Queens Puzzle (cont’d.) –4-queens puzzle FIGURE 6-13 Finding a solution to the 4-queens puzzle FIGURE 6-14 A solution to the 4-queens puzzle FIGURE 6-12 Square board for the 4-queens puzzle

Data Structures Using C++ 2E6 Recursion and Backtracking: 8-Queens Puzzle (cont’d.) Backtracking and the 4-Queens Puzzle –Rows and columns numbered zero to three –Backtracking algorithm can be represented by a tree FIGURE queens tree

Data Structures Using C++ 2E7 Recursion and Backtracking: 8-Queens Puzzle (cont’d.) 8-Queens Puzzle –Easy to determine whether two queens in same row or column –Determine if two queens on same diagonal Given queen at position (i, j ), (row i and column j ), and another queen at position (k, l ), (row k and column l ) Two queens on the same diagonal if |j – l| = |i – k|, where |j – l| is the absolute value of j – l and so on –Solution represented as an 8-tuple Use the array queensInRow of size eight Where queensInRow[k] specifies column position of the kth queen in row k

Recursion and Backtracking: 8-Queens Puzzle (cont’d.) 8-Queens Puzzle (cont’d.) Data Structures Using C++ 2E8 FIGURE x 8 square board

Data Structures Using C++ 2E9 Recursion and Backtracking: 8-Queens Puzzle (cont’d.) 8-Queens Puzzle (cont’d.) –General algorithm for the function canPlaceQueen(k, i)

10 Recursion and Backtracking: Depth first search Let’s limit the DFS to binary trees (that’d be a traversal called pre-order). Backtracking will be taken care of by the recursion. void pre-order(node& root) if (root != NULL) { visit(root); // do something pre-order(root->left ); pre-order(root->right); }

Data Structures Using C++ 2E11 Recursion and Backtracking: 8-Queens Puzzle Backtracking will be taken care of by the recursion.

Data Structures Using C++ 2E12 Recursion, Backtracking, and Sudoku Recursive algorithm –Start at first row and find empty slot –Find first number to place in this slot –Find next empty slot, try to place a number in that slot –Backtrack if necessary; place different number –No solution if no number can be placed in slot FIGURE 6-17 Sudoku problem and its solution

Data Structures Using C++ 2E13 Recursion, Backtracking, and Sudoku (cont’d.) See code on page 384 –Class implementing Sudoku problem as an ADT –General algorithm in pseudocode Find the position of the first empty slot in the partially filled grid If the grid has no empty slots, return true and print the solution Suppose the variables row and col specify the position of the empty grid position

Data Structures Using C++ 2E14 Recursion, Backtracking, and Sudoku (cont’d.) General algorithm in pseudocode (cont’d.)

Data Structures Using C++ 2E15 Recursion, Backtracking, and Sudoku (cont’d.) Function definition

Data Structures Using C++ 2E16 Summary Recursion –Solve problem by reducing it to smaller versions of itself Recursive algorithms implemented using recursive functions –Direct, indirect, and infinite recursion Many problems solved using recursive algorithms Choosing between recursion and iteration –Nature of solution; efficiency requirements Backtracking –Problem solving; iterative design technique