0% found this document useful (0 votes)
3 views8 pages

Assignment No 9

The document contains three programming assignments in C++. The first assignment implements a function to determine if a subset with a given sum exists within a set of integers. The second assignment finds the closest pair of points using both brute force and divide-and-conquer methods, while the third assignment calculates the maximum profit from a fractional knapsack problem.

Uploaded by

kskunal1322
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Assignment No 9

The document contains three programming assignments in C++. The first assignment implements a function to determine if a subset with a given sum exists within a set of integers. The second assignment finds the closest pair of points using both brute force and divide-and-conquer methods, while the third assignment calculates the maximum profit from a fractional knapsack problem.

Uploaded by

kskunal1322
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT NO-9

Q1.
#include <iostream>
#include <vector>
using namespace std;

bool isSubsetSum(vector<int>& set, int n, int sum) {


if (sum == 0) return true;
if (n == 0) return false;

if (set[n-1] > sum)


return isSubsetSum(set, n-1, sum);

return isSubsetSum(set, n-1, sum) || isSubsetSum(set, n-1, sum -


set[n-1]);
}

int main() {
vector<int> set = {3, 34, 4, 12, 5, 2};
int sum = 9;
int n = set.size();

if (isSubsetSum(set, n, sum))
cout << "True" << endl;
else
cout << "False" << endl;

return 0;
}

Q2.
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
using namespace std;

// Point structure
struct Point {
int x, y;
};

// Calculate Euclidean distance


double distance(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) +
(a.y - b.y) * (a.y - b.y));
}

// -------------------- BRUTE FORCE METHOD --------------------


void closestPairBruteForce(vector<Point>& points) {
double minDist = numeric_limits<double>::max();
Point p1, p2;

int n = points.size();
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j) {
double d = distance(points[i], points[j]);
if (d < minDist) {
minDist = d;
p1 = points[i];
p2 = points[j];
}
}

cout << "\n[Brute Force] Closest Pair: (" << p1.x << "," << p1.y << ")
and ("
<< p2.x << "," << p2.y << ") with distance ≈ " << minDist << endl;
}

// -------------------- DIVIDE AND CONQUER METHOD --------------------


// Comparators for sorting
bool compareX(Point a, Point b) { return a.x < b.x; }
bool compareY(Point a, Point b) { return a.y < b.y; }

// Find closest pair in strip


double stripClosest(vector<Point>& strip, double d) {
double minDist = d;
sort(strip.begin(), strip.end(), compareY);

for (int i = 0; i < strip.size(); ++i)


for (int j = i + 1; j < strip.size() && (strip[j].y - strip[i].y) < minDist;
++j)
minDist = min(minDist, distance(strip[i], strip[j]));

return minDist;
}

// Recursive utility function


double closestUtil(vector<Point>& pointsSortedX, int left, int right) {
if (right - left <= 3) {
double minDist = numeric_limits<double>::max();
for (int i = left; i < right; ++i)
for (int j = i + 1; j < right; ++j)
minDist = min(minDist, distance(pointsSortedX[i],
pointsSortedX[j]));
return minDist;
}

int mid = (left + right) / 2;


double dl = closestUtil(pointsSortedX, left, mid);
double dr = closestUtil(pointsSortedX, mid, right);
double d = min(dl, dr);

// Build strip around midpoint


vector<Point> strip;
for (int i = left; i < right; ++i)
if (abs(pointsSortedX[i].x - pointsSortedX[mid].x) < d)
strip.push_back(pointsSortedX[i]);

return min(d, stripClosest(strip, d));


}

// Wrapper function for divide and conquer


void closestPairDivideAndConquer(vector<Point>& points) {
vector<Point> sortedPoints = points;
sort(sortedPoints.begin(), sortedPoints.end(), compareX);
double minDist = closestUtil(sortedPoints, 0, sortedPoints.size());

cout << "[Divide & Conquer] Closest Distance ≈ " << minDist <<
endl;
}

// -------------------- MAIN FUNCTION --------------------


int main() {
vector<Point> points = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3,
4}};

cout << "Input Points:\n";


for (auto p : points)
cout << "(" << p.x << "," << p.y << ") ";
cout << endl;

// Run both methods


closestPairBruteForce(points);
closestPairDivideAndConquer(points);

return 0;
}
Q3.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Item {
int profit, weight;
double ratio;

Item(int p, int w) {
profit = p;
weight = w;
ratio = (double)p / w;
}
};

bool compare(Item a, Item b) {


return a.ratio > b.ratio;
}

double fractionalKnapsack(vector<Item>& items, int capacity) {


sort(items.begin(), items.end(), compare);
double totalProfit = 0.0;
for (auto& item : items) {
if (capacity >= item.weight) {
capacity -= item.weight;
totalProfit += item.profit;
} else {
totalProfit += item.ratio * capacity;
break;
}
}
return totalProfit;
}

int main() {
vector<Item> items = { {60, 10}, {100, 20}, {120, 30} };
int capacity = 50;

double maxProfit = fractionalKnapsack(items, capacity);


cout << "Maximum Profit = " << fixed << setprecision(2) <<
maxProfit << endl;

return 0;
}

You might also like