union find and greedy algorithms · – reading: section 5.1 • greedy algorithms – reading:...
TRANSCRIPT
![Page 1: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/1.jpg)
Union‐Find and Greedy Algorithms
CSE 101: Design and Analysis of AlgorithmsLecture 8
![Page 2: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/2.jpg)
CSE 101: Design and analysis of algorithms
• Union‐find– Reading: Section 5.1
• Greedy algorithms– Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3
• Homework 3 is due today, 11:59 PM• Homework 4 will be assigned today
– Due Oct 30, 11:59 PM
CSE 101, Fall 2018 2
![Page 3: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/3.jpg)
Kruskal’s algorithm using a DSDSprocedure kruskal(G,w)Input: undirected connected graph G with edge weights wOutput: a set of edges X that defines a minimum spanning tree of Gfor all v in Vmakeset(v)X = { }Sort the edges in E in increasing order by weightFor all edges (u,v) in E until |X| = |V| ‐ 1if find(u) find(v):Add edge (u,v) to Xunion(u,v)
CSE 101, Fall 2018 3
|V| * makeset
sort(|E|)
2 * |E| * find
(|V| ‐ 1) * union
Based on slides courtesy of Miles Jones
![Page 4: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/4.jpg)
Kruskal’s algorithm, DSDS subroutines• makeset(u)
– Creates a set with one element, u• find(u)
– Finds the set to which u belongs• union(u,v)
– Merges the sets containing u and v
• Kruskal’s algorithm|V| * makeset + 2 * |E| * find + (|V| ‐ 1) * union + sort(|E|)
CSE 101, Fall 2018 4
![Page 5: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/5.jpg)
DSDS, leader version• Keep an array leader(u) indexed by element• In each array position, keep the leader of its set• makeset(u): leader(u) = u, O(1)• find(u): return leader(u), O(1)• union(u,v): For each array position, if it is currently leader(v), then change
it to leader(u). O(|V|)
• Kruskal’s algorithm|V| * makeset + 2 * |E| * find + (|V| ‐ 1) * union + sort(|E|) | | ∗ 1 2 ∗ | | ∗ 1 | | 1 ∗ | | | |
= | |
CSE 101, Fall 2018 5
![Page 6: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/6.jpg)
DSDS, directed trees with ranks versionprocedure union(x,y)rx:=find(x)ry:=find(y)if rx=ry then returnif rank(rx)>rank(ry) then
:else
:if rank(rx)=rank(ry) thenrank(ry):=rank(rx)+1
CSE 101, Fall 2018 6
procedure makeset(x):
rank(x):=0
procedure find(x)while :
return xmakeset O(1)find O(height of tree containing x)union O(find)
![Page 7: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/7.jpg)
Height of tree• Any root node of rank k has at least vertices in its tree
• Proof– Base Case: a root of rank 0 has 1 vertex– Suppose a root of rank k has at least vertices in its tree. Then, a root of rank k+1 can only be made by unioning 2 roots each of rank k. So, a root of rank k+1 must have at least vertices in its tree.
CSE 101, Fall 2018 7
![Page 8: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/8.jpg)
Ancestors of rank k• Any vertex has at most one ancestor of rank k• Proof
– Each vertex has one pointer and ranks strictly increase along paths so each element has at most one ancestor of each rank
CSE 101, Fall 2018 8
![Page 9: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/9.jpg)
Number of vertices of a given rank• If there are n vertices overall, there can be at most vertices of rank k
• Proof– Each rank k vertex has at least 2 vertices in its tree so if there are m rank k vertices then there are m2 vertices overall
• If there are n vertices overall then m so m
CSE 101, Fall 2018 9
![Page 10: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/10.jpg)
Height of tallest tree (i.e., maximum rank)
• The maximum rank is log(n)• Proof (sort of)
– How many vertices of rank log(n) can there be?
anything more is not possible
CSE 101, Fall 2018 10
![Page 11: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/11.jpg)
Height of tallest tree (i.e., maximum rank)
• The maximum rank is log(n)• Proof (sort of)
– How many vertices of rank log(n) can there be?
anything more is not possible
CSE 101, Fall 2018 11
![Page 12: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/12.jpg)
DSDS, directed trees with ranks version• makeset(u)
– Creates a set with one element, u• find(u)
– Finds the set to which u belongs• union(u,v)
– Merges the sets containing u and v
• Kruskal’s algorithm|V| makeset + 2 |E| * find + (|V| ‐ 1) union + sort(|E|)| | 1 2| | log | | 1 log | |log | |
log log loglog
CSE 101, Fall 2018 12
makeset O(1)find O(height of tree containing x)union O(find)
![Page 13: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/13.jpg)
DSDS, directed trees with ranks version• makeset(u)
– Creates a set with one element, u• find(u)
– Finds the set to which u belongs• union(u,v)
– Merges the sets containing u and v
• Kruskal’s algorithm|V| makeset + 2 |E| * find + (|V| ‐ 1) union + sort(|E|)| | 1 2| | log | | 1 log | |log | |
log log loglog
CSE 101, Fall 2018 13
makeset O(1)find O(log(|V|))union O(log(|V|))
![Page 14: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/14.jpg)
DSDS, directed trees with ranks version
• Kruskal’s algorithm
• Can we improve the runtime of find and union?
• Is it worth it (i.e., will it actually improve the runtime of Kruskal’s algorithm)?
CSE 101, Fall 2018 14
makeset O(1)find O(log(|V|))union O(log(|V|))
![Page 15: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/15.jpg)
Path compression• We can improve the runtime of find and union by making the height of the trees shorter
• How do we do that?– Every time we call find, we do some housekeeping by moving up every vertex
CSE 101, Fall 2018 15
![Page 16: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/16.jpg)
Path compression• New find subroutine
procedure find(x)if then
return
CSE 101, Fall 2018 16
![Page 17: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/17.jpg)
Path compression• New find subroutine
procedure find(x)if then
return
CSE 101, Fall 2018 17
![Page 18: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/18.jpg)
find, path compression• Whenever you call find on a vertex v, it points v and all of its ancestors to the root
CSE 101, Fall 2018 18
![Page 19: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/19.jpg)
find, path compression and ranks• The ranks do not necessarily represent the height of the graph anymore. Will this cause problems?
• The following properties still hold1. For any x, rank(x)<rank(2. Any vertex of rank k has at least vertices in its
subtree3. If there are n vertices overall, there can be at most
vertices of rank k
CSE 101, Fall 2018 19
![Page 20: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/20.jpg)
find, path compression and ranks• The ranks do not necessarily represent the height of the graph anymore. Will this cause problems?
• The following properties still hold1. For any x, rank(x)<rank(2. Any vertex of rank k has at least vertices in its
subtree3. If there are n vertices overall, there can be at most
vertices of rank k
CSE 101, Fall 2018 20
![Page 21: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/21.jpg)
Height of tree with path compression
• How much does this help?• Recall: the height without path compression is log(n)
• With path compression, the cost of calling find m times has amortized cost of O(m log*(n))
CSE 101, Fall 2018 21
![Page 22: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/22.jpg)
log*(n)• log*(n) is the number of logs required to reduce n to a value less than 1
• For example log*(10,000)=4 becauselog 10,000 13.3loglog 10,000 log 13.3 3.7logloglog 10,000 log 3.7 1.9loglogloglog 10,000 log 1.9 0.9
• It turns out that n must be greater than for log*(n) to be greater than 5
CSE 101, Fall 2018 22
![Page 23: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/23.jpg)
log*(n)• log*(n) is the number of logs required to reduce n to a value less than 1
• For example log*(10,000)=4 becauselog 10,000 13.3loglog 10,000 log 13.3 3.7logloglog 10,000 log 3.7 1.9loglogloglog 10,000 log 1.9 0.9
• It turns out that n must be greater than for log*(n) to be greater than 5
CSE 101, Fall 2018 23
![Page 24: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/24.jpg)
DSDS, directed trees with ranks version• makeset(u)
– Creates a set with one element, u• find(u)
– Finds the set to which u belongs• union(u,v)
– Merges the sets containing u and v
• Kruskal’s algorithm|V| makeset + 2 |E| * find + (|V| ‐ 1) union + sort(|E|)| | 1 | | log∗ | | log∗ | |log | |
log∗ log• If we did not have to sort, then we could perform Kruskal’s algorithm in “linear*”
time!
CSE 101, Fall 2018 24
makeset O(1)find O(log*(|V|))union O(log*(|V|))
makeset O(1)find O(height of tree containing x)union O(find)
with new find subroutine
![Page 25: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/25.jpg)
Prim’s algorithm• Recall Kruskal’s algorithm
– Build a tree by adding the next lightest edge to a forest
• Prim’s algoritm– Build a tree by adding the next lightest edge connecting the tree to the rest of the vertices
CSE 101, Fall 2018 25
![Page 26: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/26.jpg)
Example: Prim’s algorithm
A
B
C
D E F
G
1
2
22
2
2
3 4
4
41
CSE 101, Fall 2018 26
![Page 27: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/27.jpg)
Example: Prim’s algorithm
A
B
C
D E F
G
1
2
22
2
2
3 4
4
41
CSE 101, Fall 2018 27
![Page 28: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/28.jpg)
Dijkstra’s algorithm vs Prim’s algorithm• Dijkstra’s algorithm
– Start off with sets X and F– Move s from F to X– Find the minimum of dist(u)+ℓ , among all edges such that u is in X and v
is in F– Move v from F to X and repeat
• Prim’s algorithm– Start off with sets X and F– move s from F to X– Find the minimum of , among all edges such that u is in X and v is in F– Move v from F to X and repeat
CSE 101, Fall 2018 28
![Page 29: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/29.jpg)
Prim’s algorithm• On each iteration, the subtree grows by one edge
CSE 101, Fall 2018 29
X F
![Page 30: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/30.jpg)
Prim’s algorithm• On each iteration, the subtree grows by one edge
CSE 101, Fall 2018 30
![Page 31: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/31.jpg)
Prim’s algorithm• Prim’s algorithm is just like Dijkstra’s algorithm, where we are using the value cost(v) instead of dist(v)
• We can use the same algorithm changing distto cost and we can use the same data structures
CSE 101, Fall 2018 31
![Page 32: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/32.jpg)
Prim’s algorithmprocedure prim(G,w)Input: A connected undirected graph G=(V,E) with edge weights w(e).Output: A minimum spanning tree defined by the array prev()
for all ∈ :cost(u)=infinityprev(u)=nilpick any initial vertex ucost(u)=0H=makequeue(V)while H is not emptyv=deletemin(H)for each , ∈ :if cost(z)>w(v,z) thencost(z)=w(v,z)prev(z)=v
CSE 101, Fall 2018 32
Same runtime as Dijkstra's algorithm
![Page 33: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/33.jpg)
GREEDY ALGORITHMS
CSE 101, Fall 2018 33
![Page 34: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/34.jpg)
Optimization problems• In general, when you try to solve a problem, you are trying to find the best solution from among a large space of possibilities
• The format for an optimization problem is– Instance: what does the input look like?– Solution format: what does an output look like?– Constraints: what properties must a solution have?– Objective function: what makes a solution better or worse?
CSE 101, Fall 2018 34
![Page 35: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/35.jpg)
Optimization examples• Shortest path
– Instance: directed graph G with positive edge weights vertices s,t
– Solution format: list of edges – Constraint: must form a path from s to t– Objective: minimize
CSE 101, Fall 2018 35
![Page 36: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/36.jpg)
Optimization examples• Max bandwidth path
– Instance: directed graph G with positive edge weights vertices s,t
– Solution format: list of edges – Constraint: must form a path p from s to t– Objective: maximize
∈
CSE 101, Fall 2018 36
![Page 37: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/37.jpg)
Global search vs local search• In general, there will be exponentially many possible solutions• The obvious algorithm is to try them all and take the best, but this is
usually prohibitively slow. Sometimes, this is unavoidable (unless P=NP).
• A good way to make progress towards an efficient algorithm is to break the massive global search for a solution into a series of simpler local searches for part of the solution (e.g., Which edge do we take first? Then second? …)
• If you cannot tell which local choice is best, you may still have to use exhaustive search to try out all combinations of local decisions and find the optimal one
CSE 101, Fall 2018 37
![Page 38: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/38.jpg)
The greedy method• In some cases (certainly not all) there is sufficient structure that allows you to reach the correct solution by just picking the straightforward “best” decision at each stage
• This is called the greedy method– It does not always work– Just as in life, acting in one’s immediate best interest is not always the best longer‐term strategy
CSE 101, Fall 2018 38
![Page 39: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/39.jpg)
The greedy method• In some cases (certainly not all) there is sufficient structure that allows you to reach the correct solution by just picking the straightforward “best” decision at each stage
• This is called the greedy method– It does not always work– Just as in life, acting in one’s immediate best interest is not always the best longer‐term strategy
CSE 101, Fall 2018 39
![Page 40: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/40.jpg)
Other uses of local decisions• Many of the other techniques we’ll study are also based on breaking up global search into local decisions– Backtracking– Dynamic programming– Hill‐climbing– (Stochastic search heuristics)
CSE 101, Fall 2018 40
![Page 41: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/41.jpg)
Cookies• Suppose you are the cookie monster and you have a 6x6 sheet of freshly baked cookies in front of you. The cookies are all chocolate chip cookies but they may have different sizes.
• If you are only allowed to take six cookies, how can you maximize your total cookie intake?
• Devise an algorithm to do this
CSE 101, Fall 2018 41
![Page 42: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/42.jpg)
Problem specification• Cookies
– Instance: sizes of cookies on 6x6 sheet– Solution format: set of 6 cookies– Constraint: none– Objective: maximize sum of sizes
CSE 101, Fall 2018 42
![Page 43: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/43.jpg)
Cookies1. What is an algorithm you could use to select the best option?
(The best option means the sum of all the cookie sizes is the highest possible)
CSE 101, Fall 2018 43
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 44: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/44.jpg)
Cookies
CSE 101, Fall 2018 44
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
1. What is an algorithm you could use to select the best option?
(The best option means the sum of all the cookie sizes is the highest possible)
99+97+94+92+88+85=555
![Page 45: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/45.jpg)
Problem specification• Cookies, one per row
– Instance: sizes of cookies on 6x6 sheet– Solution format: set of 6 cookies– Constraint: one cookie per row– Objective: maximize sum of sizes
CSE 101, Fall 2018 45
![Page 46: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/46.jpg)
Cookies, one per row
CSE 101, Fall 2018 46
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
2. What is an algorithm you could use to select the best option if you can only select one cookie from each row?
![Page 47: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/47.jpg)
Cookies, one per row
CSE 101, Fall 2018 47
2. What is an algorithm you could use to select the best option if you can only select one cookie from each row?
76+80+99+68+85+73=481
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 48: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/48.jpg)
Problem specification• Cookies, cannot share same row or column
– Instance: sizes of cookies on 6x6 sheet– Solution format: set of 6 cookies– Constraint: no two cookies share the same row or column
– Objective: maximize sum of sizes
CSE 101, Fall 2018 48
![Page 49: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/49.jpg)
Cookies, cannot share same row or column
CSE 101, Fall 2018 49
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
3. What is an algorithm you could use to select the best option if you can’t select 2 cookies from the same row or column?
![Page 50: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/50.jpg)
Cookies, cannot share same row or column
CSE 101, Fall 2018 50
3. What is an algorithm you could use to select the best option if you can’t select 2 cookies from the same row or column?
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 51: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/51.jpg)
Cookies, cannot share same row or column
CSE 101, Fall 2018 51
3. What is an algorithm you could use to select the best option if you can’t select 2 cookies from the same row or column?
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 52: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/52.jpg)
Cookies, cannot share same row or column
CSE 101, Fall 2018 52
3. What is an algorithm you could use to select the best option if you can’t select 2 cookies from the same row or column?
99+81+74+60+50+40=404
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 53: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/53.jpg)
Cookies, cannot share same row or column
CSE 101, Fall 2018 53
3. What is an algorithm you could use to select the best option if you can’t select 2 cookies from the same row or column?
99+81+74+60+50+40=404
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 54: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/54.jpg)
Cookies, cannot share same row or column
CSE 101, Fall 2018 54
3. What is an algorithm you could use to select the best option if you can’t select 2 cookies from the same row or column?
99+81+74+60+50+40=40499+81+72+69+47+46=414
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 55: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/55.jpg)
Cookies, cannot share same row or column
CSE 101, Fall 2018 55
3. What is an algorithm you could use to select the best option if you can’t select 2 cookies from the same row or column?
99+81+74+60+50+40=40499+81+72+69+47+46=41492+78+75+73+72+68=458
56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
![Page 56: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/56.jpg)
The greedy method• As you have seen, the greedy method does not always
work. Because of this, in order to use the greedy method, we must prove the correctness of the algorithm.– Or else, we must present a counterexample to show that a
particular greedy method will not work• Warning: Furthermore, for a single problem, there may be
more than one potential greedy strategy (i.e., more than one way to choose the “best” possible choice at each step). The problem may be solved in one way but not the other.
CSE 101, Fall 2018 56
![Page 57: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/57.jpg)
The greedy method• As you have seen, the greedy method does not always
work. Because of this, in order to use the greedy method, we must prove the correctness of the algorithm.– Or else, we must present a counterexample to show that a
particular greedy method will not work• Warning: Furthermore, for a single problem, there may be
more than one potential greedy strategy (i.e., more than one way to choose the “best” possible choice at each step). The problem may be solved in one way but not the other.
CSE 101, Fall 2018 57
![Page 58: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/58.jpg)
Immediate benefit vs opportunity costs
• Immediate benefit: how does the choice we are making now contribute to the objective function?
• Opportunity costs: How does the choice we are making now restrict future choices?
• The greedy method (usually) takes the best immediate benefit and ignore opportunity costs
• The greedy method is optimal: best immediate benefits outweigh opportunity costs
CSE 101, Fall 2018 58
![Page 59: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/59.jpg)
Cookies, one per row56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
9999 Immediate benefit
82, 97, 94, 88, 92: Opportunity costs
(Since we can have at most one of these:97)
Immediate benefit > Opportunity cost
CSE 101, Fall 2018 59
9999
![Page 60: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/60.jpg)
Cookies, cannot share same row or column56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
9999
9999 Immediate benefit
Opportunity costs
(Lose out on one in row, one in column: 97+85)
Immediate benefit < Opportunity cost
CSE 101, Fall 2018 60
![Page 61: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/61.jpg)
Cookies, cannot share same row or column56 76 69 60 75 51
61 77 74 72 80 58
82 97 94 88 99 92
47 68 59 52 65 40
78 81 79 71 85 62
50 67 73 57 70 46
9999
9999 Immediate benefit
Opportunity costs
(Lose out on one in row, one in column: 97+85)
Immediate benefit < Opportunity cost
CSE 101, Fall 2018 61
![Page 62: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/62.jpg)
Event scheduling• Suppose you are running a cookie conference and you have a collection of events (or talks) that each have a start time and an end time
• Problem: you only have one conference room!• Your goal is to schedule the most events possible that day such that no two events overlap
CSE 101, Fall 2018 62
![Page 63: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/63.jpg)
Problem specification• Event scheduling
– Instance: list of events E1, …, En; Ei = (si, fi)– Solution format: subset of E1, …, En– Constraint: no two events overlap– Objective: maximize size of subset
CSE 101, Fall 2018 63
![Page 64: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/64.jpg)
Event scheduling• Your goal is to schedule the most events possible that day
such that no two events overlap• Brute Force: Say that T is the set of events and |T|=n• Check all possibilities
– How would we do that?• Go through all subsets of T. Check if it is a valid schedule
(i.e., no conflicts) and count the number of events. • Take the maximum out of all valid schedules• How many subsets of T are there?
CSE 101, Fall 2018 64
![Page 65: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/65.jpg)
Event scheduling• Your goal is to schedule the most events possible that day
such that no two events overlap• Brute Force: Say that T is the set of events and |T|=n• Check all possibilities
– How would we do that?• Go through all subsets of T. Check if it is a valid schedule
(i.e., no conflicts) and count the number of events. • Take the maximum out of all valid schedules• How many subsets of T are there?
CSE 101, Fall 2018 65
![Page 66: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/66.jpg)
Event scheduling• Your goal is to schedule the most events possible that day
such that no two events overlap• Brute Force: Say that T is the set of events and |T|=n• Check all possibilities
– How would we do that?• Go through all subsets of T. Check if it is a valid schedule
(i.e., no conflicts) and count the number of events. • Take the maximum out of all valid schedules• How many subsets of T are there? 2n
CSE 101, Fall 2018 66
![Page 67: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/67.jpg)
Event scheduling1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 67
![Page 68: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/68.jpg)
Event scheduling1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 68
![Page 69: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/69.jpg)
• Your goal is to schedule the most events possible that day such that no two events overlap
• Exponential is too slow. Let’s try some greedy strategies:– Shortest duration– Earliest start time– Fewest conflicts (take away most conflicts)– Earliest end time
Event scheduling
CSE 101, Fall 2018 69
![Page 70: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/70.jpg)
Shortest duration1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 70
![Page 71: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/71.jpg)
Shortest duration1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 71
![Page 72: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/72.jpg)
Earliest start time1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 72
![Page 73: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/73.jpg)
Earliest start time1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 73
![Page 74: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/74.jpg)
Fewest conflicts1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 74
![Page 75: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/75.jpg)
Counterexample for fewest conflicts1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 75
![Page 76: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/76.jpg)
Earliest end time1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 76
![Page 77: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/77.jpg)
• Your goal is to schedule the most events possible that day such that no two events overlap
• Exponential is too slow. Let’s try some greedy strategies:– Shortest duration– Earliest start time– Fewest conflicts– Earliest end time (we cannot find a counterexample!)
Event scheduling
CSE 101, Fall 2018 77
![Page 78: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/78.jpg)
Event scheduling1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
CSE 101, Fall 2018 78
![Page 79: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/79.jpg)
Problem specification• Design an algorithm that uses the greedy choice of picking the next available event with the earliest end time– Instance: events each with a start and end time– Solution format: list of events– Constraints: events cannot overlap– Objective: Maximize the number of events
• Get into groups and come up with an efficient implementation
CSE 101, Fall 2018 79
![Page 80: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/80.jpg)
Initialize a queue Sort the intervals by end timePut the first event in Set For 2… :If :enqueue( ,
Return
Event scheduling implementation
CSE 101, Fall 2018 80
![Page 81: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/81.jpg)
Proving optimization algorithms correct
• Remember what we need to show– Let I be any instance of our problem, GS the greedy algorithm’s solution, and OS any other solution
– If minimization: Cost (OS) ≥ Cost(GS)– If maximization: Value(GS) ≥ Value (OS)
CSE 101, Fall 2018 81
![Page 82: Union Find and Greedy Algorithms · – Reading: Section 5.1 • Greedy algorithms – Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3 • Homework 3 is due today, 11:59](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f424440a4b96b2d14718174/html5/thumbnails/82.jpg)
Next lecture• Greedy algorithms
– Reading: Kleinberg and Tardos, sections 4.1, 4.2, and 4.3
CSE 101, Fall 2018 82