Lab 4
Lab 4
Lab Name: Implement max heap, heapify and fractional knapsack problem using greedy
programming
Max heap and heapify: A max heap is a complete binary tree in which the value of
each node is greater than or equal to the values of its children, ensuring that the largest
element is always at the root. This property makes max heaps useful for implementing
priority queues and efficient sorting algorithms like heapsort. The process of maintaining
or building this structure is called heapify. Heapify can be performed in two ways: "up-
heapify" (or bubble-up) which is used when a new element is added to the heap,
ensuring the heap property is maintained by moving the new element up the tree, and
"down-heapify" (or bubble-down) which is used when the root element is removed or
replaced, ensuring the heap property is maintained by moving the new root element
down the tree to its correct position. The time complexity of heapify operations is O(log
n), where n is the number of nodes in the heap.
Algorithm:
Alg: MAX-HEAPIFY(A, i, n)
1. l ← LEFT(i)
2. r ← RIGHT(i)
3. if l ≤ n and A[l] > A[i]
4. then largest ←l
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest ← i
9. then exchange A[i] ↔ A[largest]
10. MAX-HEAPIFY(A, largest, n)
Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← ⌊n/2⌋ down to 1
3. do MAX-HEAPIFY(A, i, n)
Code:
#include<iostream>
using namespace std;
while(j<=n)
{
if (j<n && a[j+1] > a[j])
j = j+1;
if(temp>a[j])
break;
else if(temp<= a[j])
{
a[j/2] = a[j];
j = 2 *j;
}
a[j/2] =temp;
return;
}
}
void HeapSort(int a[], int n)
{
int i, temp;
for(i = n; i>= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
maxheapify(a, 1, i-1);
}
}
void build_Maxheap(int a[], int n)
{
int i;
for(i = n/2; i>=1; i--)
maxheapify(a, i, n);
}
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
n++;
int arr[n];
for(i =1; i<n; i++)
{
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
build_Maxheap(arr, n-1);
HeapSort(arr, n-1);
cout<<"\n Sorted data ";
Algorithm:
1. Initialize totalProfit ← 0
2. Initialize curWeight ← w
3. For i ← 1 to n
a. used[i] ← 0
4. While curWeight > 0
a. maxi ← -1
b. For i ← 1 to n
i. If used[i] = 0 and (maxi = -1 or (A[1][i] / A[0][i]) > (A[1][maxi] / A[0][maxi]))
1. maxi ← i
c. If maxi = -1
i. Break
d. used[maxi] ← 1
e. If curWeight ≥ A[0][maxi]
i. curWeight ← curWeight - A[0][maxi]
ii. totalProfit ← totalProfit + A[1][maxi]
f. Else
i. totalProfit ← totalProfit + (A[1][maxi] / A[0][maxi]) * curWeight
ii. curWeight ← 0
4. Return totalProfit
FKP code:
#include<iostream>
using namespace std;
int main()
{
int array[2][100], n, w, i, curw, used[100], maxi = -1, totalprofit = 0;
curw = w;
//loop until knapsack is full
while (curw >= 0)
{
maxi = -1;
//loop to find max profit object
for (i = 0; i < n; i++)
{
if ((used[i] == 0) && ((maxi == -1) || (((float) array[1][i]/ (float) array[0][i]) > ((float)
array[1][maxi]/ (float) array[0][maxi]))))
{
maxi = i;
}
}
used[maxi] = 1;
}
Output for above code:
Conclusion: In implementing both the max heap and the fractional knapsack problem in C++,
you have efficiently solved classic optimization challenges. The max heap ensures the largest
element is always at the root, maintaining this property through efficient heapify operations. The
fractional knapsack problem is addressed using a greedy algorithm, allowing item division and
maximizing total value within a given weight capacity by sorting items based on their value-to-
weight ratio. These implementations demonstrate your proficiency in applying fundamental
algorithms to real-world problems.