1 Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs) What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Spanning Trees
Advertisements

Chapter 5: Tree Constructions
Lecture 15. Graph Algorithms
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.
Greed is good. (Some of the time)
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
1 Section 9.4 Spanning Trees. 2 Let G be a simple graph. A spanning subtree of G is a subgraph of G containing every vertex of G –must be connected; contains.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
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:
Testing for Connectedness and Cycles
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Introduction to Graphs
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
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.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
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.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
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.
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.
Minimum Spanning Tree What is a Minimum Spanning Tree.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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.
Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example.
Minimum-Cost Spanning Tree
Spanning Trees.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Outline This topic covers Prim’s algorithm:
Minimum-Cost Spanning Tree
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Minimum Spanning Tree.
Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Shortest Path Algorithm
Presentation transcript:

1 Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs) What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a Minimum-Cost Spanning Tree (MCST) ? Applications of Minimum Cost Spanning Trees. MCST Algorithms: –Kruskal’s Algorithm. Tracing Kruskal’s Algorithm Implementation. –Prim’s algorithm. Tracing Prim’s Algorithm Implementation. Review Questions.

2 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 T = (V’, E’) for G is a subgraph of G with the following properties:  V’ = V  T is connected  T is acyclic (  |E’| = |V| - 1) 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.

3 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 a spanning tree of G obtained from a breadth-first traversal starting at vertex a. Similarly, Fig (c) shows a spanning tree of G obtained from a depth-first traversal starting at vertex c. (a) Graph G (b) Breadth-first spanning tree of G starting from a (c) Depth-first spanning tree of G starting from c

4 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:

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

6 MCST Algorithms: Kruskal's Algorithm Kruskal’s algorithm finds the minimum cost spanning tree of a graph by adding edges one-by-one to an initially empty free tree 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; Running time is O(E log V)

7 Tracing 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

8 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()){ // test if there is a path from vertex // from to vertex to before adding the edge 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

9 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;} }

10 MCST Algorithms: Prim’s Algorithm The algorithm continuously increases the size of a tree starting with a single vertex until it spans all the vertices of G: Consider a weighted, connected, undirected graph G=(V, E) and a free tree T = (V T, E T ) that is a subgraph of G Initialize: V T = {x}; // where x is an arbitrary vertex from V Initialize: E T =  ; repeat{ Choose an edge (v,w) with minimal weight such that v is in V T and w is not (if there are multiple edges with the same weight, choose arbitrarily but consistently); // add an minimal weight edge that will not form a cycle Add vertex v to V T ; add edge (v, w) to E T; }until (V T == V); Time complexity (total)Graph and Minimum edge weight data structure O(V 2 )adjacency matrix and array O((V + E) log(V)) = O(E log(V))adjacency list and binary heap O(E + V log(V))Adjacency list and Fibonacci heap Time Complexity of Prim’s Algorithm:

11 Tracing Prim’s Algorithm (Example adopted from Wikipedia) We want to find a Minimum Cost Spanning Tree for the graph on the left. The numbers near the edges indicate the edge weights. Vertex D has been arbitrarily chosen as a starting point. Vertices A, B, E and F are connected to D through a single edge. A is the vertex nearest to D and will be chosen as the second vertex along with the edge AD. The next vertex chosen is the vertex nearest to either D or A. B is 9 away from D and 7 away from A, E is 15, and F is 6. F is the smallest distance away, so we highlight the vertex F and the arc DF.

12 Tracing Prim’s Algorithm (Cont’d) The algorithm carries on as above. Vertex B, which is 7 away from A, is highlighted. In this case, we can choose between C, E, and G. C is 8 away from B, E is 7 away from B, and G is 11 away from F. E is nearest, so we highlight the vertex E and the arc BE. Here, the only vertices available are C and G. C is 5 away from E, and G is 9 away from E. C is chosen, so it is highlighted along with the arc EC.

13 Tracing Prim’s Algorithm (Cont’d) Vertex G is the only remaining vertex. It is 11 away from F, and 9 away from E. E is nearer, so we highlight it and the arc EG. Now all the vertices have been selected and the minimum cost spanning tree is shown in green. In this case, it has weight 39.

14 Tracing Prim’s Algorithm: An alternative method Trace Prim’s algorithm starting at vertex a: The resulting minimum-cost spanning tree is: Note: Current Active vertex is vertex in the row with minimum weight from the previous column, v1 is the active vertex in the column where weight appears for the first time

15 Implementation of Prim’s Algorithm using MinHeap The implementation of Prim's algorithm uses an Entry class, which contains the following three fields: –know: a boolean variable indicating whether the weight of the edge from this vertex to an active vertex v1 is known, initially false for all vertices. –weight : the minimum known weight from this vertex to an active vertex v1, initially infinity for all vertices except that of the starting vertex which is 0. –predecessor : the predecessor of an active vertex v1 in the MCST, initially unknown for all vertices. public class Algorithms{ static final class Entry{ boolean known; int weight; Vertex predecessor; Entry(){ known = false; weight = Integer.MAX_VALUE; predecessor = null; }

16 Implementation of Prim’s Algorithm using MinHeap (Cont’d) make a table of values (known, weight(edge), predecessor) initially (FALSE, ,null) for each vertex except the start vertex table entry is set to (FALSE, 0, null) ; MinHeap =  ; MinHeap.enqueue( Association (0, startVertex)); // let tree T be empty; while (MinHeap is not empty) { // T has fewer than n vertices dequeue an Association (weight(e), v) from the MinHeap; Update table entry for v to (TRUE, weight(e), v); // add v and e to T for( each emanating edge f = (v, u) of v){ if( table[u].known == FALSE ) // u is not already in T if (weight(f) < table[u].weight) { // find the current min edge weight table[u].weight = weight(f); table[u].predecessor = v; enqueue(Association(weight(f), u); } The idea is to use a table array to remember, for each vertex, the smallest edge connecting T with that vertex. Each entry in table represents a vertex x. The object stored at table entry index(x) will have the predecessor of x and the corresponding edge weight.

17 Implementation of Prim’s Algorithm using MinHeap (Cont’d) Prim’s algorithm can be implemented 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)].weight = 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();

18 Implementation of Prim’s Algorithm using MinHeap (Cont'd) if(!table[n2].known && d < table[n2].weight){ table[n2].weight = d; table[n2].predecessor = v1; queue.enqueue(new Association(new Integer(d), v2)); } GraphAsLists result = new GraphAsLists(false); Iterator it = g.getVertices(); //add vertices of this graph to result while (it.hasNext()){ Vertex v = (Vertex) it.next(); result.addVertex(v.getLabel()); } // add edges to result using each vertex predecessor in table it = g.getVertices(); while (it.hasNext()){// Vertex v = (Vertex) it.next(); 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; }

19 Tracing Prim’s Algorithm from the MinHeap implementation Trace Prim’s algorithm in finding a MCST starting from vertex A: Enqueue edge weights for edges emanating from A Enqueue edge weights for edges emanating from C

20 Tracing Prim’s Algorithm from the MinHeap implementation (Cont’d) The MCST:

21 Note on MCSTs generated by 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:

22 Review Questions 1.Find the breadth-first spanning tree and depth-first spanning tree of the graph G A shown above. 2.For the graph G B shown above, trace the execution of Prim's algorithm as it finds the minimum-cost spanning tree of the graph starting from vertex a. 3.Repeat question 2 above using Kruskal's algorithm. GBGB