Presentation is loading. Please wait.

Presentation is loading. Please wait.

Provinci summer training 2010 June 17: Recursion and recursive decent parser June 24: Greedy algorithms and stable marriage July 1: Holiday July 8: Math.

Similar presentations


Presentation on theme: "Provinci summer training 2010 June 17: Recursion and recursive decent parser June 24: Greedy algorithms and stable marriage July 1: Holiday July 8: Math."— Presentation transcript:

1 Provinci summer training 2010 June 17: Recursion and recursive decent parser June 24: Greedy algorithms and stable marriage July 1: Holiday July 8: Math and number theory July 15: Coordinate geometry July 22: Graph, BFS, DFS, topological sort July 29: Graph, MST, bipartite matching Aug 5: Individual contest 1 Aug 12: Individual contest 2 Aug 19: Dynamic programming Aug 26: Team Formation Test

2 What is recursion? 1. Do something 2. Repeat Any algorithm that have the above format is using "recursion". Properties: Easy to design and write Tend to be slow (because trying all possibilities)

3 What is a greedy algorithm? 1. Make a choice that looks good (greedy choice) 2. Repeat if the problem is not yet solved

4 Examples. Event scheduling Given a set S of n events, each event i has a starting a_i and ending time b_i, where a_i and b_i are integers. Find a largest subset S’ of events such that for any two events i and j, [a_i, b_i] and [a_j, b_j] are non-overlapping. 1 2 3 4 5 6 7

5 Examples. Event scheduling Idea 1. Pick the shortest jobs whenever possible. 1 2 3 4 5 6 7 Idea 2. First-come-first-serve 1 2 3 4 5 6 7

6 Examples. Event scheduling Idea 3. Pick the job that ends first. 1 2 3 4 5 6 7 Proof that it is optimal. Let e1, e2, …, er be events picked by our algorithm Greedy. Let e1’, e2’, …, es’ be events picked by the optimal. Note that the ending time of e1 <= ending time of e1’. Therefore, after picking e1, Greedy can choose both e2 and e2’. Greedy picks e2, so ending time of e2 <= ending time e2’. Assume s > r, Greedy should pick more than r jobs. Contradiction.

7 Properties of greedy algorithms 1. Make a choice that looks good (greedy choice) 2. Repeat if the problem is not yet solved Easy to design and write Tend to be fast (because make only 1 greedy choice) Finding the correct greedy choice and proving the correctness is usually hard.

8 Examples. Event scheduling 2 Given a set S of n events, each event i has a starting a_i and ending time b_i, where a_i and b_i are integers. Divide S into minimum number of groups G1, G2, … of events such that for any two events i and j in the same group, [a_i, b_i] and [a_j, b_j] are non-overlapping. 1 2 3 4 5 6 7

9 Examples. Event scheduling 2 Idea 1. First-come-first-serve 1 2 3 4 5 6 7 Proof that it is optimal. Let r be the number of groups we use eventually. Let E be the first events put to the r-th group. Notice that we have found r jobs that are overlapping. Therefore, minimum number of groups >= r. Since we use r groups, we are optimal.

10 Properties of greedy algorithms 1. Make a choice that looks good (greedy choice) 2. Repeat if the problem is not yet solved Easy to design and write Tend to be fast (because trying all possibilities) Finding the correct greedy choice and proving the correctness is usually hard.

11 Examples. Event scheduling 3 Given a set S of n events, each event I with a size s_i and a weight w_i. Jobs can be processed starting from time 0. The weighted completion time of an event = (completion time) (w_i). w = 1 0 1 2 3 4 5 6 7 w = 23 3w = 1 Find an order that minimize the total weighted completion time.

12 Examples. Event scheduling 3 Idea 1. Shortest job first w = 4 0 1 2 3 4 5 6 7 1 w = 41 Idea 2 Highest weight first w = 2 0 1 2 3 4 5 6 7 1 w = 21

13 Examples. Event scheduling 3 Idea 3. Highest density first, where density = weight / size w = 4 0 1 2 3 4 5 6 7 1 w = 41 w = 2 0 1 2 3 4 5 6 7 1 w = 21 Proof that it is optimal. Assume that in the optimal schedule, it processes a job i with smaller density first then an other job j with larger density. Consider the change in total weighted completion time if we process j before i. Simple calculation will show that the total weighted completion time decreases, which leads to a contradiction.

14 Exercises 120, 410, 714, 10020, 10026, 10672, 10700, 10821, 10954, 10982, 11039, 11532. 240, 10954, 283, 444, 644.


Download ppt "Provinci summer training 2010 June 17: Recursion and recursive decent parser June 24: Greedy algorithms and stable marriage July 1: Holiday July 8: Math."

Similar presentations


Ads by Google