Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Recurrence Relation
1.
2. Recurrence Relations
Time and Space Complexity
Binary Search Algorithm
Nadar Saraswathi College of Arts and Science
Nagapandiyammal N
3. A recurrence relation for the sequence, a0, a1, ... is an
equation that relates an to certain of its predecessors
a0, a1, ... , an1.
Initial conditions for the sequence a0, a1, ... are
explicitly given values for a finite number of the terms
of the sequence.
4. Recurrence relations are useful in certain counting
problems.
A recurrence relation relates the n-th element of a
sequence to its predecessors.
Recurrence relations arise naturally in the analysis of
recursive algorithms.
5. A (numerical) sequence is an ordered list of number.
2, 4, 6, 8, … (positive even numbers)
0, 1, 1, 2, 3, 5, 8, … (the Fibonacci numbers)
0, 1, 3, 6, 10, 15, … (numbers of key comparisons in
selection sort)
8. What is Time complexity?
Every algorithm requires some amount of computer time to execute
its instruction to perform the task. This computer time required is
called time complexity.
Generally, running time of an algorithm depends upon the following...
Whether it is running on Single processor machine or Multi processor machine.
Whether it is a 32 bit machine or 64 bit machine
Read and Write speed of the machine.
The time it takes to perform Arithmetic operations, logical operations, return value
and assignment operations etc.,
Input data
The time complexity of an algorithm is the total amount of time required
by an algorithm to complete its execution.
9. To calculate time complexity of an algorithm, we
need to define a model machine. Let us assume a
machine with following configuration...
Single processor machine
32 bit Operating System machine
It performs sequential execution
It requires 1 unit of time for Arithmetic and Logical operations
It requires 1 unit of time for Assignment and Return value
It requires 1 unit of time for Read and Write operations
10. Now, we calculate the time complexity of following example code
by using the above defined model machine...
Example :
Consider the following piece of code...
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
11. For the above code, time complexity can be
calculated as follows...
1. Cost is the amount of computer time required for a single
operation in each line.
2. Repeatation is the amount of computer time required by each
operation for all its repeatations.
3. Total is the amount of computer time required by each operation
to execute.
12. When we design an algorithm to solve a problem, it
needs some computer memory to complete its
execution. For any algorithm, memory is required
for the following purposes...
Memory required to store program instructions
Memory required to store constant values
Memory required to store variable values
And for few other things
13. Spacecomplexityof an algorithmcanbe definedasfollows...
Generally, when a program is underexecution it usesthe computer
memory for THREE reasons.Theyareas follows . . .
Instruction Space: It is the amount of memory used to store compiled
version of instructions.
Environmental Stack: It is the amount of memory used to store
information of partially executed functions at the time of function call.
Data Space: It is the amount of memory used to store all the variables and
constants
Total amount of computer memory required by an algorithm to complete
its execution is called as space complexity of that algorithm
14. To calculate the space complexity, we must know the
memory required to store different data type values
(according to the compiler). For example, the C
Programming Language compiler requires the following...
2 bytes to store Integer value,
4 bytes to store Floating Point value,
1 byte to store Character value,
6 (OR) 8 bytes to store double value
Example :
Consider the following piece of code...
int square(int a)
{
return a*a;
}
15.
16. Binary Search Algorithm :
Binary Search is applied on the sorted array or list of large size. It's time complexity
of O(log n) makes it very fast as compared to other sorting algorithms. The only
limitation is that the array or list of elements must be sorted for the binary search
algorithm to work on it.
Implementing Binary Search Algorithm
Following are the steps of implementation that we will be following:
Start with the middle element:
If the target value is equal to the middle element of the array, then return the index of the
middle element.
If not, then compare the middle element with the target value,
If the target value is greater than the number in the middle index, then pick the
elements to the right of the middle index, and start with Step 1.
If the target value is less than the number in the middle index, then pick the elements
to the left of the middle index, and start with Step 1.
When a match is found, return the index of the element matched.
If no match is found, then return -1
17. /*
function for carrying out binary search on given array
- values[] => given sorted array
- len => length of the array
- target => value to be searched
*/
int binarySearch(int values[], int len, int target)
{
int max = (len - 1);
int min = 0;
int guess; // this will hold the index of middle elements
int step = 0; // to find out in how many steps we completed the search
while(max >= min)
{
guess = (max + min) / 2;
// we made the first guess, incrementing step by 1
step++;
18. if(values[guess] == target)
{
printf ("Number of steps required for search: %d n", step);
return guess;
}
else if(values[guess] > target)
{
// target would be in the left half
max = (guess - 1);
}
else
{
// target would be in the right half
min = (guess + 1);
}
}
// We reach here when element is not
// present in array
return -1;
}
19. int main(void)
{
int values[] = {13, 21, 54, 81, 90};
int n = sizeof(values) / sizeof(values[0]);
int target = 81;
int result = binarySearch(values, n, target);
if(result == -1)
{
printf("Element is not present in the given array.");
}
else
{
printf("Element is present at index: %d", result);
}
return 0;
}