0% found this document useful (0 votes)
29 views

Daa File

The document discusses an algorithm lab file submitted by Devesh Hooda. It includes programs on introduction to algorithms, characteristics of algorithms, asymptotic notation, and programs to add array elements and matrices. The file contains details of the programs, their algorithms, time and space complexity analysis, and source code.

Uploaded by

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

Daa File

The document discusses an algorithm lab file submitted by Devesh Hooda. It includes programs on introduction to algorithms, characteristics of algorithms, asymptotic notation, and programs to add array elements and matrices. The file contains details of the programs, their algorithms, time and space complexity analysis, and source code.

Uploaded by

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

SATYUG DARSHAN INSTITUTE OF

ENGINEERING AND TECHNOLOGY

A
LAB FILE
ON
DESIGN AND ANALYSIS OF AN ALGORITHM
( PCC_CS _407 )

Submitted To: Submitted By:


Ms. Bhawana Srivastava Devesh Hooda
ASST. PROF CSE -22 /035
CSE DEPT. B.TECH CSE-4A
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
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

AIM: Introduction and Characteristics of an algorithm.

 Definition of Algorithm:

The word Algorithm means ” A set of finite rules or instructions to be followed in calculations or
other problem-solving operations”

 Use of the Algorithms:


Algorithms play a crucial role in various fields and have many applications. Some of the key areas
where algorithms are used include:
Computer Science: Algorithms form the basis of computer programming and are used to solve problems
ranging from simple sorting and searching to complex tasks such as artificial intelligence and machine
learning.

 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:

 It should terminate after a finite time.


 It should produce at least one output.
 It should take zero or more input.
 It should be deterministic means giving the same output for the same input case.
 Every step in the algorithm must be effective i.e., every step should do some work.

 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:

 Writing an algorithm takes a long time so it is time-consuming.


 Understanding complex logic through algorithms can be very difficult.
 Branching and Looping statements are difficult to show in Algorithms(imp).

 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 Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It


may or may not take input.

 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.

 Language Independent: The Algorithm designed must be language-independent, i.e., it must


be just plain instructions that can be implemented in any language, and yet the output will be
the same, as expected.

 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

 Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to


interpret. By referring to any of the instructions in an algorithm one can clearly understand
what is to be done. Every fundamental operator in instruction must be defined without any
ambiguity.

 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.

 Effectiveness: An algorithm must be developed.

 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.

 Types of Asymptotic Notation:


The analysis of an algorithm is done based on three cases:
 Best Case (Omega Notation (Ω))
 Average Case (Theta Notation (Θ))
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
 Worst Case (Big Oh Notation(O))
 Little Omega (Ω)
 Little Theta (Θ)

1. Omega (Ω) Notation:

 Represent Best Case.


 Least Upper Bound.
 f(n) = Ω g(n).
 f(n) >= c.g(n).

2. Theta (Θ) Notation:

 Represent Average Case.


 Exact Time.
 C1.g(n) <= f(n) <= c2.g(n).

Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
3. Big Oh(O) Notation:

 Represent Worst case.


 Upper Bound (at most).
 Least Upper Bound.
 f(n) = O(g(n))
 f(n) <= c. g(n)

4. Little Omega:

 f(n) < c.g(n)

5. Little Oh:

 f(n) > c.g(n)

Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407

PROGRAM – 02
PART – 2A

AIM : WAP to add array elements.

 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() {

int arr[] = {1, 2, 3, 4, 5};


int size = sizeof(arr) / sizeof(arr[0]);
int sum = 0;

for (int i = 0; i < size; i++) {


sum += arr[i];
}

printf("Sum of all elements of the array: %d\n", sum);

Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407

 Output

PART - 2B

AIM : WAP to add the element of given 2 matrix of N x N.

 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

T(n) = (n + 1) + ( n(n + 1) ) + (n2)


= 2n2 + 2n + 1

 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];

for (int i = 0; i < ROWS; i++) {


for (int j = 0; j < COLS; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

printf("Sum of the two matrices:\n");


for (int i = 0; i < ROWS; i++) {
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
for (int j = 0; j < COLS; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}

 Output

PART - 2C

AIM : WAP to multiply the element of given 2 matrix of N x N.

 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

T(n) = (n +1) + (n(n + 1)) + (n2) + (n(n(n + 1))) + (n3)


= 2n3 + 3n2 + 2n + 1
= O(n3)

 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];

for (int i = 0; i < ROWS1; i++) {


for (int j = 0; j < COLS2; j++) {
product[i][j] = 0;
for (int k = 0; k < COLS1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

printf("Product of the two matrices:\n");


for (int i = 0; i < ROWS1; i++) {
for (int j = 0; j < COLS2; j++) {
printf("%d\t", product[i][j]);
}
printf("\n");
}

 Output

Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407

PROGRAM – 3
PART 3A

AIM : WAP to implement a Selection Sort.


 ALGORIHM

for(i=0; i<n; i++){


int min = 1;
for(j=i+1; j<n; j++){
if(a[j] < a[min]){
min = j;
}
if(min != i){
swap(a[i],a[min]);
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
}
}
}

 Time Complexity

T(n) = O(n2)

 Space Complexity

S(n) = O(n)

 Source Code

#include <stdio.h>
int main()
{
int i, j, min, swap;

int a[5] = {20, 12, 10, 15, 2};


for(i = 0; i < 4; i++){

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

AIM : WAP to implement a insertion Sort.

 Algorithm

for(i=1; i<n; i++){


temp = a[i];
j = i-1;

while(j >= 0 && a[j] > temp){


a[i+1] = a[j];

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;

while(j >= 0 && a[j] > temp){


a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}

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

AIM : WAP to implement a Binary Search.

 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

QuickSort(arr, low, high):


if low < high:
pivot = Partition(arr, low, high)
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
QuickSort(arr, low, pivot - 1)
QuickSort(arr, pivot + 1, high)

Partition(arr, low, high):


pivot = arr[high]
i = low - 1
for j = low to high - 1:
if arr[j] <= pivot:
i=i+1
Swap(arr[i], arr[j])
Swap(arr[i + 1], arr[high])
return i + 1

 Source code

#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {


if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}

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

T(n) = 2T(n / 2k) + n

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

AIM : WAP for 0/1 Knapsack

 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

T(n) = O(nw) ( where, w is the capacity of the knapsack)

 Space Complexity

S(n) = O(nw)

 Soucre Code

#include <stdio.h>

int max(int a, int b) {


return (a > b) ? a : b;
}

int main() {
int n, m, p[20], wt[20], k[20][20], i, w;

printf("Enter the no. of items:");


Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
scanf("%d", &n);
printf("Enter the capacity of knapsack:");
scanf("%d", &m);

for (i = 0; i < n; i++) {


printf("Profit [%d] = ", i+1);
scanf("%d", &p[i]);
printf("Weight[%d] = ", i+1);
scanf("%d", &wt[i]);
}

for (i = 0; i < n; i++) {


for (w = 0; w <= m; w++) {
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];
}
}
}

for(i=0; i<n; i++){


for(int j=0; j<=m; j++){
printf("%d", k[i][j]);
printf("\t");
}printf("\n");
}

printf("Maximum Profit is %d\n", k[n-1][m]);

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>

void swap(float a[], int i, int j){


float temp;

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;

printf("Enter the nummber of objects:");


scanf("%d", &n);

printf("Enter the capacity of knapsack:");


scanf("%d", &m);

printf("Enter the profits of the objects:");


for(i=0; i<n; i++){
scanf("%f", &a[i]);
}

printf("Enter the weight of the object:");


for(i=0; i<n; i++){
scanf("%f", &b[i]);
}

for(i-0; i<n; i++){


c[i] = a[i]/b[i];
}

for(i=0; i<n; i++){


for(j=i+1; j<n; j++){
if(c[i] < c[j]){
swap(c, i, j);
swap(a, i, j);
swap(b, i, j);
}
}
}

for(i=0; i<n; i++){


if(m>0 && b[i]<=m){
m = m-b[i];
p = p+a[i];
}
else{
Devesh Hooda
CSE-22/035
DESIGN AND ANALYSIS OF ALGORTHM LAB FILE
PCC-CS-407
break;
}
}

if(m>0){
p = p + a[i] * (m / b[i]);
}

printf("Maximum profit is %f", p);

 Output

Devesh Hooda
CSE-22/035

You might also like