Skiena algorithm 2007 lecture13 minimum spanning trees

37
Lecture 13: Minimum Spanning Trees Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/skiena

Transcript of Skiena algorithm 2007 lecture13 minimum spanning trees

Page 1: Skiena algorithm 2007 lecture13 minimum spanning trees

Lecture 13:Minimum Spanning Trees

Steven Skiena

Department of Computer ScienceState University of New YorkStony Brook, NY 11794–4400

http://www.cs.sunysb.edu/∼skiena

Page 2: Skiena algorithm 2007 lecture13 minimum spanning trees

Problem of the Day

Your job is to arrange n rambunctious children in a straightline, facing front. You are given a list of m statements ofthe form “i hates j”. If i hates j, then you do not want puti somewhere behind j, because then i is capable of throwingsomething at j.

1. Give an algorithm that orders the line, (or says that it isnot possible) in O(m + n) time.

Page 3: Skiena algorithm 2007 lecture13 minimum spanning trees

2. Suppose instead you want to arrange the children inrows, such that if i hates j then i must be in a lowernumbered row than j. Give an efficient algorithm to findthe minimum number of rows needed, if it is possible.

Page 4: Skiena algorithm 2007 lecture13 minimum spanning trees

Weighted Graph Algorithms

Beyond DFS/BFS exists an alternate universe of algorithmsfor edge-weighted graphs.Our adjancency list representation quietly supported thesegraphs:

typedef struct {int y;int weight;struct edgenode *next;

} edgenode;

Page 5: Skiena algorithm 2007 lecture13 minimum spanning trees

Minimum Spanning Trees

A tree is a connected graph with no cycles. A spanning tree isa subgraph of G which has the same set of vertices of G andis a tree.A minimum spanning tree of a weighted graph G is thespanning tree of G whose edges sum to minimum weight.There can be more than one minimum spanning tree in agraph → consider a graph with identical weight edges.

Page 6: Skiena algorithm 2007 lecture13 minimum spanning trees

(a) (b) (c)

Page 7: Skiena algorithm 2007 lecture13 minimum spanning trees

Why Minimum Spanning Trees?

The minimum spanning tree problem has a long history – thefirst algorithm dates back at least to 1926!.Minimum spanning tree is always taught in algorithm coursessince (1) it arises in many applications, (2) it is an importantexample where greedy algorithms always give the optimalanswer, and (3) Clever data structures are necessary to makeit work.In greedy algorithms, we make the decision of what next todo by selecting the best local option from all available choices– without regard to the global structure.

Page 8: Skiena algorithm 2007 lecture13 minimum spanning trees

Applications of Minimum Spanning Trees

Minimum spanning trees are useful in constructing networks,by describing the way to connect a set of sites using thesmallest total amount of wire.Minimum spanning trees provide a reasonable way forclustering points in space into natural groups.What are natural clusters in the friendship graph?

Page 9: Skiena algorithm 2007 lecture13 minimum spanning trees

Minimum Spanning Trees and Net Partitioning

One of the war stories in the text describes howto partition a graph into compact subgraphs bydeleting large edges from the minimum spanning tree.

(a) (b) (c) (d)

Page 10: Skiena algorithm 2007 lecture13 minimum spanning trees

Minimum Spanning Trees and TSP

When the cities are points in the Euclidean plane, theminimum spanning tree provides a good heuristic fortraveling salesman problems.The optimum traveling salesman tour is at most twice thelength of the minimum spanning tree.

The Option Traveling System tour is at most twicethe length of the minimum spanning tree.

Note: There can be more than one minimum spanning tree considered as a group with identical weight edges.

Page 11: Skiena algorithm 2007 lecture13 minimum spanning trees

Prim’s Algorithm

If G is connected, every vertex will appear in the minimumspanning tree. If not, we can talk about a minimum spanningforest.Prim’s algorithm starts from one vertex and grows the rest ofthe tree an edge at a time.As a greedy algorithm, which edge should we pick? Thecheapest edge with which can grow the tree by one vertexwithout creating a cycle.

Page 12: Skiena algorithm 2007 lecture13 minimum spanning trees

Prim’s Algorithm (Pseudocode)

During execution each vertex v is either in the tree, fringe(meaning there exists an edge from a tree vertex to v) orunseen (meaning v is more than one edge away).

Prim-MST(G)Select an arbitrary vertex s to start the tree from.While (there are still non-tree vertices)

Select the edge of minimum weight between a tree and non-tree vertexAdd the selected edge and vertex to the tree Tprim.

This creates a spanning tree, since no cycle can be introduced,but is it minimum?

Page 13: Skiena algorithm 2007 lecture13 minimum spanning trees

Prim’s Algorithm in Action

6

Kruskal(G)Prim(G,A)G

3

A

2

3

4

1

5

A

4

2 6

5

1

A

2

5

43

7

12

7

4 7

2

9

5

Page 14: Skiena algorithm 2007 lecture13 minimum spanning trees

Why is Prim Correct?

We use a proof by contradiction:Suppose Prim’s algorithm does not always give the minimumcost spanning tree on some graph.If so, there is a graph on which it fails.And if so, there must be a first edge (x, y) Prim adds suchthat the partial tree V ′ cannot be extended into a minimumspanning tree.

Page 15: Skiena algorithm 2007 lecture13 minimum spanning trees

s

x y

s

x y

v1

v2

(a) (b)

But if (x, y) is not in MST (G), then there must be a path inMST (G) from x to y since the tree is connected. Let (v, w)be the first edge on this path with one edge in V ′

Replacing it with (x, y) we get a spanning tree. with smallerweight, since W (v, w) > W (x, y). Thus you did not have theMST!!

Page 16: Skiena algorithm 2007 lecture13 minimum spanning trees

Prim’s Algorithm is correct!

Thus we cannot go wrong with the greedy strategy the waywe could with the traveling salesman problem.

Page 17: Skiena algorithm 2007 lecture13 minimum spanning trees

How Fast is Prim’s Algorithm?

That depends on what data structures are used. In the simplestimplementation, we can simply mark each vertex as tree andnon-tree and search always from scratch:

Select an arbitrary vertex to start.While (there are non-tree vertices)

select minimum weight edge between tree and fringeadd the selected edge and vertex to the tree

This can be done in O(nm) time, by doing a DFS or BFS toloop through all edges, with a constant time test per edge, anda total of n iterations.

Page 18: Skiena algorithm 2007 lecture13 minimum spanning trees

Prim’s Implementation

To do it faster, we must identify fringe vertices and theminimum cost edge associated with it fast.prim(graph *g, int start){

int i; (* counter *)edgenode *p; (* temporary pointer *)bool intree[MAXV]; (* is the vertex in the tree yet? *)int distance[MAXV]; (* distance vertex is from start *)int v; (* current vertex to process *)int w; (* candidate next vertex *)int weight; (* edge weight *)int dist; (* best current distance from start *)

for (i=1; i<=g− >nvertices; i++) {intree[i] = FALSE;distance[i] = MAXINT;parent[i] = -1;

}

distance[start] = 0;

Page 19: Skiena algorithm 2007 lecture13 minimum spanning trees

v = start;

while (intree[v] == FALSE) {intree[v] = TRUE;p = g− >edges[v];while (p ! = NULL) {

w = p− >y;weight = p− >weight;if ((distance[w] > weight) && (intree[w] == FALSE)) {

distance[w] = weight;parent[w] = v;

}

p = p− >next;}

v = 1;dist = MAXINT;for (i=1; i<= g− >nvertices; i++)

if ((intree[i] == FALSE) && (dist > distance[i])) {dist = distance[i];v = i;

}}

}

Page 20: Skiena algorithm 2007 lecture13 minimum spanning trees

Prim’s Analysis

Finding the minimum weight fringe-edge takes O(n) time –just bump through fringe list.After adding a vertex to the tree, running through itsadjacency list to update the cost of adding fringe vertices(there may be a cheaper way through the new vertex) can bedone in O(n) time.Total time is O(n2).

Page 21: Skiena algorithm 2007 lecture13 minimum spanning trees

Kruskal’s Algorithm

Since an easy lower bound argument shows that every edgemust be looked at to find the minimum spanning tree, and thenumber of edges m = O(n2), Prim’s algorithm is optimal inthe worst case. Is that all she wrote?The complexity of Prim’s algorithm is independent of thenumber of edges. Can we do better with sparse graphs? Yes!Kruskal’s algorithm is also greedy. It repeatedly adds thesmallest edge to the spanning tree that does not create a cycle.

Page 22: Skiena algorithm 2007 lecture13 minimum spanning trees

Kruskal’s Algorithm in Action

6

Kruskal(G)Prim(G,A)G

3

A

2

3

4

1

5

A

4

2 6

5

1

A

2

5

43

7

12

7

4 7

2

9

5

Page 23: Skiena algorithm 2007 lecture13 minimum spanning trees

Why is Kruskal’s algorithm correct?

Again, we use proof by contradiction.Suppose Kruskal’s algorithm does not always give theminimum cost spanning tree on some graph.If so, there is a graph on which it fails.And if so, there must be a first edge (x, y) Kruskal adds suchthat the set of edges cannot be extended into a minimumspanning tree.When we added (x, y) there previously was no path betweenx and y, or it would have created a cycleThus if we add (x, y) to the optimal tree it must create a cycle.At least one edge in this cycle must have been added after(x, y), so it must have a heavier weight.

Page 24: Skiena algorithm 2007 lecture13 minimum spanning trees

Deleting this heavy edge leave a better MST than the optimaltree? A contradiction!

Page 25: Skiena algorithm 2007 lecture13 minimum spanning trees

How fast is Kruskal’s algorithm?

What is the simplest implementation?

• Sort the m edges in O(m lg m) time.

• For each edge in order, test whether it creates a cycle theforest we have thus far built – if so discard, else add toforest. With a BFS/DFS, this can be done in O(n) time(since the tree has at most n edges).

The total time is O(mn), but can we do better?

Page 26: Skiena algorithm 2007 lecture13 minimum spanning trees

Fast Component Tests Give Fast MST

Kruskal’s algorithm builds up connected components. Anyedge where both vertices are in the same connected compo-nent create a cycle. Thus if we can maintain which verticesare in which component fast, we do not have test for cycles!

• Same component(v1, v2) – Do vertices v1 and v2 lie in thesame connected component of the current graph?

• Merge components(C1, C2) – Merge the given pair ofconnected components into one component.

Page 27: Skiena algorithm 2007 lecture13 minimum spanning trees

Fast Kruskal Implementation

Put the edges in a heapcount = 0while (count < n − 1) do

get next edge (v, w)if (component (v) 6= component(w))

add to Tcomponent (v)=component(w)

If we can test components in O(log n), we can find the MSTin O(m log m)!Question: Is O(m log n) better than O(m log m)?

Page 28: Skiena algorithm 2007 lecture13 minimum spanning trees

Union-Find Programs

We need a data structure for maintaining sets which can testif two elements are in the same and merge two sets together.These can be implemented by union and find operations,where

• Find(i) – Return the label of the root of tree containingelement i, by walking up the parent pointers until there isno where to go.

• Union(i,j) – Link the root of one of the trees (saycontaining i) to the root of the tree containing the other(say j) so find(i) now equals find(j).

Page 29: Skiena algorithm 2007 lecture13 minimum spanning trees

Same Component Test

Is si ≡ sj

t = Find(si)u = Find(sj)Return (Is t = u?)

Page 30: Skiena algorithm 2007 lecture13 minimum spanning trees

Merge Components Operation

Make si ≡ sj

t = d(si)u = d(sj)Union(t, u)

Page 31: Skiena algorithm 2007 lecture13 minimum spanning trees

Union-Find “Trees”

We are interested in minimizing the time it takes to executeany sequence of unions and finds.A simple implementation is to represent each set as a tree,with pointers from a node to its parent. Each element iscontained in a node, and the name of the set is the key atthe root:

1

4

3

6 2

7

5

(l) (r)

0

1 2

4

3 4 5

3

6

4

7

30 0

Page 32: Skiena algorithm 2007 lecture13 minimum spanning trees

Worst Case for Union Find

In the worst case, these structures can be very unbalanced:

For i = 1 to n/2 doUNION(i,i+1)

For i = 1 to n/2 doFIND(1)

Page 33: Skiena algorithm 2007 lecture13 minimum spanning trees

Who’s The Daddy?

We want the limit the height of our trees which are effectedby union’s.When we union, we can make the tree with fewer nodes thechild.

s

FIND(S)

t

S

UNION (s, t)

Since the number of nodes is related to the height, the heightof the final tree will increase only if both subtrees are of equal

Page 34: Skiena algorithm 2007 lecture13 minimum spanning trees

height!If Union(t, v) attaches the root of v as a subtree of t iff thenumber of nodes in t is greater than or equal to the number inv, after any sequence of unions, any tree with h/4 nodes hasheight at most blg hc.

Page 35: Skiena algorithm 2007 lecture13 minimum spanning trees

Proof

By induction on the number of nodes k, k = 1 has height 0.Let di be the height of the tree ti

d1

d2T2

k2 nodesk1 nodes

T1

k = k1+ k2 nodes

d is the height

If (d1 > d2) then d = d1 ≤ blog k1c ≤ blg(k1 +k2)c = blog kcIf (d1 ≤ d2), then k1 ≥ k2.d = d2 + 1 ≤ blog k2c + 1 = blog 2k2c ≤ blog(k1 + k2)c =log k

Page 36: Skiena algorithm 2007 lecture13 minimum spanning trees

Can we do better?

We can do unions and finds in O(log n), good enough forKruskal’s algorithm. But can we do better?The ideal Union-Find tree has depth 1:

N-1 leaves

... ...

On a find, if we are going down a path anyway, why notchange the pointers to point to the root?

Page 37: Skiena algorithm 2007 lecture13 minimum spanning trees

1

2

3

4 5 6 8 9

7 1112

13

1410

FIND(4)

1

4 3 7 1014

1312

115 6 8 9

2

This path compression will let us do better than O(n log n)for n union-finds.O(n)? Not quite . . . Difficult analysis shows that it takesO(nα(n)) time, where α(n) is the inverse Ackerman functionand α(number of atoms in the universe)= 5.