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
  • Instead of always selecting the first activity to finish, select the last activity to start that...

    Instead of always selecting the first activity to finish, select the last activity to start that is compatible with all previously selected activities. Accomplish this by implementing a greedy algorithm in either Python or C++. This is similar to a first-to-start algorithm, but in reverse; the goal is to select the start-times that appear later. See the example below and ensure that your output matches the example below. Include a verbal description of your algorithm, pseudocode and analysis of the...

  • Edit: The max number of activities is arbitrary. As written in question; program should ask us...

    Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered. It is needed to select activities from these activities. Max number is up to us. 323as1) Could you please help me to solve this problem? (ONLY USING C++) Problem: You are requested to create a class called “Activity”. You will use ordered link list to hold Activity’s class information. In order to...

  • 11 The following activity times are given: Determine the following a) Earliest start time b) Earliest...

    11 The following activity times are given: Determine the following a) Earliest start time b) Earliest finish time c) Latest Start Time d) Latest finish time e) Critical Path Activity Completion time( in months) 1-2 5 1-3 6 1-4 2 2-5 3 2-6 1 3-5 3 4-5 0 4-7 2 5-8 9 5-7 5 7-8 4 6-9 7 8-9 3

  • Using the information shown in the table below, what is the latest start time for activity E? ACTIVITY Immediate Pred...

    Using the information shown in the table below, what is the latest start time for activity E? ACTIVITY Immediate Predecessor ACTIVITY TIME EARLIEST START EARLIEST FINISH LATEST START LATEST FINISH SLACK A None 2 0 0 2 0 B A 1 2 3 3 4 1 C A 3 2 5 2 5 0 D B 7 3 10 4 11 E B, C 3 5 8 11 F C 5 11 5 11 0 G D ,E, F 4...

  • Consider a modification to the activity-selection problem in which each activity ai has, in...

    Full question from above:"Consider a modification to the activity-selection problem in which each activity ai has, in addition to a start and a finish time, a value vi. The objective is nolonger to maximize the number of activities scheduled, but instead to maximize the total value of the activities scheduled. That is, we wish to choose a set A ofcompatible activities such that [summation, sub k in A, of v sub k] is maximized. Give a polynomial-time algorithm for this...

  • Given is a CPM project network diagram as shown below. Activity Start A B C D...

    Given is a CPM project network diagram as shown below. Activity Start A B C D E F G H End day 0 5 6 9 6 9 8 5 4 0 a) The Project Completion time = days. b) The Earliest Start time, ES, of Activity G = days. c) The Latest Finish time, LF, of Acitivity C = days.. d) The critical activities are = (ex. Fill in answer as: ABCD) End Start

  • Consider the following project activities: Calculate the expected time (te) for each activity. Draw an Activity...

    Consider the following project activities: Calculate the expected time (te) for each activity. Draw an Activity on Node (AON) diagram to reflect the flow of these activities. Calculate the Early Start (ES), Early Finish (EF), Late Start (LS), and Late Finish (LF) for each activity. Calculate the slack for each activity. Identify all activities on the Critical Path. Use the data to calculate the probability the project will finish in 20 weeks (Hint: z-score). Activity A 8 с D E...

  • QUESTION 1 G E. H. C A Finish Start D. Complete the network activity diagram using the table below to answer questio...

    QUESTION 1 G E. H. C A Finish Start D. Complete the network activity diagram using the table below to answer questions 1-15. The expected activity completion times are provided in days. Each question is 1 point. Most Probable Activity Optimistic Pessimistic A 7 B 12 C 5 12 7 E 10 15 20 6 9 18 G 6 13 H 11 What is the early start (ES) time for activity F? 13 12 10 None of the above are...

  • Suppose n activities apply for using a common resource. Activity ai (1 ≤ i ≤ n)...

    Suppose n activities apply for using a common resource. Activity ai (1 ≤ i ≤ n) has a starting time S[i] and a finish time F[i] such that 0 < S[i] < F[i]. Two activities ai and aj (1 ≤ i, j ≤ n) are compatible if intervals [S[i], F[i]) and [S[j], F[j]) do not overlap. We assume the activities have been sorted such that S [1] ≤ S [2] ≤ …≤ S[n]. (a) Design an O(n2) dynamic programming algorithm...

  • Consider the following work breakdown structure: Start Finish Time Estimates(wks) Activity Node Node to tm tp...

    Consider the following work breakdown structure: Start Finish Time Estimates(wks) Activity Node Node to tm tp A 1 2 11 20 29 B 2 3 2 5 8 C 2 4 18 30 42 D 3 4 7 10 13 E 1 4 31 40 43 What is the estimated standard deviation in the time for activity E?

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