Fundamental Structures of Computer Science II

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
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. 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.
Dijkstra's algorithm.
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.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
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 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Minimum- Spanning Trees
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Short paths and spanning trees
Minimum Spanning Trees
Minimum Spanning Tree.
MST - Prim’s Algorithm Greedy algorithm that “grows” an MST by repeatedly finding the lowest cost edge that will connect a new vertex to MST For every.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE 373 Data Structures and Algorithms
Data Structures – LECTURE 13 Minumum spanning trees
Outline This topic covers Prim’s algorithm:
Kruskal’s Minimum Spanning Tree Algorithm
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Fundamental Structures of Computer Science II
Graph Algorithms: Shortest Path
CMSC 341 Graphs 2.
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Finding Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Algorithm Course Dr. Aref Rashad
CSE 332: Minimum Spanning Trees
Chapter 9: Graphs Spanning Trees
Presentation transcript:

Fundamental Structures of Computer Science II Graph Algorithms – 3 CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Minimum Spanning Tree Problem: Finding a minimum spanning tree in an undirected and connected graph. What is minimum spanning tree? A tree that covers (spans) all the vertices of a connected graph that has the minimum total cost of edges in the tree. A minimum spanning tree exists for a graph if and only if the graph is connected. The same problem makes sense for directed graphs also, we the solution is more difficult. CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Example 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 An undirected, connected Graph 8 4 6 5 v6 1 v7 2 v1 v2 The corresponding minimum spanning tree 1 2 v3 v4 v5 4 6 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Minimum Spanning Tree (MST) If the number of vertices of a connected undirected graph is |V|, then its minimum spanning tree will have |V| vertices |V| - 1 edges. An MST does not contain any cycles, since it is a tree. If we add an extra edge to an MST, then it will have a cycle. CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Minimum Spanning Tree (MST) Greedy approach for finding an MST for a graph works! Given a graph G, we start with an initial one vertex MST. At each stage we add one more vertex and one more edge (that connects this vertex to the previous MST), so that the edge has minimum possible value. CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II MST Applications power outlet or light Electrical wiring of a house using minimum amount of wires (cables) CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II MST Applications CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II MST Applications CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Graph Representation CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Minimum Spanning Tree for electrical eiring CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II MST Algorithms We will see two algorithms Prim’s Algorithm Kruskal’s Algorithm CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm MST is grown in successive stages. At each stage: A new vertex is added to the tree by choosing the edge (u,v) such that the cost of (u,v) is the smallest among all edges where u is in the tree and v is not. CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Prim’s Algorithm Start with vertex v1. It is the initial current tree which we will grow to an MST 2 v1 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 A connected, undirected graph G is given above. CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Prim’s Algorithm Step 1 Select an edge from graph: that is not in the current tree, that has the minimum cost, and that can be connected to the current tree. Step 1 2 v1 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Prim’s Algorithm Step 1 The edges that can be connected are: (v1,v2): cost 2 (v1,v4): cost 1 (v1,v3): cost 2 Step 1 2 v1 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Prim’s Algorithm Step 1 The edge that has the minimum cost is: (v1,v4): cost 1 (there could be more than one. In that case we could choose of of them randomly) Step 1 2 v1 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Prim’s Algorithm We include the vertex v4, that is connected to the selected edge, to the current tree. In this way we grow the tree. Step 2 2 v1 v1 v2 10 4 1 3 2 7 v3 v4 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Prim’s Algorithm Repeat previous steps: 1, 2 You can add either edge (v1, v2) or (v1, v3). Do a random tie-break. Lets add edge (v1, v2) 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v4 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Current tree grows! 2 v1 v1 v2 v2 4 1 3 10 2 v3 v4 v4 7 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Repeat steps: 1, and 2 Add either edge (v4, v3) 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Grow the tree! 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 4 6 5 8 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Add edge (v4, v7) 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 4 6 5 8 v6 1 v7 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Grow the tree! 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 4 6 5 8 v6 1 v7 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Add edge (v7, v6) 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 4 6 5 8 v6 v6 1 v7 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Grow the tree! 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 8 4 6 5 v6 v6 1 v7 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Add edge (v7, v5) 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 v5 8 4 6 5 v6 v6 1 v7 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Prim’s Algorithm Grow the tree! 2 v1 v1 v2 v2 10 4 1 3 2 7 v3 v3 v4 v4 v5 v5 8 4 6 5 v6 v6 1 v7 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Prim’s Algorithm Finished! The resulting MST is shown below! 2 v1 v1 4 6 v6 v6 1 v7 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Algorithm Implementation Very similar to Dijkstra’s shortest path algorithm. Both are greedy type of algorithms For each vertex v, we keep the following information: Known/unknown Whether we have included the vertex in current tree or not. Distance to previous node (dv): the cost of the edge that is connecting v to a known vertex that is part of current tree. Previous vertex (pv) The last known vertex, that causes a change in the value of dv CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Initial configuration of table used in Prim’s Algorithm implementation Vertex known dv pv v1 F v2 ∞ v3 v4 v5 v6 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 F v2 ∞ v3 v4 v5 v6 v7 Initial Configuration 2 F, dv:0 , pv:0 v1 v2 F, dv:∞ , pv:0 10 4 1 3 F, dv:∞ , pv:0 F, dv:∞ , pv:0 2 7 v3 v4 v5 F, dv:∞ , pv:0 8 4 6 5 v6 1 v7 F, dv:∞ , pv:0 F, dv:∞ , pv:0 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 F 2 v3 4 v4 1 v5 ∞ v6 v7 After v1 is declared known 2 T, dv:0 , pv:0 v1 v1 v2 F, dv:2 , pv:v1 10 4 1 3 F, dv:4 , pv:v1 F, dv:∞ , pv:0 2 7 v3 v4 v5 F, dv:1 , pv:v1 8 4 6 5 v6 1 v7 F, dv:∞ , pv:0 F, dv:∞ , pv:0 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 F 2 v3 v4 1 v5 7 v6 8 v7 4 After v4 is declared known 2 T, dv:0 , pv:0 v1 v1 v2 F, dv:2 , pv:v1 10 4 1 3 F, dv:2 , pv:v4 F, dv:7 , pv:v4 2 7 v3 v4 v4 v5 T, dv:1 , pv:v1 8 4 6 5 v6 1 v7 F, dv:8 , pv:v4 F, dv:4 , pv:v4 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 2 v3 F v4 1 v5 7 v6 8 v7 4 After v2 is declared known 2 T, dv:0 , pv:0 v1 v1 v2 v2 T, dv:2 , pv:v1 10 4 1 3 F, dv:2 , pv:v4 F, dv:7 , pv:v4 2 7 v3 v4 v4 v5 T, dv:1 , pv:v1 8 4 6 5 v6 1 v7 F, dv:8 , pv:v4 F, dv:4 , pv:v4 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 2 v3 v4 1 v5 F 7 v6 5 v7 4 After v3 is declared known 2 T, dv:0 , pv:0 v1 v1 v2 v2 T, dv:2 , pv:v1 10 4 1 3 T, dv:2 , pv:v4 F, dv:7 , pv:v4 2 7 v3 v3 v4 v4 v5 T, dv:1 , pv:v1 8 4 6 5 v6 1 v7 F, dv:5, pv:v3 F, dv:4 , pv:v4 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 2 v3 v4 1 v5 F 6 v7 v6 4 After v7 is declared known 2 T, dv:0 , pv:0 v1 v1 v2 v2 T, dv:2 , pv:v1 10 4 1 3 T, dv:2 , pv:v4 F, dv:6 , pv:v7 2 7 v3 v3 v4 v4 v5 T, dv:1 , pv:v1 8 4 6 5 v6 1 v7 v7 F, dv:1, pv:v7 T, dv:4 , pv:v4 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 2 v3 v4 1 v5 F 6 v7 v6 4 After v6 is declared known 2 T, dv:0 , pv:0 v1 v1 v2 v2 T, dv:2 , pv:v1 10 4 1 3 T, dv:2 , pv:v4 F, dv:6 , pv:v7 2 7 v3 v3 v4 v4 v5 T, dv:1 , pv:v1 8 4 6 5 v6 v6 1 v7 v7 T, dv:1, pv:v7 T, dv:4 , pv:v4 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 2 v3 v4 1 v5 6 v7 v6 4 After v5 is declared known 2 T, dv:0 , pv:0 v1 v1 v2 v2 T, dv:2 , pv:v1 10 4 1 3 T, dv:2 , pv:v4 T, dv:6 , pv:v7 2 7 v3 v3 v4 v4 v5 v5 T, dv:1 , pv:v1 8 4 6 5 v6 v6 1 v7 v7 T, dv:1, pv:v7 T, dv:4 , pv:v4 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Vertex known dv pv v1 T v2 2 v3 v4 1 v5 6 v7 v6 4 From the Table, read the edges of MST 2 T, dv:0 , pv:0 v1 v1 v2 v2 T, dv:2 , pv:v1 10 4 1 3 T, dv:2 , pv:v4 T, dv:6 , pv:v7 2 7 v3 v3 v4 v4 v5 v5 T, dv:1 , pv:v1 8 4 6 5 v6 v6 1 v7 v7 T, dv:1, pv:v7 T, dv:4 , pv:v4 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II void Graph::find_prim_mst( vector<Vertex> &s /* initial vertex */) { Vertex v, w; s.dist = 0; s.known = T; for ( ; ; ) v = an unknown vertex whose distance value is minimum. if (v == NOT_A_VERTEX) break; // we are finished v.known = TRUE; for each w adjacent to v if (w.known == FALSE) { if (cost_v_w < w.dist) { w.dist = cost_v_w; w.path = v; } cost_v_w is the cost of edge from vertex v to w. class Vertex { boolean known; // T or F int dist; // dv Vertex path; //pv } CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II void Graph::print_prim_mst() { Vertex v, w; for (each vertex v in G) w = v.path; print edge (v,w); } The output is the set of edges in the spanning tree. CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Running Time We execute the outer for loop at most |V| times (for each vertex). In each iteration we try to find the unknown node that have the minimum distance: O(|V|) We execute the inner for loop O(|E|) times. Therefore the running time of the algorithm in the given form is: O(|E| + |V|2) CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Running Time The given bound is good if the graph is dense: In a dense graph |E| = Ө(|V|2) In that case, the running time O(|V|2) which is O(|E|) Therefore the algorithm is very efficient in this case. CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Running Time The algorithm in the given form is not good if the graph is dense. It is inefficient. The running time can be improved if we use priority queue (binary heap). CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Running Time If we use priority queue of vertices The vertex with minimum distance is kept at the root of the heap. The outer loop is executed O(|V|) times. The search inside the outer for loop for a vertex that has the minimum distance takes O(log|V|) time. The inner loop is executed O(|E|) times. The distances can be updates O(|E|) times. The distance value of a vertex can be updated using decreaseKey() operation of binary heaps, which works in O(|V|) time. (|V| is the size of the binary heap). CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Running Time Therefore the running time of MST algorithm using priority queues is: O(|V| x log(|V|)+ |E| x log(|V|)) = O(|E| x log(|V|)) If |E| is O(|V|) then Running time is O(|V| x log(|V|) ) This is for sparse graphs. Compare this with the running time of the original algorithm (the one that does not use priority queues) for sparse graphs, which is O(|V|2) CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Kruskal’s Algorithm Select edges in the order of smallest weights and accept an edge if it does not cause a cycle. Kruskal’s algoritm maintains a forest of trees. Initially each vertex is a tree with single node There are |V| trees. Then, adding an accepted edge merges two trees in the forest When algorithm terminates, there is a single tree with |V| vertices and it is a minimum spanning tree. . CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Initial Forest 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Initial Forest Step 1 Candidate edges are shown (edges that have low cost and edges that connect two trees) Step 1 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Initial Forest Step 2 Accept one of the candidate edges: (v1, v4) (we can do random accept here). 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Initial Forest Step 3 Merge the two trees connected by that edge. Obtain a new tree in this way. 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Repeat previous steps! Edge (v6-v7) is accepted. 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Merge the two trees connected by that edge! 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Accept edge (v1, v2) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Merge the two trees connected by that edge! 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Accept edge (v3, v4) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Merge the two trees connected by that edge! 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Accept edge (v4, v7) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Merge the two trees connected by that edge! 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Accept edge (v7, v5) 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II Merge the two trees connected by that edge! 2 v1 v2 10 4 1 3 2 7 v3 v4 v5 8 4 6 5 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

The resulting MST is shown below! Finished! The resulting MST is shown below! 2 v1 v2 1 2 v3 v4 v5 4 6 v6 1 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University

Fundamental Structures of Computer Science II void Graph::kruskal() { int edgesAccepted; DisjSet s(NUM_VERTICES); PriorityQueue h(NUM_EDGES); Vertex u, v; SetType uset, vset; Edge e; h = readGraphIntoHeapArray(); h.buildHeap(); edgesAccepted = 0; while (edgesAccepted < NUM_VERTICES – 1) { h.deleteMin(e); // Edge e = (u,v) uset = s.find(u); vset = s.find(v); if (uset != vset) // Accept the edge edgesAccepted++ s.unionSets (uset,m vset); } CS 202, Spring 2003 Fundamental Structures of Computer Science II © Bilkent University