Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms:

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
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.
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
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
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
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.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Design and Analysis of Algorithms Minimum Spanning trees
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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.
Minimum Spanning Tree By Jonathan Davis.
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.
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.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Prim's Algorithm This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph to the new graph, each time.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Minimum- Spanning Trees
Contest Algorithms January 2016 Introduce the main kinds of graphs, discuss two implementation approaches, and remind you about trees 6. Intro. to Graphs.
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
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 CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Search Applications, Minimum Spanning Tree
Introduction to Algorithms
May 12th – Minimum Spanning Trees
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Kruskal’s Minimum Spanning Tree Algorithm
Minimum Spanning Tree.
Lecture 12 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Chapter 9: Graphs Spanning Trees
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 .
Presentation transcript:

Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms: 8. MSTs

A minimum spanning tree T is a subgraph of a weighted graph G which contains all the verticies of G and whose edges have the minimum summed weight. Example weighted graph G: 1. Minimum Spanning Tree

A minimum spanning tree (weight = 12): A non-minimum spanning tree (weight = 20):

Another Example (fig 4.10, CP) Contest Algorithms: 8 MSTs4

Prim's algorithm finds a minimum spanning tree T by iteratively adding edges to T. At each iteration, a minimum-weight edge is added that does not create a cycle in the current T. The new edge must be connected to a vertex which is already in T. The algorithm can stop after |V|-1 edges have been added to T. 2. Prim's Algorithm a greedy algorithm (see later) O(E log E)

For the graph G. 1) Add any vertex to T  e.g 0,T = {0} 2) Examine all the edges leaving {0} and add the vertex with the smallest weight.  edgeweight (0,1) 4 (0,2) 2 (0,4) 3  add edge (0,2), T becomes {0,2} Informal Algorithm continued

3) Examine all the edges leaving {0,2} and add the vertex with the smallest weight.  edgeweightedgeweight (0,1) 4(2,3) 1 (0,4) 3(2,4) 6 (2,5) 3  add edge (2,3), T becomes {0,2,3} continued

4) Examine all the edges leaving {0,2,3} and add the vertex with the smallest weight.  edgeweightedgeweight (0,1) 4(3,1) 5 (0,4) 3(2,4) 6 (2,5) 3 (3,5) 6  add edge (0,4) or (2,5), it does not matter  add edge (0,4), T becomes {0,2,3,4} continued

5) Examine all the edges leaving {0,2,3,4} and add the vertex with the smallest weight.  edgeweightedgeweight (0,1) 4(3,1) 5 (2,5) 3(3,5) 6 (4,5) 2  add edge (4,5), T becomes {0,2,3,4,5} continued

6) Examine all the edges leaving {0,2,3,4,5} and add the vertex with the smallest weight.  edgeweightedgeweight (0,1) 4(3,1) 5  add edge (0,1), T becomes {0,1,2,3,4,5} All the verticies of G are now in T, so we stop. continued

Resulting minimum spanning tree (weight = 12):

 no. of vertices, no. of edges  triple of numbers: one triple is an edge (u v) and weight w which must be recorded twice in the list: once for u --> v, once for v --> u e.g Input Data Format Contest Algorithms: 8 MSTs12

 An adjacency list for a undirected, unweighted graph.  A priority queue of (weight,u, v) triples in increasing weight order.  A boolean array to make if a vertex has been visited. private static ArrayList > adjList; private static PriorityQueue pq; private static boolean[] visited; Main Data Structures for Prim's Contest Algorithms: 8 MSTs13

public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java Prim "); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int numEs = sc.nextInt(); adjList = new ArrayList<>(); for (int i = 0; i < numVs; i++) { ArrayList neighbors = new ArrayList (); adjList.add(neighbors); } for (int i = 0; i < numEs; i++) { int u = sc.nextInt(); int v = sc.nextInt(); int weight = sc.nextInt(); adjList.get(u).add( new IPair(v, weight)); // u --> v adjList.get(v).add( new IPair(u, weight)); // v --> u } : Code Contest Algorithms: 8 MSTs14 see Prim.java

pq = new PriorityQueue (); // pri-queue of (weight,u,v) triples visited = new boolean[numVs]; // will contain all falses by default queueNeighbors(0); // queue neighbors of vertex 0 int mstCost = 0; while (!pq.isEmpty()) { // repeat until all vertices have been visited ITriple sEdge = pq.peek(); // (weight, u, v) pq.poll(); int v = sEdge.getZ(); if (!visited[v]) { // this vertex not in mst mstCost += sEdge.getX(); System.out.println("Added " + sEdge); queueNeighbors(v); // take v, queue all edges incident to v } System.out.println("MST cost: " + mstCost); } // end of main() Contest Algorithms: 8 MSTs15

private static void queueNeighbors(int v) /* queue all edges incident to v in the priority queue, and marked v as visited */ { visited[v] = true; ArrayList neighbors = adjList.get(v); // list of (vertex,weight) pairs for (IPair p : neighbors) { if (!visited[p.getX()]) pq.offer( new ITriple(p.getY(), v, p.getX())); // (weight,v, neighbour) triple } } // end of queueNeighbors() Contest Algorithms: 8 MSTs16

Example 1 Contest Algorithms: 8 MSTs17 see mstData.java

Execution Contest Algorithms: 8 MSTs18 (weight, u, v)

Example 2 Contest Algorithms: 8 MSTs19 see mstData1.java

Execution Contest Algorithms: 8 MSTs20 (weight, u, v)

 A disjoint-set data structure keeps track of a set of elements split into disjoint (non-overlapping) subsets.  Union-find consists of two main operations:  findSet (): report which subset a particular element is in  unionSet (): join two subsets into a single subset  others: makeSets(), isConnected(), etc. 3. Union-find see UnionFind.java

Using UnionFind Contest Algorithms: 8 MSTs unionSet(0, 1) unionSet(2, 3) see UseUnionFind.java

Contest Algorithms: 8 MSTs23 unionSet(4, 3) unionSet(0, 3)

 Krukal's algorithm finds a minimum spanning tree T by iteratively adding edges to T.  At each iteration, a minimum-weight edge is added that does not create a cycle in the current T.  The new edge can come from anywhere in G.  The algorithm can stop after |V|-1 edges have been added to T. 4. Kruskal's Algorithm a greedy algorithm (see later) O(E log E), same as Prim's

Graph G: Informal Algorithm iteredge 1(2, 3) 2(10, 11) 3(1, 5) 4(2, 6) 5(0, 1) 6(5, 9) 7(1, 2) 8(9, 10) 9(6, 7) 10(8, 9) 11(0, 4) continued

Minimum spanning tree (weight = 24):

 no. of vertices, no. of edges  triple of numbers: one triple is an edge (u v) and weight w which must be recorded twice in the list: once for u --> v, once for v --> u e.g Input Data Format Contest Algorithms: 8 MSTs27 Same as for Prim.java

 An adjacency list for a undirected, unweighted graph.  same as for Prim's algorithm  An edges list containing (weight, u, v) triples.  Use UnionFind to containing the growing sub-trees that eventually join together to create the MST. ArrayList > adjList; ArrayList edgesList; UnionFind uf = new UnionFind(numVs); Main Data Structures for Kruskal's Contest Algorithms: 8 MSTs28

public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java Kruskal "); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int numEs = sc.nextInt(); ArrayList > adjList = new ArrayList<>(); for (int i = 0; i < numVs; i++) { ArrayList neighbors = new ArrayList (); adjList.add(neighbors); } ArrayList edgesList = new ArrayList (); for (int i = 0; i < numEs; i++) { int u = sc.nextInt(); int v = sc.nextInt(); int weight = sc.nextInt(); edgesList.add( new ITriple(weight, u, v)); adjList.get(u).add( new IPair(v, weight)); // u --> v adjList.get(v).add( new IPair(u, weight)); // v --> u } Collections.sort(edgesList); // sort into increasing weight : Code Contest Algorithms: 8 MSTs29 see Kruskal.java

int mstCost = 0; UnionFind uf = new UnionFind(numVs); // all verts are in their own disjoint sets initially for (int i = 0; i < numEs; i++) { ITriple sEdge = edgesList.get(i); // smallest edge (weight, u, v) if (!uf.inSameSet(sEdge.getY(), sEdge.getZ())) { // if u and v in separate sets System.out.println("Added " + sEdge); mstCost += sEdge.getX(); // add weight uf.unionSet(sEdge.getY(), sEdge.getZ()); // join u and v sets } // the number of sets must eventually be 1 for a valid MST System.out.println("MST cost: " + mstCost); } // end of main() Contest Algorithms: 8 MSTs30

Example 1 (again) Contest Algorithms: 8 MSTs31 see mstData.java

Execution Contest Algorithms: 8 MSTs 32 (weight, u, v) The same result, but the edge choice order is different.

Example 2 Contest Algorithms: 8 MSTs33 see mstData2.java

Execution Contest Algorithms: 8 MSTs34 (weight, u, v)

Prim's algorithm chooses an edge that must be connected to a vertex in the minimum spanning tree T. Kruskal's algorithm chooses an edge from G that may or may not be connected to a vertex in T. They have the same big-oh running times, but Kruskal's algorithm can be faster for sparse graphs. 5. Differences between Prim and Kruskal

 My version of Prim's algorithm uses a priority queue, which is implemented in Java using a binary heap.  If a Fibonacci heap is used instead, Prim's can be made faster than Kruskal's.  Prims only gives a MST if the graph is connected.  Kruskal will can handle a disconnected graph, outputting a Minimum Spanning forest Contest Algorithms: 8 MSTs36