ece-250 course slides -- minimum spanning treescmoreno/ece250/2012-03... · minimum spanning trees...

57
Minimum spanning trees Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250

Upload: others

Post on 13-Jul-2020

7 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

Carlos Moreno cmoreno @ uwaterloo.ca

EIT-4103

https://ece.uwaterloo.ca/~cmoreno/ece250

Page 2: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

Standard reminder to set phones to silent/vibrate mode, please!

Page 3: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● During today's lesson:● Introduce the notion of spanning tree for a

connected graph● Discuss the notion of minimum spanning trees● Look into two algorithms to find a minimum

spanning tree:– Prim's algorithm– Kruskal's algorithm

Page 4: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Given a connected graph with n vertices, a spanning tree is a collection of n−1 edges that connect all n vertices.

Page 5: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Given a connected graph with n vertices, a spanning tree is a collection of n−1 edges that connect all n vertices.● n−1 is the minimum number of edges required to

connect n vertices, resulting in a tree structure.– If we take any vertex to be the root, we form a tree by

treating adjacent vertices as children.

Page 6: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Given a connected graph with n vertices, a spanning tree is a collection of n−1 edges that connect all n vertices.● n−1 is the minimum number of edges required to

connect n vertices, resulting in a tree structure.– If we take any vertex to be the root, we form a tree by

treating adjacent vertices as children.

● We observe that a spanning tree is not necessarily unique.

Page 7: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● This is an example of a spanning tree:

Page 8: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● For the same graph, this is also a spanning tree:

Page 9: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● As the name suggests, a spanning tree is a tree that spans the entire graph (i.e., that covers all the vertices, with connections restricted to be edges in the graph).

Page 10: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● If the graph is weighted, then a spanning tree has a weight, given by the sum of the edges that constitute the spanning tree.

Page 11: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● If the graph is weighted, then a spanning tree has a weight, given by the sum of the edges that constitute the spanning tree.● For example, the weight of this spanning tree is 20.

Page 12: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● A minimum spanning tree is a spanning tree with minimum weight.

Page 13: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● A minimum spanning tree is a spanning tree with minimum weight.● A minimum spanning tree is not necessarily unique!● That is, there may be several different spanning

trees with the same weight — a weight such that no spanning tree has a weight lower than this.

Page 14: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● A (possibly obvious) observation:● The problem of finding a minimum spanning tree

does not really make sense for unweighted graphs (which would be seen as weighted graphs with all edges having a weight of 1)

Page 15: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● A (possibly obvious) observation:● The problem of finding a minimum spanning tree

does not really make sense for unweighted graphs (which would be seen as weighted graphs with all edges having a weight of 1)

● Why?

Page 16: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● A (possibly obvious) observation:● The problem of finding a minimum spanning tree

does not really make sense for unweighted graphs (which would be seen as weighted graphs with all edges having a weight of 1)

● Why?– We recall that a tree with n nodes has exactly n-1 edges;

if all edges have equal weight, then every spanning tree is a minimum spanning tree, and finding one is trivial.

Page 17: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Examples of applications are numerous. A couple of simple ones:

Page 18: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Examples of applications are numerous. A couple of simple ones:● Supplying power to all circuit elements on a board● To a number of points (loads) in a building or local

area.– A minimum spanning tree gives the lowest-cost option.

www.commedore.ca

www.kpmb.com

Page 19: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Connecting a number of LANs (local area networks)

● Weights could represent a monetary cost. 0

Page 20: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Connecting a number of LANs (local area networks)

● Weights could represent a monetary cost.

● A minimum spanning tree would reveal the optimal (least expensive) solution.

Page 21: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Let's look at one of the algorithms (Prim's algorithm).

Page 22: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Let's look at one of the algorithms (Prim's algorithm).● Side note: as you may have noticed, you will

implement (partially) Prim's algorithm for your last lab project.

Page 23: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Prim's algorithm has certain aspects in common with Dijkstra's algorithm.● At each iteration, the spanning tree is expanded by

choosing the vertex with smallest distance to the “current” spanning tree.– Similar idea, and in fact, as we'll see, the reason why it

works (and the argument to prove that this step works) is almost identical to Dijkstra's algorithm.

Page 24: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Prim's algorithm has certain aspects in common with Dijkstra's algorithm.● At each iteration, the spanning tree is expanded by

choosing the vertex with smallest distance to the “current” spanning tree.– Similar idea, and in fact, as we'll see, the reason why it

works (and the argument to prove that this step works) is almost identical to Dijkstra's algorithm.

– A key difference is that in Dijkstra's algorithm we select the vertex with lowest distance (the “total” distance from the starting vertex) — with Prim's algorithm, we select the lowest distance given by the edge that connects to the current spanning tree.

Page 25: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Suppose we have determined a minimum spanning tree on k of the n vertices.

● We want to extend this to k+1 vertices.

Page 26: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● We choose the vertex joined by the lowest weight edge, ek.

vk + 1

ek

Page 27: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● How do we know for sure that this edge, ek, is part of the minimum spanning tree for the n vertices?

vk + 1

ek

Page 28: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Suppose it's not — that is, that vk+1 ends up being connected to the spanning tree through some other sequence of vertices.

vk + 1

Page 29: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Since ek was the smallest weight joining any vertex to the spanning tree of k nodes, we can find a spanning tree with lower weight, contradicting the assumption that this one was a minimum spanning tree.

vk + 1

Page 30: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● The algorithm is quite simple:● Initialization:

● Select a root node and set its distance as 0● Set the distance to all other vertices as ∞ ● Set all vertices to being unvisited● Set the parent pointer of all vertices to NULL

Page 31: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Then, iterate while there are unvisited vertices with distance < ∞● Select the unvisited vertex with minimum distance● Mark that vertex as visited● For each adjacent vertex, if the weight of the

connecting edge is less than the current distance associated to that vertex:

– Update the distance to equal the weight of the edge

– Set the current vertex as the parent of that adjacent vertex

Page 32: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Here's a demo/example of execution, directly from Professor Harder's slides:

Page 33: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

33

Prim’s Algorithm

Let us find the minimum spanning tree for the following undirected weighted graph

Page 34: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

34

Prim’s Algorithm

First we set up the appropriate table and initialize it

Distance Parent

1 F 0 0

2 F ∞ 0

3 F ∞ 0

4 F ∞ 0

5 F ∞ 0

6 F ∞ 0

7 F ∞ 0

8 F ∞ 0

9 F ∞ 0

Page 35: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

35

Prim’s Algorithm

Visiting vertex 1, we update vertices 2, 4, and 5

Distance Parent

1 T 0 0

2 F 4 1

3 F ∞ 0

4 F 1 1

5 F 8 1

6 F ∞ 0

7 F ∞ 0

8 F ∞ 0

9 F ∞ 0

Page 36: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

36

Prim’s Algorithm

What these numbers really mean is that at this point, we could extend the trivial tree containing just the root node by one of the three possible children:

As we wish to find a minimum spanning tree, it makes sense we add that vertex with a connecting edge with least weight

Page 37: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

37

Prim’s Algorithm

The next unvisited vertex with minimum distance is vertex 4– Update vertices 2, 7, 8– Don’t update vertex 5

Distance Parent

1 T 0 0

2 F 2 4

3 F ∞ 0

4 T 1 1

5 F 8 1

6 F ∞ 0

7 F 9 4

8 F 8 4

9 F ∞ 0

Page 38: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

38

Prim’s Algorithm

Now that we have updated all vertices adjacent to vertex 4, we can extend the tree by adding one of the edges

(1, 5), (4, 2), (4, 7), or (4, 8)

We add that edge with the leastweight: (4, 2)

Page 39: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

39

Prim’s Algorithm

Next visit vertex 2– Update 3, 5, and 6

Distance Parent

1 T 0 0

2 T 2 4

3 F 2 2

4 T 1 1

5 F 6 2

6 F 1 2

7 F 9 4

8 F 8 4

9 F ∞ 0

Page 40: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

40

Prim’s Algorithm

Again looking at the shortest edges to each of the vertices adjacent to the current tree, we note that we can add (2, 6) with the least increase in weight

Page 41: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

41

Prim’s Algorithm

Next, we visit vertex 6:– update vertices 5, 8, and 9

Distance Parent

1 T 0 0

2 T 2 4

3 F 2 2

4 T 1 1

5 F 3 6

6 T 1 2

7 F 9 4

8 F 7 6

9 F 8 6

Page 42: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

42

Prim’s Algorithm

The edge with least weight is (2, 3)– This adds the weight of 2 to the weight minimum spanning tree

Page 43: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

43

Next, we visit vertex 3 and update 5

Prim’s Algorithm

Distance Parent

1 T 0 0

2 T 2 4

3 T 2 2

4 T 1 1

5 F 2 3

6 T 1 2

7 F 9 4

8 F 7 6

9 F 8 6

Page 44: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

44

Prim’s Algorithm

At this point, we can extend the tree by adding the edge (3, 5)

Page 45: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

45

Prim’s Algorithm

Visiting vertex 5, we update 7, 8, 9

Distance Parent

1 T 0 0

2 T 2 4

3 T 2 2

4 T 1 1

5 T 2 3

6 T 1 2

7 F 4 5

8 F 1 5

9 F 5 5

Page 46: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

46

Prim’s Algorithm

At this point, there are three possible edges which we could include which will extend the tree

The edge to 8 has the least weight

Page 47: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

47

Prim’s Algorithm

Visiting vertex 8, we only update vertex 9

Distance Parent

1 T 0 0

2 T 2 4

3 T 2 2

4 T 1 1

5 T 2 3

6 T 1 2

7 F 4 5

8 T 1 5

9 F 3 8

Page 48: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

48

Prim’s Algorithm

There are no other vertices to update while visiting vertex 9

Distance Parent

1 T 0 0

2 T 2 4

3 T 2 2

4 T 1 1

5 T 2 3

6 T 1 2

7 F 4 5

8 T 1 5

9 T 3 8

Page 49: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

49

Prim’s Algorithm

And neither are there any vertices to update when visiting vertex 7

Distance Parent

1 T 0 0

2 T 2 4

3 T 2 2

4 T 1 1

5 T 2 3

6 T 1 2

7 T 4 5

8 T 1 5

9 T 3 8

Page 50: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

50

Prim’s Algorithm

At this point, there are no more unvisited vertices, and therefore we are done

If at any point, all remaining vertices had a distance of ∞, this would indicate that the graph is not connected– in this case, the minimum spanning tree would only span one connected sub-

graph

Page 51: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Prim’s Algorithm

51

Prim’s Algorithm

Using the parent pointers, we can now construct the minimum spanning tree

Distance Parent

1 T 0 0

2 T 2 4

3 T 2 2

4 T 1 1

5 T 2 3

6 T 1 2

7 T 4 5

8 T 1 5

9 T 3 8

Page 52: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Next, we'll take a quick look at Kruskal's algorithm.

Page 53: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Kruskal's algorithm takes a different — but also interesting — approach:

● Put the edges in order by weight, and add the lowest weight edge to the spanning tree if it does not create a cycle.

Page 54: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Kruskal's algorithm takes a different — but also interesting — approach:

● Put the edges in order by weight, and add the lowest weight edge to the spanning tree if it does not create a cycle.

● How do we (efficiently) determine whether adding an edge will create a cycle?

Page 55: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● How do we (efficiently) determine whether adding an edge will create a cycle?

● The idea is to keep track of subsets of vertices that are connected. When considering an edge:

– If the two vertices are in the same subset, discard the edge (it would produce a cycle)

– Otherwise (i.e., if the two vertices are in two different subsets), then all the vertices in both subsets will now be part of a single subset (since the added edge is connecting them)

● In other words, replace the two sets by the union of them.

Page 56: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Minimum spanning trees

● Notice that this is reasonably easy and efficient — we start with n sets, each containing one of the n vertices in the graph.

● Normally, one could use a disjoint sets data structure.

● But we could get away with an array where each vertex is associated with the set where it is — the set could have an arbitrary ID; the only thing that matters is that we can determine whether two vertices are in the same set; doesn't really matter what that set is called.

Page 57: ECE-250 Course Slides -- Minimum spanning treescmoreno/ece250/2012-03... · Minimum spanning trees Prim's algorithm has certain aspects in common with Dijkstra's algorithm. At each

Summary

● Notice that this is reasonably easy and efficient — we start with n sets, each containing one of the n vertices in the graph.

● Normally, one could use a disjoint sets data structure.

● But we could get away with an array where each vertex is associated with the set where it is — the set could have an arbitrary ID; the only thing that matters is that we can determine whether two vertices are in the same set; doesn't really matter what that set is called.