dave risch. project specifications there is a “knapsack” that you want to fill with the most...

7
The Knapsack Problem Dave Risch

Upload: poppy-arnold

Post on 22-Dec-2015

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a

The Knapsack ProblemDave Risch

Page 2: Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a

Project SpecificationsThere is a “knapsack” that you want to fill with the

most valuable items that are available to you.Each item has a value, weight, and value density.

(which is determined by dividing the value/weight)The items then get listed in order of value density.Uses a greedy algorithm to select the highest

valued items.Keeps selecting items until the weight of the next

best item can’t fit.At that point, it will skip that item and look for the

next item that can fit.

Page 3: Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a

GoalsCreate a NP-hard program that uses a greedy

algorithmProgram will choose the best items for you to

take within a certain weight capacity.Output the items, along with their values,

weights and value densities.

Page 4: Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a

ApplicationsApplications that use a greedy algorithm

usually fail to find the optimal solution.Yet when they do work for a certain class,

they are generally used because of their speed.

Greedy algorithms are used in applications such as Kruskal’s, Prim’s, and Dijkstra’s.

Also used in routing. A message is forwarded to the closest neighboring node.

Page 5: Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a

DesignUse object programming to make items with value,

weights and value densities.Put objects in an array, and sort objects in order of

their densities.Loop through, choosing the highest value density

objects.Add weights until the next item won’t fit the weight

restriction.Skip that object and check the object after to see if it

will fit and so on until the weight is maxed out. Add selected items to a list, output the item, value,

weight and value density.

Page 6: Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a

TestingTo test the program, I will use different

values, weights, value densities to see if it chooses the correct items.

Use different weight restrictions to see if it will choose an item even though the item before it exceeded the max weight.

Add or remove items that you can take to mix things up.

Page 7: Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a

Conclusions so farAfter further researching, I found you can

use dynamic programming to make the knapsack problem more efficient than if you use a greedy algorithm.