Minimum Spanning Tree What is a Minimum Spanning Tree.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Spanning Trees
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. Prim’s Algorithm. –Animated Example. –Implementation. Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Testing for Connectedness and Cycles
1 Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs) What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a.
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
Introduction to Graphs
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graph Traversals Depth-First Traversal. The Algorithm.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. What is a Minimum-Cost Spanning Tree. Prim’s Algorithm. –Animated.
Design and Analysis of Algorithms Minimum Spanning trees
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
Chapter 2 Graph Algorithms.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Introduction to Graph Theory
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
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.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms:
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Minimum Spanning Trees
Minimum Spanning Tree What is a Minimum Spanning Tree.
Shortest Path Algorithm
Minimum Spanning Tree Chapter 13.6.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Minimum Spanning Trees
Graphs Chapter 13.
Testing for Connectedness and Cycles
Kruskal’s Algorithm for finding a minimum spanning tree
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Networks Kruskal’s Algorithm
Fundamental Structures of Computer Science II
Shortest Path Algorithm
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Shortest Path Algorithm
Presentation transcript:

Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing Minimum Spanning Trees. What is a Minimum-Cost Spanning Tree. Applications of Minimum Cost Spanning Trees. Prim’s Algorithm. Example. Implementation. Kruskal’s algorithm. Review Questions. COSC 301: Data Structures

What is a Minimum Spanning Tree. Let G = (V, E) be a simple, connected, undirected graph that is not edge-weighted. A spanning tree of G is a free tree (i.e., a tree with no root) with | V | - 1 edges that connects all the vertices of the graph. Thus a minimum spanning tree for G is a graph, T = (V’, E’) with the following properties: V’ = V T is connected T is acyclic. A spanning tree is called a tree because every acyclic undirected graph can be viewed as a general, unordered tree. Because the edges are undirected, any vertex may be chosen to serve as the root of the tree. COSC 301: Data Structures

Constructing Minimum Spanning Trees Any traversal of a connected, undirected graph visits all the vertices in that graph. The set of edges which are traversed during a traversal forms a spanning tree. For example, Fig:(b) shows the spanning tree obtained from a breadth-first traversal starting at vertex b. Similarly, Fig:(c) shows the spanning tree obtained from a depth-first traversal starting at vertex c. (a) Graph G (b) Breadth-first spanning tree of G rooted at b (c) Depth-first spanning tree of G rooted at c COSC 301: Data Structures

What is a Minimum-Cost Spanning Tree For an edge-weighted , connected, undirected graph, G, the total cost of G is the sum of the weights on all its edges. A minimum-cost spanning tree for G is a minimum spanning tree of G that has the least total cost. Example: The graph Has 16 spanning trees. Some are: The graph has two minimum-cost spanning trees, each with a cost of 6: COSC 301: Data Structures

Applications of Minimum-Cost Spanning Trees Minimum-cost spanning trees have many applications. Some are: Building cable networks that join n locations with minimum cost. Building a road network that joins n cities with minimum cost. Obtaining an independent set of circuit equations for an electrical network. In pattern recognition minimal spanning trees can be used to find noisy pixels. COSC 301: Data Structures

Prim’s Algorithm Prim’s algorithm finds a minimum cost spanning tree by selecting edges from the graph one-by-one as follows: It starts with a tree, T, consisting of the starting vertex, x. Then, it adds the shortest edge emanating from x that connects T to the rest of the graph. It then moves to the added vertex and repeats the process. Consider a graph G=(V, E); Let T be a tree consisting of only the starting vertex x; while (T has fewer than IVI vertices) { find a smallest edge connecting T to G-T; add it to T; } COSC 301: Data Structures

Example Trace Prim’s algorithm starting at vertex a: The resulting minimum-cost spanning tree is: COSC 301: Data Structures

Implementation of Prim’s Algorithm. Prims algorithn can be implememted similar to the Dijskra’s algorithm as shown below: public static Graph primsAlgorithm(Graph g, Vertex start){ int n = g.getNumberOfVertices(); Entry table[] = new Entry[n]; for(int v = 0; v < n; v++) table[v] = new Entry(); table[g.getIndex(start)].distance = 0; PriorityQueue queue = new BinaryHeap(g.getNumberOfEdges()); queue.enqueue(new Association(new Integer(0), start)); while(!queue.isEmpty()) { Association association = (Association)queue.dequeueMin(); Vertex v1 = (Vertex) association.getValue(); int n1 = g.getIndex(v1); if(!table[n1].known){ table[n1].known = true; Iterator p = v1.getEmanatingEdges(); while (p.hasNext()){ Edge edge = (Edge) p.next(); Vertex v2 = edge.getMate(v1); int n2 = g.getIndex(v2); Integer weight = (Integer) edge.getWeight(); int d = weight.intValue(); COSC 301: Data Structures

Implementation of Prim’s Algorithm Cont'd if(!table[n2].known && table[n2].distance > d){ table[n2].distance = d; table[n2].predecessor = v1; queue.enqueue(new Association(new Integer(d), v2)); } GraphAsLists result = new GraphAsLists(false); Iterator it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex) it.next(); result.addVertex(v.getLabel()); it = g.getVertices(); if (v != start){ int index = g.getIndex(v); String from = v.getLabel(); String to = table[index].predecessor.getLabel(); result.addEdge(from, to, new Integer(table[index].distance)); return result; COSC 301: Data Structures

Kruskal's Algorithm. Kruskal’s algorithm also finds the minimum cost spanning tree of a graph by adding edges one-by-one. enqueue edges of G in a queue in increasing order of cost. T =  ; while(queue is not empty){ dequeue an edge e; if(e does not create a cycle with edges in T) add e to T; } return T; COSC 301: Data Structures

Example for Kruskal’s Algorithm. Trace Kruskal's algorithm in finding a minimum-cost spanning tree for the undirected, weighted graph given below: The minimum cost is: 24 COSC 301: Data Structures

Implementation of Kruskal's Algorithm public static Graph kruskalsAlgorithm(Graph g){ Graph result = new GraphAsLists(false); Iterator it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex)it.next(); result.addVertex(v.getLabel()); } PriorityQueue queue = new BinaryHeap(g.getNumberOfEdges()); it = g.getEdges(); while(it.hasNext()){ Edge e = (Edge) it.next(); if (e.getWeight()==null) throw new IllegalArgumentException("Graph is not weighted"); queue.enqueue(e); while (!queue.isEmpty()){ Edge e = (Edge) queue.dequeueMin(); String from = e.getFromVertex().getLabel(); String to = e.getToVertex().getLabel(); if (!result.isReachable(from, to)) result.addEdge(from,to,e.getWeight()); return result; adds an edge only, if it does not create a cycle COSC 301: Data Structures

Implementation of Kruskal's Algorithm – Cont’d public abstract class AbstractGraph implements Graph { public boolean isReachable(String from, String to){ Vertex fromVertex = getVertex(from); Vertex toVertex = getVertex(to); if (fromVertex == null || toVertex==null) throw new IllegalArgumentException("Vertex not in the graph"); PathVisitor visitor = new PathVisitor(toVertex); this.preorderDepthFirstTraversal(visitor, fromVertex); return visitor.isReached(); } private class PathVisitor implements Visitor { boolean reached = false; Vertex target; PathVisitor(Vertex t){target = t;} public void visit(Object obj){ Vertex v = (Vertex) obj; if (v.equals(target)) reached = true; public boolean isDone(){return reached;} boolean isReached(){return reached;} COSC 301: Data Structures

Prim’s and Kruskal’s Algorithms Note: It is not necessary that Prim's and Kruskal's algorithm generate the same minimum-cost spanning tree. For example for the graph: Kruskal's algorithm (that imposes an ordering on edges with equal weights) results in the following minimum cost spanning tree: The same tree is generated by Prim's algorithm if the start vertex is any of: A, B, or D; however if the start vertex is C the minimum cost spanning tree is: COSC 301: Data Structures

Review Questions GB Find the breadth-first spanning tree and depth-first spanning tree of the graph GA shown above. For the graph GB shown above, trace the execution of Prim's algorithm as it finds the minimum-cost spanning tree of the graph starting from vertex a. Repeat question 2 above using Kruskal's algorithm. COSC 301: Data Structures