Download presentation

Presentation is loading. Please wait.

Published byBrady Silversmith Modified about 1 year ago

1
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution. Applicable to a wide range of problems (can often come close to the optimal even if it is not guaranteed). Notable greedy algorithms in this course: Minimum spanning tree algorithms Dijkstra’s single source shortest path algorithm Optimization often goes through a sequence of steps.

2
Activity Selection Set S = {1, 2, …, n } of activities. time compatible (no overlap) Problem: Find the largest set A of compatible events. s s f f ii j j incompatible events (overlap) i j k

3
Overlapping Subproblems Recursively try all possible compatible subsets. 1 A ? S S'S' S' = {i S | S f } i 1 S–{1} yes no 2 A ? yesno 2 A ? S–{1,2} S'' yesno S'' S'–{2} S'' = {i S | S f } i 2

4
A Greedy Solution // The input activities are in order by increasing finish time: f f … f 1 2 n // // Otherwise, sort them first. Greedy-Activity-Selector(s, f ) // (n) without the sorting n = length[s]; A = {1} j = 1 // last activity scheduled (current activity) for i = 2 to n do if s f // next activity starts after current one finishes then A = A + {i} j = i return A i j // update the last scheduled activity

5
An Example Step 1 1 Step 2 1 Step Step Step 5 2 incompatible – discard the event 4 compatible – schedule the event 14 6 A = {1, 4, 6} schedule

6
Greedy Choice Claim 1 There exists an optimal schedule A S such that activity 1 is in A. Proof Suppose B S is an optimal schedule. There are two cases: (1) If 1 is in B then let A = B. (2) Otherwise, let k be the first activity in B... k Let A = B {k} + {1}: Since f f, activities in A are compatible. Thus A is also optimal. 1 k... 1

7
Optimal Substructure Claim 2 Let A be an optimal schedule, then A {1} is an optimal schedule for S' = { i in S | s f } i 1 Proof Suppose not true. Then there exists an optimal schedule B for S' with |B| > | A – {1} | = |A| – 1. Then the following solution to S has more activities than A....B:B: A – {1}:... 1 B + {1}: Contradiction.

8
Correctness of Greedy Algorithm Combine Claims 1 and 2 and induct on the number of choices: Theorem Algorithm Greedy-Activity-Selector produces solutions of maximum size for the activity-selection problem. Local optimal (greedy) choice → globally optimal solution In activity-selection

9
Greedy Algorithm vs Dynamic Programming Dynamic programming solves subproblems first, then makes a decision. Greedy algorithm makes decision first, then solve subproblems. (Greedy-choice property gains efficiency.) Both techniques rely on the presence of optimal substructure. The optimal solution contains the optimal solutions to subproblems.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google