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

Binary Search : Successful case(element

found in the list)


Binary Search : UnSuccessful case(element
not found in the list)
Recursive BinarySearch Algorithm
int BinarySearch(int *a,const int key,const int low,const int high)
{ // Search the sorted array a[low],….a[high] for key
If(low<=high){
int mid={low+high)/2;
if(key=a[mid]) return mid+1;
else if(key<a[mid])
return (BinarySearch(a,key,low,mid-1);
else
return (BinarySearch(a,key,mid+1,high);
}//end of if
return 0; // if element not found
}
Permutations Generator
A permutation, also called an
“arrangement number” or “order,” is a
rearrangement of the elements of an
ordered list S into a one-to-one
correspondence with S itself.
•A string of length n has n! permutation.
Ex: Permutations of set {A,B,C} are
{(A,B,C),(A,C,B),(B,A,C),(B,C,A),
(C,A,B),( C,B,A)} 
Permutations Generator….
The answer can be constructed by writing
•A followed by all permutations of{B,C}
•B followed by all permutations of {A,C}
•C followed by all permutations of {A,B}
“followed by all permutations of” is the clue
to recursion
It implies that we can solve the problem for
a set with n elements if we have an
algorithm that works on n-1 elements
Algorithm for Permutations
void Permutations(char *a,const int k,const int m)
{ //Generate all permutations of a[k]…a[m]
if(k==m){
for(inti=0;i<=m;i++) cout<<a[i]<<“ “;
cout<<endl;
else //a[k:m] has more than one permutation.
//Generate these recursively
for(i=k;i<=m;i++){
swap(a[k],a[i]);
Permutations(a,k+1,m);
swap(a[k],a[i]);
Exercise
• Write a recursive algorithm for computing
binomial coefficient
B(n, k) = B(n − 1, k − 1) + B(n − 1, k).

Note: Identify the base conditions when the


recursive call has to terminate
The Tower of Hanoi
How To Play
• The puzzle consists of p pegs and d disks, with each disk
being a different size.
• The object is to relocate the entire tower to a different
peg.
• The stipulations are that only one disk can be moved at a
time and no disk can be placed atop a smaller disk.

We will examine the puzzle when p  3 and 3  d  8 .


Peg Labeling
In order to explain the solutions we must distinguish between the pegs.
So let the peg on which the tower sits be the source peg. Call the peg you
wish to move to the destination peg. Call the peg necessary for auxiliary
moves the auxiliary peg.

Source Peg Auxiliary Peg Destination Peg

NOTE: A new tower will be created with every move. Consequently, the pegs
which we initially called the source, auxiliary and destination pegs will constantly
change.
Preliminary Observations
Let q equal to the minimum number of moves necessary to
complete the relocation. Examining the case when d = 3 we
find q = 7.
d=4 q = 15
d=5 q = 31
d=6 q = 63
d=7 q = 127
d=8 q = 255
Can you see the pattern?

http://www.cut-the-knot.org/recurrence/hanoi.html
Observations Continued
The following equation gives q for any d:

q  2d  1

This will be true for all d provided that p = 3. In the case of De


Parville’s story the priests had to relocate a tower of 64 disks.
Therefore:
q  264  1  18, 446, 744, 073, 709,551, 615
Supposing that the priests were proficient enough to make a
move a second, the world would end in 584,942,417,355.1
years.
0 Move Sequence for d = 3
1

7
More Observations
• When looking at the case of d = 3 the first move must be
made to the destination peg to ensure a solution with minimal
q.
• Similarly, for d = 4 the first move must be made to the
auxiliary peg.
• It will be the case that for odd d the first move must always
be made to the destination peg. For even d the first move
must always be made to the auxiliary peg.
Recurrence Relations
Another way of approaching this problem is to look at the
first d – 1 disks. The goal would then be to first relocate
those disks to the auxiliary peg. Next one would simply
move the last disk to the destination peg. Finally, the solution
would be achieved by relocating the d – 1 disk tower to the
destination peg. This method gives rise to the notion of
recurrence.
0 Move Sequence for d = 3
1

7
Recurrence Relations Continued
Consider T(d), where T(d) = q and d = 3 and q = 7. We know
T(3) = 7. Using the recurrence relation we can see that
T (3)  2  T (3  1)  1
In this example T(3-1) is the number of moves to relocate the
top two disks. Clearly this must be done twice: once to
uncover the largest disk and again to re-cover it, therefore it is
multiplied by two. The “+1” comes from the single move of
the largest disk from the source to destination peg.

Likewise, for all d except d = 0 the following holds:


T (d )  2  T (d  1)  1
NOTE: T(0) = 0
Algorithm
void TowersofHanoi(int n,int *x,int *y,int *z)
{//Move the top n disks from tower x to tower y
if(n>=1)
{
TowersofHanoi(n-1,x,z,y);
cout<<“move top disk from
tower”<<x<<“to tower “<<y;
TowersofHanoi(n-1,z,y,x);
}
}
Class Exercises
What is T(15)? 2 ^15  1  32768  1  32767
Draw the move sequence for d = 4.

You might also like