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

Post on 05-Oct-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

POSCAT Seminar 10 :Graph 3

yougatup @ POSCAT

1

Topic

Topic today

− Minimum Spanning Tree

• Cut Property

• Prim Algorithm

• Kruskal Algorithm

POSCAT Seminar 1-216 July 2014

yougatup

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

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

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

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 ?

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 ?

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 ?

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 (𝑇)

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 !

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

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

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

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

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

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

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

?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 !!

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

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

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

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

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

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

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

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

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

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

top related