Code Monkey home page Code Monkey logo

knapsack's Introduction

Knapsack Problem Solver

This repository contains Python implementations of three algorithms for solving the knapsack problem:

- Greedy algorithm
- Dynamic programming algorithm
- Recursive memoization algorithm

It also includes test cases to validate the output of each algorithm using the unittest library.

Contributor

  • Ibrahim
  • Reza Hatta Pratama
  • Ryan Rafael

Problem Description

The knapsack problem is a classic combinatorial optimization problem. It involves choosing a subset of items, each with a weight and a value, to maximize the total value while ensuring that the total weight does not exceed a given capacity.

Algorithms

Greedy Algorithm

The greedy algorithm takes items in decreasing order of value-to-weight ratio until the knapsack is full. It's a simple and fast approach, but it doesn't always produce the optimal solution.

Dynamic Programming Algorithm

The dynamic programming algorithm builds a table of solutions for subproblems and uses it to find the optimal solution for the full problem. It guarantees an optimal solution but has higher computational complexity.

Recursive Memoization Algorithm

The recursive memoization algorithm uses recursion to explore possible solutions while storing intermediate results to avoid redundant calculations. It's similar to dynamic programming but uses a top-down approach.

Usage

  1. Clone this repository:
git clone https://github.com/your-username/knapsack-solver.git
  1. Install the required dependency:
pip install unittest
  1. Run the test cases:
python -m unittest tests.py

Contributing

Feel free to contribute to this repository by:

  • Reporting issues
  • Suggesting improvements
  • Submitting pull requests

License

This project is licensed under the MIT License. See the LICENSE file for details.

knapsack's People

Contributors

ibrahimfe avatar ryanrdf avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

ryanrdf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.