comp3600/6466 –algorithms · minimum spanning tree (mst) •spanning tree of a graph g(v, e) is a...

18
COMP3600/6466 – Algorithms Greedy [Lev] 9.1, 9.2, [CLRS] 16.2, 23.2 Hanna Kurniawati https://cs.anu.edu.au/courses/comp3600/

Upload: others

Post on 08-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP3600/6466 –Algorithms · 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

COMP3600/6466 – Algorithms Greedy

[Lev] 9.1, 9.2, [CLRS] 16.2, 23.2

Hanna Kurniawati

https://cs.anu.edu.au/courses/comp3600/

Page 2: COMP3600/6466 –Algorithms · 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

Today• What is it?• Requirements• Example: Minimum Spanning Tree• Kruskal Algorithm• Prim Algorithm

Page 3: COMP3600/6466 –Algorithms · 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

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

Page 4: COMP3600/6466 –Algorithms · 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

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.

Page 5: COMP3600/6466 –Algorithms · 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

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

Page 6: COMP3600/6466 –Algorithms · 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

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

Page 7: COMP3600/6466 –Algorithms · 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
Page 8: COMP3600/6466 –Algorithms · 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

TodayüWhat is it?üRequirements• Example: Minimum Spanning Tree• Kruskal Algorithm• Prim Algorithm

Page 9: COMP3600/6466 –Algorithms · 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

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., ∑'∈)* 𝑤(𝑒)

Page 10: COMP3600/6466 –Algorithms · 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

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

Page 11: COMP3600/6466 –Algorithms · 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

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

Page 12: COMP3600/6466 –Algorithms · 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

Kruskal Algorithm• Start with |V| disjoint tree• Add the edges one by one from the edge

with the smallest weight

Page 13: COMP3600/6466 –Algorithms · 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

Example [CLRS p. 632-633]

Page 14: COMP3600/6466 –Algorithms · 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

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

Page 15: COMP3600/6466 –Algorithms · 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

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

Page 16: COMP3600/6466 –Algorithms · 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

Prim Algorithm• Grow a single tree, starting from an

arbitrary vertex• Expand the tree using the edge with the

smallest weight

Page 17: COMP3600/6466 –Algorithms · 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

Example [CLRS p.635]

Page 18: COMP3600/6466 –Algorithms · 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

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