28
Sep
0

[C++] Greedy algorithm (algorithme glouton) for knapsack problem

Here is an old code for the knapsack problem solved using the greedy algorithm.

/** Berenger (contact@berenger.eu)
  *
  * Greedy algorithm (algorithme glouton) for knapsack problem.
  *
  * Even if the Greedy algorithm do not alway give the best (optimal)
  * solution it is good to know how to implement it since it is
  * very easy to understand and can solve many problems.
  *
  * g++ main.cpp -O2 -o test
  */

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

template <class T>
T Min(const T& v1, const T& v2){
    return (v1 < v2 ? v1 : v2);
}

// The stuff
struct Stuff {
    std::string name;   // Object name
    int weight;         // Object weight (in bag)
    int value;          // Object value (to maximize)
    int nb;             // Number of this object avaiblable
};

bool stuffValueCmp(const Stuff& s1,const Stuff s2) {
    // Sort using value only
    // return (s1.value > s2.value);
    // Sort with v/w
    return (s1.value/s1.weight > s2.value/s2.weight);
}

// The greedy algorithm
template <class T>
int Glouton(const std::vector<T>& objects, const int MaxWeights, std::vector<int>& occurences ){
    int currentWeight = 0;
    for(int idxObject = 0; idxObject < objects.size() ; ++idxObject){
        if( objects[idxObject].weight < MaxWeights - currentWeight){
            occurences[idxObject] = Min( (MaxWeights - currentWeight) / objects[idxObject].weight, objects[idxObject].nb);
            currentWeight += occurences[idxObject] * objects[idxObject].weight;
        }
    }

    return currentWeight;
}

// The main
int main(){
    std::vector<Stuff> stuffs;

    // Create the Stuff
    int currentStuff = 0;
    stuffs.resize(currentStuff + 1);
    stuffs[currentStuff].name = "Food";
    stuffs[currentStuff].weight = 1;
    stuffs[currentStuff].value = 3;
    stuffs[currentStuff].nb = 3;

    ++currentStuff;
    stuffs.resize(currentStuff + 1);
    stuffs[currentStuff].name = "Drink";
    stuffs[currentStuff].weight = 2;
    stuffs[currentStuff].value = 2;
    stuffs[currentStuff].nb = 10;

    ++currentStuff;
    stuffs.resize(currentStuff + 1);
    stuffs[currentStuff].name = "Computer";
    stuffs[currentStuff].weight = 4;
    stuffs[currentStuff].value = 1;
    stuffs[currentStuff].nb = 1;

    ++currentStuff;
    stuffs.resize(currentStuff + 1);
    stuffs[currentStuff].name = "Map";
    stuffs[currentStuff].weight = 1;
    stuffs[currentStuff].value = 5;
    stuffs[currentStuff].nb = 4;

    // Sort based on the value greater to lower
    sort(stuffs.begin(), stuffs.end(), stuffValueCmp);

    // Solve
    static const int MaxWeight = 10;
    std::vector<int> nbStuffInBag(stuffs.size(), 0);
    const int currentWeight = Glouton(stuffs, MaxWeight, nbStuffInBag);

    // Print
    int totalValue = 0;
    std::cout << "The weight of our bag is " << currentWeight << " (Max is " << MaxWeight << ")" << std::endl;
    for(int idxStuff = 0 ; idxStuff < stuffs.size() ; ++ idxStuff){
        std::cout << "There is " << nbStuffInBag[idxStuff] << " " << stuffs[idxStuff].name << std::endl;
        totalValue += stuffs[idxStuff].value * nbStuffInBag[idxStuff];
    }
    std::cout << "The total value of our bag is " << totalValue << std::endl;

    return 0;
}
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.

Comments are closed.

Celadon theme by the Themes Boutique