Minimum Spanning Trees CSE 373 Data Structures Lecture 21.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
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.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Discussion #36 Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
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.
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.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
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 Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimum spanning tree Prof Amir Geva Eitan Netzer.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Analysis of Algorithms
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph (II) Shortest path, Minimum spanning tree GGuy
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.
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 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.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
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.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Minimum Spanning Trees and Shortest Paths
Lecture 12 Algorithm Analysis
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Minimum Spanning Trees
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
Outline This topic covers Prim’s algorithm:
Minimum-Cost Spanning Tree
Hannah Tang and Brian Tjaden Summer Quarter 2002
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
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
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Minimum Spanning Trees CSE 373 Data Structures Lecture 21

3/10/03Min. Spanning tree - Lecture 202 Recall Spanning Tree Given (connected) G(V,E) a spanning tree T(V’,E’): ›Spans the graph (V’ = V) ›Forms a tree (no cycle); E’ has |V| -1 edges

3/10/03Min. Spanning tree - Lecture 203 Minimum Spanning Tree Edges are weighted: find minimum cost spanning tree Applications ›Find cheapest way to wire your house ›Find minimum cost to send a message on the Internet

3/10/03Min. Spanning tree - Lecture 204 Strategy for Minimum Spanning Tree For any spanning tree T, inserting an edge e new not in T creates a cycle ›Removing any edge e old from the cycle gives back a spanning tree ›If e new has a lower cost than e old we have progressed!

3/10/03Min. Spanning tree - Lecture 205 Strategy Strategy: ›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

3/10/03Min. Spanning tree - Lecture 206 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 lower cost edge not yet in a tree that does not create a cycle and expand to include it somewhere in the forest

3/10/03Min. Spanning tree - Lecture 207 Prim’s algorithm Starting from empty T, choose a vertex at random and initialize V = {1), E’ ={}

3/10/03Min. Spanning tree - Lecture 208 Prim’s algorithm Choose the vertex u not in V such that edge weight from u to a vertex in V is minimal (greedy!) V={1,3} E’= {1,3)

3/10/03Min. Spanning tree - Lecture 209 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= {1,3,4} E’= {(1,3),(3,4)} V={1,3,4,5} E’={(1,3),(3,4),(4,5)} …. V={1,3,4,5,2,6} E’={(1,3),(3,4),(4,5),(5,2),(2,6)}

3/10/03Min. Spanning tree - Lecture 2010 Prim’s algorithm Repeat until all vertices have been chosen V={1,3,4,5,2,6} E’={(1,3),(3,4),(4,5),(5,2),(2,6)} Final Cost: = 10

3/10/03Min. Spanning tree - Lecture 2011 Prim’s Algorithm Implementation Assume adjacency list representation Initialize connection cost of each node to “inf” and “unmark” them Choose one node, say v and set cost[v] = 0 and prev[v] =0 While they are unmarked nodes Select the unmarked node u with minimum cost; mark it For each unmarked node w adjacent to u if cost(u,w) < cost(w) then cost(w) := cost (u,w) prev[w] = u Looks a lot like Dijkstra’s algorithm!

3/10/03Min. Spanning tree - Lecture 2012 Prim’s algorithm Analysis Like Dijkstra’s algorithm If the “ Select the unmarked node u with minimum cost ” is done with binary heap then O((n+m)logn)

3/10/03Min. Spanning tree - Lecture 2013 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

3/10/03Min. Spanning tree - Lecture 2014 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

3/10/03Min. Spanning tree - Lecture 2015 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))

3/10/03Min. Spanning tree - Lecture 2016 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)

3/10/03Min. Spanning tree - Lecture 2017 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

3/10/03Min. Spanning tree - Lecture 2018 Example

3/10/03Min. Spanning tree - Lecture 2019 Initialization Initially, Forest of 6 trees F= {{1},{2},{3},{4},{5},{6}} Edges in a heap (not shown)

3/10/03Min. Spanning tree - Lecture 2020 Step 1 Select edge with lowest cost (2,5) Find(2) = 2, Find (5) = 5 Union(2,5) F= {{1},{2,5},{3},{4},{6}} 1 edge accepted 1

3/10/03Min. Spanning tree - Lecture 2021 Step 2 Select edge with lowest cost (2,6) Find(2) = 2, Find (6) = 6 Union(2,6) F= {{1},{2,5,6},{3},{4}} 2 edges accepted 1 1

3/10/03Min. Spanning tree - Lecture 2022 Step 3 Select edge with lowest cost (1,3) Find(1) = 1, Find (3) = 3 Union(1,3) F= {{1,3},{2,5,6},{4}} 3 edges accepted 1 1 1

3/10/03Min. Spanning tree - Lecture 2023 Step 4 Select edge with lowest cost (5,6) Find(5) = 2, Find (6) = 2 Do nothing F= {{1,3},{2,5,6},{4}} 3 edges accepted 1 1 1

3/10/03Min. Spanning tree - Lecture 2024 Step 5 Select edge with lowest cost (3,4) Find(3) = 1, Find (4) = 4 Union(1,4) F= {{1,3,4},{2,5,6}} 4 edges accepted

3/10/03Min. Spanning tree - Lecture 2025 Step 6 Select edge with lowest cost (4,5) Find(4) = 1, Find (5) = 2 Union(1,2) F= {{1,3,4,2,5,6}} 5 edges accepted : end Total cost = 10 Although there is a unique spanning tree in this example, this is not generally the case

3/10/03Min. Spanning tree - Lecture 2026 Kruskal’s Algorithm Analysis Initialize forest O(n) Initialize heap O(m), m = |E| Loop performed m times ›In the loop one Deletemin O(logm) ›Two Find, each O(logn) ›One Union (at most) O(1) So worst case O(mlogm) = O(mlogn)

3/10/03Min. Spanning tree - Lecture 2027 Time Complexity Summary Recall that m = |E| = O(V 2 ) = O(n 2 ) Prim’s runs in O((n+m) log n) Kruskal’s runs in O(mlogm) = O(mlogn) 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