-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractionalKnapsack.java
131 lines (117 loc) · 3.26 KB
/
fractionalKnapsack.java
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.Scanner;
/*
items : 0 name of item
1 weight
2 profit
3 ratio of (profit/weight)
4 considered for selection? (0=no, 1=yes)
selected : 0 name of item
1 fraction selected
2 weight added to totalWeight
*/
class fractionalKnapsack{
int numOfItems, capacity;
Object[][] items;
int totalWeight;
float totalProfit;
Object[][] selected;
int k; //for selected array
void inputItems()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of items: ");
numOfItems = sc.nextInt();
items = new Object[5][numOfItems];
selected = new Object[3][numOfItems];
for(int i=0;i<numOfItems;i++)
{
System.out.print("Name of Item " + (i+1) + " : ");
items[0][i] = sc.next();
System.out.print("Weight of Item " + (i+1) + " : ");
items[1][i] = sc.nextInt();
System.out.print("Profit associated with Item " + (i+1) + " : ");
items[2][i] = sc.nextInt();
System.out.println();
//Calculating ratio of profit to weight
items[3][i] = (float)((Integer)items[2][i])/(Integer)items[1][i];
//Setting initial state of selection to 0
items[4][i] = 0;
//Setting initial selected fraction to 0
selected[1][i] = 0;
selected[2][i] = 0;
}
System.out.print("Enter maximum capacity of the knapsack: ");
capacity = sc.nextInt();
}
void selectItems()
{
int selectedIndex=0;
float maxRatio=0;
totalWeight = 0;
totalProfit = 0;
k=0; //for selected array
while(totalWeight != capacity)
{
//To find item with maxRatio
maxRatio=0; //IMPPPPP
for(int i=0;i<numOfItems;i++)
{
if((Integer)items[4][i] == 1)
{
continue;
}
else
{
if((Float)items[3][i]>maxRatio)
{
maxRatio = (Float)items[3][i];
selectedIndex = i;
}
}
}
//Set status of selected item to 1
items[4][selectedIndex] = 1;
//check if weight of selected item is less than (capacity-totalweight)
if((Integer)items[1][selectedIndex] <= (capacity-totalWeight))
{
totalWeight = totalWeight + (Integer)items[1][selectedIndex];
totalProfit = totalProfit + (Integer)items[2][selectedIndex];
selected[0][k] = (String)items[0][selectedIndex];
selected[1][k] = 1;
selected[2][k] = (Integer)items[1][selectedIndex];
k++;
//System.out.println((String)items[0][selectedIndex]+" "+totalWeight+" "+totalProfit);
}
else
{
int available = capacity - totalWeight;
float fraction = (float)available/(Integer)items[1][selectedIndex];
selected[0][k] = (String)items[0][selectedIndex];
selected[1][k] = fraction;
selected[2][k] = available;
k++;
totalWeight = totalWeight + available;
totalProfit = totalProfit + (Integer)items[2][selectedIndex]*fraction;
//System.out.println((Integer)items[2][selectedIndex]*fraction);
}
}
}
void displayOutput()
{
System.out.println("Selected Items: ");
for(int i=0;i<k;i++)
{
System.out.println("("+selected[1][i]+")" + " " + selected[0][i]);
}
System.out.println();
System.out.println("Total Profit: " + totalProfit);
System.out.println("Total Weight: " + totalWeight);
}
public static void main(String atrgs[])
{
fractionalKnapsack obj = new fractionalKnapsack();
obj.inputItems();
obj.selectItems();
obj.displayOutput();
}
}