Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Similar documents
Spanning Trees. Lecture 22 CS2110 Spring 2017

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Spanning Trees. Lecture 20 CS2110 Spring 2015

SPANNING TREES. Lecture 21 CS2110 Spring 2016

Undirected Graphs. Hwansoo Han

Spanning trees. Suppose you have a connected undirected graph

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

23.2 Minimum Spanning Trees

Lecture 6 Basic Graph Algorithms

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Chapter 9 Graph Algorithms

Introduction to Optimization

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Chapter 9 Graph Algorithms

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

The Algorithms of Prim and Dijkstra. Class 19

Chapter 9 Graph Algorithms

Homework Assignment #3 Graph

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Recitation 9. Prelim Review

CSE 100: GRAPH ALGORITHMS

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

CS1 Lecture 31 Apr. 3, 2019

Outline. Graphs. Divide and Conquer.

L22-23: Graph Algorithms

CS 206 Introduction to Computer Science II

Minimum Spanning Trees Ch 23 Traversing graphs

CS : Data Structures

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

GRAPHS Lecture 17 CS2110 Spring 2014

Data Structures Brett Bernstein

Design and Analysis of Algorithms

Graph Traversals. CS200 - Graphs 1

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

15 211: RECITATION7, SECTION M. Hui Han Chin,

CSC 421: Algorithm Design & Analysis. Spring 2015

Trees Algorhyme by Radia Perlman

Total Score /1 /20 /41 /15 /23 Grader


Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

Minimum Spanning Trees

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

CS4800: Algorithms & Data Jonathan Ullman

GRAPHS Lecture 19 CS2110 Spring 2013

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Info 2950, Lecture 16

LECTURE 26 PRIM S ALGORITHM

tree follows. Game Trees

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Priority queue ADT part 1

Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications

CS270 Combinatorial Algorithms & Data Structures Spring Lecture 19:

Lecture 25 Spanning Trees

Minimum Spanning Trees My T. UF

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Week 11: Minimum Spanning trees

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

Minimum Spanning Trees. CSE 373 Data Structures

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Unit 2: Algorithmic Graph Theory

val(y, I) α (9.0.2) α (9.0.3)

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

Graph Algorithms (part 3 of CSC 282),

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

Graphs - II CS 2110, Spring 2016

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Section 08: Solutions

Algorithm Design (8) Graph Algorithms 1/2

CS 310 Advanced Data Structures and Algorithms

1 Minimum Spanning Trees (MST) b 2 3 a. 10 e h. j m

Trees. Arash Rafiey. 20 October, 2015

Artificial Intelligence (Heuristic Search)

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Algorithm Design Techniques. Hwansoo Han

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

CS 6783 (Applied Algorithms) Lecture 5

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Lecture 20: Other Algorithms on Graphs. Undirected Trees. Spanning Trees

Graphs. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. What is a Graph? b d f h j

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)

Tree. number of vertices. Connected Graph. CSE 680 Prof. Roger Crawfis

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

CSE 100: GRAPH SEARCH

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Module 11: Additional Topics Graph Theory and Applications

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Transcription:

/9/7 Prelim, assignments Prelim is Tuesday. See the course webpage for details. Scope: up to but not including today s lecture. See the review guide for details. Deadline for submitting conflicts has passed. A was due last night. Late penalty points per day, up to days. No exceptions the solution is used in A7! Lecture CS0 Spring 07 Spanning Trees A7 due next Thursday /7. It s short; 0-0 lines including comments. Do it before the prelim and it doubles as studying Dijkstra! A Note on Dijkstra S F Far off w f w. For s, d[s] is length of shortest v s path.. For f, d[f] is length of shortest v f path of form f. Edges leaving S go to F. Theorem: For a node f in F with min d value, d[f] is its shortest path length S= { ; F= { v ; d[v]= 0; while F { { f= node in F with min d value; Remove f from F, add it to S; for each neighbor w of f { if (w not in S or F) { d[w]= d[f] + wgt(f, w); add w to F; else if (d[f] + wgt (f,w) < d[w]) { d[w]= d[f] + wgt(f, w); Undirected trees An undirected graph is a tree if there is exactly one simple path between any pair of What s the root? It doesn t matter! Any vertex can be root. #E = #V connected no cycles Any two of these properties imply the third and thus imply that the graph is a tree Facts about trees Spanning trees A spanning tree of a connected undirected graph (V, E) is a subgraph (V, E') that is a tree Same set of V E' E (V, E') is a tree Same set of V Maximal set contains no cycle Same set of V Three equivalent definitions

/9/7 Spanning trees: examples Finding a spanning tree: Subtractive method Start with the whole graph it is connected While there is a cycle: Pick an edge of a cycle and throw it out the graph is still connected (why?) One step of the algorithm Maximal set of edges that contains no cycle nondeterministic algorithm http://mathworld.wolfram.com/spanningtree.html 7 Aside: How can you find a cycle in an undirected graph? Aside: How can you find a cycle in an undirected graph? /** Visit all nodes REACHABLE* from u. Pre: u is unvisited.. */ public static void dfs(int u) { Stack s= (u); while (s is not empty) { u= s.pop(); if (u has not been visited) { visit u; for each edge (u, v) leaving u: s.push(v); Aside: How can you find a cycle in an undirected graph? /** true if the nodes reachable from u have a cycle. */ public static boolean hascycle(int u) { Stack s= (u); while (s is not empty) { u= s.pop(); if (u has been visited) { return true; else { visit u; for each edge (u, v) leaving u: s.push(v); return false; Finding a spanning tree: Subtractive method Start with the whole graph it is connected While there is a cycle: Pick an edge of a cycle and throw it out the graph is still connected (why?) One step of the algorithm Maximal set of edges that contains no cycle nondeterministic algorithm

/9/7 Finding a spanning tree: Additive method Aside: How do you find connected components? Start with no edges While the graph is not connected: Choose an edge that connects connected components and add it the graph still has no cycle (why?) nondeterministic algorithm Tree edges will be red. Dashed lines show original edges. Left tree consists of connected components, each a node Aside: How do you find connected components? /** Visit all nodes REACHABLE* from u. Pre: u is unvisited.. */ public static void dfs(int u) { Stack s= (u); while (s is not empty) { u= s.pop(); if (u has not been visited) { visit u; for each edge (u, v) leaving u: s.push(v); Aside: How do you find connected components? /** Return the set of nodes in u s connected component. */ public static Set<int> getcomponent(int u) { Stack s= (u); Set C = (); while (s is not empty) { u= s.pop(); if (u has not been visited) { visit u; C.add(u); for each edge (u, v) leaving u: s.push(v); return C; Finding a spanning tree: Additive method Spanning trees: examples Start with no edges While the graph is not connected: Choose an edge that connects connected components and add it the graph still has no cycle (why?) nondeterministic algorithm Tree edges will be red. Dashed lines show original edges. Left tree consists of connected components, each a node http://mathworld.wolfram.com/spanningtree.html 8

/9/7 9 Minimum spanning trees Suppose edges are weighted (> 0) We want a spanning tree of minimum cost (sum of edge weights) Some graphs have exactly one minimum spanning tree. Others have several trees with the same minimum cost, each of which is a minimum spanning tree Useful in network routing & other applications. For example, to stream a video Greedy algorithm A greedy algorithm follows the heuristic of making a locally optimal choice at each stage, with the hope of finding a global optimum. Example. Make change using the fewest number of coins. Make change for n cents, n < 00 (i.e. < $) Greedy: At each step, choose the largest possible coin If n >= 0 choose a half dollar and reduce n by 0; If n >= choose a quarter and reduce n by ; As long as n >= 0, choose a dime and reduce n by 0; If n >=, choose a nickel and reduce n by ; Choose n pennies. 0 Greedy algorithm doesn t always work! Greediness doesn t work here A greedy algorithm follows the heuristic of making a locally optimal choice at each stage, with the hope of finding a global optimum. Doesn t always work Example. Make change using the fewest number of coins. Coins have these values: 7,, Greedy: At each step, choose the largest possible coin Consider making change for 0. The greedy choice would choose: 7,,,. But, is only coins. You re standing at point x, and your goal is to climb the highest mountain. Two possible steps: down the hill or up the hill. The greedy step is to walk up hill. But that is a local optimum choice, not a global one. Greediness fails in this case. x Finding a minimal spanning tree Suppose edges have > 0 weights Minimal spanning tree: sum of weights is a minimum We show two greedy algorithms for finding a minimal spanning tree. They are versions of the basic additive method we have already seen: at each step add an edge that does not create a cycle. At each step, add an edge (that does not form a cycle) with minimum weight edge with weight MST using Kruskal s algorithm edge with weight Kruskal: add an edge with minimum weight. Can have a forest of trees. Prim (JPD): add an edge with minimum weight but so that the added edges (and the nodes at their ends) form one tree One of the s The Red edges need not form tree (until end)

/9/7 Kruskal Start with the all the nodes and no edges, so there is a forest of trees, each of which is a single node (a leaf). At each step, add an edge (that does not form a cycle) with minimum weight We do not look more closely at how best to implement Kruskal s algorithm which data structures can be used to get a really efficient algorithm. Leave that for later courses, or you can look them up online yourself. MST using Prim s algorithm (should be called JPD algorithm ) Developed in 90 by Czech mathematician Vojtěch Jarník. Práce Moravské Přírodovědecké Společnosti,, 90, pp. 7. (in Czech) Developed in 97 by computer scientist Robert C. Prim. Bell System Technical Journal, (97), pp. 89 0 Developed about 9 by Edsger Dijkstra and published in in 99. Numerische Mathematik, 9 7 (99) We now investigate Prim s algorithm At each step, add an edge (that does not form a cycle) with minimum weight, but keep added edge connected to the start (red) node edge with weight One of the s Prim s algorithm The edge with weight Difference between Prim and Kruskal Prim requires that the constructed red tree always be connected. Kruskal doesn t But: Both algorithms find a minimal spanning tree Here, Prim chooses (0, ) Kruskal chooses (, ) 0 Here, Prim chooses (0, ) Kruskal chooses (, ) 0 Difference between Prim and Kruskal Prim requires that the constructed red tree always be connected. Kruskal doesn t But: Both algorithms find a minimal spanning tree Difference between Prim and Kruskal Prim requires that the constructed red tree always be connected. Kruskal doesn t But: Both algorithms find a minimal spanning tree Here, Prim chooses (0, ) Kruskal chooses (, ) 0 Here, Prim chooses (0, ) Kruskal chooses (, ) 0 If the edge weights are all different, the Prim and Kruskal algorithms construct the same tree.

/9/7 Given: graph (V, E) (sets of and edges) Output: tree (V, E), where V = V E is a subset of E (V, E) is a minimal spanning tree sum of edge weights is minimal V= {an arbitrary node of V; E= {; //inv: (V, E) is a tree, V V, E E while (V.size() < V.size()) { Pick an edge (u,v) with: min weight, u in V, v not in V; Add v to V; Add edge (u, v) to E Consider having a set S of edges with the property: If (u, v) an edge with u in V and v not in V, then (u,v) is in S V: red nodes E: red edge S: edges leaving red nodes V= {an arbitrary node of V; E= {; //inv: (V, E) is a tree, V V, E E while (V.size() < V.size()) { Pick an edge (u,v) with: min weight, u in V, v not in V; Add v to V; Add edge (u, v) to E Consider having a set S of edges with the property: If (u, v) an edge with u in V and v not in V, then (u,v) is in S V: red nodes E: red edges S: edges leaving red nodes V= {an arbitrary node of V; E= {; //inv: (V, E) is a tree, V V, E E while (V.size() < V.size()) { Pick an edge (u,v) with: min weight, u in V, v not in V; Add v to V; Add edge (u, v) to E Consider having a set S of edges with the property: If (u, v) an edge with u in V and v not in V, then (u,v) is in S V: red nodes E: red edges S: edges leaving red nodes Note: the edge with weight is not in in S this avoids cycles V= {an arbitrary node of V; E= {; //inv: (V, E) is a tree, V V, E E S= set of edges leaving the single node in V; while (V.size() < V.size()) { Pick ----------------------------- an edge (u,v) with: Remove from S an edge ----------------------------- min weight, u in V, (u, v) with min weight ----------------- v not in V; -------------------- Add v to V; Add -------------------------- edge (u, v) to E if v is not in V: add v to V; add (u,v) to E; add edges leaving v to S Consider having a set S of edges with the property: If (u, v) an edge with u in V and v not in V, then (u,v) is in S V= {start node; E= {; S= set of edges leaving the single node in V; //inv: (V, E) is a tree, V V, E E, // All edges (u, v) in S have u in V, // if edge (u, v) has u in V and v not in V, (u, v) is in S while (V.size() < V.size()) { Remove from S an edge (u, v) with min weight; if (v not in V) { add v to V; add (u,v) to E; add edges leaving v to S Question: How should we implement set S?

/9/7 V= {start node; E= {; S= set of edges leaving the single node in V; //inv: (V, E) is a tree, V V, E E, // All edges (u, v) in S have u in V, // if edge (u, v) has u in V and v not in V, (u, v) is in S while (V.size() < V.size()) { Remove from S a min-weight edge (u, v); if (v not in V) { add v to V; add (u,v) to E; add edges leaving v to S #V #E log #E log #E Implement S as a heap. Use adjacency lists for edges Thought: Could we use for S a set of nodes instead of edges? Yes. We don t go into that here 7 Application of minimum spanning tree Maze generation using Prim s algorithm The generation of a maze using Prim's algorithm on a randomly weighted grid graph that is 0x0 in size. https://en.wikipedia.org/wiki/maze_generation_algorithm #Randomized_Kruskal.7s_algorithm 8 Graph algorithms MEGA-POLL! In this undirected graph, all edge weights are. Which of the following visit the nodes in the same order as Prim()? Always break ties by choosing the lower-numbered node first. In tree traversals, use node as the tree s root. -Dijkstra() -BFS() -DFS() -Preorder tree traversal -Postorder tree traversal -Level order tree traversal 8 7 9 0 Greedy algorithms Suppose the weights are all. Then Dijkstra s shortest-path algorithm does a breath-first search! Dijkstra s and Prim s algorithms look similar. The steps taken are similar, but at each step Dijkstra s chooses an edge whose end node has a minimum path length from start node Prim s chooses an edge with minimum length Breadth-first search, Shortest-path, Prim Greedy algorithm: An algorithm that uses the heuristic of making the locally optimal choice at each stage with the hope of finding the global optimum. Dijkstra s shortest-path algorithm makes a locally optimal choice: choosing the node in the Frontier with minimum L value and moving it to the Settled set. And, it is proven that it is not just a hope but a fact that it leads to the global optimum. Similarly, Prim s and Kruskal s locally optimum choices of adding a minimum-weight edge have been proven to yield the global optimum: a minimum spanning tree. BUT: Greediness does not always work! while (a vertex is unmarked) { v= best unmarked vertex mark v; for (each w adj to v) update D[w]; c(v,w) is the v w edge weight Similar code structures Breadth-first-search (bfs) best: next in queue update: D[w] = D[v]+ Dijkstra s algorithm best: next in priority queue update: D[w] = min(d[w], D[v]+c(v,w)) Prim s algorithm best: next in priority queue update: D[w] = min(d[w], c(v,w)) 7

/9/7 Traveling salesman problem Given a list of cities and the distances between each pair, what is the shortest route that visits each city exactly once and returns to the origin city? The true TSP is very hard (called NP complete) for this we want the perfect answer in all cases. Most TSP algorithms start with a spanning tree, then evolve it into a TSP solution. Wikipedia has a lot of information about packages you can download But really, how hard can it be? How many paths can there be that visit all of 0 cities?,,9,9,,07,70,8,89,07,7,7,08,,8,,77,70,000,000,000 Graph Algorithms Search Depth-first search Breadth-first search Shortest paths Dijkstra's algorithm Minimum spanning trees Prim's algorithm Kruskal's algorithm 8