Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Lecture 4

1) RECURSION
2) BACKTRACKING
3) LOOK AHEAD

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

If the number is 5 then process


main() will be
{
int factnum; 5 * Factorial (5-1)
cin>>num; 4 * Factorial (4-1)
factnum = factorial(num);
cout<<factnum; 3 * Factorial (3-1)
return 0;
} 2 * Factorial (2-1)
1
waitheart_iba@yahoo.com
Previous program
• #include<iostream.h>
• long factorial(long number)
• {if(number==1)return 1;
• else
• return(number*factorial(number-1));
• }
• void main()
• {
• int factnum;
• cin>>factnum;
• factnum=factorial(factnum);
• cout<<factnum;
• }

waitheart_iba@yahoo.com
Recursion
• EG 2 : Sum of n Natural Numbers:
int sum(n) int sum(n)
{ { int i,sum;
if (n==1) for (i=1;i<=n;i++)
return 1; sum = sum+i;
else return sum;
return( n+ sum(n-1));
}
}
waitheart_iba@yahoo.com
Previous program
• recursive • non_-recursive
• #include<iostream.h>
• #include<iostream.h> • void main()
• int sum(int n) • {int i ,num,sum=0;
• {if(n==1) • cin>>num;
• return 1; • for(i=1;i<=num;i++)
• else return (n*sum(n-1)); • sum+=i;
• } • cout<<sum;
• void main() • }
• {
• int i;
• cin>>i;
• i=sum(i);
• cout<<i;}

waitheart_iba@yahoo.com
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

waitheart_iba@yahoo.com
Binary Search

1 4 7 8 14 16 28 35

1 4 7 8 14 16 28 35

waitheart_iba@yahoo.com
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
tries another possibility
(This is called backtracking).

waitheart_iba@yahoo.com
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


waitheart_iba@yahoo.com
4 Queens Problem

waitheart_iba@yahoo.com
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--;
} }
waitheart_iba@yahoo.com
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.
waitheart_iba@yahoo.com
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
}}
waitheart_iba@yahoo.com
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:

123
456
78
A number is moveable if it is adjacent to the empty square.

waitheart_iba@yahoo.com

You might also like