{"id":295,"date":"2026-11-13T12:37:15","date_gmt":"2026-11-13T04:37:15","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-weighted-intervals-choose-by-dynamic-programming\/"},"modified":"2026-11-13T21:58:45","modified_gmt":"2026-11-13T13:58:45","slug":"how-weighted-intervals-choose-by-dynamic-programming","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-weighted-intervals-choose-by-dynamic-programming\/","title":{"rendered":"How weighted intervals choose by dynamic programming"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Weighted interval scheduling<\/strong> picks a compatible set of intervals whose weights sum to the maximum. Sort by finishing time. For each interval j, let p(j) be the rightmost interval that finishes before j starts. Then the optimum for the first j intervals is the larger of weight(j) plus the optimum at p(j), and the optimum that skips j. Computed from left to right, the recurrence is <strong>O(n log n)<\/strong> because each p(j) is a binary search. Unweighted activity selection is the special case where every weight is 1, and there the earliest finish is enough.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">When every selected interval scores one point, taking the interval that finishes earliest leaves the largest possible remainder, and a greedy scan is optimal. A weight breaks that exchange. A heavy interval that finishes late can be worth more than several light intervals that finish early and overlap it. The choice at j is then a real branch: take j and lose everything that overlaps it, or skip j and keep the best schedule that did not need it.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Sorting by finish time makes the branch a single index. Every interval compatible with j lies inside the prefix that finishes before j starts, and p(j) is the last index of that prefix. The subproblem on a prefix depends only on shorter prefixes.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">For positive weights the recurrence computes the maximum weight of a compatible subset of the whole set. Time is O(n log n) for the sort and the binary searches, plus O(n) for the table. Extra memory is the table of n optima and the predecessor index used to reconstruct one subset. Ties can be broken either way. The interval convention, closed or half-open, has to match the comparison inside p(j).<\/p>\n\n\n<p class=\"wp-block-paragraph\">Negative weights on an optional interval would simply never be taken. The standard statement uses positive weights. The algorithm does not model a resource that can take two intervals at once. That generalization is a different scheduling problem.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Sort the intervals by nondecreasing finish time.<\/li>\n<li>For each j, binary search the largest i &lt; j whose finish is compatible with j&#8217;s start. That index is p(j), or 0 if none exists.<\/li>\n<li>Set opt(0) = 0. For j from 1 to n, set opt(j) to the maximum of weight(j) + opt(p(j)) and opt(j \u2212 1).<\/li>\n<li>opt(n) is the maximum weight.<\/li>\n<li>Walk backward from n. If the take branch wins at j, include j and jump to p(j). Otherwise step to j \u2212 1.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The recurrence needs arbitrary positive weights. Equal weights reduce to maximum cardinality, which activity selection solves by earliest finish without a table. Weighted interval scheduling is the prefix optimum.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the earliest finish is taken under unequal weights<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Run the unweighted greedy scan and sum the weights of what it took<\/td>\n<td>Earliest finish maximizes the count. A heavier overlapping interval can lose the scan and win the weighted optimum.<\/td>\n<\/tr>\n<tr>\n<td>Define p(j) as the nearest start time instead of the latest compatible finish<\/td>\n<td>The prefix property came from sorting by finish. A start-time neighbor is not the subproblem the recurrence calls.<\/td>\n<\/tr>\n<tr>\n<td>Fill the table in arrival order<\/td>\n<td>opt(j) reads opt(p(j)) and opt(j \u2212 1). Both indexes are smaller only in finish order.<\/td>\n<\/tr>\n<tr>\n<td>Treat a shared endpoint as compatible without fixing the interval model<\/td>\n<td>A closed interval that ends at the next start overlaps it under a closed model. The binary search has to use the same test as the problem statement.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Unit weights are activity selection. A capacity-limited set of items is a knapsack. Weighted interval scheduling is the finish-order dynamic program.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment sorts weighted intervals by finish time. p(j) is the latest interval compatible with j, found by binary search. It prints the maximum weight <code>8<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import bisect\nintervals = [(1, 3, 5), (2, 5, 6), (4, 6, 5), (6, 7, 4), (5, 8, 11), (7, 9, 2)]\nintervals.sort(key=lambda iv: iv[1])\nfinish = [iv[1] for iv in intervals]\np = []\nfor i, (start, end, weight) in enumerate(intervals):\n    p.append(bisect.bisect_right(finish, start) - 1)\nopt = [0] * (len(intervals) + 1)\nfor j in range(1, len(intervals) + 1):\n    take = intervals[j - 1][2] + opt[p[j - 1] + 1]\n    opt[j] = max(take, opt[j - 1])\nassert opt[-1] == 17\nprint(opt[-1])\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Weighted interval scheduling selects non-overlapping paid slots, such as ads or billable jobs, by dynamic programming on finish time. Earliest-finish greedy is optimal only when every interval has the same value. The weighted case has to look at the best compatible predecessor.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the table when every weight is 1. Earliest finish is optimal and simpler. Keep the recurrence when intervals carry unequal values and overlaps are forbidden.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for weighted interval scheduling returns p(j) and the choice between taking j and skipping j. <strong>Weighted interval scheduling maximizes the weight of non-overlapping intervals by a finish-order recurrence, taking j together with p(j) or skipping j.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Weighted interval scheduling picks a compatible set of intervals whose weights sum to the maximum. Sort by finishing time. For each interval j, let p(j) be the rightmost interval that finishes before j starts. Then the optimum for the first j intervals is the larger of weight(j)&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,33],"tags":[],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/295\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}