Loading…
DAA · Greedy · Program 15 · In the record
Write a program in c to solve the fractional knapsack problem using the greedy method and find its execution time using the time function.
Algorithm: FractionalKnapsack(v, w, n, W) 1. Start 2. For i <- 1 to n do 3. r[i] <- v[i]/w[i] 4. x[i] <- 0 5. Sort the items in decreasing order of r[i] (merge sort) 6. remaining <- W, total <- 0 7. For each item i in sorted order, while remaining > 0 do 8. if w[i] <= remaining then 9. x[i] <- 1 10. remaining <- remaining - w[i] 11. else 12. x[i] <- remaining/w[i] 13. remaining <- 0 14. total <- total + x[i]*v[i] 15. print "Maximum value" total 16. Stop
1#include<stdio.h>2#include<time.h>3 4double r[50];5int count=0;6 7void merge(int a[],int low,int mid,int high) {8 int n1=mid-low+1,n2=high-mid;9 int left[n1],right[n2];10 int i,j,k;11 12 for(i=0;i<n1;i++)13 left[i]=a[low+i];14 for(j=0;j<n2;j++)15 right[j]=a[mid+1+j];16 17 i=0;18 j=0;19 k=low;20 while(i<n1 && j<n2) {21 count++;22 if(r[left[i]]>=r[right[j]])23 a[k++]=left[i++];24 else25 a[k++]=right[j++];26 }27 while(i<n1)28 a[k++]=left[i++];29 while(j<n2)30 a[k++]=right[j++];31}32 33void mergeSort(int a[],int low,int high) {34 if(low<high) {35 int mid=(low+high)/2;36 mergeSort(a,low,mid);37 mergeSort(a,mid+1,high);38 merge(a,low,mid,high);39 }40}41 42int main() {43 int n,i,k;44 double W;45 printf("Enter the number of items:");46 scanf("%d",&n);47 double v[n+1],w[n+1],x[n+1];48 int order[n];49 printf("Enter the values of %d items:",n);50 for(i=1;i<=n;i++)51 scanf("%lf",&v[i]);52 printf("Enter the weights of %d items:",n);53 for(i=1;i<=n;i++)54 scanf("%lf",&w[i]);55 printf("Enter the capacity of the knapsack:");56 scanf("%lf",&W);57 58 clock_t start=clock();59 for(i=1;i<=n;i++) {60 r[i]=v[i]/w[i];61 x[i]=0;62 order[i-1]=i;63 }64 mergeSort(order,0,n-1);65 double remaining=W,total=0;66 for(k=0;k<n && remaining>0;k++) {67 i=order[k];68 count++;69 if(w[i]<=remaining) {70 x[i]=1;71 remaining=remaining-w[i];72 }73 else {74 x[i]=remaining/w[i];75 remaining=0;76 }77 total=total+x[i]*v[i];78 }79 clock_t end=clock();80 double time_taken=(double)(end-start)/CLOCKS_PER_SEC;81 82 printf("\nItems in decreasing order of value/weight:\nItem\tValue\tWeight\tv/w\tFraction");83 for(k=0;k<n;k++) {84 i=order[k];85 printf("\n%d\t%g\t%g\t%g\t%g",i,v[i],w[i],r[i],x[i]);86 }87 printf("\nMaximum value:%g",total);88 printf("\nNumber of comparisons:%d",count);89 printf("\nTime taken:%f seconds",time_taken);90 printf("\nTime complexity: O(n log n)");91 printf("\nSpace complexity: O(n)");92 printf("\n");93 return 0;94}4 ⏎ 10 40 30 50 ⏎ 5 4 6 3 ⏎ 10Enter the number of items:4 Enter the values of 4 items:10 40 30 50 Enter the weights of 4 items:5 4 6 3 Enter the capacity of the knapsack:10 Items in decreasing order of value/weight: Item Value Weight v/w Fraction 4 50 3 16.6667 1 2 40 4 10 1 3 30 6 5 0.5 1 10 5 2 0 Maximum value:105 Number of comparisons:8 Time taken:0.000006 seconds Time complexity: O(n log n) Space complexity: O(n)
Run program 15 in your browser
C · clang → WebAssembly. Change the code or the input, run it, break it, and see what happens.