0% found this document useful (0 votes)
14 views43 pages

Linear Search: A Simple Search: Traverses Found Exhausted

The document discusses search algorithms, including linear and binary search, explaining their methodologies and complexities. It also covers recursion, particularly in the context of calculating Fibonacci numbers and solving the Tower of Hanoi problem. The document illustrates how recursive patterns can be used to derive explicit formulas for problem-solving.

Uploaded by

arthurvai213141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views43 pages

Linear Search: A Simple Search: Traverses Found Exhausted

The document discusses search algorithms, including linear and binary search, explaining their methodologies and complexities. It also covers recursion, particularly in the context of calculating Fibonacci numbers and solving the Tower of Hanoi problem. The document illustrates how recursive patterns can be used to derive explicit formulas for problem-solving.

Uploaded by

arthurvai213141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Linear Search: A Simple Search

• A search traverses the collection until


o The desired element is found
o Or the collection is exhausted

• If the collection is ordered, we might not have to


look at all elements
o We can stop looking when we know the
element cannot be in the collection.
Un-Ordered Iterative Array Search
void main()
{
int LSearch(int a[], int, int);
int i, arr[10], n, x;
printf(“\nEnter the number of Elements “);
scanf(“%d”, &n);
printf(“\nEnter the Elements “);
for(i=0,i<n;i++) scanf(“%d”, &arr[i]);
printf(“\nEnter the element to be searched”);
Scanf(“%d”, &x);
if(LSearch(arr,n,x)<n)
printf(“\nElement Found at Location %d” LSearch(arr,n,x)+1);
else
printf(“\nElement not Found”);
int LSearch(int Array[], int n,int Num){
int i;
for (i=0;i<n, i++)
if(Array[i]==Num) return i;
return n;}
Complexity – Linear Search

• Best Case – Only one comparison – О (1)


•Worst Case – n comparisons – О (n)
•Average Case
 To locate 1st element – 1 comparison
 To locate 2nd element – 2 comparisons
 ......
 To locate k-th element – k comparisons
 ....
 To locate n-th element – n comparisons
• Total comparisons = 1+2+3+ ...+ n= n(n+1)/2
• Average number of comparisons =n(n+1)/2/n = (n+1)/2
• Complexity = О (n)
A Better Search Algorithm
• Of course we could use our simpler search and
traverse the array

• But we can use the fact that the array is sorted to


our advantage

• This will allow us to reduce the number of


comparisons
Binary Search
• Requires a sorted array or a binary search
tree.

• Cuts the “search space” in half each time.

• Keeps cutting the search space in half until


the target is found or has exhausted the all
possible locations.
Binary Search Algorithm

look at “middle” element


if no match then
look left (if need smaller)
or
right (if need larger)

1 7 9 12 33 42 59 76 81 84 91 92 93 99

Look for 42
Binary Search – Recursive Version
int BSearch(int Array[], int low, int high, int x)
{
int mid;
if(low<=high)
{
mid= (low+high)/2;
if (Array[mid]== x) return mid;
else
if(Array[mid]>x) BSearch (Array, low, mid-1,x);
else BSearch(Array, mid+1, high,x);
}
return -999;
}
Binary Search – Iterative Version
int BSearch(int Array[], int low, int high, int x)
{
int mid;
while(low<=high)
{
mid= (low+high)/2;
if (Array[mid]== x) return mid;
else
if(Array[mid]>x) high = mid-1;
else low = mid+1;
}
return -999;
}
Complexity – Binary Search

• Best Case – Searching of Middle element – О(1)


• Average Case
n
T (n)  T ( )  (1)
2
 (log n)

• Worst Case – One more than Average Case


Recursion
Fibonacci Numbers

 n, if n  1
F ( n)  
 F (n  1)  F (n  2), if n  2
Fibonacci Numbers
• Computing the nth Fibonacci number recursively:
o F(n) = F(n-1) + F(n-2)
int Fib(int n)
o F(0) = 0 {
o F(1) = 1 if (n <= 1)
return 1;
o Top-down approach else
return Fib(n - 1) + Fib(n - 2);
}

F(n)

F(n-1) + F(n-2)

F(n-2) + F(n-3) F(n-3) + F(n-4)


Fibonacci Numbers
• What is the Recurrence relationship?
o T(n) = T(n-1) + T(n-2) + 1
• What is the solution to this?
o Clearly it is O(2n), but this is not tight.
o A lower bound is (2n/2).
o You should notice that T(n) grows very similarly to
F(n), so in fact T(n) = (F(n)).
• Obviously not very good, but we know that
there is a better way to solve it!
Fibonacci Numbers
o Computing the nth Fibonacci number using a bottom-up approach:
o F(0) = 0
o F(1) = 1
o F(2) = 1+0 = 1
o …
o F(n-2) =
o F(n-1) =
o F(n) = F(n-1) + F(n-2)

• Efficiency: 0 1 1 . . . F(n-2) F(n-1) F(n)


o Time – O(n)
o Space – O(n)
Fibonacci Numbers

• The bottom-up approach is only (n).


• Why is the top-down so inefficient?
o Recomputes many sub-problems.
– How many times is F(n-5) computed?
F(n)

F(n-1) + F(n-2)
n levels

F(n-2) + F(n-3) F(n-3) + F(n-4)


… … …
Fibonacci Numbers

Fib(5)
+

Fib(4) Fib(3)
+ +

Fib(3) Fib(2) Fib(2) Fib(1)


+ + +
Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0)
+
Fib(1) Fib(0)
Recursion
• Recursion is a programming technique in which a
call to a method appears in that method’s body
• (i.e., a method calls itself: this is called direct
recursion, otherwise indirect recursion)
• Once you understand recursive methods, they are
often simpler to write than their iterative
equivalents.
• In modern programming languages, recursive
methods may run a bit slower than equivalent
iterative methods; in a typical application, this time
is insignificant.
The Tower Of Hanoi
The Tower Of Hanoi
• The Tower of Hanoi (sometimes referred
to as the Tower of Brahma or the End of
the World Puzzle) was invented by the
French mathematician, Edouard Lucas, in
1883.
• He was inspired by a legend that tells of a
Hindu temple where the pyramid puzzle
might have been used for the mental
discipline of young priests.
The Tower Of Hanoi
• The Legend says that at the beginning of time, the
priests in the temple were given a stack of 64 gold
disks, each one a little smaller than the one
beneath it.
• Their assignment was to transfer the 64 disks from
one of three poles to another, with one important
proviso a large disk could never be placed on top
of a smaller one.
• The priests worked very efficiently, day and night.
When they finished their work, the myth said, the
temple would crumble into dust and the world
would vanish.
• How many moves ( & how long) would the priests
need to take to complete the task??
Start here - Instructions
1. Transfer all the disks from one pole to another
pole.
2. You may move only ONE disk at a time.
3. A large disk may not rest on top of a smaller
one at any time.

1
2
3
A B C
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Recursive Solution (n=3)
Try this one!

1
2
3
4
A B C

Shortest number of moves??


And this one

1
2
3
4
5
A B C

Shortest number of moves??


Now try this one!

1
2
3
4
5
6
A B C
Shortest number of moves??
Where's the maths in this game?

• From the moves necessary to transfer one, two,


and three disks, we can find a recursive pattern
- a pattern that uses information from one step
to find the next step.
• Unfortunately, if we want to know how many
moves it will take to transfer 64 disks from post
A to post C, we will first have to find the moves
it takes to transfer 63 disks, 62 disks, and so
on.
• Therefore, the recursive pattern will not be
much help in finding the number of moves or
• However, the recursive pattern can help us generate
more numbers to find an explicit (non-recursive)
pattern. Here's how to find the number of moves
needed to transfer larger numbers of disks from post A
to post C, when M = the number of moves needed to
transfer n-1 disks from post A to post C:
• for 1 disk it takes 1 move to transfer 1 disk from post
A to post C;
• for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3
• for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7
• for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 =
15
• for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 =
31
• for 6 disks... ?
The Tower Of Hanoi

• Number of Disks Number of Moves


1 1
2 3
3 7
4 15
5 31
6 63
The Tower Of Hanoi

• Number of Disks (n) Number of Moves


1 21 - 1 = 2 - 1 = 1
2 22 - 1 = 4 - 1 = 3 3
23 - 1 = 8 - 1 = 7
4 24 - 1 = 16 - 1 = 15
5 25 - 1 = 32 - 1 = 31
6 26 - 1 = 64 - 1 = 63
The Tower Of Hanoi

• So, the formula for finding the number of steps


it takes to transfer n disks from post A to post C
is:
n
2 –1
• 264-1=18,446,744,073,709,551,615 moves!
• If the priests worked day and night, making one
move every second it would take slightly more
than 580 billion years to accomplish the job!
• Which is far, far longer than some scientists
estimate the solar system will last.
Tower of Hanoi
#include <stdio.h>
void towers(int, char, char, char);
void main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the
Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
}
Tower of Hanoi
void towers(int num, char from_stk, char to_stk, char
aux_stk)
{
if (num == 1)
{
printf("\n Move disk 1 from Stack %c to Stack %c",
from_stk, to_stk);
return;
}
towers(num - 1, from_stk, aux_stk, to_stk);
printf("\n Move disk %d from Stack %c to Stack%c", num,
from_stk, to_stk);
towers(num - 1, aux_stk, to_stk, from_stk);
}

You might also like