{"id":131,"date":"2026-11-13T12:32:14","date_gmt":"2026-11-13T04:32:14","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-activity-selection-takes-the-earliest-finish\/"},"modified":"2026-11-13T21:50:58","modified_gmt":"2026-11-13T13:50:58","slug":"how-activity-selection-takes-the-earliest-finish","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-activity-selection-takes-the-earliest-finish\/","title":{"rendered":"How activity selection takes the earliest finish"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Activity selection<\/strong> chooses a largest set of mutually compatible intervals. Sort the activities by finishing time and take the next activity that starts at or after the finish of the last chosen one. The greedy choice is optimal: some maximum set uses an activity that finishes first, and after it is chosen the same problem remains on the activities that start later. The sort dominates, at <strong>O(n log n)<\/strong>. A scan of an already sorted list is linear.<\/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\">Two activities conflict when their open intervals overlap. The goal is the maximum number of activities, not the maximum total length and not the maximum weight. Weighted interval scheduling is a different problem and needs dynamic programming. Unweighted selection has the greedy structure because taking the activity that frees the resource soonest leaves the largest possible remainder.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A sort by start time, or by duration, does not have that exchange argument. A short activity that starts first can block several later activities that would have fit beside a slightly later short one. Earliest finish is the sort key that works.<\/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\">The set produced by earliest-finish selection has the maximum cardinality. Time is O(n log n) for the sort plus a linear scan. Extra memory is the sorted array. Ties in finish time can be broken arbitrarily. An activity that touches another only at an endpoint is compatible if the model treats the intervals as half-open. The writeup has to match the problem&#8217;s closed or open convention. A fully closed interval that ends when the next starts is a conflict under a closed model and compatible under a half-open model.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm does not maximize a sum of weights. A heavy activity that finishes late can be the right weighted choice and the wrong cardinality choice.<\/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 activities by nondecreasing finish time. Break ties arbitrarily.<\/li>\n<li>Pick the first activity. Set the current finish to its finish time.<\/li>\n<li>Scan the rest. When an activity starts at or after the current finish, under the interval convention in use, pick it and update the current finish.<\/li>\n<li>The picked set is a maximum compatible subset.<\/li>\n<li>If the objective is a weighted sum, stop and use the weighted interval DP instead of this scan.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Cardinality is the objective. Weighted interval scheduling sorts by finish time too and then runs a binary search plus a DP, and it can reject the earliest-finishing activity. A room-assignment problem that minimizes the number of resources is interval graph coloring, a different greedy algorithm that sorts by start time.<\/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 activities are sorted by duration<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Pick the shortest remaining activity<\/td>\n<td>A short activity in the middle of several others can block two activities the earliest-finish rule would have kept. Shortest-first has no optimality proof for cardinality.<\/td>\n<\/tr>\n<tr>\n<td>Sort by start time and take the same scan<\/td>\n<td>The first activity can be the one that runs the longest and blocks everything. Earliest start is not the greedy choice.<\/td>\n<\/tr>\n<tr>\n<td>Apply the scan to weighted values and claim the maximum weight<\/td>\n<td>The exchange argument used equal weights. A weighted optimum can skip the earliest finish.<\/td>\n<\/tr>\n<tr>\n<td>Treat a shared endpoint as compatible without stating the interval model<\/td>\n<td>Closed intervals that touch conflict in some statements and not in others. The comparison has to match the statement.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Weighted non-overlapping intervals are a dynamic program. Coloring an interval graph to assign rooms sorts by start time. Activity selection is the earliest-finish cardinality greedy algorithm.<\/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\">The Bash script and the Python script sort activities by finish time and take the next one that starts at or after the previous finish. Half-open intervals. Both print the three chosen starts <code>1 5 8<\/code>.<\/p>\n\n<pre><code class=\"language-bash\"># Parallel arrays, already sorted by finish time.\nstart=(1 3 0 5 5 8)\nend=(4 5 6 7 9 9)\nn=${#start[@]}\nfinish=-1\nout=()\nfor ((i=0; i&lt;n; i++)); do\n  if (( start[i] &gt;= finish )); then\n    out+=(&quot;${start[i]}&quot;)\n    finish=${end[i]}\n  fi\ndone\necho &quot;${out[*]}&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">acts = [(1, 4), (3, 5), (0, 6), (5, 7), (5, 9), (8, 9)]\nacts.sort(key=lambda iv: iv[1])\nchosen = []\nfinish = -10**9\nfor start, end in acts:\n    if start &gt;= finish:\n        chosen.append((start, end))\n        finish = end\nassert chosen == [(1, 4), (5, 7), (8, 9)]\nprint(*(s for s, _ in chosen))\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\">Earliest-finish selection is the greedy scheduler for non-overlapping jobs: classrooms, broadcast slots, and a single machine that cannot preempt. Weighted intervals need the dynamic program instead, because the earliest finish is no longer the best value.<\/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 earliest-finish when activities have values. Use the weighted DP. Keep the sort by finish time when every selected activity counts as one and the goal is as many as possible.<\/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 activity selection returns the earliest-finish sort and the reason a shortest-first sort is not optimal. <strong>Activity selection maximizes the number of compatible intervals by always taking the one that finishes earliest among those that do not conflict with the ones already taken.<\/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: Activity selection chooses a largest set of mutually compatible intervals. Sort the activities by finishing time and take the next activity that starts at or after the finish of the last chosen one. The greedy choice is optimal: some maximum set uses an activity that finishes first,&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,37],"tags":[],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-greedy"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/131","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=131"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":530,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions\/530"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}