Lecture#3 - Greedy Algorithm
Lecture#3 - Greedy Algorithm
• Problem: Pick k scores out of n scores such that the sum of these k scores is the
largest.
• Algorithm:
FOR i = 1 to k
pick out the largest number and
delete this number from the input.
ENDFOR
• A set of activities (conferences) – each conference has a start time and a finish time:
Conf ID 1 2 3 4 5 6 7 8 9 10 11
Start Time 1 3 0 5 3 5 6 8 8 2 12
Finish Time 4 5 6 7 8 9 10 11 12 13 14
• A problem that may have many feasible solutions and each solution has a value.
• A greedy algorithm works in phases – taking the best solution right now, without
regard for future consequences.
• Greedy strategy usually progresses in a top-down fashion, making one greedy choice
after another, reducing each problem to a smaller one.
• Two ingredients that are exhibited by most problems that lend themselves to a greedy
strategy:
• Greedy-Choice property - when we have a choice to make, make the one that looks best
right now.
• Optimal Substructure - an optimal solution to the problem contains within it optimal
solutions to sub-problems
Optimal Substructure
• make a sequence of choices
• each choice is the one that seems best so far, only depends on what's been done so far
A feasible set (of candidates) is promising if it can be extended to produce not merely a
solution, but an optimal solution to the problem. [ An empty set is always promising why?
]
• Problem #2: Find the minimum # of 4, 3, and 1 cent coins to make up 6 cents.
• Optimal solutions:
• change making for “normal” coin denominations, (minimum/maximum) spanning tree,
single-source shortest paths, scheduling problems, Huffman Codes
• Approximations
• Traveling salesman problem (TSP), knapsack problem, other combinatorial optimization
problems (CSP, WTAP, VRP)
• Take the item with the highest ratio and • Positive value W.
• If the items are already sorted into decreasing order of vi / wi, then the while-loop takes
a time in O(n);
• As main time taking step is sorting, the whole problem can be solved in
• O(n log n) using merge/quick sort => sort: O(n log n), loop: O(n)
• O(n2) using selection/bubble sort => sort: O(n2), loop: O(n)
• O(n) using max-heap sort => heap: O(n) loop: O(log n)