Daa File
Daa File
A
LAB FILE
ON
DESIGN AND ANALYSIS OF AN ALGORITHM
( PCC_CS _407 )
INDEX
Sr.no. Name Of Program Date Of Date Of Signature
Experimen Submission
t
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
PROGRAM - 01
Definition of Algorithm:
The word Algorithm means ” A set of finite rules or instructions to be followed in calculations or
other problem-solving operations”
Mathematics: Algorithms are used to solve mathematical problems, such as finding the optimal
solution to a system of linear equations or finding the shortest path in a graph.
Operations Research: Algorithms are used to optimize and make decisions in fields such as
transportation, logistics, and resource allocation.
Artificial Intelligence: Algorithms are the foundation of artificial intelligence and machine
learning, and are used to develop intelligent systems that can perform tasks such as image
recognition, natural language processing, and decision-making.ics
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
Data Science: Algorithms are used to analyse, process, and extract insights from large
amounts of data in fields such as marketing, finance, and healthcare.
Properties of Algorithms:
Advantages of Algorithms:
It is easy to understand.
An algorithm is a step-wise representation of a solution to a given problem.
In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier
for the programmer to convert it into an actual program.
Disadvantages of Algorithms:
Characteristic of Algorithm:
Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be
clear in all aspects and must lead to only one meaning.
Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it
should be well-defined as well. It should produce at least 1 output.
Finite-ness: The algorithm must be finite, i.e., it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can be executed
with the available resources. It must not contain some future technology or anything.
Input: An algorithm has zero or more inputs. Each that contains a fundamental operator must
accept zero or more inputs.
Output: An algorithm produces at least one output. Every instruction that contains a
fundamental operator must accept zero or more inputs.
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
Finiteness: An algorithm must terminate after a finite number of steps in all test cases. Every
instruction which contains a fundamental operator must be terminated within a finite amount of
time. Infinite loops or recursive functions without base conditions do not possess finiteness.
Asymptotic Notation:
In mathematics, asymptotic notation, also known as asymptotic, is a method of describing the
limiting behaviour of a function. In computing, asymptotic analysis of an algorithm refers to
defining the mathematical boundation of its run-time performance based on the input size. For
example, the running time of one operation is computed as f(n), and maybe for another operation,
it is computed as g(n2). This means the first operation running time will increase linearly with the
increase in n and the running time of the second operation will increase exponentially
when n increases. Similarly, the running time of both operations will be nearly the same if n is
small in value.
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
3. Big Oh(O) Notation:
4. Little Omega:
5. Little Oh:
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
PROGRAM – 02
PART – 2A
Algorithm
Sum (A,n){
S = 0; ------------------------------------------------------------ (1) unit time
For(i=0 ; i<n ; i++){ --------------------------------------------- (n + 1) unit time
S = S + A[i]; ------------------------------------------------ (n) unit time
}
Return S; ----------------------------------------------------------- (1) unit time
}
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
Time complexity
T(n) = 1 + (n + 1) + n + 1
= 2n + 3
Space Complexity
A=n
n=1
S=1
i=1
S(n) = n + 1 + 1 + 1
=n+3
= O(n)
Source Code
#include <stdio.h>
int main() {
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
Output
PART - 2B
Algorithm
ADD ( A,B,n ){
For(i=0 ; i<n ; i++){
For(j=0 ; j<n ; j++){
C[i,j] = A[i,j] + B[i,j] ;
}
}
}
Time Complexity
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
Space Complexity
S(n) = 3n2 + 3
= O(n2)
Source Code
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix1[ROWS][COLS] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int matrix2[ROWS][COLS] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int sum[ROWS][COLS];
Output
PART - 2C
Algorithm
Mul ( A,B,n ){
For(i=0 ; i<n ; i++){
For(j=0 ; j<n ; j++){
C[i,j] = 0;
For(k=0 ; k<n ; k++){
C[i,j] = A[i,j] * B[i,j];
}
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
}
}
}
Time Compilexity
Space Complexity
S(n) = n2 + n2 + n2 + 1 +3
= 3n2
Source Code
#include <stdio.h>
#define ROWS1 3
#define COLS1 3
#define ROWS2 3
#define COLS2 3
int main() {
int matrix1[ROWS1][COLS1] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
int matrix2[ROWS2][COLS2] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int product[ROWS1][COLS2];
Output
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
PROGRAM – 3
PART 3A
Time Complexity
T(n) = O(n2)
Space Complexity
S(n) = O(n)
Source Code
#include <stdio.h>
int main()
{
int i, j, min, swap;
min=i;
for(j = i + 1; j < 5; j++){
if(a[min] > a[j])
min=j;
}
if(min != i){
swap=a[i];
a[i]=a[min];
a[min]=swap;
}
}
printf("Sorted Array:");
for(i = 0; i < 5; i++)
printf("%d \t", a[i]);
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
}
Output Code
PART – 3B
Algorithm
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
j--;
}
a[j+1] = temp;
}
Time Complexity
T(n) = O(n2)
Space Complexity
S(n) = O(n)
Source Code
#include <math.h>
#include <stdio.h>
int main(){
int i, j, temp;
int a[5] = {9, 5, 1, 4, 3};
for(i=0; i< 5; i++){
temp = a[i];
j = i-1;
printf("Sorted Array:");
for(i=0; i<5; i++){
printf("%d \t", a[i]);
}
}
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
Output
PROGRAM – 4
Algorithm
for(i=0; i<n; i++){
mid = (beg+end)/2;
if(a[mid] == key){
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
return mid;
}
else if(a[mid] > key){
end = mid-1;
}
else{
beg = mid+1;
}
}
Time Complexity
T(n) = O(nlogn)
Space Complexity
S(n) = O(1)
Source Code
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements :");
scanf("%d",&n);
printf("Enter %d integers : \n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find :");
scanf("%d", &key);
low = 0;
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
-+if(low > high)
printf("Not found! %d isn't present in the list.", key);
return 0;
}
Output
PROGRAM – 5
AIM : WAP to implement a Quick sort for Average and Worst Case.
Algorithm
Source code
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main(){
int i, n, arr[100];
printf("Enter the size of array : ");
scanf("%d", &n);
printf("Enter the element in array : ");
for(i=0; i<n; i++){
scanf("%d", &arr[i]);
}
printf("Unsortd array : ");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("Sorted array : ");
printArray(arr, n);
}
PART – 5A
Average Case
Recursive Equation
Time Complexity
T(n) = O(nlogn)
Space Complexity
S(n) = O(logn)
Output
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
PART – 5B
Worst Case
Recursive Equation
T(n) = T(n – k) + n
Time Complexity
T(n) = O(n2)
Space complexity
S(n) = O(logn)
Output
PROGRAM – 6
PART – 6A
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
Approach
Dynamic Approach
Algorithm
For i=0 to n
For w=0 to m
if( i == 0 && w == 0){
k[i][w] = 0}
else if( wt[i] <= w){
k[i][w] = max( p[i] + k[i-1][w-wt[i]], k[i -1][w]) }
else{
k[i][w] = k[i-1][w] }
Time Complexity
Space Complexity
S(n) = O(nw)
Soucre Code
#include <stdio.h>
int main() {
int n, m, p[20], wt[20], k[20][20], i, w;
printf("Items selected:\n");
i = n - 1;
w = m;
while (i >= 0 && w >= 0) {
if (i == 0 && k[i][w] > 0) {
printf("%d\t", i+1);
break;
}
if (k[i][w] != k[i-1][w]) {
printf("%d\t", i+1);
w -= wt[i];
}
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
i--;
}
return 0;
}
Output
PART – 6B
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
AIM : WAP for Fraction Knapsack
Approach
Greedy Approach
Algorithm
For i=1 to n
Calculate profit/weight
Sort objects in decreasing order of p/w ratio
For i =1 to n
if(m > 0 && wi < m){
m = m – wi
p = p – pi }
else{
break }
if (m > 0){
p = p + (m / wi)}
Time Complexity
T(n) = O(nlogn)
Space Complexity
S(n) = O(n)
Source Code
#include <stdio.h>
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int main(){
int i, j, n, m;
float c[100], a[100], b[100], y, p=0;
if(m>0){
p = p + a[i] * (m / b[i]);
}
Output
Devesh Hooda
CSE-22/035