Minimum Spanning Trees - University of Missouri–St....

22
Minimum Spanning Trees [for connected & undirected graphs] Course: CS 5130 - Advanced Data Structures and Algorithms Instructor: Dr. Badri Adhikari

Transcript of Minimum Spanning Trees - University of Missouri–St....

Page 1: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Minimum Spanning Trees[for connected & undirected graphs]

Course: CS 5130 - Advanced Data Structures and Algorithms Instructor: Dr. Badri Adhikari

Page 2: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Wiring problem in Electronic circuit designSeveral components are made electrically equivalent by wiring them together.

To interconnect a set of n pins, we can use an arrangement of n-1 wires, each connecting two pins.

Of course, of the many arrangements possible, the one that uses the least amount of wire is desirable.

How many possible configurations are there for a given number of nodes n? Pins in a circuit board

We would like to connect all the orange pins

Page 3: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Wiring problem in Electronic circuit designThe wiring problem with a connected, undirected graph G = (V, E):

● V is the set of pins, ● E is the set of possible interconnections between pairs of pins,● For each edge (u,v) ∈ E, we have a weight (u, v) specifying the cost

(amount of wire needed) to connect u and v.

We wish to find an acyclic subset T ⊆ E that connects all of the vertices and whose total weight w(T) = ∑ w(u, v), where (u,v) ∈ T, is minimized.

Since T is acyclic and connects all of the vertices, it must form a tree, called spanning tree since it ‘spans’ the graph G.

The problem of determining the T is called ‘minimum-spanning-tree problem’.

Pins in a circuit board

We would like to connect all the orange ones

Page 4: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

ExampleAll lines (dotted and straight) represent the edges of the graph.

Dark lines (undotted) represent the edges of MST.

Is this a MST?

b

a

e

c d

gf

Page 5: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Solving the MST problemTwo algorithms for solving the minimum-spanning tree problem:

(a) Kruskal’s algorithm, and(b) Prim’s algorithm

Both use greedy strategy to obtain the optimal solution.

Both rely on a generic method.

Page 6: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Greedily growing a minimum spanning treeWe have a connected, undirected graph G = (V, E) with weight function w:E→R. We wish to find a MST for G.

Manage a set of edges A, which is a subset of some MST.

At each step, we determine an edge (u,v) that we can add to A so that A ∪ {(u,v)} is also a subset of a minimum spanning tree.

How to recognize safe edges?

An edge (u,v) is safe for a set A such that adding (u,v) to A, maintains A as a subset of some MST.

We say, edge (u,v) is safe edge for A.

Page 7: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Greedily growing a minimum spanning treeA cut (S, V-S) of an undirected graph G = (V, E) is a partition of V.

An edge (u,v) ∈ E crosses the 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.

An edge is light edge crossing a cut if its weight is the minimum of any edge crossing the cut.

There can be more than one light edge crossing a cut in case of ties.

Page 8: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A.

b

a

e

u v

g

fS

V-S

1

3

73

5

A

A

A

A- A is a subset of MST.- The cut respects A.- An edge is safe for A if it

maintains A.

Page 9: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A.

Proof (approach):

Let T be a minimum spanning tree that includes A, and assume that T does not contain the light edge (u, v), since if it does, we are done!

(a) Assume that (u, v) is not in the MST but (x, y) is.

(b) Construct a new tree T’ = T - {(x,y)} + {(u,v)}.

(c) Show that T’ is MST as well.

(d) Show that (u,v) is safe for A.Lightedge

Page 10: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Proof:Let T be a minimum spanning tree that includes A, and assume that T does not contain the light edge (u, v).The edge (u,v) forms a cycle with the edges on the simple path p from u to v in T. Since u and v are in the opposite sides of the cut (S, V-S), at least one edge in T lies on the simple path p and also crosses the cut. Let (x, y) be such edge.The edge (x,y) is not in A, because the cut respects A. Since (x, y) is on the unique simple path from u to v in T, removing (x,y) breaks T into two components. Adding (u,v) reconnects them to form a new spanning tree T’ = T - {(x,y)} ∪ {(u,v)}.

Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A.

Page 11: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Proof (continued):Since (u,v) is a light edge crossing (S, V-S) and (x, y) also crosses the cut, w(u, v) ≤ w(x, y). Therefore,w(T’) = w(T) - w(x,y) + w(u,v)

≤ w(T)But T is a minimum spanning tree, so that w(T) ≤ w(T’); thus T’ must be a minimum spanning tree.

How is (u,v) safe edge for A?We have A ⊆ T’and A ∪ {(u, v)} ⊆ T’.Since T’ is a MST (u,v) must be safe for A.

Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A.

Page 12: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Corollary: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, and let C = (VC, EC) be a connected component (tree) in the forest GA = (V, A). If (u,v) is a light edge connecting C to some other component in GA, then (u,v) is safe for A.

Proof:

The cut (VC, V-VC) respects A, and (u, v) is a light edge for this cut. Therefore, (u,v) is safe for A.

b

a

e

u v

g

fVC

V-VC

1

3

73

5

C

Page 13: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

The algorithms of Kruskal and PrimIn Kruskal’s algorithm, the set A is a forest whose vertices are all those of the given graph. The safe edge added to A is always a least-weight edge in the graph that connects two distinct components.

In Prim’s algorithm, the set A forms a single tree. The safe edge added to A is always a least-weight edge connecting the tree to a vertex not in tree.

b

a

e

u v

g

f

1

3

73

5A

A

A

b

a

e

u v

g

f

1

3

73

5

A

A

A

A

Kruskal’s algorithm Prim’s algorithm

Page 14: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Kruskal’s algorithmKruskal’s algorithm finds a safe edge to add to the growing forest, i.e. an edge (u, v) of least weight to connect any two trees in the forest.

One tree is one connected component.

C1 and C2 are the two trees that are connected by (u,v). Since (u,v) is a light edge connecting C1 to some other tree, the Corollary implies that (u, v) is safe for C1.

Where will Kruskal’s algorithm start?

C1

C2

Page 15: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

https://en.wikipedia.org/wiki/Kruskal%27s_algorithm#/media/File:KruskalDemo.gif

Page 16: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Running time of Kruskal’s algorithm

Running time of Kruskal’s = O(E lg E)

|E| ≤ |V|2 ⟹ O (lg E) = O(lg V2) = O(2 lg V) = O(lg V)

So, running time of Kruskal’s = O(E lg V)

O(V)

O(E lg E)

O(E)

Page 17: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Prim’s algorithmThe Prim’s algorithm has the property that the edges in the set A always form a single tree.

The tree starts from an arbitrary root vertex r and grows until the tree spans all the vertices in V.

At each step, a light edge that connects A to an isolated vertex (one on which an edge of A is incident).

By the Corollary, this rule adds only edges that are safe for A; therefore when the algorithm terminates, the edges in A form a minimum spanning tree.

b

a

e

u v

g

f

1

3

73

5

A

A

A

A

Page 18: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Prim’s algorithmAll vertices that are not in the tree reside in a min-priority queue Q based on a key attribute.

For each vertex v, the attribute v.key is the minimum weight of any edge connecting v to a vertex in the tree; v.key = ∞ if no such edge exists.

The attribute v.π names the parent of v in the tree.

The algorithm implicitly maintains the set A as:

A = {(v, v.π) : v ∈ V - {r} - Q}

When the algorithm terminates, the min-priority queue A is empty; the minimum spanning tree A of G is:

A = {(v, v.π) : v ∈ V - {r}}

b

a

e

u v

g

f

1

3

73

5

A

A

A

A

Page 19: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

https://en.wikipedia.org/wiki/Prim%27s_algorithm#/media/File:PrimAlgDemo.gif

Page 20: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

Running time of Prim’s algorithmTotal time for all calls to Extract-Min operations is O(V * time for Extract-Min()).

We scan the adjacency list of each vertex once which takes O(E) time. Let’s assume that the time needed for “v.key = w(u,v)” is x so that total time is O(x E). Every time a v.key is updated, the heap has to be updated.

Queue implementation using binary min-heap:Time for Extract-Min = O(lgV)x = O(lgV)Total running time = O(V lgV + E lgV ) = O(E lgV)

Queue implementation using Fibonacci heap:Time for Extract-Min = O(lgV)x = O(1)Total running time = O(V lgV + E)

O(V)

O(lgV)

Loop runs O(E)times

Page 21: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

ClassworkObtain MST for the graph using:

(a) Kruskal’s algorithm (should be easy and quick).

(b) Prim’s algorithm

Page 22: Minimum Spanning Trees - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/06 - Minimum... · 2017-12-06 · The minimum-spanning-tree problem is finding

SummaryThe minimum-spanning-tree problem is finding the minimum set of edges that connect all the vertices and have minimum weight total.

Running time of Kruskal’s algorithm is O(E lg V).

Running time of Prim’s algorithm is O(E + V lgV) when we use Fibonacci heap for priority queue.

This means that for a sparse graph when E is close to V, both have similar running time, whereas for dense graph, Prim’s is faster.