'How to Implement Max Heaps and Item Class To Create Greedy Algorithm?
public class MaxHeap {
class Item{
private double weight;
private int value;
private int ID;
private double factor;
public Item(double weight, int value, int id, double factor){
this.weight = weight;
this.value = value;
this.ID = id;
this.factor = factor;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public double getFactor() {
return factor;
}
public void setFactor(double factor) {
this.factor = factor;
}
}
private int[] Heap;
private int size;
private int maxsize;
// Constructor to initialize an
// empty max heap with given maximum
// capacity
public MaxHeap(int maxsize)
{
// This keyword refers to current instance itself
this.maxsize = maxsize;
this.size = 0;
Heap = new int[this.maxsize];
}
// Method 1
// Returning position of parent
private int parent(int pos) { return (pos - 1) / 2; }
// Method 2
// Returning left children
private int leftChild(int pos) { return (2 * pos) + 1; }
// Method 3
// Returning right children
private int rightChild(int pos){ return (2 * pos) + 2; }
// Method 4
// Returning true of given node is leaf
private boolean isLeaf(int pos)
{
if (pos > (size / 2) && pos <= size) {
return true;
}
return false;
}
// Method 5
// Swapping nodes
private void swap(int fpos, int spos)
{
int tmp;
tmp = Heap[fpos];
Heap[fpos] = Heap[spos];
Heap[spos] = tmp;
}
// Method 6
// Recursive function to max heapify given subtree
private void maxHeapify(int pos)
{
if (isLeaf(pos))
return;
if (Heap[pos] < Heap[leftChild(pos)]
|| Heap[pos] < Heap[rightChild(pos)]) {
if (Heap[leftChild(pos)]
> Heap[rightChild(pos)]) {
swap(pos, leftChild(pos));
maxHeapify(leftChild(pos));
}
else {
swap(pos, rightChild(pos));
maxHeapify(rightChild(pos));
}
}
}
// Method 7
// Inserts a new element to max heap
public void insert(double weight, int value, int id)
{
Heap[size] = value;
// Traverse up and fix violated property
int current = size;
while (Heap[current] > Heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
size++;
}
// Method 8
// To display heap
public int extractMax()
{
int popped = Heap[0];
Heap[0] = Heap[size--];
maxHeapify(0);
return popped;
}
public static void main(String[] arg)
{
MaxHeap maxHeap = new MaxHeap(67);
// Inserting nodes
// Custom inputs
maxHeap.insert(23,505,0);
maxHeap.insert(26,352,1);
maxHeap.insert(20,458,2);
maxHeap.insert(18,220,3);
maxHeap.insert(32,354,4);
maxHeap.insert(27,414,5);
maxHeap.insert(29,498,6);
maxHeap.insert(26,545,7);
maxHeap.insert(30,473,8);
maxHeap.insert(27,543,9);
maxHeap.print();
System.out.println("The max val is "
+ maxHeap.extractMax());
}
}
public static void main(String[] args) {
// TODO code application logic here
}
}
I was tasked with trying to create a max heap and and item class and implement the two in solving a basic knapsack problem. I have been playing around with it and I cannot figure out how I can use my max heap class and item class in order to create a greedy algorithm. Any help is appreciated. I am also not sure how necessary the "knapsack" class is as I had just taken that from geeksforgeeks and was trying to take bits and pieces and implement it into mine. Apologies if this is too much code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
