~/ learn/ comp-372/ cards/ Greedy strategy & activity selection
1 of 5

Type the greedy activity selector (CLRS GREEDY-ACTIVITY-SELECTOR)

Type the greedy activity selector (CLRS GREEDY-ACTIVITY-SELECTOR)

Answer

GREEDY-ACTIVITY-SELECTOR(s, f, n) A = {a[1]} k = 1 for m = 2 to n if s[m] >= f[k] A = A union {a[m]} k = m return A

Activities are assumed pre-sorted so f[1] <= f[2] <= ... <= f[n]. k tracks the most recently selected activity; f[k] is the latest finish time chosen so far. The test s[m] >= f[k] checks compatibility with the whole selected set at once. The loop is Theta(n) after the O(n lg n) sort.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/50b0ebf0-80e0-444e-9070-6a0a1607ea20/flashcard utf-8 LF