Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6722567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:27:45+00:00 2026-05-26T09:27:45+00:00

This is my first shot at brute-forcing the NP-complete knapsack problem . In this

  • 0

This is my first shot at brute-forcing the NP-complete knapsack problem. In this form you have a list of items which must be thrown off a plane each with a weight and cost. The goal is to throw out some remain_weight while minimizing cost.

Each recursion level(y direction if graphed) is a new remain_weight after items have been selected. A for loop searches through all the items(x direction if graphed)

Test Case 1 - Works
Item / Weight / Cost
0      100     101
1      300     297

What is the best way to put these two functions in a class.

enum item_type {weight, cost};
int algo(int &cost_low, int &cost_high, int throw_weight, int item_id, int item_matrix[][2])
{
    int quantity,remainder;
    quantity=throw_weight/item_matrix[item_id][weight];
    remainder=throw_weight%item_matrix[item_id][weight];
    if(remainder==0)
    {
        cost_low=(quantity-1)*item_matrix[item_id][cost];
        cost_high=quantity*item_matrix[item_id][cost];
        throw_weight-=(quantity-1)*item_matrix[item_id][weight];
    }
    else
    {
        cost_low=(quantity)*item_matrix[item_id][cost];
        cost_high=(quantity+1)*item_matrix[item_id][cost];
        throw_weight-=(quantity)*item_matrix[item_id][weight];
    }
    return throw_weight;
}
int branch(int remain_weight)
{
    static int depth_level = 0;
    static int cost_present=32000;
    int remain_weight_next;
    int cost_low, cost_high, cost_branch;
    depth_level++;
    cout << "Entering at depth: " << depth_level << " :remain_weight: " << remain_weight << endl ;
    int item_id, item_count=2; 
    int item_matrix[][2] = 
    {
        {100, 101},
        {300, 297},
    //  {400, 401},
    //  {800, 800}, 
    //  {1200, 1200}, 
    //  {1999, 1800},
    //  {2000, 2000},
    };
    for(item_id=0; item_id<item_count; ++item_id)
    {
        cout << "--For loop id is: " << item_id << endl; 
        if(item_matrix[item_id][weight]<remain_weight)
        {
            cout << "----item_weight: " << item_matrix[item_id][weight] << " : is less than remain_weight : " << remain_weight << endl;
            remain_weight_next=algo(cost_low,cost_high,remain_weight,item_id,item_matrix); 
            cost_branch = branch(remain_weight_next);
            cost_present=cost_low + cost_branch;  
            if(cost_present>cost_high)
                cost_present=cost_high;
            cout << "--**remain_weight: " << remain_weight << endl;
            cout << "--**cost_low: " << cost_low << endl;
            cout << "--**cost_high: " << cost_high << endl;
            cout << "--**cost_branch: " << cost_branch << endl;
        }
        else
        {
            cout << "----item_weight: " << item_matrix[item_id][weight] << " : is greater than remain_weight : " << remain_weight << endl;
            if(cost_present>item_matrix[item_id][cost])
                cost_present=item_matrix[item_id][cost];
        }
        cout << "--**cost_present: " << cost_present << endl;
    }
    cout << "Leaving at Depth: " << depth_level << endl;
    depth_level--;
    return cost_present;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:27:46+00:00Added an answer on May 26, 2026 at 9:27 am

    int &cost_low, int &cost_high is a tip-off. If a function is called repeatedly, and on each iteration modifies the same objects, then that function and those objects should probably be members of the same class.

    If you look further, you see that algo also works on cost_matrix[] and weight_matrix[] (No, it’s not a 2D array). These could also become members.

    branch is a bit complex because you ‘re mixing up things. It’s recursive, but you also initialize item_matrix in each and every recursion. No problem once you’ve moved item_matrix into a class; the ctor will then initialize it. But do allocate that class outside branch() for the same recursive reasons.

    Finally, be a bit more compact. Don’t define objects early; define them when you have a value. Dare to write cout << "Entering at depth: " << ++depth_level;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have any background in programming and this is my first shot. I
this my first shot at this awesome new (to me) programmers site, I hope
problem euler #5 i found the solution but i don't know why this first
Using Dozer to map two objects, I have: /** /* This first class uses
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
This is my first shot at writing a stored procedure. I'm trying to get
There must be something I'm missing here. I have this nice, pretty Oracle SQL
I've been developing for Android for awhile but this is my first shot at
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
Saw this piece of code in a Ruby on Rails book. This first one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.