Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Name - Aryan Gupta Reg. No. 199301088 Section - B

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Name – Aryan Gupta

Reg. no. 199301088


Section – B

Ans.1)

Code –

# include<stdio.h>
# include<conio.h>

void knapsack(int n, float weight[], float profit[], float capacity)


{
float x[20], tp= 0;
int i, j, u;
u=capacity;

for (i=0;i<n;i++)
x[i]=0.0;

for (i=0;i<n;i++)
{
if(weight[i]>u)
break;
else
{
x[i]=1.0;
tp= tp+profit[i];
u=u-weight[i];
}
}

if(i<n)
x[i]=u/weight[i];

tp= tp + (x[i]*profit[i]);

printf("\nThe result vector is:- ");


for(i=0;i<n;i++)
printf("%f ",x[i]);

printf("\nMaximum profit is:- %f", tp);

void main()
{
float weight[20], profit[20], capacity;
int n, i ,j;
float ratio[20], temp;
printf ("\nEnter the no. of objects:- ");
scanf ("%d", &n);

printf ("\nEnter the wts and profits of each object:- \n");


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

printf ("\nEnter the capacity of knapsack:- ");


scanf ("%f", &capacity);

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


{
ratio[i]=profit[i]/weight[i];
}

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


{
for(j=i+1;j< n; j++)
{
if(ratio[i]<ratio[j])
{
temp= ratio[j];
ratio[j]= ratio[i];
ratio[i]= temp;

temp= weight[j];
weight[j]= weight[i];
weight[i]= temp;

temp= profit[j];
profit[j]= profit[i];
profit[i]= temp;
}
}
}
knapsack(n, weight, profit, capacity);
}

Output –
Ans.2)

Code –

#include <stdio.h>
#include <stdlib.h>

typedef struct node


{
int frequency;
char data;
struct node *left;
struct node *right;
}node;

int heap_array_size = 100; // size of array storing heap


int heap_size = 0;
const int INF = 100000;

//function to swap nodes


void swap( node *a, node *b ) {
node t;
t = *a;
*a = *b;
*b = t;
}
/*
function to print tree
https://www.codesdope.com/blog/article/binary-tree-in-c-linked-
representation-traversals/
*/
void inorder(struct node *root)
{
if(root!=NULL) // checking if the root is not null
{
inorder(root->left); // visiting left child
printf(" %d ", root->frequency); // printing data at root
inorder(root->right);// visiting right child
}
}

/*
function for new node
*/
node* new_node(char data, int freq) {
node *p;
p = malloc(sizeof(struct node));
p->data = data;
p->frequency = freq;
p->left = NULL;
p->right = NULL;
return p;
}

//function to get right child of a node of a tree


int get_right_child(int index) {
if((((2*index)+1) <= heap_size) && (index >= 1))
return (2*index)+1;
return -1;
}

//function to get left child of a node of a tree


int get_left_child(int index) {
if(((2*index) <= heap_size) && (index >= 1))
return 2*index;
return -1;
}

//function to get the parent of a node of a tree


int get_parent(int index) {
if ((index > 1) && (index <= heap_size)) {
return index/2;
}
return -1;
}

/*
Functions taken from minimum priority queue
https://www.codesdope.com/blog/article/priority-queue-using-heap/
https://www.codesdope.com/blog/article/heap-binary-heap/
*/

void insert(node A[], node* a, int key) {


heap_size++;
A[heap_size] = *a;
int index = heap_size;
while((index>1) && (A[get_parent(index)].frequency > a->frequency)) {
swap(&A[index], &A[get_parent(index)]);
index = get_parent(index);
}
}

node* build_queue(node c[], int size) {


node* a = malloc(sizeof(node)*heap_array_size); // a is the array to store
heap
int i;
for(i=0; i<size; i++) {
insert(a, &c[i], c[i].frequency); // inserting node in array a(min-queue)
}
return a;
}

void min_heapify(node A[], int index) {


int left_child_index = get_left_child(index);
int right_child_index = get_right_child(index);

// finding smallest among index, left child and right child


int smallest = index;

if ((left_child_index <= heap_size) && (left_child_index>0)) {


if (A[left_child_index].frequency < A[smallest].frequency) {
smallest = left_child_index;
}
}

if ((right_child_index <= heap_size && (right_child_index>0))) {


if (A[right_child_index].frequency < A[smallest].frequency) {
smallest = right_child_index;
}
}

// smallest is not the node, node is not a heap


if (smallest != index) {
swap(&A[index], &A[smallest]);
min_heapify(A, smallest);
}
}

node* extract_min(node A[]) {


node minm = A[1];
A[1] = A[heap_size];
heap_size--;
min_heapify(A, 1);
node *z;
// copying minimum element
z = malloc(sizeof(struct node));
z->data = minm.data;
z->frequency = minm.frequency;
z->left = minm.left;
z->right = minm.right;
return z; //returning minimum element
}

// Huffman code
node* greedy_huffman_code(node C[]) {
node *min_queue = build_queue(C, 6); // making min-queue

while(heap_size > 1) {
node *h = extract_min(min_queue);
node *i = extract_min(min_queue);
node *z;
z = malloc(sizeof(node));
z->data = '\0';
z->left = h;
z->right = i;
z->frequency = z->left->frequency + z->right->frequency;
insert(min_queue, z, z->frequency);
}
return extract_min(min_queue);
}

int main() {
node *a = new_node('a', 15);
node *b = new_node('b', 25);
node *c = new_node('c', 5);
node *d = new_node('d', 30);
node *e = new_node('e', 25);
node *f = new_node('f', 40);
node C[] = {*a, *b, *c, *d, *e , *f};

printf("Huffman tree is in the following sequence:\n\n");

node* z = greedy_huffman_code(C);
inorder(z); //printing tree
printf("\n");

return 0;
}
Output –

You might also like