-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.java
More file actions
89 lines (82 loc) · 2.83 KB
/
Knapsack.java
File metadata and controls
89 lines (82 loc) · 2.83 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
import java.util.*;
public class Knapsack {
static int n, W;
static int[] wt, val;
static int maxValBacktrack = 0;
static void backtrack(int i, int weight, int value) {
if (i == n) {
if (value > maxValBacktrack) maxValBacktrack = value;
return;
}
backtrack(i + 1, weight, value);
if (weight + wt[i] <= W)
backtrack(i + 1, weight + wt[i], value + val[i]);
}
static class Node implements Comparable<Node> {
int level, profit, weight;
double bound;
public int compareTo(Node o) {
return Double.compare(o.bound, bound);
}
}
static double bound(Node u) {
if (u.weight >= W) return 0;
double profitBound = u.profit;
int j = u.level + 1;
int totweight = u.weight;
while (j < n && totweight + wt[j] <= W) {
totweight += wt[j];
profitBound += val[j];
j++;
}
if (j < n)
profitBound += (W - totweight) * val[j] / (double) wt[j];
return profitBound;
}
static int branchBound() {
PriorityQueue<Node> Q = new PriorityQueue<>();
Node u = new Node(), v = new Node();
v.level = -1; v.profit = 0; v.weight = 0;
v.bound = bound(v);
int maxProfit = 0;
Q.add(v);
while (!Q.isEmpty()) {
v = Q.poll();
if (v.bound > maxProfit && v.level < n - 1) {
u.level = v.level + 1;
u.weight = v.weight + wt[u.level];
u.profit = v.profit + val[u.level];
if (u.weight <= W && u.profit > maxProfit)
maxProfit = u.profit;
u.bound = bound(u);
if (u.bound > maxProfit)
Q.add(new Node(){{
level = u.level; profit = u.profit;
weight = u.weight; bound = u.bound;
}});
u.weight = v.weight;
u.profit = v.profit;
u.bound = bound(u);
if (u.bound > maxProfit)
Q.add(new Node(){{
level = u.level; profit = u.profit;
weight = u.weight; bound = u.bound;
}});
}
}
return maxProfit;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
wt = new int[n];
val = new int[n];
for (int i = 0; i < n; i++) val[i] = sc.nextInt();
for (int i = 0; i < n; i++) wt[i] = sc.nextInt();
W = sc.nextInt();
backtrack(0, 0, 0);
System.out.println("Backtracking Max Value: " + maxValBacktrack);
int maxValBB = branchBound();
System.out.println("Branch and Bound Max Value: " + maxValBB);
}
}