Cse232 L4
Cse232 L4
LAB REPORTS - 04
COURSE TITLE- Algorithms Lab
COURSE CODE- CSE 232
Submitted To:
Md. Sajid Ahmed Chowdhury
Lecturer
Department of CSE
BUBT
Submitted By:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int capacity = 10; // Knapsack capacity
};
return 0;
}
1
Explanation:
This program implements the Fractional Knapsack Problem using a greedy algorithm. The goal is to
maximize the total value of items that can be carried in a knapsack with a limited capacity. The greedy approach
ensures that the items with the highest value-to-weight ratio are taken first, either fully or partially, depending
on the remaining capacity of the knapsack.
1. Compare Function:
• The compare function is used to sort the items in decreasing order based on their value-to-weight ratio.
• ratio1 and ratio2 calculate the value-to-weight ratio for each item.
• The function returns true if the ratio of the first item is greater than that of the second item, ensuring
that the items are sorted in descending order of their efficiency (value per unit weight).
• The function begins by sorting the items based on their value-to-weight ratio using the compare
function.
• It then iterates over the sorted list of items:
o Full Item: If the knapsack has enough remaining capacity to carry the full item, it adds the item's
value to the total value and reduces the knapsack’s capacity by the item's weight.
o Fractional Item: If the knapsack cannot carry the full item, it takes a fraction of the item,
calculates the proportional value that fits in the remaining capacity, and adds it to the total value.
The function then returns the total accumulated value after taking the fraction.
• The total value is returned as the maximum profit that the knapsack can carry.
3. Main Function:
• The main function initializes the capacity of the knapsack and defines a list of items, where each item is
represented by a pair of integers—its weight and value.
• It then calls the fractionalKnapsack function to calculate the maximum total value the knapsack can
carry, given the capacity and items.
• Finally, it prints the result, showing the total value or profit of the items that can be taken in the
knapsack.
2
CODE Task - 02
#include <iostream>
#include <vector>
#include <algorithm>
}else
{
int main() {
int capacity = 10; // Knapsack capacity
return 0;
}
Explanation:
This program implements the Fractional Knapsack Problem using a greedy algorithm. The aim is to
maximize the value of items that can be carried in a knapsack, where items can be taken fractionally based on
their value-to-weight ratio. This approach allows us to pick items in order of their efficiency (i.e., value per unit
weight), starting with the most valuable per unit weight.
1. Compare Function:
• The compare function is used to sort the items based on their value-to-weight ratio.
• The value-to-weight ratio is represented by the second element (value) in each item, but the weight (first
element) is not being utilized in the code for comparison in the current version.
• The function compares the value of each item (ratio1 and ratio2), sorting the items in decreasing
order of their value. This ensures that the most valuable items are processed first.
• Sorting: The function begins by sorting the items in decreasing order of their value-to-weight ratio
using the compare function.
• Processing Items:
o The loop iterates through the sorted list of items.
o Full Item: If the knapsack has enough remaining capacity to carry the full item, it adds the
product of the item's weight and value to the total value (this is a mistake in the code, it should
ideally add value to the total value).
o Partial Item: If the knapsack cannot carry the full item, it takes the fraction of the item that can
fit in the remaining capacity and adds the product of the value and remaining capacity to the
total value. Once a partial item is taken, the loop terminates.
• The function returns the totalValue, representing the maximum value the knapsack can carry.
3. Main Function:
• The main function initializes the capacity of the knapsack and defines a set of items. Each item is
represented as a pair of integers, where the first integer is the weight and the second is the value.
• It then calls the fractionalKnapsack function to calculate the total maximum value the knapsack can
carry.
• Finally, it prints the result, showing the total value that can be accommodated in the knapsack.
4
CODE Task - 03
#include <iostream>
#include <vector>
#include <algorithm>
int arrows = 1; // Start with one arrow for the first balloon
int lastArrow = points[0][1]; // Shoot the first arrow at the end of the first
balloon
return arrows;
}
int main() {
vector<vector<int>> points1 = {{10, 16}, {2, 8}, {1, 6}, {7, 12}};
vector<vector<int>> points2 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
vector<vector<int>> points3 = {{1, 2}, {2, 3}, {3, 4}, {4, 5}};
cout << "Minimum arrows for points1: " << findMinArrows(points1) << endl;
cout << "Minimum arrows for points2: " << findMinArrows(points2) << endl;
cout << "Minimum arrows for points3: " << findMinArrows(points3) << endl;
return 0;
}
5
Explanation:
This code solves the minimum number of arrows needed to burst all the balloons using a greedy
algorithm. The goal is to minimize the number of arrows required to burst all balloons represented by intervals
in a 2D array. Each balloon is defined by a pair of integers [xstart, xend], where xstart is the start of the
balloon's horizontal diameter and xend is the end.