DAA Lab Assignment 1
DAA Lab Assignment 1
A] Fractional Knapsack
Aim:
To write the algorithm, code, screenshot, test cases (2) and the
result of the Fractional Knapsack Problem using Greedy Strategy
C Language.
Algorithm:
Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)
for i = 1 to n
do x[i] = 0
weight = 0
for i = 1 to n
if weight + w[i] ≤ W then
x[i] = 1
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
break
return x
Code:
#include<stdio.h>
int n=4;
int c[10]={10,40,20,24};//weight
int v[10]={100,280,120,120};//profit
int W = 60;
void simple_fill() {
int cur_w;
float tot_v;
int i, maxi;
int used[10];
cur_w = W;
while (cur_w > 0) {
maxi = -1;
for (i = 0; i < n; ++i)
if ((used[i] == 0) &&
((maxi == -1) || ((float)v[i]/c[i] >(float)v[maxi]/c[maxi])))
maxi = i;
used[maxi] = 1;
cur_w -= c[maxi];
tot_v += v[maxi];
if (cur_w >= 0)
printf("Added object %d (%d$, %dKg) completely in the bag. Space
left: %d.\n", maxi + 1, v[maxi], c[maxi], cur_w);
else {
printf("Added %d%% (%d$, %dKg) of object %d in the bag.\n",
(int)((1 + (float)cur_w/c[maxi]) * 100), v[maxi], c[maxi], maxi + 1);
tot_v -= v[maxi];
tot_v += (1 + (float)cur_w/c[maxi]) * v[maxi];
}
}
Execution:
Result:
Thus, we successfully verified and drove output using Fractional
Knapsack Problem using Greedy Strategy C Language.
B] Huffman Coding
Aim:
To write the algorithm, code, screenshot, test cases (2) and the
result of the Huffman Coding using Greedy Strategy C Language.
Algorithm:
Procedure Huffman(C): // C is the set of n characters and related information
n = C.size
Q = priority_queue()
for i = 1 to n
n = node(C[i])
Q.push(n)
end for
while Q.size() is not equal to 1
Z = new node()
Z.left = x = Q.pop
Z.right = y = Q.pop
Z.frequency = x.frequency + y.frequency
Q.push(Z)
end while
Return Q
Code:
// C program for Huffman Coding
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_HT 100
return temp;
}
{
struct MinHeap* minHeap
= (struct MinHeap*)malloc(sizeof(struct MinHeap));
// current size is 0
minHeap->size = 0;
minHeap->capacity = capacity;
// A utility function to
// swap two min heap nodes
void swapMinHeapNode(struct MinHeapNode** a,
struct MinHeapNode** b)
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
++minHeap->size;
int i = minHeap->size - 1;
while (i
&& minHeapNode->freq
< minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeapNode;
}
int n = minHeap->size - 1;
int i;
printf("\n");
}
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
{
struct MinHeapNode *left, *right, *top;
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
// Assign 1 to right edge and recur
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
{
// Construct Huffman Tree
struct MinHeapNode* root
= buildHuffmanTree(data, freq, size);
int main()
{
return 0;
}
Program:
Execution:
Test Case 1:
Test Case :
Result:
Thus, we successfully verified and drove output using Huffman
Coding using Greedy Strategy C Language.
Dynamic Programming
Aim:
To write the algorithm, code, screenshot, test cases (2) and the
result of the Matrix Chain Multiplication using Dynamic
Programming C Language.
Algorithm:
Begin
define table minMul of size n x n, initially fill with all 0s
for length := 2 to n, do
fir i:=1 to n-length, do
j := i + length – 1
minMul[i, j] := ∞
for k := i to j-1, do
q := minMul[i, k] + minMul[k+1, j] + array[i-1]*array[k]*array[j]
if q < minMul[i, j], then minMul[i, j] := q
done
done
done
return minMul[1, n-1]
End
Code:
#include <stdio.h>
#include<limits.h>
#define INFY 999999999
long int m[20][20];
int s[20][20];
int p[20],i,j,n;
void matmultiply(void)
{
long int q;
int k;
for(i=n;i>0;i--)
{
for(j=i;j<=n;j++)
{
if(i==j)
m[i][j]=0;
else
{
for(k=i;k<j;k++)
{
q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
}
}
void main()
{
int k;
printf("Enter the no. of elements: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
m[i][i]=0;
m[i][j]=INFY;
s[i][j]=0;
}
printf("\nEnter the dimensions: \n");
for(k=0;k<=n;k++)
{
printf("P%d: ",k);
scanf("%d",&p[k]);
}
matmultiply();
printf("\nCost Matrix M:\n");
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
printf("m[%d][%d]: %ld\n",i,j,m[i][j]);
i=1,j=n;
printf("\nMultiplication Sequence : ");
print_optimal(i,j);
printf("\nMinimum number of multiplications is : %d ",
MatrixChainOrder(p, 1, n));
}
Program:
Execution:
Test Case 1:
Test Case: 2
Result:
Thus, we successfully verified and drove output using Matrix
Chain Multiplication using Dynamic Programming C Language.
Aim:
To write the algorithm, code, screenshot, test cases (2) and the
result of the Longest Common Subsequence using Dynamic
Programming C Language.
Algorithm:
/*
* find the length of the LCS
*/
for(r = 0; r <= XLen; r++) {
if(r == 0 || c == 0) {
L[r][c] = 0;
} else {
/*
* Print LCS
*/
r = XLen;
c = YLen;
i = L[r][c];
char LCS[i+1];
/*
* setting the NULL character at the end of LCS character array.
* as we know in C programming language, NULL character is used
* to denote the end of a string
*/
LCS[i] = '\0';
i--;
r--;
c--;
r--;
} else {
c--;
//print result
printf("Length of the LCS: %d\n", L[XLen][YLen]);
printf("LCS: %s\n", LCS);
int q,w;
for (q=0;q<YLen;q++)
{
for (w=0;w<XLen;w++)
{
printf("%d ",L[q][w]);
}
printf ("\n");
}
}
int main(void) {
//the two sequences
char X[] = "ABCF";
char Y[] = "ABCDEF";
return 0;
}
Program:
Execution:
Test Case 1:
Test Case 2:
Result:
Thus, we successfully verified and drove output using Longest
Common Subsequence using Dynamic Programming C Language.
Divide And Conquer
A] Maximum Subarray
Aim:
To write the algorithm, code, screenshot, test cases (2) and the
result of the Maximum Subarray using Divide and Conquer C
Language.
Algorithm:
int maxSubarraySum ( int A [] , int n)
{
int max_sum = 0
for(i = 0 to n-1)
{
for(j = i to n-1)
{
int sum = 0
for(k = i to j)
sum = sum + A[k]
if(sum > max_sum)
max_sum = sum
}
}
return max_sum
}
Code:
#include <stdio.h>
#include <limits.h>
// Utility function to find the maximum of two numbers
int max(int x, int y) {
return (x > y) ? x : y;
}
int main(void)
{
int arr[] = { 2, -4, 1, 9, -6, 7, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Program:
Execution:
Test Case 1:
Test Case 2:
Result:
Thus, we successfully verified and drove output using Maximum
Subarray using Divide and Conquer C Language.
B] Karatsuba faster integer multiplication
Aim:
To write the algorithm, code, screenshot, test cases (2) and the
result of the Karatsuba faster integer multiplication using Divide
and Conquer C Language.
Algorithm:
// The main function that adds two bit sequences and returns the addition
string addBitStrings( string first, string second )
{
string result; // To store the sum bits
return result;
}
// The main function that multiplies two bit strings X and Y and returns
// result as long integer
long int multiply(string X, string Y)
{
// Find the maximum of lengths of x and Y and make length
// of smaller string same as that of larger string
int n = makeEqualLength(X, Y);
// Base cases
if (n == 0) return 0;
if (n == 1) return multiplyiSingleBit(X, Y);
Result:
Thus, we successfully verified and drove output using Karatsuba
faster integer multiplication using Divide and Conquer C
Language.