Git Product home page Git Product logo

basemax / 0-1knapsackdynamicprogrammingc Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 25 KB

This is an implementation of the 0-1 knapsack problem in C using dynamic programming. The problem consists of a set of items, each with a weight and a value, and a knapsack with a maximum weight capacity. The goal is to determine the subset of items that maximizes the total value of the knapsack without exceeding its weight capacity.

License: GNU General Public License v3.0

C 100.00%
c knapsack knapsack-problem knapsack-problem-dynamic knapsack-solution knapsack-solver knapsack01 knapsack0-1

0-1knapsackdynamicprogrammingc's Introduction

0-1 Knapsack Problem - Dynamic Programming in C

This is an implementation of the 0-1 knapsack problem in C using dynamic programming. The problem consists of a set of items, each with a weight and a value, and a knapsack with a maximum weight capacity. The goal is to determine the subset of items that maximizes the total value of the knapsack without exceeding its weight capacity.

How to use

To use this implementation, include the knapsack.c file in your project and call the knapSack() function with the following parameters:

  • W: the maximum weight capacity of the knapsack
  • wt[]: an array of weights for each item
  • val[]: an array of values for each item
  • n: the number of items

The function will return the maximum value that can be put in the knapsack without exceeding its weight capacity.

#include "0-1knapsack.c"

int main() {
    int W = 50;
    int wt[] = {10, 20, 30};
    int val[] = {60, 100, 120};
    int n = sizeof(wt)/sizeof(wt[0]);
    printf("%d", knapSack(W, wt, val, n));

    return 0;
}

Note

This implementation uses dynamic memory allocation to create a 2D array with variable size, so please remember to free the allocated memory after using the function.

Time and Space Complexity

The time complexity of this implementation is O(nW) where n is the number of items, W is the knapsack capacity. The space complexity is also O(nW) for the 2D array.

I hope this implementation helps you solve the 0-1 knapsack problem in your project. If you have any questions or suggestions, feel free to reach out.

Copyright (c) 2022, Max Base

0-1knapsackdynamicprogrammingc's People

Watchers

 avatar

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.