Question

Suppose that you are scheduling a room. You are given a group of activities each of...

Suppose that you are scheduling a room. You are given a group of activities each of which has a start
and stop time. Two activities are compatible if they do not overlap (one activity finishes before another
one starts). For example, in the following activities, activity A is compatible with activities B and D but
not activity C:


Activity Start Time Stop time
A 1 2
B 2 5
C 1 3
D 5 6


The room has a start time and an end time in which it is available.
Your goal is to write a recursive method to schedule compatible activities that result in the maximum
usage of the room. The usage of the room is defined as the total duration of the scheduled activities,
that is, the sum of (stop time – start time) for all the activities scheduled to run in the room.
For example suppose that the start time and end time in which the room is available is [1,7] for the
above table. Hence, the possible schedules are:
1. Activities A, B,D: with room usage = (2-1)+(5-2) +(6-5) = 5
2. Activities C, D: with room usage = (3-1)+(6-5) = 2
3. Activities A,D: with room usage (5-2)+(6-5) =4
4. Activities A, B: with room usage (2-1)+(5-2)= 4
5. Activities B, D: with room usage (5-2)+(6-5) = 4
Therefore, the set of activities {A,B,D} gives the optimal schedule with the maximum room usage.

What you need to do:
1- Create a class Activitiy.java with three data fields : activityName, startTime, and stopTime . Create accessor (get) and mutator (set) methods for each data field.
2- Create a class Scheduling.java with the following method in it:


public Activity[] getOptimalSchedule( int roomStartTime, int roomEndtime, Activity[] activities)


This method gets the availability span of the room as well as a set of activities to choose from and returns an optimal schedule i.e., a selected set of non-overlapping activities that can be scheduled to run in the room and gives the maximum possible usage of the room. Note, depending on your input, the correct answer may not be unique (that is, there might be several valid schedules with the same total room usage)

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

Algorithm:

We will first sort the activity array according to the stop time. Then we will apply recursion. Sorting allows us to ease the traversal for checking if any range overlaps with another.

In recursion, we will keep the track of last used index, so that we can check if we can the the current index into consideration or not by checking its intersection with the previous index. Accordingly we will update our answer array. In the recur function, k denotes the number of activities that we have so far considered, prev denotes the last index used and idx denotes the current index. time denotes the range covered so far, and answer array keeps the current solution in hand. When we reach the last index, we know that we have considered one permutation and hence we update our final solution accordingly.

prev = -1 tells none has been taken into consideration. inTheDomain function tells if the current activity is in the room bounds or not. intersect function checks if there's any overlapping.

Here's the code of Activity.java:


public class Activity {
private String activityName;
private int startTime, stopTime;
  
void setStartTime(int x){
this.startTime = x;
}
void setStopTime(int x){
this.stopTime = x;
}
void setActivityName(String x){
this.activityName = x;
}
String getActivityName(){
return this.activityName;
}
int getStartTime(){
return this.startTime;
}
int getStopTime(){
return this.stopTime;
}
  
public int compare(Activity a, Activity b){
if(a.startTime > b.startTime){
return 1;
}
else if(a.startTime < b.startTime){
return 0;
}
else{
if(a.stopTime > b.stopTime){
return 1;
}
else{
return 0;
}
}
}
Activity(String name, int startTime, int stopTime){
this.activityName = name;
this.startTime = startTime;
this.stopTime = stopTime;
}
}

Here's the code for Scheduling.java:


import java.util.*;
public class Scheduling {
private int globalAns, roomStartTime, roomEndTime;
private Activity[] solution;
public int intersect(Activity a, Activity b){
if(a.getStartTime() < b.getStartTime() && a.getStopTime() < b.getStopTime()) return 0;
if(b.getStartTime() < a.getStartTime() && b.getStopTime() < a.getStopTime()) return 0;
return 1;
}
public int inTheDomain(Activity a){
if(a.getStartTime() >= this.roomStartTime && a.getStopTime() <= this.roomEndTime) return 1;
return 0;
}
public void recur(Activity[] activities, Activity[] answer, int idx, int k, int prev, int time){
if(idx == activities.length){
if(time > this.globalAns){
for(int i = 0; i < k; i++){
this.solution[i] = answer[i];
}
}
return;
}
if(inTheDomain(activities[idx]) == 0){
recur(activities, answer, idx+1, k, prev, time);
}
else if(prev == -1 || intersect(activities[idx], activities[prev]) == 0){
recur(activities, answer, idx+1, k, prev, time);
int x = (activities[idx].getStopTime() - activities[idx].getStartTime());
answer[k] = activities[idx];
recur(activities, answer, idx+1, k+1, idx, time+x);
}
else{
recur(activities, answer, idx+1, k, prev, time);
}
}
  
public Activity[] getOptimalSchedule(int roomStartTime, int roomEndtime, Activity[] activities){
Activity[] answer = new Activity[activities.length];
this.solution = new Activity[activities.length];
this.globalAns = 0;
this.roomStartTime = roomStartTime;
this.roomEndTime = roomEndtime;
Arrays.sort(activities, new Comparator<Activity>() {
public int compare(Activity a, Activity b) {
return a.getStopTime() - b.getStopTime();
}
});
recur(activities, answer, 0, 0, -1, 0);
return this.solution;
}
}

And here's the Driver.java for the main function:


import java.util.*;
class Driver{
public static void main(String args[]){
Scheduling s = new Scheduling();
Activity[] activities = new Activity[4];
activities[0] = new Activity("A", 1, 2);
activities[1] = new Activity("B", 2, 5);
activities[2] = new Activity("C", 1, 3);
activities[3] = new Activity("D", 5, 6);
Activity[] solution = new Activity[4];
solution = s.getOptimalSchedule(1, 7, activities);
for(int i = 0; solution[i] != null; i++){
System.out.print(solution[i].getActivityName());
}
System.out.println();
}
}

Add a comment
Know the answer?
Add Answer to:
Suppose that you are scheduling a room. You are given a group of activities each of...
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
  • 7. Given a set of n activities with start time and finish time F; of an...

    7. Given a set of n activities with start time and finish time F; of an i activity. Find the maximum size set of mutually compatible activities. Implement following string matching algorithms and analyze time complexities: 8. a) Naïve method b) Rabin karp Algorithm c) Finite state Automaton algorithm 9. A hash table is a data structure used to implement an associative array, al structure that can map keys to values. Implement Hashing using Linear and Quadratic Probing.

  • 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...

  • 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...

  • 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:...

  • 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...

  • .8-6. A project consists of 12 activities, represented by the project network below, where number by each arc repres...

    .8-6. A project consists of 12 activities, represented by the project network below, where number by each arc represents the duration (in days) of the associated activity 4 12 4 12 10 a) Find the earliest time, latest time, and slack for each event as well as the slack for each activity. Also identify the critical path. (b) The project has been scheduled to be completed in the minimum possible time.If all previous activities have begun as early as possible...

  • Ron Satterfield's excavation company uses both Gantt scheduling charts and Gantt load charts. Today, which is...

    Ron Satterfield's excavation company uses both Gantt scheduling charts and Gantt load charts. Today, which is the end of day 7, Ron is reviewing the Gantt chart depicting these schedules: Job 1 2 3 4 5 6 7 8 9 10 11 0 0 0 Job #151 was scheduled to begin on day 3 and to take 5 days. It got started on time and is 1 day ahead of schedule. Job #177 was scheduled to begin on day 2...

  • Ron Satterfield's excavation company uses both Gantt scheduling charts and Gantt load charts. Today, which is the e...

    Ron Satterfield's excavation company uses both Gantt scheduling charts and Gantt load charts. Today, which is the end of day 7, Ron is reviewing the Gantt chart depicting these schedules: Day Job 123 4567 89 1011 Job #151 was scheduled to begin time and is 1 day ahead of schedule. on day 3 and to take 6 days. It got started on 151 Job #177 was scheduled to begin on day 3 and take 5 days. It is currently on...

  • 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 #2. (20 marks) The following table shows activities related to a small project and their...

    Question #2. (20 marks) The following table shows activities related to a small project and their weekly usage of labors. Smooth (level) the resource usage so that a preferred resource usage is obtained. Activity IPA Duration week) Labor/week start start start Selo F G H Question #3. (15marks) For the project information of question2 above, and referring to the field report below, perform a revised schedule, then comment on it. At the end of week 5 the following field information...

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