C++ Program to Solve the Fractional Knapsack Problem

/* program to implement fractional knapsack problem using greedy programming */

 #include <iostream>

using namespace std;
int main()
{
    int capacity, no_items, cur_weight, item;
    int used[10];
    float total_profit;
    int i;
    int weight[10];
    int value[10];

    cout<<("Enter the capacity of knapsack:\n");
    cin>>(capacity);

    cout<<("Enter the number of items:\n");
    cin>>(no_items);

    cout<<"Enter the weight and value of "<<no_items<<"item:\n";
    for (i = 0; i < no_items; i++)
    {
        cout<<"Weight "<<i<<"th item"<<"=";
        cin>>weight[i];
        cout<<"value "<<i<<"th item"<<"=";
        cin>>value[i];
    }

    for (i = 0; i < no_items; ++i)
        used[i] = 0;

    cur_weight = capacity;
    while (cur_weight > 0)
    {
        item = -1;
        for (i = 0; i < no_items; ++i)
            if ((used[i] == 0) &&
                ((item == -1) || ((float) value[i] / weight[i] > (float) value[item] / weight[item])))
                item = i;

        used[item] = 1;
        cur_weight -= weight[item];
        total_profit += value[item];
        if (cur_weight >= 0)
            cout<<"Added object="<<item + 1<<"\n"<<"profit="<<value[item]<<"\n"<<"weight="<<weight[item]<<"\n"<<"completely in the bag Space left="<<cur_weight<<"\n";
        else
        {
            int item_percent = (int) ((1 + (float) cur_weight / weight[item]) * 100);
            cout<<"Addedobject in the bag="<<item + 1<<"\n"<<"profit="<<value[item]<<"\n"<<"weight="<<weight[item]<<"\n";
            total_profit -= value[item];
            total_profit += (1 + (float)cur_weight / weight[item]) * value[item];
        }
    }

    cout<<"Filled the bag with objects total Profit.\n"<<total_profit;
}


Comments

Popular Posts