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

  • SEARCH
  • Home
  • 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 7644629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:44:11+00:00 2026-05-31T09:44:11+00:00

I recently wrote a program to solve the backpack problem using python. It works

  • 0

I recently wrote a program to solve the backpack problem using python. It works great and generally follows the greedy algorithm (i.e. find out the best solution for every step until it’s end)

But I need to optimise it based on the greedy algorithm
(That is a part of my homework)

So could you please provide me some fundamental idea(s) to improve it?

    Item Name             Weight          Profit
    Ammunition             3.00            95.00
    Bread                  3.60            90.00
    Firewood               2.50            56.00
    Olive Oil              2.40            45.00
    Water                  3.70            67.00
    Weapon                 4.80            79.73

This is the output of my current project. The bag capacity has been limited to 20kg, the data cannot be changed, but I need a better idea to improve it. Thank you!

I am not sure about the code or the solution, but I think it is all related to “efficiency”

  • 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-31T09:44:13+00:00Added an answer on May 31, 2026 at 9:44 am

    Here I will use the terms “space” and “weight” synonymously.

    Once thing you can do is calculate the ratio profit/weight for each item. The difference in ratios times some space is the best possible improvement for that space. For example if you had empty space and might be able to squeeze some other item Z in if you rearranged, the maximum profit you could achieve from that space would be (Zratio-0ratio)*weight. You can thus generate a candidate solution based on the greedy algorithm, then use this to bound possible improvements. In general though you will want to approach this from a dynamic programming standpoint.

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

Sidebar

Related Questions

I recently wrote a short algorithm to calculate happy numbers in python. The program
I recently wrote a parser in Python using Ply (it's a python reimplementation of
I recently wrote an extremely basic edge detection algorithm that works on an array
A coworker recently wrote a program in which he used a Python list as
Recently I wrote a program using sockets in C, to connect to an HTTP
I recently wrote a program that used a simple producer/consumer pattern. It initially had
I recently wrote a post: Weird Error in C++ Program: Removing Printout Breaks Program
I recently wrote a program that takes inputted char data, tests if it is
I recently wrote a java crawler program that finds the video links in a
I recently wrote a small AJAX-based chat program. The clients ping the server occasionally

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.