Question

In: Civil Engineering

Qu 1: (a) Suppose that instead of always selecting the first activity to finish, we select...

Qu 1:
(a) Suppose that instead of always selecting the first activity to finish, we select the last activity to start that is compatible with all previously selected activities. In the case, the activities are assumed to be sorted in increasing order of start time.
Modify the Greedy_Activity_Selector algorithm to work with this approach to find a subset of maximum number of mutually compatible activities.

(b) Use the following list of activities to show how your algorithm in (a) above will work to find a
maximum set of mutually compatible activities.

Activity start time Finish time
1 0 6
2 1 4
3 2 12
4 3 5
5 3 7
6 5 8
7 5 9
8 6 10
9 8 12
10 9 11
11 12 14

Note: Show your result graphically, highlighting the selected activities with a
different color.

Solutions

Expert Solution

Solution :-

C++ Code :-

#include<bits/stdc++.h>
using namespace std;

// structure to hold activity data
struct Activity
{
int startTime;
int finishTime;
};
int main()
{
int numActivity;
cout<<"Enter number of activities :- \n"<<endl;
cin>>numActivity;

Activity activities[numActivity];

cout<<"Enter start time and finish time for activities :-"<<endl;

for(int i=0;i<numActivity;i++)
{
cout<<"Enter for activity "<<i+1<<" :- ";

cin>>activities[i].startTime>>activities[i].finishTime;
}


// Apply the algorithm for result

vector<Activity> result; // vector to hold the activities included in resultant set

int last = numActivity - 1; // index of last included activity

result.push_back(activities[numActivity - 1]);

for(int i=numActivity-2;i>=0;i--)
{
// include the activity only if it is compatible with already included
if(activities[i].finishTime <= activities[last].startTime)
{
result.push_back(activities[i]);
last = i;
}
}

cout<<"Maximum number of activities that you can carry out is "<<result.size()<<endl;

cout<<"Activities included :- ";

for(int i=result.size()-1;i>=0;i--)
{
cout<<"{"<<result[i].startTime<<", "<<result[i].finishTime<<"} ";
}
cout<<endl;
}

Sample input/output :-

Enter number of activities :-

11
Enter start time and finish time for activities :-
Enter for activity 1 :- 0 6
Enter for activity 2 :- 1 4
Enter for activity 3 :- 2 12
Enter for activity 4 :- 3 5
Enter for activity 5 :- 3 7
Enter for activity 6 :- 5 8
Enter for activity 7 :- 5 9
Enter for activity 8 :- 6 10
Enter for activity 9 :- 8 12
Enter for activity 10 :- 9 11
Enter for activity 11 :- 12 14
Maximum number of activities that you can carry out is 4
Activities included :- {3, 5} {5, 9} {9, 11} {12, 14}


Related Solutions

Problem 1. Suppose we have 4 memory modules instead of 8 in Figures 4.6 and 4.7....
Problem 1. Suppose we have 4 memory modules instead of 8 in Figures 4.6 and 4.7. Draw the memory modules with the addresses they contain using: a) High-order Interleaving and b) Low-order interleaving.
Suppose that a firm always announces a yearly dividend at the end of the first quarter...
Suppose that a firm always announces a yearly dividend at the end of the first quarter of the year, but then pays the dividend out as four equal quarterly payments. If the next such "annual" dividend has been announced as $5.40, it is exactly one quarter until the first quarterly dividend from that $5.40, the effective annual required rate of return on the company's stock is 12 percent, and all future "annual" dividends are expected to grow at 4 percent...
TB Problem Qu. 1-288 Balerio Corporation's relevant ... Balerio Corporation's relevant range of activity is 8,000...
TB Problem Qu. 1-288 Balerio Corporation's relevant ... Balerio Corporation's relevant range of activity is 8,000 units to 11,000 units. When it produces and sells 10,000 units, its average costs per unit are as follows: Average Cost per Unit Direct materials $ 6.40 Direct labor $ 3.20 Variable manufacturing overhead $ 1.50 Fixed manufacturing overhead $ 14.40 Fixed selling expense $ 2.80 Fixed administrative expense $ 2.00 Sales commissions $ 0.80 Variable administrative expense $ 0.70 Required: a. For financial...
1) In this experiment, we purified cyclohexene by distillation. Could we have used recrystallization instead to...
1) In this experiment, we purified cyclohexene by distillation. Could we have used recrystallization instead to purify? Explain. 2) At the end we stopped the distillation with some material left in the vial. Why is it considered unsafe to distill until dryness? please help!
1. Suppose in the country A, the velocity of money in the country A is always...
1. Suppose in the country A, the velocity of money in the country A is always stable. Answer the following questions: a. What is the quantity equation? (Please indicate and explain each variable) (2%) b. Suppose the price level in the period t-1 is Pt-1, and the price level in the period t is Pt in the country A. Please use Pt and Pt-1 to represent the inflation rate during the period t-1 and the period t in the country...
1. Suppose instead that a flight has 58 seats and the likelihood of a person not...
1. Suppose instead that a flight has 58 seats and the likelihood of a person not showing up is 9%. The airline sells 62 tickets. a) What is the likelihood the flight is overbooked? b) In order to do this problem using the binomial random variable we have to assume that people are acting ________________ from one another. Is this realistic? Why or why not?
1) Why do we use H3PO4 instead of H2SO4 as a catalyst for the synthesis of...
1) Why do we use H3PO4 instead of H2SO4 as a catalyst for the synthesis of cyclohexene? 2) Why do we use H3PO4 instead of HCl as a catalyst for the synthesis of cyclohexene? 3) What alkene(s) would be produced on dehydration of each of the following alcohols? If more than one product is possible, use Zaitsev’s rule to predict which product would be formed in greater amounts. a) 2-methylcyclohexanol b) 2,2-dimethylcyclohexanol c) 1,2-cyclohexanediol
Suppose that we will randomly select a sample of 64 measurements from a population having a...
Suppose that we will randomly select a sample of 64 measurements from a population having a mean equal to 18 and a standard deviation equal to 5. (a) Describe the shape of the sampling distribution of the sample mean Picture. Do we need to make any assumptions about the shape of the population? Why or why not? ; , because the sample size is . (b) Find the mean and the standard deviation of the sampling distribution of the sample...
We suppose that the number of errors Z within the first year for a fabrication is...
We suppose that the number of errors Z within the first year for a fabrication is Poisson distributed with waiting value 2.5. We also know that the number of errors Z wont be overrided or a replacement is needed for the fabrication. a. Find the probabilility that within that year the errors will occur within the probability ?? = ?? (1 ≤ ?? <5). b. The manufacturer wants to replace a maximum. 10% of all of the products. What is...
Chapter 14: Put It in Writing Activity: Suppose that a survey reveals that first-graders from various...
Chapter 14: Put It in Writing Activity: Suppose that a survey reveals that first-graders from various ethnic groups in a local school hold prejudiced attitudes toward one another. Imagine that you have been hired to develop a program to help these children become less prejudiced and more accepting of members of other ethnic groups. Write a one-page description of two or three classroom activities that would help you accomplish this goal and tell why you think they would work. Do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT