poscat seminar 10 : graph 3 · spanning tree definition a tree which “spans” whole vertices...

50
POSCAT Seminar 10 : Graph 3 yougatup @ POSCAT 1

Upload: others

Post on 05-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: POSCAT Seminar 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

POSCAT Seminar 10 :Graph 3

yougatup @ POSCAT

1

Page 2: POSCAT Seminar 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

Topic

Topic today

− Minimum Spanning Tree

• Cut Property

• Prim Algorithm

• Kruskal Algorithm

POSCAT Seminar 1-216 July 2014

yougatup

Page 3: POSCAT Seminar 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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 10 : Graph 3 · Spanning Tree Definition A tree which “spans” whole vertices i.e. a tree with n vertices POSCAT Seminar 1-3 16 July 2014 yougatup 1 2 3 4 6 5 7

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