0% found this document useful (0 votes)
35 views5 pages

Shantanu-3 3

The document is an experiment report submitted by a student. It includes solving two problems based on branch and bound algorithms. The first problem involves finding the maximum number of pairwise disjoint beautiful pairs in two arrays. The second problem involves determining the minimum number of miles a person would need to walk to maintain their weight after eating cupcakes in different orders. The student implemented solutions to both problems and included code snippets. The learning outcomes from the experiment are also summarized which include improving problem solving skills, understanding of optimization techniques, and algorithm design analysis skills.

Uploaded by

Srijan Mishra
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)
35 views5 pages

Shantanu-3 3

The document is an experiment report submitted by a student. It includes solving two problems based on branch and bound algorithms. The first problem involves finding the maximum number of pairwise disjoint beautiful pairs in two arrays. The second problem involves determining the minimum number of miles a person would need to walk to maintain their weight after eating cupcakes in different orders. The student implemented solutions to both problems and included code snippets. The learning outcomes from the experiment are also summarized which include improving problem solving skills, understanding of optimization techniques, and algorithm design analysis skills.

Uploaded by

Srijan Mishra
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/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-3.3

Student Name: Shantanu Rai UID: 21BCS8582


Branch: BE - CSE Section/Group: 21BCS_SN-911-A
Semester: 5th Date of Performance: 23-10-23
Subject Name: Advance Programming Lab-1 Subject Code: 21CSP-314

Aim: Implement the problems based on Branch and Bound


Objective:
The objective of implementing problems based on Branch and Bound is to optimize
the solution for combinatorial or search problems by systematically exploring the
solution space, pruning branches that are guaranteed to be suboptimal.
Problem 1:
You are given two arrays, and , both containing integers.
A pair of indices is beautiful if the element of array is equal to the element of array . In other
words, pair is beautiful if and only if . A set containing beautiful pairs is called a beautiful set.
A beautiful set is called pairwise disjoint if for every pair belonging to the set there is no repetition
of either or values. For instance, if and the beautiful set is not pairwise disjoint as there is a
repetition of , that is .
Your task is to change exactly element in so that the size of the pairwise disjoint beautiful set is
maximum.
Function Description
Complete the beautifulPairs function in the editor below. It should return an integer that represents
the maximum number of pairwise disjoint beautiful pairs that can be formed.
beautifulPairs has the following parameters:
• A: an array of integers
• B: an array of integers

Script / Code:
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int beautifulPairs(vector<int> A, vector<int> B) {
int n = A.size();
unordered_map<int, int> freqA, freqB;
for (int i = 0; i < n; i++) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

freqA[A[i]]++;
freqB[B[i]]++;
}
int common = 0;
for (int i = 0; i < n; i++) {
if (freqA[A[i]] > 0 && freqB[A[i]] > 0) {
common++;
freqA[A[i]]--;
freqB[A[i]]--;
}
}
if (common == n) {
// If all elements are common, change one element to make it non-common
return n - 1;
} else {
return common + 1;
}
}
int main() {
int n;
cin >> n;
vector<int> A(n), B(n);

for (int i = 0; i < n; i++) {


cin >> A[i];
}
for (int i = 0; i < n; i++) {
cin >> B[i];
}
int result = beautifulPairs(A, B);
cout << result << endl;
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 2:
Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a calorie count, and
Marc can walk a distance to expend those calories. If Marc has eaten cupcakes so far, after
eating a cupcake with calories he must walk at least miles to maintain his weight.
Example

If he eats the cupcakes in the order shown, the miles he will need to walk are . This is not
the minimum, though, so we need to test other orders of consumption. In this case, our
minimum miles is calculated as .
Given the individual calorie counts for each of the cupcakes, determine the minimum
number of miles Marc must walk to maintain his weight. Note that he can eat the
cupcakes in any order.
Function Description
Complete the marcsCakewalk function in the editor below.
marcsCakewalk has the following parameter(s):
• int calorie[n]: the calorie counts for each cupcake
Returns
• long: the minimum miles necessary
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Script / Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long marcsCakewalk(vector<int> calorie) {
// Sort the cupcakes in descending order
sort(calorie.rbegin(), calorie.rend());
long long total_miles = 0;
// Calculate the minimum miles
for (int i = 0; i < calorie.size(); i++) {
total_miles += (1LL << i) * calorie[i];
}
return total_miles;
}
int main() {
int n;
cin >> n;
vector<int> calorie(n);
for (int i = 0; i < n; i++) {
cin >> calorie[i];
}

long long result = marcsCakewalk(calorie);


cout << result << endl;

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

LEARNING OUTCOMES :

1. Problem-solving Skills: Students will develop a strong problem-solving


mindset as they learn to break down complex problems into smaller
subproblems and systematically explore potential solutions.

2. Optimization Techniques: Students will gain a deep understanding of


optimization strategies, learning how to efficiently prune unpromising
branches and improve the overall efficiency of search algorithms.

3. Algorithm Design and Analysis: This experience will enhance their ability
to design and analyze algorithms, helping them apply Branch and Bound
techniques to various real-world scenarios and evaluate their effectiveness.

You might also like