{"id":28491056,"url":"https://github.com/dssgabriel/knapsack","last_synced_at":"2025-09-04T07:42:03.990Z","repository":{"id":107156236,"uuid":"324832542","full_name":"dssgabriel/knapsack","owner":"dssgabriel","description":"A C implementation of algorithms solving the Knapsack problem with different levels of efficiency.","archived":false,"fork":false,"pushed_at":"2021-03-24T16:02:34.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-07T18:52:28.029Z","etag":null,"topics":["algorithmic","c","knapsack"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dssgabriel.png","metadata":{"files":{"readme":"README.org","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-12-27T19:21:46.000Z","updated_at":"2022-03-10T09:14:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"bb9ea7ef-05ff-47a1-ab02-3d238fb5c65e","html_url":"https://github.com/dssgabriel/knapsack","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dssgabriel/knapsack","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dssgabriel%2Fknapsack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dssgabriel%2Fknapsack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dssgabriel%2Fknapsack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dssgabriel%2Fknapsack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dssgabriel","download_url":"https://codeload.github.com/dssgabriel/knapsack/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dssgabriel%2Fknapsack/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273574112,"owners_count":25129882,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-04T02:00:08.968Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithmic","c","knapsack"],"created_at":"2025-06-08T07:31:18.835Z","updated_at":"2025-09-04T07:42:03.967Z","avatar_url":"https://github.com/dssgabriel.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"#+TITLE:    Knapsack problem\n#+AUTHOR:   Gabriel Dos Santos\n\n** Introduction\nThe knapsack problem is a problem in combinatorial optimization: given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. The problem often arises in resource allocation where the decision makers have to choose from a set of non-divisible projects or tasks under a fixed budget or time constraint, respectively.\n\n** Description\nTo put this into perspective, a delivery or logistics company (like UPS, DHL, etc...) faces a similar situation as they have to handle ~n~ number of packages of varying weights ~p0~, ~p1~, ..., ~pn~. To deliver these packages, they use trucks that can be loade up to a maximum weight ~P~.\nTheir goal is to put the packages into the trucks, while also using the least amount of trucks possible. The last implies that the weight sum of packages is less or equal to ~P~, the maximum weight that a truck can carry.\n\nA solution to this problem is to represent the packages as a list of integers (representing the weight of each package), and the trucks as lists of the weights of packages they contain.\n\nFor example, for a maximum of 10 tons per truck and a list of packages (in tons): (1, 4, 1, 6, 7, 9, 3, 2, 8), then the following arrangement: (100, 100, 200, 600), (300, 700), (400), (800) and (900) is a solution using five trucks.\n\nA solution is said to be optimal if it uses the least amount of trucks for all the given solutions.\n\nThree algorithms that answer this problem have been implemented:\n- the first one fills the trucks with the packages as they come in the list (no space optimization).\n- the second algorithm first sorts the list of weights (using an implementation of the ~quicksort algorithm~), reverses it, and then loads the trucks with the packages as they come (minimal optimization).\n- the third algorithm optimizes the loading of the trucks by applying the same steps as the second one and then finds complementary weight to load the trucks (optimal solution).\n\n** Memory optimizations\nThe simplest and most intuitive way to store the list of trucks and the weights they carry is to use a matrix.\nFor the example above we get the following:\n#+BEGIN_SRC\n( 1 1 2 6 0 0 0 0 0 0 )\n( 3 7 0 0 0 0 0 0 0 0 )\n( 4 0 0 0 0 0 0 0 0 0 )\n( 8 0 0 0 0 0 0 0 0 0 )\n( 9 0 0 0 0 0 0 0 0 0 )\n( 0 0 0 0 0 0 0 0 0 0 )\n( 0 0 0 0 0 0 0 0 0 0 )\n( 0 0 0 0 0 0 0 0 0 0 )\n( 0 0 0 0 0 0 0 0 0 0 )\n#+END_SRC\n\nIt becomes pretty obvious that storing the algorithm's result in a matrix is not efficient at all, as, in this case, 90% of the memory allocated is initialized but not used.\n\nAnother solution is to use a CSR (Compressed Sparse Row) representation, as adviced by [[https://github.com/yaspr][yaspr]].\nIt instead uses three one-dimensional arrays that each compute a different information:\n- the first one stores the values ~V~,\n- the second one stores the column index ~COL_INDEX~,\n- the third one encodes the index in ~V~ and ~COL_INDEX~, where the given row starts ~ROW_INDEX~.\nThis gives us the following result for the Description's example:\n#+BEGIN_SRC\nV         = [ 1 1 2 6 3 7 4 8 9 ]\nCOL_INDEX = [ 0 1 2 3 0 1 0 0 0 ]\nROW_INDEX = [ 0 5 6 8 9 10 ]\n#+END_SRC\n\nFor futher reading and documentation, please refer to [[https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)][Sparse matrix Wikipedia page]].\n\n** Installing and running\nFirst, clone the repository:\n#+BEGIN_SRC sh\ngit clone https://github.com/dssgabriel/knapsack.git\n#+END_SRC\n\nThen cd into the directory and compile:\n#+BEGIN_SRC sh\ncd knapsack\nmake\n#+END_SRC\n\nFinally, run with the values that you want (or use ~make run~ for a demo):\n#+BEGIN_SRC sh\ntarget/main -[matrix | csr] [NB_PACKAGES] [MAX_PACKAGE_WEIGHT] [MAX_TRUCK_WEIGHT]\n#+END_SRC\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdssgabriel%2Fknapsack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdssgabriel%2Fknapsack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdssgabriel%2Fknapsack/lists"}