E V E V E V E V A V A V E V E V E V E V E A V A E V A E A ...
COMP3600/6466 –Algorithms · Minimum Spanning Tree (MST) •Spanning tree of a graph G(V, E) is a...
Transcript of COMP3600/6466 –Algorithms · Minimum Spanning Tree (MST) •Spanning tree of a graph G(V, E) is a...
COMP3600/6466 – Algorithms Greedy
[Lev] 9.1, 9.2, [CLRS] 16.2, 23.2
Hanna Kurniawati
https://cs.anu.edu.au/courses/comp3600/
Today• What is it?• Requirements• Example: Minimum Spanning Tree• Kruskal Algorithm• Prim Algorithm
What is Greedy Algorithms?• A greedy algorithm always makes the
choice that looks best at the moment, and then solve the sub-problem that remains• The idea is locally optimal choice lead to a
globally optimal solution• There are many problems where the above
idea is correct. However of course, not always • Note: The choices do not depend on any
future choices
Requirements• Optimal sub-structure (same as that in
dynamic programming)• Optimal solution to the problem is formed by
optimal solutions to sub-problems• Greedy-choice property• Globally optimal solution can be formed from
locally optimal solutions.
Greedy Algorithm vs Dynamic Programming• Both requires optimal sub-structure properties
but the greedy-choice property would determine whether we need greedy or dynamic programming• Example: 0-1 knapsack vs fractional knapsack• Knapsack problem: There’s n items to take. Each
item-i is worth $v and weight w kg. If I can only take a total of W kg, which items should I take to get the highest possible value V? • 0-1: Can only take or not take an item• Fractional: Can take a fraction of an item
Greedy Algorithm vs Dynamic Programming• Knapsack example:• Suppose we can take a total of 50 kg• Suppose there’s 3 items:• Item-1: 10kg, $60• Item-2: 20kg, $100• Item-3: 30kg, $120
TodayüWhat is it?üRequirements• Example: Minimum Spanning Tree• Kruskal Algorithm• Prim Algorithm
Minimum Spanning Tree (MST)• Spanning tree of a graph G(V, E) is a
subgraph of G’(V’, E’), where V’ = V and E’ is a subset of E, such that G’ forms a tree• A tree is a connected, acyclic graph• Given an undirected weighted graph G(V, E, 𝑤) where 𝑤:𝐸 → 𝑅 is the weight, a minimum spanning tree of G (denoted as MST(G)) is a spanning tree G’(V’, E’) of G with minimum weight, i.e., ∑'∈)* 𝑤(𝑒)
Optimal sub-structure of MST• Suppose:• G(V, E) is an undirected graph AND• T is an MST of G, which contains the edge e AND• G’(V’, E’) is a subgraph of G where E’ = E \ e• If T’ is an MST of G’, then T’ U {e} is an MST
of G
Greedy-choice property of MST• Given an undirected weighted graph G(V,
E, 𝑤)• For any cut (S, V \ S) of G, any least-weight
crossing edge e = {u, v} with u ∈ S and v ∉ S is in some MST of G
Kruskal Algorithm• Start with |V| disjoint tree• Add the edges one by one from the edge
with the smallest weight
Example [CLRS p. 632-633]
632 Chapter 23 Minimum Spanning Trees
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
27 6
(a) (b)
(c) (d)
(e)
(g)
(f)
(h)
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
23.2 The algorithms of Kruskal and Prim 633
(i) (j)
(k) (l)
(n)(m)
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 6
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 622
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 6
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 6
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
2
2
Prim Algorithm• Grow a single tree, starting from an
arbitrary vertex• Expand the tree using the edge with the
smallest weight
Example [CLRS p.635]
23.2 The algorithms of Kruskal and Prim 635
(a) (b)
(c) (d)
(e) (f)
(g) (h)
(i)
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
27 6
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
27 6
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
27 6
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62
b
a
h
c
g
i
d
f
e
4
8
11
8 79
10144
21
7 62