CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
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 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
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 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:
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)
Data Structures Graphs. Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E)
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.
CSE 589 Applied Algorithms Course Introduction. CSE Lecture 1 - Spring Instructors Instructor –Richard Ladner –206.
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 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
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.
Minimum- Spanning Trees
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
Graph Search Applications, Minimum Spanning Tree
Instructor: Lilian de Greef Quarter: Summer 2017
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
Lecture 22 Minimum Spanning Tree
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
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
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
Hannah Tang and Brian Tjaden Summer Quarter 2002
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Richard Anderson Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
Weighted Graphs & Shortest Paths
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
CSE 332: Minimum 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:

CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007

2 A Hidden Tree Start End

3 Spanning Tree in a Graph Vertex = router Edge = link between routers Spanning tree - Connects all the vertices - No cycles

4 Undirected Graph G = (V,E) –V is a set of vertices (or nodes) –E is a set of unordered pairs of vertices V = {1,2,3,4,5,6,7} E = {{1,2},{1,6},{1,5},{2,7},{2,3}, {3,4},{4,7},{4,5},{5,6}} 2 and 3 are adjacent 2 is incident to edge {2,3}

5 Spanning Tree Problem Input: An undirected graph G = (V,E). G is connected. Output: T contained in E such that –(V,T) is a connected graph –(V,T) has no cycles

6 Spanning Tree Algorithm ST(i: vertex) mark i; for each j adjacent to i do if j is unmarked then Add {i,j} to T; ST(j); end{ST} Main T := empty set; ST(1); end{Main}

7 Example of Depth First Search ST(1)

8 Example Step ST(1) ST(2) {1,2}

9 Example Step ST(1) ST(2) ST(7) {1,2} {2,7}

10 Example Step ST(1) ST(2) ST(7) ST(5) {1,2} {2,7} {7,5}

11 Example Step ST(1) ST(2) ST(7) ST(5) ST(4) {1,2} {2,7} {7,5} {5,4}

12 Example Step ST(1) ST(2) ST(7) ST(5) ST(4) ST(3) {1,2} {2,7} {7,5} {5,4} {4,3}

13 Example Step ST(1) ST(2) ST(7) ST(5) ST(4) ST(3) {1,2} {2,7} {7,5} {5,4} {4,3}

14 Example Step ST(1) ST(2) ST(7) ST(5) ST(4) {1,2} {2,7} {7,5} {5,4} {4,3}

15 Example Step ST(1) ST(2) ST(7) ST(5) ST(4) {1,2} {2,7} {7,5} {5,4} {4,3}

16 Example Step ST(1) ST(2) ST(7) ST(5) {1,2} {2,7} {7,5} {5,4} {4,3}

17 Example Step ST(1) ST(2) ST(7) ST(5) ST(6) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

18 Example Step ST(1) ST(2) ST(7) ST(5) ST(6) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

19 Example Step ST(1) ST(2) ST(7) ST(5) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

20 Example Step ST(1) ST(2) ST(7) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

21 Example Step ST(1) ST(2) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

22 Example Step ST(1) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

23 Best Spanning Tree Each edge has the probability that it won’t fail Find the spanning tree that is least likely to fail

24 Example of a Spanning Tree Probability of success =.85 x.95 x.89 x.95 x 1.0 x.84 =.5735

25 Minimum Spanning Trees Given an undirected graph G=(V,E), find a graph G’=(V, E’) such that: –E’ is a subset of E –|E’| = |V| - 1 –G’ is connected – is minimal Applications: wiring a house, power grids, Internet connections G’ is a minimum spanning tree.

26 Minimum Spanning Tree Problem Input: Undirected Graph G = (V,E) and a cost function C from E to the reals. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes

27 Find the MST Your Turn A C B D F H G E

28 Two Different Approaches Prim’s Algorithm Almost identical to Dijkstra’s Kruskals’s Algorithm Completely different!

29 Prim’s algorithm Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight. G v known

30 Prim’s Algorithm for MST A node-based greedy algorithm Builds MST by greedily adding nodes 1.Select a node to be the “root” mark it as known Update cost of all its neighbors 2.While there are unknown nodes left in the graph a.Select an unknown node b with the smallest cost from some known node a b.Mark b as known c.Add (a, b) to MST d.Update cost of all nodes adjacent to b

31 Find MST using Prim’s v4v4 v7v7 v2v2 v3v3 v5v5 v6v6 v1v1 Start with V VKwnDistancepath v1 v2 v3 v4 v5 v6 v7 Your Turn Order Declared Known: V1V1

32 Prim’s Algorithm Analysis Running time: Same as Dijkstra’s:O(|E| log |V|) Correctness: Proof is similar to Dijkstra’s

33 Kruskal’s MST Algorithm Idea: Grow a forest out of edges that do not create a cycle. Pick an edge with the smallest weight. G=(V,E) v

34 Kruskal’s Algorithm for MST An edge-based greedy algorithm Builds MST by greedily adding edges 1.Initialize with empty MST all vertices marked unconnected all edges unmarked 2.While there are still unmarked edges a.Pick the lowest cost edge (u,v) and mark it b.If u and v are not already connected, add (u,v) to the MST and mark u and v as connected to each other Doesn’t it sound familiar?

35 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

36 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

37 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

38 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

39 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

40 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

41 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

42 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

43 Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

44 Example of Kruskal 8, {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

45 Data Structures for Kruskal Sorted edge list Disjoint Union / Find –Union(a,b) - union the disjoint sets named by a and b –Find(a) returns the name of the set containing a {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

46 Example of DU/F {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} Find(5) = 7 Find(4) = 7

47 Example of DU/F {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} Find(1) = 1 Find(6) = 7

48 Example of DU/F {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} Union(1,7)

49 Kruskal’s Algorithm with DU / F Sort the edges by increasing cost; Initialize A to be empty; for each edge {i,j} chosen in increasing order do u := Find(i); v := Find(j); if not(u = v) then add {i,j} to A; Union(u,v);

50 Kruskal code void Graph::kruskal(){ int edgesAccepted = 0; DisjSet s(NUM_VERTICES); while (edgesAccepted < NUM_VERTICES – 1){ e = smallest weight edge not deleted yet; // edge e = (u, v) uset = s.find(u); vset = s.find(v); if (uset != vset){ edgesAccepted++; s.unionSets(uset, vset); } 2|E| finds |V| unions |E| heap ops

51 Find MST using Kruskal’s A C B D FH G E Your Turn Total Cost: Now find the MST using Prim’s method. Under what conditions will these methods give the same result?

52 Kruskal’s Algorithm: Correctness It clearly generates a spanning tree. Call it T K. Suppose T K is not minimum: Pick another spanning tree T min with lower cost than T K Pick the smallest edge e 1 =(u,v) in T K that is not in T min T min already has a path p in T min from u to v  Adding e 1 to T min will create a cycle in T min Pick an edge e 2 in p that Kruskal’s algorithm considered after adding e 1 (must exist: u and v unconnected when e 1 considered)  cost(e 2 )  cost(e 1 )  can replace e 2 with e 1 in T min without increasing cost! Keep doing this until T min is identical to T K  T K must also be minimal – contradiction!

53 Reducing Best to Minimum Let P(e) be the probability that an edge doesn’t fail. Define: Minimizing is equivalent to maximizing because