Question

Activity Selection Last-to-Start | C++ Implementation Develop a program with a greedy algorithm that instead of always...

Activity Selection Last-to-Start | C++ Implementation

Develop a program with a greedy algorithm that instead of always selecting the first activity to finish, instead selects the last activity to start that is compatible with all previously selected activities.

The program should read input from a file named “act.txt”. The file contains lists of activity sets with number of activities in the set in the first line followed by lines containing the activity number, start time & finish time.

Example act.txt:
11
1 1 4
2 3 5
3 0 6
4 5 7
5 3 9
6 5 9
7 6 10
8 8 11
9 8 12
10 2 14
11 12 16
3
3 1 8
2 1 2
1 3 9

In the above example the first activity set contains 11 activities with activity 1 starting at time 1 and finishing at time 4, activity 2 starting at time 3 and finishing at time 5, etc.. The second activity set contains 3 activities with activity 3 starting at time 1 and finishing at time 8 etc. Note: the activities in the file are not in any sorted order.

Your results including the number of activities selected and their order should be outputted to the terminal. For the above example the results are:

Set 1
Number of activities selected = 4
Activities: 2 4 8 11

Set 2
Number of activities selected = 2
Activities: 2 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution: Greedy Algorithm: Step 1: Sorted the activities in increasing order of finish times. Step 2: Create a vector to stoC++ Code: # includeくbits/stdc++.h> using namespace std; //main function int main () //read the given file freopen(act.txt,sd.push back(p[i].second.second); It = 1; //print the given result cout << Set << sn << endl; cout << Number of activitieact.txt: main.cpp act.txt 2 1 1 4 3 2 3 5 4 3 0 6 5 4 57 6 5 3 9 7 6 5 9 8 7 6 10 9 88 11 10 9 8 12 11 10 2 14 12 11 12 16 13

EDITABLE CODE:

#include

using namespace std;

//main function

int main()

{

   //read the given file

   freopen("act.txt", "r", stdin);

   //declare the variable for at for activities

//st for start time and ft for finish time

   //sn for set number

   int tt = 1, at, st, ft, sn = 1;

   int i;

   while (cin >> tt)

   {

       //pair template using vector

       vector>> p;

       for (i = 0; i < tt; ++i)

       {

           cin >> at >> st >> ft;

           p.push_back(make_pair(ft, make_pair(st, at)));     

       }

       //sort the activities

       sort(p.begin(), p.end());

       //initializes the last lt is 0

       int lt = 0;

       //declare the vector variable sd for selected Activities

       vector sd;

       //push the selected activities

       sd.push_back(p[0].second.second);

       for (i = 1; i < tt; ++i)

       {

           if (p[i].second.first >= p[lt].first)

           {

               sd.push_back(p[i].second.second);

               lt = i;

           }

       }

       //print the given result

       cout << "Set " << sn << endl;

       cout << "Number of activities selected = " << sd.size() << "\n";

       cout << "Activities: ";

       for (int i:sd)

       {

           cout << i << " ";

       }

       cout << "\n";

       sn++;

   }

}

Add a comment
Know the answer?
Add Answer to:
Activity Selection Last-to-Start | C++ Implementation Develop a program with a greedy algorithm that instead of always...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT