You are on page 1of 12

WEEK:10 PROGRAM-1

1. Given a list of activities with their starting time and finishing time. Your goal is to
select maximum number of activities that can be performed by a single person such that
selected activities must be non-conflicting. Any activity is said to be non-conflicting if
starting time of an activity is greater than or equal to the finishing time of the other
activity. Assume that a person can only work on a single activity at a time.

SOURCE CODE
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Activity {
 int start;
 int finish;
 int index;
};
bool compareActivity(const Activity& a1, const Activity& a2) {
 return a1.finish < a2.finish;
}
void selectNonConflictingActivities(vector<int>& startTimes, vector<int>&
finishTimes) {
 int numActivities = startTimes.size();
 vector<Activity> activities(numActivities);
 for (int i = 0; i < numActivities; i++) {
 activities[i].start = startTimes[i];
 activities[i].finish = finishTimes[i];
 activities[i].index = i + 1;
 }
 sort(activities.begin(), activities.end(), compareActivity);
 int count = 1;
 vector<int> selectedActivities;
 selectedActivities.push_back(activities[0].index);
 int prevFinishTime = activities[0].finish;
 for (int i = 1; i < numActivities; i++) {
 if (activities[i].start >= prevFinishTime) {
 count++;
 selectedActivities.push_back(activities[i].index);
 prevFinishTime = activities[i].finish;
 }
 }
 cout << "No. of non-conflicting activities: " << count << endl;
 cout << "List of selected activities: ";
 for (int i = 0; i < selectedActivities.size(); i++) {
 cout << selectedActivities[i];
 if (i != selectedActivities.size() - 1) {
 cout << ", ";
 }
 }
 cout << endl;
}
int main() {
 int numActivities;
 cout << "Enter the number of activities: ";
 cin >> numActivities;
 vector<int> startTimes(numActivities);
 vector<int> finishTimes(numActivities);
 cout << "Enter the starting times of the activities: ";
 for (int i = 0; i < numActivities; i++) {
 cin >> startTimes[i];
 }
 cout << "Enter the finishing times of the activities: ";
 for (int i = 0; i < numActivities; i++) {
 cin >> finishTimes[i];
 }
 selectNonConflictingActivities(startTimes, finishTimes);
 return 0;
}

OUTPUT
PROGRAM-2

2 . Given a long list of tasks. Each task takes specific time to accomplish it and each task
has a deadline associated with it. You have to design an algorithm and implement it
using a program to find maximum number of tasks that can be completed without
crossing their deadlines and also find list of selected tasks .

SOURCE CODE
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Task {
 int time;
 int deadline;
 int index;
};
bool compareTask(const Task& t1, const Task& t2) {
 return t1.deadline < t2.deadline;
}
void selectTasks(vector<int>& times, vector<int>& deadlines) {
 int numTasks = times.size();
 vector<Task> tasks(numTasks);
 for (int i = 0; i < numTasks; i++) {
 tasks[i].time = times[i];
 tasks[i].deadline = deadlines[i];
 tasks[i].index = i + 1;
 }
 sort(tasks.begin(), tasks.end(), compareTask);
 int maxTasks = 0;
 vector<int> selectedTasks;
 vector<bool> slots(numTasks, false);
 for (int i = 0; i < numTasks; i++) {
 for (int j = min(numTasks, tasks[i].deadline) - 1; j >= 0; j--) {
 if (!slots[j]) {
 slots[j] = true;
 maxTasks++;
 selectedTasks.push_back(tasks[i].index);
 break;
 }
 }
 }
 cout << "Max number of tasks = " << maxTasks << endl;
 cout << "Selected task numbers: ";
 for (int i = 0; i < selectedTasks.size(); i++) {
 cout << selectedTasks[i];
 if (i != selectedTasks.size() - 1) {
 cout << ", ";
 }
 }
 cout << endl;
}
int main() {
 int numTasks;
 cout << "Enter the number of tasks: ";
 cin >> numTasks;
 vector<int> times(numTasks);
 vector<int> deadlines(numTasks);
 cout << "Enter the time taken for each task: ";
 for (int i = 0; i < numTasks; i++) {
 cin >> times[i];
 }
 cout << "Enter the deadlines for each task: ";
 for (int i = 0; i < numTasks; i++) {
 cin >> deadlines[i];
 }
 selectTasks(times, deadlines);
 return 0;
}

OUTPUT
PROGRAM-3

3. Given an unsorted array of elements, design an algorithm and implement it using a


program to find whether majority element exists or not. Also find median of the array.
A majority element is an element that appears more than n/2 times, where n is the size
of array.

SOURCE CODE
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isMajorityElement(const vector<int>& arr, int& majorityElement) {
 int n = arr.size();
 int count = 1;
 majorityElement = arr[0];
 for (int i = 1; i < n; i++) {
 if (arr[i] == majorityElement) {
 count++;
 } else {
 count--;
 if (count == 0) {
 majorityElement = arr[i];
 count = 1;
 }
 }
 }
 count = 0;
 for (int i = 0; i < n; i++) {
 if (arr[i] == majorityElement) {
 count++;
 }
 }
 return (count > n / 2);
}
double findMedian(const vector<int>& arr) {
 int n = arr.size();
 if (n % 2 == 1) {
 return arr[n / 2];
 } else {
 return (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
 }
}
int main() {
 int n;
 cout << "Enter the size of the array: ";
 cin >> n;
 vector<int> arr(n);
 cout << "Enter the elements of the array: ";
 for (int i = 0; i < n; i++) {
 cin >> arr[i];
 }
 sort(arr.begin(), arr.end());
 int majorityElement;
 bool hasMajorityElement = isMajorityElement(arr, majorityElement);
 cout << (hasMajorityElement ? "yes" : "no") << endl;
 cout << "Median: " << findMedian(arr) << endl;
 return 0;
}

OUTPUT
WEEK:11 PROGRAM-1

1. Given a sequence of matrices, write an algorithm to find most efficient way to


multiply these matrices together. To find the optimal solution, you need to find
the order in which these matrices should be multiplied.

SOURCE CODE
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int matrixChainOrder(const vector<int>& dimensions) {
 int n = dimensions.size() - 1;
 vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
 for (int i = 0; i < n; i++) {
 dp[i][i] = 0;
 }
 for (int len = 2; len <= n; len++) {
 for (int i = 0; i < n - len + 1; i++) {
 int j = i + len - 1;
 for (int k = i; k < j; k++) {
 int operations1 = dp[i][k];
 int operations2 = dp[k + 1][j];
 int totalOperations = operations1 + operations2 + dimensions[i] *
dimensions[k + 1]
* dimensions[j + 1];
 dp[i][j] = min(dp[i][j], totalOperations);
 }
 }
 }
 return dp[0][n - 1];
}
int main() {
 int n;
 cout << "Enter the number of matrices: ";
 cin >> n;
 vector<int> dimensions(n + 1);
 cout << "Enter the dimensions of the matrices:" << endl;
 for (int i = 0; i <= n; i++) {
 cin >> dimensions[i];
 }
 int minOperations = matrixChainOrder(dimensions);
 cout << "Minimum number of operations: " << minOperations << endl;
 return 0;
}
OUTPUT
WEEK:12 PROGRAM-1

1.Given two sequences, Design an algorithm and implement it using a program to find
the length of longest subsequence present in both of them. A subsequence is a sequence
that appears in the same relative order, but not necessarily contiguous.

SOURCE CODE
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int longestCommonSubsequence(const string& seq1, const string& seq2) {
 int n1 = seq1.length();
 int n2 = seq2.length();
 vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
 for (int i = 1; i <= n1; i++) {
 for (int j = 1; j <= n2; j++) {
 if (seq1[i - 1] == seq2[j - 1]) {
 dp[i][j] = dp[i - 1][j - 1] + 1;
 } else {
 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
 }
 }
 }
 return dp[n1][n2];
}
int main() {
 string seq1, seq2;
 cout << "Enter the first sequence: ";
 getline(cin, seq1);
 cout << "Enter the second sequence: ";
 getline(cin, seq2);
 int length = longestCommonSubsequence(seq1, seq2);
 cout << "Length of the longest common subsequence: " << length << endl;
 return 0;
}
OUTPUT
PROGRAM-2

2. Given a knapsack of maximum capacity w. N items are provided, each having its own
value and weight. Design an algorithm and implement it using a program to find the list
of the selected items such that the final selected content has weight <= w and has
maximum value. Here, you cannot break an item i.e. either pick the complete item or
don't pick it. (0-1 property).

SOURCE CODE
# include<stdio.h>
void knapsack(int n, float weight[], float profit[], float capacity) {
   float x[20], tp = 0;
   int i, j, u;
   u = capacity;
   for (i = 0; i < n; i++)
      x[i] = 0.0;
   for (i = 0; i < n; i++) {
      if (weight[i] > u)
         break;
      else {
         x[i] = 1.0;
         tp = tp + profit[i];
         u = u - weight[i];
      }
   }
   if (i < n)
      x[i] = u / weight[i];
   tp = tp + (x[i] * profit[i]);
   printf("\nMaximum profit is:- %f", tp);

}
int main() {
   float weight[20], profit[20], capacity;
   int num, i, j;
   float ratio[20], temp;
   printf("\nEnter the no. of objects:- ");
   scanf("%d", &num);
   printf("\nEnter the weights and profits of each object:- ");
   for (i = 0; i < num; i++) {
      scanf("%f %f", &weight[i], &profit[i]);
   }
   printf("\nEnter the capacity of knapsack:- ");
   scanf("%f", &capacity);
   for (i = 0; i < num; i++) {
      ratio[i] = profit[i] / weight[i];
   }
   for (i = 0; i < num; i++) {
      for (j = i + 1; j < num; j++) {
         if (ratio[i] < ratio[j]) {
            temp = ratio[j];
            ratio[j] = ratio[i];
            ratio[i] = temp;
            temp = weight[j];
            weight[j] = weight[i];
            weight[i] = temp;

            temp = profit[j];
            profit[j] = profit[i];
            profit[i] = temp;
         }
      }
   }
   knapsack(num, weight, profit, capacity);
   return(0);
}

OUTPUT

You might also like