GreedyAlgorithms
GreedyAlgorithms
Big problem
sub- sub-
problem problem
Big problem
• Goal:
• Schedule maximum-size subset of mutually compatible
activities
Activity Selection Problem
• Assumption: The activities are sorted by their finish
times
• DP??
Activity Selection Problem
• Let’s make a greedy choice
Big problem
sub-
problem
sub-
sub-
proble
m
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
• 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,
• Goal: Find out the shortest path weight from the source
to a given node / other nodes.
Dijkstra’s Algorithm
• A path weight of a, , 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
8 tree is the
7 sum of the weights
on the edges. B C D
It has cost
4 9 67
2
11 4
A I 14 E
7 6
8 10
A tree is a
1 2 connected
H G F
graph 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
4 9 37
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
8 7 cycle
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