answersLogoWhite

0

#include<iostream>

#include<vector>

#include<cassert>

template<typename _Ty>

class subset

{

public:

using value_type = typename _Ty::const_iterator;

using container_type = std::vector<value_type>;

using iterator = typename container_type::iterator;

using const_iterator = typename container_type::const_iterator;

using reverse_iterator = typename container_type::reverse_iterator;

using const_reverse_iterator = typename container_type::const_reverse_iterator;

using size_type = typename container_type::size_type;

~subset (void) {};

explicit subset (const _Ty&);

subset (const subset&) = delete;

subset (subset&&) = delete;

subset& operator= (const subset&) = delete;

subset& operator= (subset&&) = delete;

const container_type& first (size_type);

const container_type& next (void);

private:

size_type m_size; // desired size of the subset

container_type m_set; // iterators to the full set

container_type m_subset; // subset of the full set of iterators

const_iterator find (const value_type&) const;

const bool get_next (const bool = true);

};

template<typename _Ty>

inline subset<_Ty>::subset (const _Ty& set):

m_size {set.size()}, m_set {}, m_subset {}

{

for (value_type it=set.begin(); it!=set.end(); ++it)

m_set.push_back (it);

m_set.shrink_to_fit();

}

template<typename _Ty>

inline typename subset<_Ty>::const_iterator subset<_Ty>::find (

const value_type& value) const

{

const_iterator it=m_set.begin();

while (it!=m_set.end() && *it!=value) ++it;

return it;

}

template<typename _Ty>

inline const typename subset<_Ty>::container_type& subset<_Ty>::first (

size_type count)

{

assert (count <= m_set.size());

m_size = count;

m_subset.clear();

for (const_iterator it=m_set.begin(); count--; ++it)

m_subset.push_back (*it);

return m_subset;

}

template<typename _Ty>

inline const bool subset<_Ty>::get_next (

const bool pop /* = true */)

{

const_reverse_iterator sub_it = m_subset.rbegin();

const_iterator set_it = find (*sub_it);

if (pop)

m_subset.pop_back();

if (++set_it != m_set.end())

m_subset.push_back (*set_it);

return m_subset.size() 1U)

return m_subset;

const value_type value = m_set.back();

m_set.pop_back();

--m_size;

next();

m_set.push_back (value);

if (m_size++ != m_subset.size())

return m_subset;

get_next (false);

return m_subset;

}

struct item

{

size_t value {0};

size_t weight {0};

item (size_t v=0, size_t w=0): value{v}, weight{w} {}

};

using vector = std::vector<item>;

item sum (vector& v)

{

item result;

for( auto i : v)

{

result.weight += i.weight;

result.value += i.value;

}

return result;

}

vector knapsack (vector items, size_t capacity)

{

vector v;

subset<vector> set {items};

for (size_t n=items.size(); n; --n)

{

subset<vector>::container_type selection {set.first (n)};

for (; selection.size(); selection=set.next())

{

vector t;

for (auto i : selection)

{

t.push_back (*i);

}

item s = sum (t);

if (s.weight <= capacity && sum(v).value < s.value)

v = t;

}

}

return v;

}

int main()

{

size_t capacity {4};

vector items {{1,8},{2,4},{3,1},{2,5},{2,3}};

vector rucksack = knapsack (items, capacity);

std::cout << "Items:\n" << std::endl;

for (auto i : items)

std::cout << '£' << i.value << '\t' << i.weight << "kg" << std::endl;

std::cout << std::endl;

std::cout << "Optimal selection for capacity " << capacity << "kg\n" << std::endl;

for (auto r : rucksack)

std::cout << '£' << r.value << '\t' << r.weight << "kg" << std::endl;

std::cout << std::endl;

item s = sum (rucksack);

std::cout << "Total value and weight:\n" << std::endl;

std::cout << '£' << s.value << '\t' << s.weight << "kg" << std::endl;

std::cout << std::endl;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Algorithm of push and pop in c plus plus?

pop push c++ programming


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


How does algorithm and c plus plus program get along?

They are bosom-friends.


What are the set of steps used to solve a problem in C?

An Algorithm


What is GCD in C Plus Plus?

A C++ implementation of the Binary GCD (Stern's) algorithm is shown in the Related Link below.


How do you find a largest algorithm in c plus plus?

#define max (a, b) ((a) &gt;= (b)) ? (a) : (b)


What is dry run in c plus plus terminology?

A manual check of the algorithm to ensure its correctness.


How do you check a user's text input in C plus plus?

Use an SLR parser algorithm.


What are data structures and algorithms in C?

algorithm is a step by step procedure to solve a problem in c,


How do you write algorith of C programs?

There is no specific Hard and Fast rule for writing algorithm. The normal method is the following: 1. get a problem 2. find or invent an algorithm to solve it 3. implement the algorithm in a programming language (C, for example)


What is solution in c program?

The program itself is the solution. All programs are a solution to a given problem; that's the entire point of writing a program, to solve a problem. The program's algorithm specifies how the problem is solved and it's the programmer's job to convert that algorithm into working code.


Does every algorithm have a c program?

Yes. More generally, every algorithm (defined as a sequence of finite steps to solve a problem that can be easily understood by a human) can be converted into machine code such that the algorithm can be understood by a machine. The C programming language is just one such method of converting algorithms into working machine code.