Greedy Method for Maximizing Non-Overlapping Intervals
Overview of the Approach:
1. Arrange the Intervals:
Begin by sorting all given time intervals in ascending order according to their end times.
This method ensures that you always consider the interval that releases the resource the
earliest.
2. Pick Compatible Intervals:
Use a variable (for instance, lastEnd) to remember the nish time of the most recently
selected interval. Loop through the sorted list and, for each interval, if its starting time is at
least as late as lastEnd, add it to the selection and update lastEnd to this interval's
ending time.
3. Display the Selected Intervals:
Finally, output the set of intervals that were chosen, representing the maximum number of
non-overlapping intervals.
Revised C++ Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Structure to represent an interval with start and finish
times.
struct TimeInterval {
int start;
int finish;
};
// Comparator to order intervals by their finish times.
bool compareIntervals(const TimeInterval &first, const
TimeInterval &second) {
return first.finish < second.finish;
}
// Function to select the maximum set of non-overlapping
intervals.
vector<TimeInterval> selectIntervals(const
vector<TimeInterval>& intervals) {
vector<TimeInterval> sortedIntervals = intervals;
sort(sortedIntervals.begin(), sortedIntervals.end(),
compareIntervals);
fi
vector<TimeInterval> selected;
int lastEnd = -1; // Initialize to a value less than any
possible start time.
// Iterate over each interval and choose if it doesn't
overlap with the previous one.
for (const auto &interval : sortedIntervals) {
if (interval.start >= lastEnd) {
selected.push_back(interval);
lastEnd = interval.finish;
}
}
return selected;
}
int main() {
// Sample set of intervals.
vector<TimeInterval> intervals = {
{1, 4}, {3, 5}, {0, 6}, {5, 7}, {3, 9},
{5, 9}, {6, 10}, {8, 11}, {8, 12}, {2, 14}, {12, 16}
};
cout << "Original Intervals:" << endl;
for (const auto &interval : intervals) {
cout << "[" << interval.start << ", " <<
interval.finish << "] ";
}
cout << "\n\n";
// Apply the greedy algorithm to select non-overlapping
intervals.
vector<TimeInterval> chosenIntervals =
selectIntervals(intervals);
cout << "Selected Intervals:" << endl;
for (const auto &interval : chosenIntervals) {
cout << "[" << interval.start << ", " <<
interval.finish << "] ";
}
cout << "\n";
return 0;
}
Explanation of the Test Case
• Input:
The test data includes 11 intervals (e.g., the interval [1, 4] starts at 1 and ends at 4).
• Process:
First, the intervals are sorted by their end times. Then, the algorithm iterates through the
sorted list and selects intervals that start at or after the nish time of the last chosen interval.
For example, after selecting [1, 4], it chooses [5, 7] next because its start time is not
earlier than 4.
• Output:
The program prints both the original list of intervals and the nal list of non-overlapping
intervals, thereby maximizing the number of requests that can be accommodated.
fi
fi