Mahim DSA 9
Mahim DSA 9
Submitted for
Submitted by:
Mahim Dixit
3EC1
(BE- ELECTRONICS & COMMUNICATION ENGINEERING)
Submitted to:
Dr. Ankit Soni
(Assistant Professor, ECED)
Objective: To understand and implement the brute-force approach for solving the
subset sum problem, where the goal is to determine whether a subset of integers exists
that adds up to a given target sum.
Tasks:
1. Write a program that takes a list of integers and a target sum as input, and
checks whether any subset of the list sums up to the target using brute-force.
Example:
Input:
Set = {3, 34, 4, 12, 5, 2}
Sum = 9
Expected Output:
True (because subset {4, 5} sums to 9)
Sol:
Code:
#include <iostream>
#include <vector>
// Brute‑force
int n = arr.size();
vector<vector<int>> solutions;
int sum = 0;
vector<int> subset;
sum += arr[i];
subset.push_back(arr[i]);
if (sum == target) {
solutions.push_back(subset);
return solutions;
int main() {
cout << "DSA Lab Assignment 09 under guidance of Dr. Ankit Soni \n\n";
// Read input
int n, target;
cout << "Enter number of elements: ";
cin >> n;
vector<int> arr(n);
// Output results
if (!solutions.empty()) {
cout << "Target Sum True: Subsets found with sum " << target << ":\n";
for (const auto& subset : solutions) {
} else {
cout << "False: No subset sums to " << target << "\n";
return 0;
Output:
Experiment 2: Closest Pair of Points Using Brute Force and Divide & Conquer
Tasks:
1. Write a program that receives a list of 2D points and finds the pair of points with the
minimum Euclidean distance using both approaches.
Example
Input:
Expected Output:
Code:
#include <bits/stdc++.h>
// Euclidean distance
// Brute-force O(n^2)
int n = pts.size();
for(int i=0;i<n;i++) for(int j=i+1;j<n;j++){
return {bp,best};
int n = r-l+1;
if(n <= 3) {
return brute(tmp);
int m = (l+r)/2;
double d = best.second;
vector<Point> strip;
for(int i=l;i<=r;i++)
sort(strip.begin(), strip.end(),
for(int i=0;i<strip.size();i++){
for(int j=i+1; j<strip.size() &&
return best;
sort(pts.begin(), pts.end(),
int main(){
vector<Point> pts(n);
cout<<"\n\n";
auto bf = brute(pts);
auto dc = closestDC(pts);
cout<<fixed<<setprecision(4)
<<"Brute: ("<<bf.first.first.x<<","<<bf.first.first.y
<<") distance="<<bf.second<<"\n"
<<"D&C : ("<<dc.first.first.x<<","<<dc.first.first.y
<<") distance="<<dc.second<<"\n";
// Correctness
else
// Performance notes
cout << "As n grows, the divide‑and‑conquer approach will significantly outperform
brute‑force.\n";
return 0;
}
Output:
Objective: To implement the greedy algorithm to solve the fractional knapsack problem,
demonstrating how to achieve maximum profit by taking items (or fractions of items) based
on their profit-to-weight ratio.
Tasks:
1. Write a program that calculates the maximum possible profit for a given list of items with
weights and profits and a knapsack of limited capacity. The solution must allow taking
fractions of items.
Example
Input:
Capacity = 50
Expected Output:
240.0 (Pick all of item 1 and 2, and 2/3 of item 3)
Code:
#include <iostream>
#include <vector>
#include <algorithm>
struct Item {
};
int main() {
cin >> n;
// Read capacity
double capacity;
// Echo input
cout << "(" << profits[i] << "," << weights[i] << ")"
vector<Item> items;
// Greedy selection
if (remaining == 0) break;
maxProfit += it.profit;
remaining -= it.weight;
} else {
maxProfit += it.profit * f;
cout << " " << f << " of item(ratio=" << it.ratio << ")\n"; // fraction taken
:contentReference[oaicite:6]{index=6}
remaining = 0;
// Output result
return 0;
}
Output:
---------x----------x---------------x---------------x--------------------x------------x--------------