algorithm
interval scheduling
maximum sum
dynamic programming
computational problem

Algorithm to find the maximum sum in a sequence of overlapping intervals

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In many computational problems, particularly those involving scheduling, genetic sequencing, or even gaming, sequences of overlapping intervals are a common occurrence. The challenge of finding a maximum sum from these overlapping intervals involves developing an algorithm that can efficiently process and compute the sums to produce an optimal result. This article presents a comprehensive look at one such algorithm, designed for this purpose.

Problem Definition

Given a set of intervals, each with a starting point, an endpoint, and a positive numeric value (representing some "weight"), the task is to find a subset of non-overlapping intervals that provides the maximum sum of weights.

Input and Output

Input: A list of intervals. Each interval is represented as a tuple (start, end, value) . • Output: A subset of non-overlapping intervals with the highest possible sum of values.

Algorithm Overview

The typical approach to solving this problem is to leverage dynamic programming. The intervals must be sorted by their ending times to help facilitate a sequence that maximizes the value without overlap. Here is a step-by-step description of the algorithm:

  1. Sort Intervals: First, sort all intervals based on their end times. This sorting step ensures that when choosing an interval, all preceding intervals are candidates for forming a valid, non-overlapping sequence.
  2. Dynamic Programming Setup: Utilize a dynamic programming table (dp[] ), where dp[i] represents the maximum sum of interval weights ending at interval i .
  3. Populate the DP Table: For each interval, determine the maximum sum by considering: • Including the current interval and adding its value to the highest possible value from non-overlapping preceding intervals. • Excluding the current interval and taking the highest sum computed thus far.
    The computation for each dp[i] is given by:
    dp[i]=max(interval[i].value+dp[p(i)],dp[i1])dp[i] = \max(\text{interval}[i].\text{value} + \text{dp}[p(i)], dp[i-1])
    Here, p(i) returns the index of the last interval that does not overlap with interval i .
  4. Extract the Solution: The highest value in the dp[] table after processing all intervals corresponds to the maximum achievable sum of non-overlapping intervals.

Example

Consider the intervals: [(1, 3, 5), (2, 5, 6), (4, 6, 5), (6, 7, 4), (5, 8, 11), (7, 9, 2)] .

  1. Sort Intervals: Sorted intervals based on end times: [(1, 3, 5), (2, 5, 6), (4, 6, 5), (6, 7, 4), (7, 9, 2), (5, 8, 11)] .
  2. Process Intervals: Compute dp[] as follows: • dp[0] = 5 (Base case) • dp[1] = max(6 + dp[0], dp[0]) = max(6, 5) = 6
    dp[2] = max(5 + dp[0], dp[1]) = max(10, 6) = 10
    dp[3] = max(4 + dp[2], dp[2]) = max(14, 10) = 14
    dp[4] = max(11 + dp[0], dp[3]) = max(16, 14) = 14
    dp[5] = max(2 + dp[3], dp[4]) = max(16, 14) = 16
  3. Solution: dp[5] = 16 is the maximum sum attainable with these intervals.

Complexity

The time complexity of this algorithm is primarily due to sorting, which is O(nlogn)O(n \log n), and populating the dp[] table, which is O(n)O(n). Thus, the overall complexity is O(nlogn)O(n \log n). The space complexity is O(n)O(n) due to the dp[] array.

Table of Key Points

StepDescription
InputList of intervals with start, end, and weight
SortingSort intervals based on end times
DP InitializationInitialize a list dp[] to store maximum sums at each step
DP Table ComputationCompute dp[i] using the formula: dp[i] = max(interval[i].value + dp[p(i)], dp[i-1])
------
Time ComplexityO(nlogn)O(n \log n)
Space ComplexityO(n)O(n)

Additional Considerations

Handling Edge Cases: Check for and handle cases where intervals have the same end time or overlap completely. Consider constraints like minimum and maximum lengths or values of intervals. • Parallel Computation: With proper adjustments, this algorithm could be parallelized for distributed computing environments to handle massive datasets.

The algorithm for finding the maximum sum in a sequence of overlapping intervals is not only elegant but also efficient, allowing for practical application in a variety of fields. By focusing on non-overlapping subsequences within sorted intervals, it takes a problem that could seem computationally intensive and solves it with a streamlined, logical approach.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms