Minimum Spanning Trees

Slides:



Advertisements
Similar presentations
Great Theoretical Ideas in Computer Science for Some.
Advertisements

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 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Minimum Spanning Tree Algorithms
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.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
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 Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
University of Texas at Arlington Srikanth Vadada Kishan Kumar B P Fall CSE 5311 Solving Travelling Salesman Problem for Metric Graphs using MST.
SPANNING TREES Lecture 21 CS2110 – Spring
Minimum Spanning Trees
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
1 Chapter 4 Minimum Spanning Trees Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Minimum Spanning Trees
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Great Theoretical Ideas in Computer Science
Minimum Spanning Trees and Shortest Paths
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Graph Algorithm.
Discrete Mathematics for Computer Science
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
Spanning Trees.
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Graphs.
Minimum-Cost Spanning Tree
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Spanning Trees Lecture 20 CS2110 – Spring 2015.
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Topological Sorting Minimum Spanning Trees Shortest Path
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Minimum Spanning Trees CSE 373 Data Structures

CSE 373 AU 05 - Minimum Spanning Trees Given (connected) graph G(V,E), a spanning tree T(V’,E’): Is a subgraph of G; that is, V’  V, E’  E. Spans the graph (V’ = V) Forms a tree (no cycle); So, E’ has |V| -1 edges 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Minimum Spanning Trees Edges are weighted: find minimum cost spanning tree Applications Find cheapest way to wire your house Find minimum cost to send messages on the Internet 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Strategy for Minimum Spanning Tree For any spanning tree T, inserting an edge enew not in T creates a cycle But Removing any edge eold from the cycle gives back a spanning tree If enew has a lower cost than eold we have progressed! 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Strategy Strategy for construction: Add an edge of minimum cost that does not create a cycle (greedy algorithm) Repeat |V| -1 times Correct since if we could replace an edge with one of lower cost, the algorithm would have picked it up 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Two Algorithms Prim: (build tree incrementally) Pick lower cost edge connected to known (incomplete) spanning tree that does not create a cycle and expand to include it in the tree Kruskal: (build forest that will finish as a tree) Pick lowest cost edge not yet in a tree that does not create a cycle. Then expand the set of included edges to include it. (It will be somewhere in the forest.) 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm A B C D F E 10 1 5 8 3 6 2 4 Starting from empty T, choose a vertex at random and initialize V = {A}, E’ ={} G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm A Choose the vertex u not in V such that edge weight from u to a vertex in V is minimal (greedy!) V={A,C} E’= {(A,C) } 10 5 1 3 8 B C D 1 1 6 4 2 F E G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm Repeat until all vertices have been chosen Choose the vertex u not in V such that edge weight from v to a vertex in V is minimal (greedy!) V= {A,C,D} E’= {(A,C),(C,D)} V={A,C,D,E} E’={(A,C),(C,D),(D,E)} …. V={A,C,D,E,B,F,G} E’={(A,C),(C,D),(D,E), . (E,B),(B,F),(E,G)} A B C D F E 10 1 5 8 3 6 2 4 G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm A B C D F E 10 1 5 8 3 6 2 4 G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm A B C D F E 10 1 5 8 3 6 2 4 G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm A B C D F E 10 1 5 8 3 6 2 4 G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm A B C D F E 10 1 5 8 3 6 2 4 G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Prim’s algorithm Repeat until all vertices have been chosen Final Cost: 1 + 3 + 4 + 1 + 1 + 6 = 16 A B C D F E 10 1 5 8 3 6 2 4 G 6 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Prim’s Algorithm Implementation Assume adjacency list representation Initialize connection cost of each node to “inf” and set “unvisited” Choose one node, say v and set cost[v] = 0 and prev[v] =0 While they are unmarked nodes Select the unvisited node u with minimum cost; mark it For each unvisited node w adjacent to u if weight(u,w) < cost(w) then cost(w) := weight (u,w) prev[w] = u How is this different from Dijkstra’s alg? 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Prim’s algorithm Analysis If the “Select the unmarked node u with minimum cost” is done with binary heap then O((|V|+|E|)log |V|) 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Interlude: Some easy problems Topological sort Shortest path 2-coloring (bipartite?) Network flow (max flow, min cut) Minimum spanning tree Planarity Eulerian path (visit each edge) 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Eulerian Path Visit all edges (bridges) exactly once Vertices w/ odd degrees must be endpoints A path can’t have more than two endpoints 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Some hard problems 2-SAT (not a graph problem per se) Subset sum (not a graph problem per se) Traveling Salesman Problem (TSP) Graph isomorphism Vertex coloring Vertex covering Independent set Hamiltonian path (visit each vertex) 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Easy & Hard Problems complexity class P: decision problem computed in polynomial time complexity class NP: a solution is verifiable in polynomial time complexity class NP-complete: if C in NP-C and C in P, then P=NP Does P=NP??? 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Reducing hard problems Is it possible to have a fast solution to X? No, if I could use X to solve Y, and Y is proven to be slow. Suppose I were told that Hamiltonian path is a hard problem. We can prove that TSP is also a hard problem, because TSP w/ edges all weighted 1 solves Hamiltonian. 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Function/Optimization problems vs. Decision problems Find the smallest number N such that… Is there an N such that… Formulate function problem through a binary search of decision problems.. Is there an N such that.. N/2.. N/4.. 3N/8.. 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Primality and Factoring Polynomial: Is a number prime? Hard: What are the factors of an n-bit #? Hard: Does n-bit # have a factor w/ less than m bits? How hard? Current best alg is exponential.. Good, for encryption. But there’s no proof it’s NP-C… it could be “easier” than TSP. Quantum computer could factor in poly-time 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

What if you need to solve a hard problem quickly? Is n small enough that brute force is fine? Can you add assumptions/heuristics? Is an approximate solution good enough? e.g. for TSP, when triangle inequality holds, MST provides a factor-2 approx 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Kruskal’s Algorithm Select edges in order of increasing cost Accept an edge to expand tree or forest only if it does not cause a cycle Implementation using adjacency list, priority queues and disjoint sets 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Kruskal’s Algorithm Initialize a forest of trees, each tree being a single node Build a priority queue of edges with priority being lowest cost Repeat until |V| -1 edges have been accepted { Deletemin edge from priority queue If it forms a cycle then discard it else accept the edge – It will join 2 existing trees yielding a larger tree and reducing the forest by one tree } The accepted edges form the minimum spanning tree 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Detecting Cycles If the edge to be added (u,v) is such that vertices u and v belong to the same tree, then by adding (u,v) you would form a cycle Therefore to check, Find(u) and Find(v). If they are the same discard (u,v) If they are different Union(Find(u),Find(v)) 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Properties of trees in K’s algorithm Vertices in different trees are disjoint True at initialization and Union won’t modify the fact for remaining trees Trees form equivalent classes under the relation “is connected to” u connected to u (reflexivity) u connected to v implies v connected to u (symmetry) u connected to v and v connected to w implies a path from u to w so u connected to w (transitivity) 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

K’s Algorithm Data Structures Adjacency list for the graph To perform the initialization of the data structures below Disjoint Set ADT’s for the trees (recall Up tree implementation of Union-Find) Binary heap for edges 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Example A 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Initialization A Initially, Forest of 6 trees F= {{A},{B},{C},{D},{E},{F}} Edges in a heap (not shown) 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Step 1 A Select edge with lowest cost (B,E) Find(B) = B, Find(E) = E Union(B,E) F= {{A},{B,E},{C},{D},{F}} 1 edge accepted 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Step 2 A Select edge with lowest cost (B,F) Find(B) = B, Find(F) = F Union(B,F) F= {{A},{B,E,F},{C},{D}} 2 edges accepted 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Step 3 A Select edge with lowest cost (A,C) Find(A) = A, Find (C) = C Union(A,C) F= {{A,C},{B,E,F},{D}} 3 edges accepted 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Step 4 A Select edge with lowest cost (E,F) Find(E) = B, Find (F) = B Do nothing F= {{A,C},{B,E,F},{D}} 3 edges accepted 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Step 5 A Select edge with lowest cost (C,D) Find(C) = A, Find (D) = D Union(A,D) F= {{A,C,D},{B,E,F}} 4 edges accepted 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

CSE 373 AU 05 - Minimum Spanning Trees Step 6 Select edge with lowest cost (D,E) Find(D) = A, Find (E) = B Union(A,B) F= {{A,C,D,B,E,F}} 5 edges accepted : end Total cost = 10 Although there is a unique spanning tree in this example, this is not generally the case A 10 5 1 8 3 B C D 6 1 1 4 2 F E 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Kruskal’s Algorithm Analysis Initialize forest O(n) Initialize heap O(m), m = |E| Loop performed m times In the loop one deleteMin O(log m) Two Find, each O(log n) One Union (at most) O(1) So worst case O(m log m) = O(m log n) 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees

Time Complexity Summary Recall that m = |E| = O(V2) = O(n2 ) Prim’s runs in O((|V|+|E|) log |V|) Kruskal’s runs in: O(|V| log |E|) = O(|V| log |V|) In practice, Kruskal has a tendency to run faster since graphs might not be dense and not all edges need to be looked at in the deleteMin operations 11/29/2018 CSE 373 AU 05 - Minimum Spanning Trees