CS 4407 Algorithms Greedy Algorithms and Minimum Spanning Trees Prof. Gregory Provan Department of Computer Science University College Cork 1
Sample MST 6 5 4 9 14 10 2 3 8 15
Greedy Algorithms When are greedy algorithms optimal? Must satisfy the optimal substructure property Optimal sub-problem must participate in optimal problem
What makes a greedy algorithm? Feasible Has to satisfy the problem s constraints Locally Optimal The greedy part Has to make the best local choice among all feasible choices available on that step If this local choice results in a global optimum then the problem has optimal substructure Irrevocable Once a choice is made it can t be un-done on subsequent steps of the algorithm Simple examples: Playing chess by making best move without lookahead Giving fewest number of coins as change Simple and appealing, but don t always give the best solution
Definition Spanning Tree A spanning tree of a graph G is a tree (acyclic) that connects all the vertices of G once i.e. the tree spans every vertex in G A Minimum Spanning Tree (MST) is a spanning tree on a weighted graph that has the minimum total weight such that w(t) is minimum w( T ) = w( u, v) u, v T
Sample MST Which links to make this a MST? 6 5 4 9 14 10 2 3 8 15 Optimal substructure: A subtree of the MST must in turn be a MST of the nodes that it spans.
MST Claim Claim: Say that M is a MST If we remove any edge (u,v) from M then this results in two trees, T1 and T2. T1 is a MST of its subgraph while T2 is a MST of its subgraph. Then the MST of the entire graph is T1 + T2 + the smallest edge between T1 and T2 If some other edge was used, we wouldn t have the minimum spanning tree overall
Greedy Algorithm We can use a greedy algorithm to find the MST. Two common algorithms Kruskal Prim
Example: Kruskal s MST Algorithm Idea: Greedily construct the MST Go through the list of edges and make a forest that is a MST At each vertex, sort the edges Edges with smallest weights examined and possibly added to MST before edges with higher weights Edges added must be safe edges that do not ruin the tree property.
Activity-Selection Problem Problem: get your money s worth out of a carnival Buy a wristband that lets you onto any ride Lots of rides, each starting and ending at different times Your goal: ride as many rides as possible Another, alternative goal that we don t solve here: maximize time spent on rides Welcome to the activity selection problem
Formally: Activity-Selection Given a set S of n activities s i = start time of activity i f i = finish time of activity i Find max-size subset A of compatible activities 3 2 1 4 6 5 Assume (wlog) that f 1 f 2 f n
Activity Selection Problem Problem: Schedule an exclusive resource in competition with other entities. Formal Definition: Set S={1,2, n} of activities. Each activity has a start time s i and a finish time f j, where s i <f j. Activities i and j are compatible if they do not overlap. The activity selection problem select a maximum-size set of mutually compatible activities.
Greedy Choice Property Activity selection problem exhibits the greedy choice property: Locally optimal choice globally optimal sol n Theorem: if S is an activity selection problem sorted by finish time, then optimal solution A S such that {1} A Sketch of proof: if optimal solution B that does not contain {1}, can always replace first activity in B with {1} (Why?). Same number of activities, thus optimal.
Activity Selection: A Greedy Algorithm So actual algorithm is simple: Sort the activities by finish time Schedule the first activity Then schedule the next activity in sorted list which starts after previous activity finishes Repeat until no more activities Intuition is even more simple: Always pick the shortest ride available at the time
Greedy Activity Selection Just march through each activity by finish time and schedule it if possible:
Activity Selection Example Schedule job 1, then try rest: (end up with 1, 4, 8): T=1 T=2 T=3 T=4 T=5 T=6 T=7 T=8 T=9 T=10 T=11 T=12 T=13 1 1 1 1 Runtime?
Activity Selection: Optimal Substructure Let k be the minimum activity in A (i.e., the one with the earliest finish time). Then A - {k} is an optimal solution to S = {i S: s i f k } In words: once activity #1 is selected, the problem reduces to finding an optimal solution for activity-selection over activities in S compatible with #1 Proof: if we could find optimal solution B to S with B > A - {k}, Then B U {k} is compatible And B U {k} > A