Minimum spanning trees

Similar documents
LECTURE 26 PRIM S ALGORITHM

Dijkstra's Algorithm

Graphs. Carlos Moreno uwaterloo.ca EIT

Heap sort. Carlos Moreno uwaterloo.ca EIT

Heap sort. Carlos Moreno uwaterloo.ca EIT

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

22 Elementary Graph Algorithms. There are two standard ways to represent a

CS 310 Advanced Data Structures and Algorithms

22 Elementary Graph Algorithms. There are two standard ways to represent a

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Minimum Spanning Trees

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

CHAPTER 13 GRAPH ALGORITHMS

Undirected Graphs. Hwansoo Han

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Spanning Trees. Lecture 20 CS2110 Spring 2015

Exam 3 Practice Problems

Greedy Algorithms. Previous Examples: Huffman coding, Minimum Spanning Tree Algorithms

What is a minimal spanning tree (MST) and how to find one

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSE 100: GRAPH ALGORITHMS

Minimum Spanning Trees My T. UF

Minimum Spanning Trees

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Minimum Spanning Trees and Shortest Paths

Kruskal's MST Algorithm

CS 6783 (Applied Algorithms) Lecture 5

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Chapter 9 Graph Algorithms

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

looking ahead to see the optimum

Chapter 9 Graph Algorithms

Week 11: Minimum Spanning trees

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Problem Set 2 Solutions

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Week 12: Minimum Spanning trees and Shortest Paths

Minimum Spanning Trees and Shortest Paths

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Ma/CS 6a Class 8: Eulerian Cycles

Chapter 9 Graph Algorithms

Kruskal s MST Algorithm

Greedy Algorithms. This is such a simple approach that it is what one usually tries first.

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

Lecture Summary CSC 263H. August 5, 2016

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

7.3 Spanning trees Spanning trees [ ] 61

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Lecture 19. Broadcast routing

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Lecture 3: Graphs and flows

6.1 Minimum Spanning Trees

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Assignment 5: Solutions

SEVENTH EDITION and EXPANDED SEVENTH EDITION

Trees Algorhyme by Radia Perlman

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Foundations of Discrete Mathematics

Treewidth and graph minors

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

Trees. Carlos Moreno uwaterloo.ca EIT

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

CS 4349 Lecture October 18th, 2017

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

Lecture 8: The Traveling Salesman Problem

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Discrete mathematics

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

Chapter 23. Minimum Spanning Trees

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Introduction to Optimization

Distributed minimum spanning tree problem

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Minimum Spanning Tree

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

CSE 21 Mathematics for Algorithm and System Analysis

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

5 MST and Greedy Algorithms

Depth First Search A B C D E F G A B C 5 D E F 3 2 G 2 3

Transcription:

Carlos Moreno cmoreno @ uwaterloo.ca EI-3 https://ece.uwaterloo.ca/~cmoreno/ece5

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

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

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

Given a connected graph with n vertices, a spanning tree is a collection of n edges that connect all n vertices. n 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.

Given a connected graph with n vertices, a spanning tree is a collection of n edges that connect all n vertices. n 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.

his is an example of a spanning tree:

or the same graph, this is also a spanning tree:

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).

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

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

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

A minimum spanning tree is a spanning tree with minimum weight. A minimum spanning tree is not necessarily unique! hat 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.

A (possibly obvious) observation: he 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 )

A (possibly obvious) observation: he 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 ) Why?

A (possibly obvious) observation: he 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 ) Why? We recall that a tree with n nodes has exactly n- edges; if all edges have equal weight, then every spanning tree is a minimum spanning tree, and finding one is trivial.

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

Examples of applications are numerous. A couple of simple ones: Supplying power to all circuit elements on a board o a number of points (loads) in a building or local area. A minimum spanning tree gives the lowest-cost option. www.kpmb.com www.commedore.ca

Connecting a number of LANs (local area networks) Weights could represent a monetary cost.

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.

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

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.

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.

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.

Suppose we have determined a minimum spanning tree on k of the n vertices. We want to extend this to k+ vertices.

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

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

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

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 +

he algorithm is quite simple: Initialization: Select a root node and set its distance as Set the distance to all other vertices as Set all vertices to being unvisited Set the parent pointer of all vertices to NULL

hen, iterate while there are unvisited vertices with distance < Select the unvisited vertex with minimum distance Mark that vertex as visited or 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

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

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

irst we set up the appropriate table and initialize it Distance Paren t 3 5 6 7 8 9 3

Visiting vertex, we update vertices,, and 5 Distance Paren t 3 5 8 6 7 8 9 35

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 36

he next unvisited vertex with minimum distance is vertex Update vertices, 7, 8 Don t update vertex 5 Distance Paren t 3 5 8 6 7 9 8 8 9 37

Now that we have updated all vertices adjacent to vertex, we can extend the tree by adding one of the edges (, 5), (, ), (, 7), or (, 8) We add that edge with the least weight: (, ) 38

Next visit vertex Update 3, 5, and 6 Distance Paren t 3 5 6 6 7 9 8 8 9 39

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

Next, we visit vertex 6: update vertices 5, 8, and 9 Distance Paren t 3 5 3 6 6 7 9 8 7 6 9 8 6

he edge with least weight is (, 3) his adds the weight of to the weight minimum spanning tree

Next, we visit vertex 3 and update 5 Distance Paren t 3 5 3 6 7 9 8 7 6 9 8 3 6

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

Visiting vertex 5, we update 7, 8, 9 Distance Paren t 3 5 3 6 7 5 8 5 9 5 5 5

At this point, there are three possible edges which we could include which will extend the tree he edge to 8 has the least weight 6

Visiting vertex 8, we only update vertex 9 Distance Paren t 3 5 3 6 7 5 8 5 9 3 7 8

here are no other vertices to update while visiting vertex 9 Distance Paren t 3 5 3 6 7 5 8 5 9 3 8 8

And neither are there any vertices to update when visiting vertex 7 Distance Paren t 3 5 3 6 7 5 8 5 9 3 9 8

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 subgraph 5

Using the parent pointers, we can now construct the minimum spanning tree Distance Paren t 3 5 3 6 7 5 8 5 9 3 5 8

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

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.

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?

How do we (efficiently) determine whether adding an edge will create a cycle? he 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.

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.

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.