Minimum Spanning Trees Chapter 23 1 CPTR 430 Algorithms Minimum - - PowerPoint PPT Presentation

▶
minimum spanning trees
SMART_READER_LITE
LIVE PREVIEW

Minimum Spanning Trees Chapter 23 1 CPTR 430 Algorithms Minimum - - PowerPoint PPT Presentation

Minimum Spanning Trees Chapter 23 1 CPTR 430 Algorithms Minimum Spanning Trees Motivation Electonic circuitry design Make the pins of multiple components electrically equivalent Wire them together: use n 1 wires to


slide-1
SLIDE 1

Minimum Spanning Trees

Chapter 23

CPTR 430 Algorithms Minimum Spanning Trees

1

slide-2
SLIDE 2

Motivation

■ Electonic circuitry design ■ Make the pins of multiple components electrically equivalent ■ “Wire” them together: use n

  • 1 wires to connect n pins, each wire

connecting two pins

■ Use the shortest amount of wire as possible

CPTR 430 Algorithms Minimum Spanning Trees

2

slide-3
SLIDE 3

Model of Solution

■ Use an undirected, connected graph G

  • ✁

V

✂

E

✄

, where

❚ V is the set of pins ❚ E is the set of possible interconnections between pairs of pins ❚ For each edge

✁

u

✂

v

✄ ☎

E, w

✁

u

✂

v

✄

is the cost (length of wire) needed to connect pin u to pin v

■ Task: find an acyclic subset T

✆

E that connects all the vertices such

that

w

✁

T

✄
  • ∑
✝

u

✞

v

✟✡✠

T

w

✁

u

✂

v

✄

is minimized

CPTR 430 Algorithms Minimum Spanning Trees

3

slide-4
SLIDE 4

Minimum Spanning Tree

■ T is acyclic and T connects all vertices

  • T is a tree that spans G

■ The problem of finding T is called the minimum spanning tree problem ❚ “Minimum” means minimum weight; all spanning trees have the same

number of edges:

✁

V

✁
  • 1

1 2 3 4 5 6 8 7 1 2 4 14 8 4 11 7 10 9 2 6 8 7

CPTR 430 Algorithms Minimum Spanning Trees

4

slide-5
SLIDE 5

MSTs are Not Unique

w(T) = w(T ) = 37 37 1 2 3 4 5 6 8 7 1 2 4 14 8 4 11 7 10 9 2 6 8 7 1 2 3 4 5 6 8 7 1 2 4 14 8 4 11 7 10 9 2 6 8 7

CPTR 430 Algorithms Minimum Spanning Trees

5

slide-6
SLIDE 6

Popular MST Algorithms

■ Kruskal’s algorithm ■ Prim’s algorithm ■ Both are greedy algorithms ■ Both commonly run in O

✁

E lgV

✄

time

CPTR 430 Algorithms Minimum Spanning Trees

6

slide-7
SLIDE 7

Generic Greedy Algorithm

■ Both Kruskal’s and Prim’s algorithms are variations of this basic idea ■ Loop invariant: Prior to each iteration, A is a subset of some minimum

spanning tree

■ Each step determines an edge

✁

u

✂

v

✄

that can be added to A without violating this invariant

I.e., A

  • ✁✂

u

✄

v

☎✆ ✝

some MST

■ Such an edge is called a safe edge for A

CPTR 430 Algorithms Minimum Spanning Trees

7

slide-8
SLIDE 8

Generic Greedy Algorithm

public static Graph genericMST(Graph G) { Graph A = G.copyWithoutEdges(); while ( A is not a spanning tree for G ) { A.addEdge(an edge in G that is safe for A); } return A; }

Loop invariant: Prior to each iteration, A is a subset of T, some minimum spanning tree

■ Initialization: Before the loop is executed once, A

  • ✁
✂

A

✝

T

■ Maintenance: The loop maintains the invariant by adding only safe edges ■ Termination: A

  • a MST
✂

A

✝

T

CPTR 430 Algorithms Minimum Spanning Trees

8

slide-9
SLIDE 9

Safe Edge

■ The trick is finding the safe edge ■ Within the while loop a safe edge exists because for some MST T: ❚ The invariant requires that A

✆

T

❚ Entry into the body of the while is granted only when A

  • T
  • ✁

edge

✁

u

✂

v

✄

, where

✁

u

✂

v

✄ ☎

T and

✁

u

✂

v

✄ ✂ ☎

A

public static Graph genericMST(Graph G) { Graph A = G.copyWithoutEdges(); while ( A does not span G ) { A.addEdge(an edge in G that is safe for A); } return A; }

CPTR 430 Algorithms Minimum Spanning Trees

9

slide-10
SLIDE 10

Cut

■ A cut

✁

S

✂

V

  • S
✄
  • f an undirected graph G
  • ✁

V

✂

E

✄

partitions V

S S V − S V − S

5 6 8 7 2 6 1 2 4 14 8 4 11 7 10 9 7 8 1 2 3 4

CPTR 430 Algorithms Minimum Spanning Trees

10

slide-11
SLIDE 11

Crossing a Cut

■ An edge

✁

u

✂

v

✄ ☎

E crosses cut

✁

S

✂

V

  • S
✄

if one of its endpoints is in S and the other is in V

  • S

■ A cut respects a set A of edges if no edge in A crosses the cut ■ A light edge crossing a cut has the minimum weight of all edges

crossing the cut There can be multiple light edges in case of ties

S S V − S V − S

5 6 8 7 2 6 1 2 4 14 8 4 11 7 10 9 7 8 1 2 3 4

CPTR 430 Algorithms Minimum Spanning Trees

11

slide-12
SLIDE 12

Safe Edge, Formally

(Theorem 23.1) Let

■ G

  • ✁

V

✂

E

✄

be a connected, undirected, weighted graph

■ A be a subset of E such that A is a subset of some minimum spanning

tree for G

■

✁

S

✂

V

  • S
✄

be any cut of G that respects A

■

✁

u

✂

v

✄

be a light edge crossing

✁

S

✂

V

  • S
✄
  • Edge
✁

u

✂

v

✄

is safe for A

CPTR 430 Algorithms Minimum Spanning Trees

12

slide-13
SLIDE 13

Proof

■ Let T be a MST that includes A ■ Assume T does not include

✁

u

✂

v

✄

(if it does, then

✁

u

✂

v

✄

is trivially safe)

■ Edge

✁

u

✂

v

✄

would form a cycle with the edges on path p from u to v in

T (such a path exists because T spans G)

p

u v y x

■ Edges of T are shown, not

edges in G

■ Edges of A are highlighted ■ Navy blue vertices are in S;

yellow vertices are in V

  • S

CPTR 430 Algorithms Minimum Spanning Trees

13

slide-14
SLIDE 14

Proof

■ u and v are on opposite sides of the cut

✁

S

✂

V

  • S
✄
  • ✁

at least one edge in T on path p that also crosses the cut

■ Let

✁

x

✂

y

✄

be such an edge

■

✁

x

✂

y

✄ ✂ ☎

A, since the cut respects A

■ Since

✁

x

✂

y

✄

is found on a unique path

u p

  • v, removing
✁

x

✂

y

✄

and adding

✁

u

✂

v

✄

creates a new spanning tree, T

✁

p

u v y x

CPTR 430 Algorithms Minimum Spanning Trees

14

slide-15
SLIDE 15

Proof

T

✁
  • T
  • ✁

x

✂

y

✄ ✁ ✂
  • ✁

u

✂

v

✄ ✁

Is T

✁

a MST?

■

✁

u

✂

v

✄

is a light edge crossing cut

✁

S

✂

V

  • S
✄

and

✁

x

✂

y

✄

also crosses cut

✁

S

✂

V

  • S
✄
  • w
✁

u

✂

v

✄ ✄

w

✁

x

✂

y

✄
  • w
✁

T

✁ ✄
  • w
✁

T

✄
  • w
✁

x

✂

y

✄ ☎

w

✁

u

✂

v

✄ ✄

w

✁

T

✄

■ T is a MST

  • w
✁

T

✄ ✄

w

✁

T

✁ ✄

■ w

✁

T

✁ ✄ ✄

w

✁

T

✄

and w

✁

T

✄ ✄

w

✁

T

✁ ✄
  • w
✁

T

✁ ✄
  • w
✁

T

✄
  • T
✁

is also a MST

■ A

✆

T and

✁

x

✂

y

✄ ✂ ☎

A

  • A
✆

T

✁

■ A

✂
  • ✁

u

✂

v

✄ ✁ ✆

T

✁

and T

✁

is a MST

  • ✁

u

✂

v

✄

is safe for A

✆

CPTR 430 Algorithms Minimum Spanning Trees

15

slide-16
SLIDE 16

Corollary 23.2

Let

■ G

  • ✁

V

✂

E

✄

be a connected, undirected, weighted graph

■ A

✆

E be included in some MST for G

■ C

  • ✁

VC

✂

EC

✄

be a connected component (tree) in the forest GA

  • ✁

V

✂

A

✄ ✁

u

✂

v

✄

is a light edge connecting C to some other component in GA

  • ✁

u

✂

v

✄

is safe for A How does this follow from Theorem 23.1?

CPTR 430 Algorithms Minimum Spanning Trees

16

slide-17
SLIDE 17

Proof

■ The cut

✁

VC

✂

V

  • VC
✄

respects A

■

✁

u

✂

v

✄

is a light edge for this cut

  • ✁

u

✂

v

✄

is safe for A

✆

Both Kruskal’s and Prim’s algorithms rely on Corollary 23.2

CPTR 430 Algorithms Minimum Spanning Trees

17

slide-18
SLIDE 18

Kruskal vs. Prim

■ Both provide an efficient way of selecting the safe edge ■ Kruskal’s algorithm: ❚ A is a forest ❚ The safe edge is the least weight edge in the graph that connects two

distinct components

■ Prim’s algorithm: ❚ A is a single tree ❚ The safe edge is the least weight edge in the graph that connects the

tree A to a vertex not in the tree

CPTR 430 Algorithms Minimum Spanning Trees

18

slide-19
SLIDE 19

Kruskal’s Algorithm

■ Based on the genericMST() algorithm ■ Safe edge = the minimum weight edge that connects any two trees in

the coalescing forest of trees, A

■ Suppose C1 and C2 are the two trees, and that

✁

u

✂

v

✄

is the minimum weight edge that connects them

■

✁

u

✂

v

✄

has to be a lightweight edge connecting C1 to some other tree (Why?)

  • ✁

u

✂

v

✄

is safe for C1 (by Corollary 23.2)

CPTR 430 Algorithms Minimum Spanning Trees

19

slide-20
SLIDE 20

Kruskal’s Algorithm, Take Two

Said another way:

■ Kruskal’s algorithm begins with a forest of

✁

V

✁

trees

❚

Each tree contains

  • ne vertex
  • Every vertex in G

forms exactly one tree

❚ Each tree contains no edges ■ At each step, Kruskal’s algorithm adds the lowest weight edge from E

that does not form a cycle

❚ Two trees coalesce into a single tree

CPTR 430 Algorithms Minimum Spanning Trees

20

slide-21
SLIDE 21

Kruskal’s Algorithm is Greedy

Kruskal’s algorithm is greedy, since at each step it adds the edge of the least possible weight The current optimum turns out to be a part of the overall optimum solution

CPTR 430 Algorithms Minimum Spanning Trees

21

slide-22
SLIDE 22

Implementation Details

■ The edges in E must be sorted in nondecreasing order by weight ■ Consider each edge

✁

u

✂

v

✄

in order:

❚ Both endpoints u and v belong to the same tree

  • ✁

u

✂

v

✄

would form a cycle

  • do not add
✁

u

✂

v

✄

to the forming MST

❚ Otherwise, u and v belong to the same tree

  • add
✁

u

✂

v

✄

to the forming MST

CPTR 430 Algorithms Minimum Spanning Trees

22

slide-23
SLIDE 23

Implementation Details

■ Define the relation over V,

  • u
✂

v

☎

V: u

✁

v

  • u and v belong to the

same tree

■

✁

partitions V into equivalance classes

■ The disjoint-set data structure is ideal for implementing the equivalance

classes (See Chapter 21)

CPTR 430 Algorithms Minimum Spanning Trees

23

slide-24
SLIDE 24

Disjoint-set

■ We want a good implementation of the disjoint-set data structure: ❚ use union-by-rank ❚ use path compression ■ These yield the asymptotically fastest implementation known

CPTR 430 Algorithms Minimum Spanning Trees

24

slide-25
SLIDE 25

Analysis of Running Time

■ Initializing A takes O

✁

1

✄

time

■ Making

the intial equivalance classes (via

makeSet())

takes

O

✁

Vα

✁

V

✄ ✄

time

■ Sorting the edges takes O

✁

E lgE

✄

time

■ The for loop performs O

✁

E

✄

findSet() and union() operations on

the disjoint-set forest requiring O

✁

Eα

✁

V

✄ ✄

time

■

✁

E

✁
  • ✁

V

✁
  • 1, since G is connected
  • all the disjoint-set operations (makeSet(), findSet(),

and union()) require O

✁ ✁

V

✄

α

✁

V

✄ ✄ ☎

O

✁ ✁

E

✄

α

✁

V

✄ ✄
  • O
✁

Eα

✁

V

✄ ✄

time (Why?)

CPTR 430 Algorithms Minimum Spanning Trees

25

slide-26
SLIDE 26

Analysis of Running Time

■ α

✁ ✁

V

✁ ✄
  • O
✁

lgV

✄
  • O
✁

lgE

✄
  • The total time for Kruskal’s algorithm is O
✁

E lgE

✄

■

✁

E

✁
  • ✁

V 2

✁
  • lg
✁

E

✁
  • O
✁

lgV

✄
  • The total time for Kruskal’s algorithm is O
✁

E lgV

✄

CPTR 430 Algorithms Minimum Spanning Trees

26

slide-27
SLIDE 27

Prim’s Algorithm

■ Based on the genericMST() algorithm (as is Kruskal’s algorithm) ■ Safe edge = the minimum weight edge that connects the forming MST

A to an isolated vertex in GA

  • ✁

V

✂

A

✄

■

✁

u

✂

v

✄

has to be a lightweight edge connecting A to an isolated vertex (Why?)

  • ✁

u

✂

v

✄

is safe for A (by Corollary 23.2)

■ Like Kruskal’s algorithm, Prim’s algorithm is greedy, since it always

chooses the minimum weight edge to add to the tree A

CPTR 430 Algorithms Minimum Spanning Trees

27

slide-28
SLIDE 28

How to Select the Minumum Weight Edge?

■ All the vertices that are not part of the MST A are placed in a min-priority

queue Q

❚ Initially, Q

  • V

❚ Finally, Q

  • ■ Priority is based on edge weight:

❚ key

✁

v

✂

is the minumum weight of any edge connecting v to a vertex in the MST A

(key

✄

v

☎
  • ∞ means such an edge does not exist)

❚ π

✁

v

✂

is the parent of v in the tree A

❚ During the algorithm’s execution, r is the root of A

  • A
  • ✁

v

✂

π

✁

v

✂ ✄ ✁

v

☎

V

  • r
✁
  • Q
✁

❚ Initially, A

  • and Q
  • V

❚ Finally, A

  • ✁

v

✂

π

✁

v

✂ ✄ ✁

v

☎

V

  • r
✁ ✁

and Q

  • CPTR 430 Algorithms

Minimum Spanning Trees

28

slide-29
SLIDE 29

Prim’s Algorithm—Initialization

■ Set the key of each vertex to ∞, O

✁

V

✄

time

■ Set the specified root’s key to zero, O

✁

1

✄

time

■ Add all the vertices in G to queue Q, O

✁

V

✄

time

❚ buildMinHeap() to form the min-priority queue takes O

✁

V

✄

time The combined initialization activity requires

O

✁

V

✄ ☎

O

✁

1

✄ ☎

O

✁

V

✄
  • O
✁

V

✄

time

CPTR 430 Algorithms Minimum Spanning Trees

29

slide-30
SLIDE 30

The Loop

Prim’s algorithm maintains the following three-part loop invariant: Prior to each iteration of the outer loop:

  • 1. A
  • ✁

v

✂

π

✁

v

✂ ✄ ✁

v

☎

V

  • r
✁
  • Q
✁
  • 2. The vertices already placed in the MST are members of V
  • Q

3.

  • v
☎

Q:

■ π

✁

v

✂ ✂
  • null
  • key
✁

v

✂
  • ∞

■ π

✁

v

✂ ✂
  • null
  • key
✁

v

✂

is the weight of a light edge

✁

v

✂

π

✁

v

✂ ✄

connecting v to some vertex already in the MST

CPTR 430 Algorithms Minimum Spanning Trees

30

slide-31
SLIDE 31

Proof—Initialization

Prior to the first iteration of the outer loop:

  • 1. Q
  • V
  • A
  • ✁

v

✂

π

✁

v

✂ ✄ ✁

v

☎

V

  • r
✁
  • Q
✁
  • 2. No vertices have been placed in the MST, so the vertices already placed

in the MST are members of V

  • Q
  • 3.
  • v
☎

Q

✂

π

✁

v

✂
  • null
  • (vacuously true)

CPTR 430 Algorithms Minimum Spanning Trees

31

slide-32
SLIDE 32

Proof—Maintenance

Prior to each iteration of the outer loop:

■ The top of the min-priority queue is vertex u, the endpoint of a light

edge crossing cut

✁

V

  • Q
✂

Q

✄

Exception: prior to the fi rst iteration u

  • r, but no cut yet exists, since V
  • Q
✂

V

  • Q
  • ✁

❚ The algorithm removes u from Q and adds it to the set V

  • Q of

vertices in the forming MST

  • before the start of the next iteration, the vertices placed in the

MST are those in V

  • Q (maintains Part 2 of the invariant)
  • ❚ The algorithm adds
✁

u

✂

π

✁

u

✂ ✄

to A

  • since u
☎

V

  • Q, the invariant A
  • ✁

v

✂

π

✁

v

✂ ✄ ✁

v

☎

V

  • r
✁
  • Q
✁

remains for the next iteration (maintains Part 1 of the invariant)

  • CPTR 430 Algorithms

Minimum Spanning Trees

32

slide-33
SLIDE 33

Maintenance (cont.)

■ The inner loop updates, if necessary, key and π fields

  • v
☎

Q that are

adjacent to u

❚ If a lower weight edge from the forming MST to v is found (that is,

✁

u

✂

v

✄

, since u is now part of the proto-MST), then

✁

v

✂

π

✁

v

✂ ✄

, where

π

✁

v

✂
  • u, becomes the light edge connecting v to the tree, and key
✁

v

✂

is its weight (maintains Part 3 of the invariant)

  • CPTR 430 Algorithms

Minimum Spanning Trees

33

slide-34
SLIDE 34

Proof—Termination

When the loop terminates

■ A

  • ✁

v

✂

π

✁

v

✂ ✄ ✁

v

☎

V

  • r
✁ ✁

(Part 1 of the invariant)

  • ■ The vertices already placed in the MST are those in V

(Part 2 of the invariant)

  • ❚ I.e., The MST connects all vertices

❚ This establishes the correctness of Prim’s algorithm ■ Q

  • Part 3 of the invariant is vacuously true

(Part 3 of the invariant)

  • CPTR 430 Algorithms

Minimum Spanning Trees

34

slide-35
SLIDE 35

Efficiency of Prim’s Algorithm

■ Recall that the initialization requires O

✁

V

✄

time

■ The outer loop is executed

✁

V

✁

times

■ Each extractMin() operation takes O

✁

lgV

✄
  • the time to perform all extractMin() operations is O
✁

V lgV

✄

■ The sum of the lengths of all adjacency lists is 2

✁

E

✁
  • the inner loop executes O
✁

E

✄

times

CPTR 430 Algorithms Minimum Spanning Trees

35

slide-36
SLIDE 36

Efficiency of Prim’s Algorithm

■ Add a Boolean flag (one bit suffices) to each vertex to indicate if it is in

the queue

  • the check for v
☎

Q within the inner loop takes O

✁

1

✄

time

❚ Set the flag to true when the vertex is enqueued ❚ Reset the flag to false when the vertex is extracted ■ The reassignment of the key field of a vertex potentially requires the

heap to be restructured

❚ The decreaseKey() operation in a min-heap takes O

✁

lgV

✄

time

■ Total time to perform Prim’s algorithm is therefore

O

✁

V lgV

☎

E lgV

✄
  • O
✁

E lgV

✄

CPTR 430 Algorithms Minimum Spanning Trees

36

slide-37
SLIDE 37

Kruskal’s vs. Prim’s Algorithm

■ Both have the same asymptotic time complexities ❚ O

✁

E lgV

✄

■ Prim’s can be improved somewhat ❚ Use a Fibonacci min-heap (Chapter 20) instead of a binary min-heap ❚ Amortized decreaseKey() operations drop to O

✁

1

✄

each

❚ Running time for Prim’s algorithm falls to

O

✁

E

☎

V lgV

✄

CPTR 430 Algorithms Minimum Spanning Trees

37