1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD

Slides:



Advertisements
Similar presentations
Computers playing games. One-player games Puzzle: Place 8 queens on a chess board so that no two queens attack each other (i.e. on the same row, same.
Advertisements

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
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.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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
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.
Java Programming: Guided Learning with Early Objects Chapter 11 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.
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
1 Data Structures CSCI 132, Spring 2014 Lecture 18 Recursion and Look-Ahead.
Introduction to State Space Search
Kruse/Ryba ch051 Object Oriented Data Structures Recursion Introduction to Recursion Principles of Recursion Backtracking: Postponing the Work Tree-Structured.
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.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Recursion To understand recursion, you first have to understand recursion.
CS212: Data Structures and Algorithms
CSG3F3/ Desain dan Analisis Algoritma
Recursion.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Recursion Salim Arfaoui.
Backtracking, Search, Heuristics
Recursion Topic 5.
Recursion what is it? how to build recursive algorithms
Stacks & Recursion.
Recursion (Part 2).
CSSE 230 Day 25 Skip Lists.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
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
CSCI 104 Backtracking Search
Programming in Java: lecture 10
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"?
CSE 143 Lecture 19 More Recursive Backtracking
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures Using Java
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 11.
Back Tracking.
Analysis and design of algorithm
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Recursion Data Structures.
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Exercise: Dice roll sum Write a method diceSum similar to diceRoll, but it also accepts a desired sum and prints only arrangements that add up to.
Recursion.
CSE 143 Lecture 18 More Recursive Backtracking
CSE 143 Lecture 18 More Recursive Backtracking
Exercise: Dice roll sum
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.
Backtracking, Search, Heuristics
Presentation transcript:

1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD Lecture 4 1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD

Recursion What is Recursion? A recursive function is a function that calls itself. EG: finding the factorial of a number: non-recursive long factorial (long n) fact = 1; for (i=1;i<=n;i++)       fact = fact*i; return fact; Recursive method                        long factorial (long number) {       if (number == 1) return 1;      else return(number*factorial(number - 1));  } main( ) - Next slide

If the number is 5 then process will be Recursion If the number is 5 then process will be 5 * Factorial (5-1) 4 * Factorial (4-1) 3 * Factorial (3-1) 2 * Factorial (2-1) 1 main() {  int factnum; cin>>num; factnum = factorial(num); cout<<factnum; return 0; }

Recursion EG 2 : Sum of n Natural Numbers: int sum(n) { if (n==1) } return 1; else return( n+ sum(n-1)); } int sum(n) { int i,sum; for (i=1;i<=n;i++) sum = sum+i; return sum; }

Recursion Other Examples: Binary Search - searching for an element Towers of Hanoi One Must Move the disks from the A to B with the following rules 1) a disk can be placed in any one , move one at a time 2) No large disk over a small disk A B C

Binary Search 1 4 7 8 14 16 28 35 1 4 7 8 14 16 28 35

Back Tracking What is BackTracking? In this method the algorithm attempts to complete a solution by constructing partial solutions and then extends the partial solution toward completion. When an inconsistency occurs the algorithm 'backs up' by removing the most recent construction and trys another possibility (This is called backtracking).  

Back Tracking Example : 8 queens problem. There is 8x8 Chess Board and there are 8 queens All the 8 queens have to be placed on the chess board so that no two queens are on the same row,column and diagonal (ie) no two attack each other

4 Queens Problem

Back Tracking void AddQueen(void) { for (every unguarded position p on the board)      {                Place a queen in position p;                     n ++;                    if (n == 8) Print the configuration;                     else Addqueen();                     Remove the queen from position p;                     n--; } }

Look Ahead In some games the advantage will always be with the player who can think several moves ahead. Thus this class of recursive algorithm involves a look ahead procedure which find-out possible future moves, evaluates them and then selects the best before carrying out the move.  

Look Ahead A general description might be: Void LookAhead () { Obtain a stack of possible moves if recursion terminates (the base case) return one move and the value else for each move on the stack do make the move and Lookahead select the best value return the move and value }}

Look Ahead Try to apply this to the game of 8. The game consists of a 3x3 board containing numbers from 1 to 8 put randomly. Therefore there is one space left for numbers to move. The aim of the game is to move the numbers to rearrange them in the following sequence: 1 2 3 4 5 6 7 8 A number is moveable if it is adjacent to the empty square.