forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.java
More file actions
108 lines (95 loc) · 1.91 KB
/
Knapsack.java
File metadata and controls
108 lines (95 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//Knapsack Algo
import java.util.*;
class Element
{
double weight,profit,ratio;
int id;
Element(double p,double w)
{
profit=p;
weight=w;
ratio=p/w;
}
}
class KnapSack
{
Scanner ip = new Scanner(System.in);
int n;
double capacity,currentCapacity,profit=0;
Element E[];
KnapSack(int x,double y)
{
n=x;
capacity=y;
currentCapacity=capacity;
E=new Element[n];
inputData();
sortInc();
insert();
}
void inputData()
{
for(int i=0;i<n;i++)
{
System.out.print("Enter profit and weight of element " +(i+1)+" : ");
E[i]=new Element(ip.nextDouble(),ip.nextDouble());
E[i].id=i+1;
}
}
void sortInc() //Sort in increasing order of p/w ratio
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(E[i].ratio<E[j].ratio)
{
Element temp=E[i];
E[i]=E[j];
E[j]=temp;
}
}
}
}
void insert()
{
for(int i=0;currentCapacity!=0;i++)
{
if(currentCapacity>=E[i].weight)
{
System.out.println("Insert Element "+E[i].id);
profit=profit+E[i].profit;
currentCapacity=currentCapacity-E[i].weight;
}
else
{
double temp=currentCapacity/E[i].weight;
profit=profit+temp*E[i].profit;
System.out.println("Insert Fraction "+temp+" of Element "+E[i].id);
currentCapacity=0;
}
}
System.out.println("Profit : "+profit);
}
}
class sjs7007_Knapsack
{
public static void main(String[] args)
{
Scanner ip = new Scanner(System.in);
System.out.print("Enter number of elements and capacity of Knapsack : ");
int n=ip.nextInt(); //no. of elements
double w=ip.nextDouble();
KnapSack K = new KnapSack(n,w);
}
}
/* Output
Enter number of elements and capacity of Knapsack : 3 40
Enter profit and weight of element 1 : 20 10
Enter profit and weight of element 2 : 10 20
Enter profit and weight of element 3 : 15 34
Insert Element 1
Insert Element 2
Insert Fraction 0.29411764705882354 of Element 3
Profit : 34.411764705882355
*/