Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 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 0 1 2 3 4 5 3 4 5 1 63 2 6 2

3 A minimum spanning tree (weight = 12): A non-minimum spanning tree (weight = 20): 0 1 2 3 4 5 3 4 5 1 63 2 2 6 0 1 2 3 4 5 3 4 5 1 63 2 6 2

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

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

6 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 0 1 2 3 4 5 3 4 5 1 63 2 6 2

7 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

8 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

9 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

10 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

11 Resulting minimum spanning tree (weight = 12): 0 1 2 3 4 5 3 4 5 1 6 3 2 2 6

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. 5 7 0 1 4 0 2 4 0 3 6 0 4 6 1 2 2 2 3 8 3 4 9 Input Data Format Contest Algorithms: 8 MSTs12

13  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

14 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

15 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

16 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

17 Example 1 Contest Algorithms: 8 MSTs17 see mstData.java 5 7 0 1 4 0 2 4 0 3 6 0 4 6 1 2 2 2 3 8 3 4 9

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

19 Example 2 Contest Algorithms: 8 MSTs19 see mstData1.java 6 9 0 1 4 0 2 2 0 4 3 1 3 5 2 3 1 2 4 6 2 5 3 3 5 6 4 5 2

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

21  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

22 Using UnionFind Contest Algorithms: 8 MSTs22 01234 unionSet(0, 1) 0 1234 unionSet(2, 3) 0 12 34 see UseUnionFind.java

23 Contest Algorithms: 8 MSTs23 unionSet(4, 3) 0 12 3 4 0 12 34 unionSet(0, 3) 0 1 2 3 4

24  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

25 Graph G: Informal Algorithm 0 12 3 4 5 6 7 8 9 10 11 2 31 3 4 3 4 2 1 3 2 3 4 3 31 3 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

26 Minimum spanning tree (weight = 24): 0 12 3 4 5 6 7 8 9 10 11 231 3 4 3 42 1 3 2 3 4 3 31 3

27  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. 5 7 0 1 4 0 2 4 0 3 6 0 4 6 1 2 2 2 3 8 3 4 9 Input Data Format Contest Algorithms: 8 MSTs27 Same as for Prim.java

28  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

29 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

30 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

31 Example 1 (again) Contest Algorithms: 8 MSTs31 see mstData.java 5 7 0 1 4 0 2 4 0 3 6 0 4 6 1 2 2 2 3 8 3 4 9

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

33 Example 2 Contest Algorithms: 8 MSTs33 see mstData2.java 12 17 0 1 2 1 2 3 2 3 1 4 5 4 5 6 3 6 7 3 8 9 3 9 10 3 10 11 1 0 4 3 4 8 4 1 5 1 5 9 2 2 6 2 6 10 4 3 7 3 7 11 3

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

35 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

36  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


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

Similar presentations


Ads by Google