Varendra University Dept. of CSE: Course Title: Algorithms Lab Course Code: CSE 226

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

বরেন্দ্র বিশ্ববিদ্যালয়

Varendra University
Dept. of CSE
Lab Report-03
Course Title : Algorithms Lab
Course Code : CSE 226
Submitted By Submitted To
Name : MD. Arnob Mustakim Tokey Ahmmed
ID : 201311015 Lecturer
Semester : 5th Dept. of CSE
Section : A Varendra University
Dept. of CSE, Md. Mosiur Rahman
Varendra University Sweet
Lecturer (Provisional)
Dept. of CSE
Varendra University

Date of Issue : 8th June 2021


Date of Submission : 20th June 2021
Deadline of Submission : 20th June 2021

Signature

MD Arnob Mustakim CSE 226 201311015


Problem 1. Activity Selection Problem.

Theory: The activity selection problem is a mathematical optimization problem.


Our first illustration is the problem of scheduling a resource among several
challenge activities. We find a greedy algorithm provides a well designed and
simple method for selecting a maximum- size set of manually compatible
activities.

Suppose S = {1, 2....n} is the set of n proposed activities. The activities share
resources which can be used by only one activity at a time,Tennis Court, Lecture
Hall, etc. Each Activity "i" has Start Time(st) and a Finish Time(ft), where st≤ft. If
selected activity "i" take place meanwhile the half-open time interval [st,ft).
Activities i and j are compatible if the intervals (st, ft) and [st, ft) do not overlap (i
and j are compatible if st ≥ft or st ≥ft). The activity-selection problem chosen the
maximum- size set of mutually consistent activities.

This is the dispute of optimally scheduling unit-time tasks on a single processor,


where each job has a deadline and a penalty that necessary be paid if the
deadline is missed.

A unit-time task is a job, such as a program to be rush on a computer that needed


precisely one unit of time to complete. Given a finite set S of unit-time tasks, a
schedule for S is a permutation of S specifying the order in which to perform
these tasks. The first task in the schedule starts at time 0 and ends at time 1; the
second task begins at time 1 and finishes at time 2, and so on.

Time-Complexity: The time-complexity of Activity Selection Problem is O(n log n)


Code:

1. #include<bits/stdc++.h>
2. using namespace std;
3.
4. class Activity
5. {
6. public:
7. int id,start,finish;
8. Activity(int id, int start, int finish)
9. {
10. this->id = id;
11. this->start = start;

MD Arnob Mustakim CSE 226 201311015


12. this->finish = finish;
13. }
14. };
15.  
16. int main()
17. {
18. Activity* ac;
19.  
20. multimap<int,Activity*>mp;
21. map<int,Activity*>::iterator it;
22.  
23. freopen("activity.txt","r",stdin);
24.  
25. int number_of_activities;
26. cin >> number_of_activities;
27.  
28. int start,finish;
29. for(int i=1; i<=number_of_activities; i++)
30. {
31. cin >> start >> finish;
32. ac = new Activity(i,start,finish);
33. mp.insert(make_pair(finish,ac));
34. }
35.  
36. int present_finish_time = 0;
37. int task = 0;
38. for(it=mp.begin(); it != mp.end(); it++)
39. {
40. if(it->second->start > present_finish_time)
41. {
42. task++;
43. present_finish_time = it->first;
44. }
45. }
46. cout << task << endl;
47.  
48. return 0;
49. } 

Output:

MD Arnob Mustakim CSE 226 201311015


Problem 3. Fractional Knapsack Problem.
Theory: Given a set of items, each with a weight and a value, determine a subset
of items to include in a collection so that the total weight is less than or equal to
a given limit and the total value is as large as possible.
The knapsack problem is in combinatorial optimization problem. It appears as a
subproblem in many, more complex mathematical models of real-world
problems.
Time-Complexity: The time-complexity of Fractional Knapsack is O(n log n).
Code:

1. #include <bits/stdc++.h>
2. using namespace std;
3.  
4. class Knapsack
5. {
6. public:
7. int id;
8. int value;
9. int weight;
10. Knapsack(int id, int value, int weight)
11. {
12. this->id = id;
13. this->value = value;
14. this->weight = weight;
15. }
16. };
17.  
18. int main()
19. {
20. Knapsack* ks;
21.  
22. multimap<int,Knapsack*, greater<int> >mp;
23. map<int,Knapsack*>::iterator it;
24.  
25. freopen("knapsack.txt","r",stdin);
26.  
27. int products,bag_size;
28. cin >> products >> bag_size;
29.  
30. int value,weight;

MD Arnob Mustakim CSE 226 201311015


31. for(int i=1; i<=products; i++)
32. {
33. cin >> value >> weight;
34. ks = new Knapsack(i,value,weight);
35. mp.insert(make_pair(value/weight,ks));
36. }
37. int profit = 0;
38. for(it=mp.begin(); it != mp.end(); it++)
39. {
40. if(bag_size == 0) break;
41. if(bag_size > it->second->weight)
42. {
43. profit += it->second->value;
44. bag_size -= it->second->weight;
45. }
46. else
47. {
48. int v = it->second->value;
49. profit += (v*bag_size)/it->second->weight;
50. bag_size = 0;
51. }
52. }
53. cout << profit << endl;
54. return 0;
55. }
56.  
57.  

Output:

MD Arnob Mustakim CSE 226 201311015

You might also like