Name - Aryan Gupta Reg. No. 199301088 Section - B
Name - Aryan Gupta Reg. No. 199301088 Section - B
Name - Aryan Gupta Reg. No. 199301088 Section - B
Ans.1)
Code –
# include<stdio.h>
# include<conio.h>
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]);
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);
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>
/*
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;
}
/*
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/
*/
// 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};
node* z = greedy_huffman_code(C);
inorder(z); //printing tree
printf("\n");
return 0;
}
Output –