poscat seminar 11

50
POSCAT Seminar 10 : Graph 3 yougatup @ POSCAT 1

Upload: hyungyu-shin

Post on 12-Jan-2017

122 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Poscat seminar 11

POSCAT Seminar 10 :Graph 3

yougatup @ POSCAT

1

Page 2: Poscat seminar 11

Topic

Topic today

− Minimum Spanning Tree

• Cut Property

• Prim Algorithm

• Kruskal Algorithm

POSCAT Seminar 1-216 July 2014

yougatup

Page 3: Poscat seminar 11

Spanning Tree

Definition

A tree which “spans” whole vertices

i.e. a tree with n vertices

POSCAT Seminar 1-316 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 4: Poscat seminar 11

Spanning Tree

Definition

A tree which “spans” whole vertices

i.e. a tree with n vertices

POSCAT Seminar 1-416 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 5: Poscat seminar 11

Minimum Spanning Tree

Problem

Find a spanning tree with minimum cost

POSCAT Seminar 1-516 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 6: Poscat seminar 11

Minimum Spanning Tree

Problem

Find a spanning tree with minimum cost

POSCAT Seminar 1-616 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Is it MST ?

Page 7: Poscat seminar 11

Minimum Spanning Tree

Problem

Find a spanning tree with minimum cost

POSCAT Seminar 1-716 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Is it MST ?

Page 8: Poscat seminar 11

Cut Property

Theorem

Suppose edge X are part of a MST of 𝐺 = (𝑉, 𝐸). Pick any subset of

nodes S for which X does not cross between S and 𝑉\S, and let 𝑒

be the lightest edge across this partition. Then 𝑋 ∪ {𝑒} is part of

some MST

POSCAT Seminar 1-816 July 2014

yougatup

Proof ?

Page 9: Poscat seminar 11

Cut Property

Theorem

Suppose X is a set of edges which are part of a MST of 𝐺 = (𝑉, 𝐸).

Pick any subset of nodes S for which X does not cross between S

and 𝑉\S, and let 𝑒 be the lightest edge across this partition. Then

𝑋 ∪ {𝑒} is part of some MST

POSCAT Seminar 1-916 July 2014

yougatup

Assume 𝑒 ∉ 𝑇. Then we can constructa different MST 𝑇′ containing 𝑋 ∪ 𝑒by altering T slightly.

Compare the cost(𝑇′) and cost (𝑇)

Page 10: Poscat seminar 11

Cut Property

Theorem

Suppose X is a set of edges which are part of a MST of 𝐺 = (𝑉, 𝐸).

Pick any subset of nodes S for which X does not cross between S

and 𝑉\S, and let 𝑒 be the lightest edge across this partition. Then

𝑋 ∪ {𝑒} is part of some MST

POSCAT Seminar 1-1016 July 2014

yougatup

by cut property, we can derivebeautiful greedy algorithm !

Page 11: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1116 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 12: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1216 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 13: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1316 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 14: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1416 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 15: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1516 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 16: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1616 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 17: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1716 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

?

Page 18: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1816 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

?

It make a cycle !∴ we can’t select it

Page 19: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-1916 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 20: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-2016 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 21: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-2116 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 22: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-2216 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 23: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-2316 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Done ! we have no remaining edges

Page 24: Poscat seminar 11

Kruskal Algorithm

Approach

For each iteration, choose minimum edge which doesn’t make

a cycle. Then it will make a MST by cut property.

POSCAT Seminar 1-2416 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Done ! we have no remaining edges

Page 25: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ?

POSCAT Seminar 1-2516 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 26: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-2616 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 27: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

If a edge connects two vertices with different group, it will never

make a cycle.

POSCAT Seminar 1-2716 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 28: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

If not, it will make a cycle !

POSCAT Seminar 1-2816 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

Page 29: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-2916 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

1 2 3 4

5 6 7 8

Page 30: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3016 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

1 2 3 4

5 6 7 8

Page 31: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3116 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

1 23 4

5 6 7 8

Page 32: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3216 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

1 23 4

5 6 7 8

Page 33: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3316 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

3 4

5 6 7 8

Page 34: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3416 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

3 4

5 6 7 8

Page 35: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3516 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

3 4

5

6

7 8

Page 36: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3616 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

3 4

5

6

7 8

Page 37: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3716 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34

5

6

7 8

Page 38: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3816 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34

5

6

7 8

Page 39: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-3916 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34

5

6

7 8

Page 40: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-4016 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34

5

6

7 8

No !!

Page 41: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-4116 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34

5

6

7 8

Page 42: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-4216 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34 5

6 7

8

Page 43: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-4316 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34 5

6 7

8

Page 44: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-4416 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34 5

6 7

8

Page 45: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-4516 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34 5

6 7

8

Page 46: Poscat seminar 11

Kruskal Algorithm

Question

How can we determine whether adding a edge makes a cycle or

not ? by using Disjoint Set !

POSCAT Seminar 1-4616 July 2014

yougatup

1 2

3

4

6

5

7

8

3

1 1

2 3

1 2

9

3

12

34 5

6 7

8

Page 47: Poscat seminar 11

Kruskal Algorithm

Implementation

− We have to sort the set of edges via their costs with STL

− Union & Find

Quite simple !

POSCAT Seminar 1-4716 July 2014

yougatup

Page 48: Poscat seminar 11

Kruskal Algorithm

Implementation

− We have to sort the set of edges via their costs with STL

− Union & Find

Quite simple !

Analysis ?

POSCAT Seminar 1-4816 July 2014

yougatup

Page 49: Poscat seminar 11

Kruskal Algorithm

Implementation

− We have to sort the set of edges via their costs with STL

− Union & Find

Quite simple !

Analysis ? Sorting takes O(𝐸 log𝐸)

Each iteration takes almost O(1)

because we use Union & Find

POSCAT Seminar 1-4916 July 2014

yougatup

Page 50: Poscat seminar 11

Kruskal Algorithm

Implementation

− We have to sort the set of edges via their costs with STL

− Union & Find

Quite simple !

Analysis ? Sorting takes O(𝐸 log𝐸)

Each iteration takes almost O(1)

because we use Union & Find O(𝑬 𝐥𝐨𝐠𝑬 )

POSCAT Seminar 1-5016 July 2014

yougatup