GreedyAlgorithms
GreedyAlgorithms
Big problem
sub-problem sub-problem
Big problem
• Goal:
• Schedule maximum-size subset of mutually compatible activities
Activity Selection Problem
• Assumption: The activities are sorted by their finish times
𝑓1 ≤ 𝑓2 ≤ 𝑓3 ≤ … ≤ 𝑓𝑛
• DP??
Activity Selection Problem
• Let’s make a greedy choice
• Choose the activity that leaves the resource available for as many
other activities as possible
• Make the choice that looks best in the current problem, without
considering results from subproblems.
Elements of Greedy Strategy
Big problem
sub-problem
sub-sub-
problem
Knapsack Problems
• 0-1 Knapsack Problem
• Choice at each step
• The choice usually depends on the solutions to subproblems.
• Goal: Find out the shortest path weight from the source to a given
node / other nodes.
Single Source Shortest Path
• Subpaths of shortest paths are shortest paths
• Proof:
• Decompose the shortest path into smaller sub-paths
p = 𝑣1 ∼ 𝑣𝑖 ∼ 𝑣𝑗 ∼ 𝑣𝑘
• The subpaths are 𝑝0𝑖 , 𝑝𝑖𝑗 , 𝑝𝑗𝑘
• Assume there exists 𝑝𝑖𝑗 ′ such that 𝑤(𝑝𝑖𝑗 ′) < 𝑤(𝑝𝑖𝑗 )
• Then replacing 𝑝𝑖𝑗 with 𝑝𝑖𝑗 ′ gives a shorter path
• Cycle
• Shortest path cannot contain cycle
• Just dropping the cycle gives a lower cost path
Dijkstra’s Algorithm
• Given
• A weighted and directed graph, 𝐺 = (𝑉, 𝐸)
• A source, 𝑠
• Non-negative weights on edges, 𝑤 𝑢, 𝑣 ≥ 0
• Goal: Find out the shortest path weight from the source to a given
node / other nodes.
Dijkstra’s Algorithm
• A path weight of a, 𝑝 = < 𝑣1 , 𝑣2 , … , 𝑣𝑘 >, is
• Inductive Hypothesis:
• At the start of each iteration, 𝑣 = 𝛿(𝑠, 𝑣) for all 𝑣 ∈ 𝑆
Dijkstra’s Algorithm
• Correctness of Dijkstra’s Algorithm
• At some iteration, v is extracted from the priority queue
• y first node not in S on the shortest path P to u
• x predecessor of y on P
𝑦. 𝑑 ≥ 𝑣. 𝑑
𝛿 𝑠, 𝑥 + 𝑤 𝑥, 𝑦 ≥ 𝛿 𝑠, 𝑢 + 𝑤(𝑢, 𝑣)
𝑤 𝑃 ≥ 𝑤(𝑠 ∼ 𝑣)
Minimum Spanning Tree
• A spanning tree is a tree that connects all of the vertices.
• The cost of a spanning tree is the sum of the weights on the edges.
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Minimum Spanning Tree
• A spanning tree is a tree that connects all of the vertices.
• The cost of a spanning tree is the sum of the weights on the edges.
8 7
B C D
It has cost 67
4 9
2
11 4
A I 14 E
7 6
8 10
A tree is a
1 2 connected graph
H G F
with no cycles!
Minimum Spanning Tree
• A minimum spanning tree is a tree with minimum cost that connects
all of the vertices.
8 7
B C D
It has cost 37
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
Some Definitions
• An (𝑆, 𝑉 − 𝑆) partition is a cut of 𝑉 of an undirected graph 𝐺 =
(𝑉, 𝐸)
8 7
Root Node B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
44
Prim’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
45
Prim’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
46
Prim’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
47
Prim’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
48
Prim’s Algorithm
Prim’s Algorithm
• Proof of correctness?
• Later
Minimum Spanning Tree
• Greedy Strategy 2 (Kruskal’s Algorithm):
• Start with each node as a separate tree
• Consider the edges in ascending order of their weights
• Include the minimum weight edge between two disjoint trees to connect
them into a single tree
• Discard the edge if it creates a cycle
• Terminate when all the nodes are included
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
52
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
53
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
54
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
55
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
56
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
57
Kruskal’s Algorithm
Causing a cycle
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
58
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
59
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
60
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
61
Kruskal’s Algorithm
8 7
B C D
4 9
2
11 4
A I 14 E
7 6
8 10
1 2
H G F
62
Kruskal’s Algorithm
Kruskal’s Algorithm
Proof of Correctness?
• Later
Cut Property
• Assume that all edge costs are distinct.
• Let S be any subset of nodes that is neither empty nor equal to all of
V, and let edge e =(v, w) be the minimum cost edge with one end in S
and the other in V −S.
• Prim’s Algorithm
• Apply cut property
Reference
• Greedy Algorithms
• CLRS 4th ed. Sections 15.1, 15.2
• Dijkstra’s Algorithm
• CLRS 4th ed. Sections 22 (intro), 22.3