1. NADAR SARASWATHI COLLEGE OF ARTS
AND SCIENCE
DEPARTMENT OF COMPUTER SCIENCE
KNAPSACK PROBLEMS
NAME : P.ANUSUYA
CLASS : I-M.SC(CS)
2. KNAPSACK PROBLEM :
• Given weights and values of n items, put these items in a knapsack of
capacity W to get the maximum total value in the knapsack. In other
words, given two integer arrays val[0..n-1] and wt[0..n-1] which
represent values and weights associated with n items respectively.
Also given an integer W which represents knapsack capacity, find out
the maximum value subset of val[] such that sum of the weights of
this subset is smaller than or equal to W.
• You cannot break an item, either pick the complete item or don’t pick
it (0-1 property).
3.
4. KNAPSACK PROBLEM EXAMPLE
:
You are given weights and values of N items, put these items in a knapsack of
capacity W to get the maximum total value in the knapsack. Note that we have
only one quantity of each item.
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which
represent values and weights associated with N items respectively. Also given an
integer W which represents knapsack capacity, find out the maximum value
subset of val[] such that sum of the weights of this subset is smaller than or
equal to W. You cannot break an item, either pick the complete item or don’t pick
it (0-1 property).Complete the function knapSack() which takes maximum
capacity W, weight array wt[], value array val[], and the number of items n as a
parameter and returns the maximum possible value you can get.
7. TRAVELING SALES PROBLEM :
• Given a set of cities and distances between every pair of cities, the problem
is to find the shortest possible route that visits every city exactly once and
returns to the starting point.
• Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian
cycle problem is to find if there exists a tour that visits every city exactly
once. Here we know that Hamiltonian Tour exists (because the graph is
complete) and in fact, many such tours exist, the problem is to find a
minimum weight Hamiltonian Cycle.
• For example, consider the graph shown in the figure on the right side. A TSP
tour in the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is
80.
• The problem is a famous NP-hard problem. There is no polynomial-time
known solution for this problem.
8.
9. EFFICIENCY CONSIDERATION :
• The efficiency of an algorithm defines the number of computational resources used by an
algorithm and time taken by an algorithm to produce the desired result.
• An algorithm which takes fewer resources and computes results in a minimum time for a
problem then that algorithm is known as efficient.
• The efficiency of an algorithm defines the number of computational resources used by an
algorithm and time taken by an algorithm to produce the desired result.
• The efficiency of the algorithm is measured based on the usage of different resources and
minimum running time.
• Since every algorithm uses computer resources to run, But to analyze an algorithm,
execution time and internal memory are essential to consider.