CCCS 314 – Design and Analysis of Algorithms LAB 3 [PLO K2, CLO 2.2] Topic: Brute Force Algorithms 1. Exhaustive Search: Knapsack Problem 2. Exhaustive Search: Travel Salesperson Problem 3. Validation Knapsack Problem Code 4. Validation of Trav el Salesperson Code
Added by Heidi V.
Step 1
Let's think step by step. Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 65 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Write a program to solve the Knapsack problem using backtracking algorithm. Your code should contain a method called Knapsack, the method takes two parameters, the first is a 2xN array of integers that represents the items and their weight and value and the second is an integer that represents the maximum weight of the knapsack. Assume that the initial state is an empty Knapsack, and the actions are putting objects in the Knapsack and if the weight of selected items exceeds the total weight then you have to backtrack. Your method should print (on the screen) three lines: The first one represent the chosen items(indices), the second line gives the total weight of the selected items and the last one represent the total value of the selected items. An example: weight 7 3 9 12 10 value 4 3 4 7 8 total weight 20 Output: 0 1 4 20 15 Your code should run in real time. (Hint: the order in which you select the actions will affect the run time).
Akash M.
23.1 Knapsack Problem. Consider a picnicker who will be carrying a knapsack that holds a maximum amount b of stuff. Suppose that our picnicker must decide what to take and what to leave behind. The jth thing that might be taken occupies aj units of space in the knapsack and will bring cj amount of enjoyment. The knapsack problem then is to maximize enjoyment subject to the constraint that the stuff brought must fit into the knapsack: maximize Σ cjxj subject to Σ ajxj ≤ b, xj ∈ {0,1} This apparently simple problem has proved difficult for general-purpose branch-and-bound algorithms. To see why, analyze the special case in which each thing contributes the same amount of enjoyment; i.e. cj = c for all j, and takes up exactly two units of space, i.e. aj = 2 for all j. Suppose also that the knapsack holds n units of stuff. What is the optimal solution when n is even? When n is odd? (b) How many subproblems must the branch-and-bound algorithm consider when n is odd?
Amman Z.
Consider the code that we finished with for the Subset Sum problem: SubsetSum (S1...n, K) Initialize Table0...K = -1 everywhere Table0 = 0 for(i = 1 to n) for(j = K downto 1) if(Tablej < 0 and j >= Si and Tablej-si >= 0) Tablej = i (In the code, I use subscripts for array values, because otherwise Canvas interprets them...poorly.) It produces a Table (indices 0 to n) such that, if Tablej < 0, then a Knapsack of size j cannot be filled with the given items. On the other hand, if the value is non-negative, it will be the smallest possible maximum index used to fill a knapsack of the given size. Given S[1,2,3,4] = 2, 6, 3, 8 and K=15, show the table after the code above runs. 0 1 2 3 4 0 -1 -1 -1 -1 5 6 7 8 9 -1 -1 -1 -1 4 10 11 12 13 14 3 1 3 2 -1 15 4
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD