You are on page 1of 8

K. J.

Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)

Batch: B3 Roll No.: 16010120114

Experiment No. 5

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

Title: Implementation of Knapsack Problem using Greedy strategy

Objective: To learn the Greedy strategy of solving the problems for different types of problems

CO to be achieved:
Sr. No Objective

CO 1 Analyze the asymptotic running time and space complexity of algorithms.

CO 2 Describe various algorithm design strategies to solve different problems


and analyze

Complexity.

CO 3 Develop string matching techniques

CO 4 Describe the classes P, NP, and NP-Complete

Books/ Journals/ Websites referred:


1. Ellis horowitz, Sarataj Sahni, S.Rajsekaran,” Fundamentals of computer
algorithm”, University Press

Department of Computer Engineering


Page No AOA Sem IV Jan-May 2021
K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

2. T.H.Cormen ,C.E.Leiserson,R.L.Rivest and C.Stein,” Introduction to


algortihtms”,2nd Edition ,MIT press/McGraw Hill,2001
3. http://lcm.csa.iisc.ernet.in/dsa/node184.htm
4. http://students.ceid.upatras.gr/~papagel/project/kruskal.htm
5. http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAl
gor/kruskalAlgor.html
6. http://lcm.csa.iisc.ernet.in/dsa/node183.html
7. http://students.ceid.upatras.gr/~papagel/project/prim.htm
8. http://www.cse.ust.hk/~dekai/271/notes/L07/L07.pdf

Pre Lab/ Prior Concepts:


Data structures, Concepts of algorithm analysis

Historical Profile:

The knapsack problem represents constraint satisfaction optimization problems’ family. Based on
nature of constraints, the knapsack problem can be solved with various problem solving strategies.
Typically, these problems represent resource optimization solution.

Given a set of n inputs, find a subset called feasible solution, of the n inputs subject to some
constraints, and satisfying a given objective function. If the objective function is maximized or
minimized, the feasible solution is optimal. · It is a locally optimal method.

New Concepts to be learned:


Application of algorithmic design strategy to any problem, Greedy method of problem solving
Vs other methods of problem solving, optimality of the solution, knapsack problem and their
applications

Department of Computer Engineering


Page No AOA Sem IV Jan-May 2021
K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

Knapsack Problem Algorithm

Example: Knapsack Problem

Department of Computer Engineering


Page No AOA Sem IV Jan-May 2021

K. J. Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)

package AOA;

import java.util.*;

public class Knapsack {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of objects available : ");
int n = s.nextInt();
int weight[] = new int[n];
int profit[] = new int[n];
float[] ratio = new float[n];
int objects[] = new int[n];
int temp1, temp2, temp3;
float temp;
System.out.println("---------------------------------------------------------
----------------------");
for (int i = 0; i < n; i++) {
System.out.print("Enter Weight of object " + (i + 1) + " : ");
weight[i] = s.nextInt();
System.out.print("Enter Profit for object " + (i + 1) + " : ");
profit[i] = s.nextInt();
ratio[i] = (float) profit[i] / weight[i];
objects[i] = i + 1;
System.out.println();
}
System.out.println("\n\nCalculating P/W Ratio : ");
System.out.println("---------------------------------------------------------
----------------------");
System.out.println(
padding("Object") + "|" + padding("Weight") + "|" + padding("Profit") + "|" +
padding("P/W Ratio") + "|");
for (int i = 0; i < n; i++) {
System.out.println(padding("" + (i + 1)) + "|" + padding("" + weight[i]) + "|" +
padding("" + profit[i]) + "|"
+ padding("" + ratio[i]) + "|");

Department of Computer Engineering


Page No AOA Sem IV Jan-May 2021

K. J. Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[i];
ratio[i] = ratio[j];
ratio[j] = temp;
temp1 = profit[i];
profit[i] = profit[j];
profit[j] = temp1;
temp2 = weight[i];
weight[i] = weight[j];
weight[j] = temp2;
temp3 = objects[i];
objects[i] = objects[j];
objects[j] = temp3;
}
}
}
System.out.println("\n\nSorted objects in decreasing order of P/W Ratio : ");
System.out.println("---------------------------------------------------------
----------------------");
System.out.println(
padding("Object") + "|" + padding("Weight") + "|" + padding("Profit") + "|" +
padding("P/W Ratio") + "|");
for (int i = 0; i < n; i++) {
System.out.println(padding("" + objects[i]) + "|" + padding("" + weight[i]) + "|"
+ padding("" + profit[i]) + "|"
+ padding("" + ratio[i]) + "|");
}
System.out.print("\n\nEnter Knapsack Capacity : ");
int W = s.nextInt();
float P = 0;
System.out.println("---------------------------------------------------------
----------------------");
System.out.println(padding("Object") + "|" + padding("Weight") + "|" +
padding("PROFIT") + "|"
+ padding("Total Profit") + "|" + padding("W remaining") + "|");

Department of Computer Engineering


Page No AOA Sem IV Jan-May 2021

K. J. Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)

for (int i = 0; i < n && W != 0; i++) {


if (weight[i] <= W) {
W = W - weight[i];
P += profit[i];
System.out.println(padding("" + objects[i]) + "|" + padding("" +
weight[i]) + "|" + padding("" + profit[i])
+ "|" + padding("" + P) + "|" + padding("" + W) + "|"); } else {
float x = (float) W * profit[i] / weight[i];
P += x;
System.out.println(padding("" + objects[i]) + "|" + padding("" + W) + "|" +
padding("" + profit[i]) + "|"
+ padding("" + P) + "|" + padding("" + 0) + "|"); W = 0;
}
}
System.out.print("\n\nMaximum profit is : " + P);
}

public static String padding(String input) {


return String.format("%6s", String.format("%-12s", input)); }
}

Department of Computer Engineering


Page No AOA Sem IV Jan-May 2021

K. J. Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering
Page No AOA Sem IV Jan-May 2021

K. J. Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)

Complexity Analysis of Knapsack Algorithm


Conclusion: In this experiment we implemented the Fractional Knapsack Algorithm that uses
the Greedy Approach to find the local maximum benefit that can be obtained. We also analysed
the time complexity of this algorithm. The following implementation of Knapsack Algorithm is
performed using Java Programming Language.

Department of Computer Engineering


Page No AOA Sem IV Jan-May 2021

You might also like