Question: LAB 3 [ PLO K 2 , CLO 2 . 2 ] Topic: Brute Force Algorithms 1 . Exhaustive Search: Knapsack Problem 2 . Exhaustive
LAB
PLO K CLO
Topic: Brute Force Algorithms
Exhaustive Search: Knapsack Problem
Exhaustive Search: Travel Salesperson Problem
Validation Knapsack Problem Code
Validation of Travel Salesperson Code
Student Name:
Student ID:
Exercise : Exhaustive Search: Knapsack Problem
Example of a onedimensional knapsack problem:
In Fig. which boxes should be placed in the bag to
maximize the value amount of money while keeping the
overall weight under or equal to kg The solution is
that we will pick all boxes except the green box. In this
case the total weigh of the Knapsack will be Kg and the
value of the picked items will be $which is the
maximum you can have
The exhaustive search approach is that you try all
possible combinations and find the items that will have
maximum overall value and also fit in the knapsack.
Implement this approach in the language of your choice.
The code will take following inputs:
The capacity of the knapsack: C
The weights of the available items: W which should be an Nx vector, where N is
the number of available items, and Wi weight of item i
The values of the available items: V which should be an Nx vector, and Vi
value of item i
The code will have the following outputs:
Kingdom of Saudi Arabia
Ministry of Education
University of Jeddah
College of Science and Computer Engineering
Fig. : Knapsack Problem
The items picked: P which is a Nx vector such that Pi if item i is not picked
and Pi if item i is picked. For example, P means that items
and are picked.
Total value of the picked items
Total weight of the picked items
function items wt vt BFknapsackw v capacity
w ;
v ;
capacity ;
n numelw;
vt; total value
wt; total weight
items; picked items
S decbin:n ; all possible exhaustive search
options
for i:sizeS
j Si:; get a possible combinition in S
j findj; indices of picked items in this
combinition
w sumwj; picked weight
if wcapacity, continue; end infeasible solution
v sumvj; picked weight
if vvt store if this cominition is best so
far
vtv;
wtw;
itemsj;
end
end
end
PART B: Run your code for the following inputs:
C
W
V
Write the output of your code below:
Items Picked P
Total value of picked items
Total weight of picked items
Exercise : Exhaustive Search: Travel Salesperson Problem
PART A: A salesman need to travel to all nodes shown in Figure on
right, starting from the first node and coming back to the same
place. Each node is visited only once. Distance from each node to
every other node is also shown in the figure. You can represent this
graph in the form of matrix given below.
D
Rows and columns correspond to nodes in order and an element Di j represents the
distance between node i and j
One way to solve this problem is to perform Exhaustive Search, ie we try all possible
routes and find which one is the shortest.
You are required to implement this algorithm, in the language of your choice. The
algorithm should have the following inputs:
A NxN distance matrix D The element Dij gives distance from node i to j and
distance from a node i to itself Dii
The code's outputs should be:
The best route the numbers of nodes in the order in which they are traversed For
example represents traveling from node to to to to
The minimum distance traveled by the salesperson
function bestpath,mind travelsalespersondm
dm;;;; for debugging
number of nodes
n sizedm;
nodes :n;
possible paths
paths repmatnodes factorialn n;
paths::end permsnodes:n;
distances for all paths
mind Inf;
for i:sizepaths
p pathsi:;
d ;
for j:numelp
dddmpjpj;
end
if d mind
mind d;
bestpath p;
end
end
dispbestpath;
dispmind;
end
PART B: Run your code for the distance matrix given in Part A and write the output below.
the best path:
minimum distance:
Exercise : Validation Knapsack Problem Code
You will be provided an input. Run you code in Exercise on this input and write your
answers in the provided space on blackboard.
Exercise : Validation TSP Problem Code
You will be provided an input. Run you code in Exercise on this input and write your
answers in the provided space on blackboard.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
