Lecture 4 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

Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
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?
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Chapter 15 Recursive Algorithms.
Ryan Kinworthy 2/26/20031 Chapter 7- Local Search part 1 Ryan Kinworthy CSCE Advanced Constraint Processing.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Review of Recursion. Definition: Recursion - The process of a function calling itself.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Recursion Chapter 5.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
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.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of 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.
NxN Queens Problem Q- -- -Q Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START.
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
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
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.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
CS212: Data Structures and Algorithms
CSG3F3/ Desain dan Analisis Algoritma
COMP 51 Week Fourteen Recursion.
Backtracking, Search, Heuristics
Recursion what is it? how to build recursive algorithms
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
CSE 143 Lecture 19 More Recursive Backtracking
Data Structures Using Java
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
Back Tracking.
Analysis and design of algorithm
Recursion Data Structures.
CSE 143 Lecture 18 More Recursive Backtracking
CSE 143 Lecture 18 More Recursive Backtracking
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

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

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 ABC

Binary Search

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: A number is moveable if it is adjacent to the empty square.