Minimum Cost Spanning Trees CSC263 Tutorial 10. Minimum cost spanning tree (MCST) What is a minimum cost spanning tree? – Tree No cycles; equivalently,

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
1 Spanning Trees Lecture 20 CS2110 – Spring
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Two Discrete Optimization Problems Problem #2: The Minimum Cost Spanning Tree Problem.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Minimum Spanning Trees
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Minimum Spanning Trees Easy. Terms Node Node Edge Edge Cut Cut Cut respects a set of edges Cut respects a set of edges Light Edge Light Edge Minimum Spanning.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.
Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Minimum Spanning Trees Problem Description Why Compute MST? MST in Unweighted Graphs MST in Weighted Graphs –Kruskal’s Algorithm –Prim’s Algorithm 1.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Tree.
Spanning Trees.
Data Structures – LECTURE 13 Minumum spanning trees
Hannah Tang and Brian Tjaden Summer Quarter 2002
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Tree.
Fundamental Structures of Computer Science II
Minimum spanning trees
Lecture 12 Algorithm Analysis
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Presentation transcript:

Minimum Cost Spanning Trees CSC263 Tutorial 10

Minimum cost spanning tree (MCST) What is a minimum cost spanning tree? – Tree No cycles; equivalently, for each pair of nodes u and v, there is only one path from u to v – Spanning Contains every node in the graph – Minimum cost Smallest possible total weight of any spanning tree

Minimum cost spanning tree (MCST) Let’s think about simple MCSTs on this graph: a a b b c c d d

Minimum cost spanning tree (MCST) Black edges and nodes are in T Is T a minimum cost spanning tree? Not spanning; d is not in T. a a b b c c d d

Minimum cost spanning tree (MCST) Black edges and nodes are in T Is T a minimum cost spanning tree? Not a tree; has a cycle. a a b b c c d d

Minimum cost spanning tree (MCST) Black edges and nodes are in T Is T a minimum cost spanning tree? Not minimum cost; can swap edges 4 and 2. a a b b c c d d

Minimum cost spanning tree (MCST) Which edges form a MCST? a a b b c c d d a a b b c c d d

Quick Quiz If we build a MCST from a graph G = (V, E), how may edges does the MCST have? When can we find a MCST for a graph?

An application of MCSTs Electronic circuit designs (from Cormen et al.) – Circuits often need to wire together the pins of several components to make them electrically equivalent. – To connect n pins, we can use n - 1 wires, each connecting two pins. – Want to use the minimum amount of wire. – Model problem with a graph where each pin is a node, and every possible wire between a pair of pins is an edge.

A few other applications of MCSTs Planning how to lay network cable to connect several locations to the internet Planning how to efficiently bounce data from router to router to reach its internet destination Creating a 2D maze (to print on cereal boxes, etc.)

Building a MCST In the book’s terminology, we find a light edge crossing the cut (T, V-T) The book proves that adding |V|-1 such edges will create a MCST

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Running Prim’s algorithm a a b b c c d d g g i i h h j j f f e e h h

Implementing Prim’s Algorithm How can we do this efficiently? Finding lots of minimums? Use a priority queue!

Adding a priority queue What should we store in the priority queue? – Edges – From nodes in T to nodes not in T What should we use as the key of an edge? – Weight of the edge

Prim’s Algorithm with a priority queue PrimMCST(V, E, r) – Q := new priority queue – For each u in V: inTree[u] = false, parent[u] = nil – inTree[r] = true, parent[r] = r – Add every edge that touches r to Q – While Q is not empty Do Q.Extract-Min to get edge e = (u, v) If not inTree[v] then – inTree[v] = true, parent[v] = u – Add every edge that touches v to Q where r is any arbitrary starting node

Small optimization PrimMCST(V, E, r) – Q := new priority queue – For each u in V: inTree[u] = false, parent[u] = nil – inTree[r] = true, parent[r] = r – Add every edge that touches r to Q – While Q is not empty Do Q.Extract-Min to get edge e = (u, v) If not inTree[v] parent[v] = nil then – inTree[v] = true, parent[v] = u – Add every edge that touches v to Q

Analysis of running time O(|E| log |E|) = O(|E| log (|V| 2 )) = O(|E| 2 log |V|) = O(|E| log |V|) ϴ(|V|) ϴ(|adj(r)| log |E|) ϴ(log |E|) ϴ(|adj(v)| log |E|) ϴ(|E| log |E|)

Java Implementation - 1

Java Implementation - 2

An example input

Java Implementation - 3

Java Implementation - 4 Outputting the answer: The answer: What does this look like? Recall: the root is its own parent.

Recall our earlier solution by hand: Drawing the answer

Fun example: generating 2D mazes Prim’s algorithm maze building video How can we use Prim’s algorithm to do this? 2. Set all edge weights to random values! 3. Run Prim’s algorithm starting from any node. 1. Create a graph that is a regular m x n grid.

Fun example: generating 2D mazes After Prim’s, we end up with something like: