Professional Documents
Culture Documents
AOP UNIT-1
AOP UNIT-1
AOP UNIT-1
They are used to solve problems and carry out tasks in computer
systems, such as sorting data, searching for information, image
processing and much more.
Algorithm as a technology:
These resources should be used wisely and algorithms that are different in
terms of time or space.
Insertion sort take time roughly equal to c1n2(c is constant that does not
depend on n) to sort n times.
Merge sort take time roughly equal to c2nlgn(c2 is another constant that does
not depend on n) where lg n stands for log n . 2
Insertion sort usually has smaller constant factor then merge sort. So that
c1<c2.
Computer A:
2.(10^6)^2 instructions/ (10^7 instructions/second )=2000 seconds
Computer B:
50.10^6 lg 10^6 instructions/ (10^7 instructions/second )=100 seconds
By using an algorithm where running time grows more slowly, even with a
poor complier.
Computer B runs 20 times faster than computer A.
Algorithm analysis:
Algorithm analysis is the process of evaluating the performance of an
algorithm, usually in terms of its time and space complexity.
Time complexity: Time complexity is a measure of how long an algorithm
takes to run, based on the size of the input.
Both time and space complexity are expressed using big 0 notation,
which captures the worst-case scenario of an algorithm’s behavior.
Time and space complexity are important because they affect the
scalability and feasibility of an algorithm.
If an algorithm has a high time complexity, it may take too long to run on
large inputs.
Fractional knapsack.
Merge sort.
Dynamic programming:
The approach of dynamic programming is similar to divide and conquer. The
difference is that whenever we have recursive function calls with the same
result, instead of calling them again we try to store the result in data structure
in the form of table and retrieve the result from the table. Thus, the overall
time complexity is reduced.
Linear programming:
In linear programming, there are inequalities in terms of inputs and
maximizing or minimizing some linear functions of inputs.
Ex: maximum flow of directed graph.
Backtracking:
This technique is very useful in solving combinatorial problems that have single
unique solution.
Asymptotic notation:
1. Big O notation:
Big O notation represents the upper bound of the running time of an
algorithm. Therefore, it gives the worst-case complexity of an
algorithm.
It returns the highest possible output value (big-O) for a given input.
Mathematically,
O(g(n)) = { f(n)} then there exist positive constants like C and n0 such
that 0 ≤ f(n) ≤ Cg(n) : n ≥ n0 }.
Ex: Linear search: O(N), where N is the number of elements in the given
array
Theta notation encloses the function from above and below. Since it
represents the upper and lower bound of the running time of an
algorithm, it is used for analyzing the average-case complexity of an
algorithm.
Let g and f be the function from the set of natural numbers to itself. The
function f is said to be Θ(g), if there are constants c1, c2 > 0 and a
natural number n0 such that c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0.
Theta notation
Mathematically,
Θ (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such
that 0 ≤ c1 * g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0}.
The execution time serves as both a lower and upper bound on the
algorithm’s time complexity.
Ex: (N2), (N log N), (3*N2) gives average time complexity as Θ(N2)
Let g and f be the function from the set of natural numbers to itself. The
function f is said to be Ω(g), if there is a constant c > 0 and a natural
number n0 such that c*g(n) ≤ f(n) for all n ≥ n0.
Omega notation(Ω-Notation)
Mathematically,
Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤
cg(n) ≤ f(n) for all n ≥ n0 }.
Ex: {(N2+N), (4*N), (N2+LogN)} gives the best time complexity as Ω(N2)
Growth of function:
Fundamental algorithm:
Algorithm:
1. Start.
2. Read value of a
3. Read value of b
4. c = a
5. a = b
6. b = c
7. print a
8. print b
9. end
program:
#include <stdio.h>
int main()
{
int a, b, temp;
a = 11;
b = 99;
printf("values before swapping \n a = %d, b = %d \n\n",a,b);
temp = a;
a = b;
b = temp;
printf("After Swapping the values /n a = %d, b = %d", a, b);
}
Algorithm
Step1: start
Step2: get the values of ‘n’ number that need to be summed up.
Step3: initialize sum to zero.
Step4: initialize i to 1.
Step5: while i is less than or equal to n, do the following steps:
a) read the ith number
b) add the ith number to current value of sum.
c) Increment i by 1.
Step6: print the value of sum.
Step7:stop.
Program:
#include <stdio.h>
int main()
{
int a=5, b=10, sum = 0;
for (int i = a; i <= b; ++i) {
sum = sum + i
printf("Sum = %d", sum);
return 0;
}
Fractional computation:
Product of all consecutive integer numbers upto n is called factorial of a
number and is denoted by n :
Ex: 5! = 120
Algorithm
Step1: start
Step2: declare variable n, fact, i.
Step3: read number from user.
Step4: initialize variable fact = 1 and i = 1.
Step5:reapeat until i<=number:
a) Fact = fact * i.
b) i = i +1.
Step6: print fact.
Step7:stop.
Program
#include<stdio.h>
Void main()
{
int i, n, fact=1;
printf("Enter a number: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d", n, fact);
return 0;
}
Algorithm
Step1: start
Step2: take integer variable a, b, c.
Step3: a = 0, b = 0.
Step4: display a, b.
Step5:c = a + b
Step6: display c.
Step7:set a = b, b = c.
Step8:repeat from 5 – 7, for n times.
Step9: stop.
Program
#include<stdio.h>
int main()
{
int a, b, c, I, n;
n = 4;
a = b= 1;
printf("%d %d", a, b);
for(i=1;i<=n-2;i++)
{
C = a + b;
Printf(“%d”, c);
a=b;
b=c;
}
return 0;
}
Algorithm
Step1: start
Step2:read a number num.
Step3: set num = 0, dup = num.
Step4: while num>0 true continue else go to step 8.
Step5: set rem = num % 10.
Step6: set sum = sum * 10 + rem.
Step7:set num = num/10 go to step 4.
Step8:print sum value that is reverse number. Step9: stop.
Program
#include<stdio.h>
int main()
{
int n, rev = 0, remainder;
printf("enter an integer:");
scanf(“%d”, &n);
while (n! = 0)
{
Remainder = n%10;
Rev = rev *10 + remainder;
n1=10;
}
Printf(“reversed number = %d”, rev);
return 0;
}