Spanning trees. Suppose you have a connected undirected graph

Similar documents
Homework Assignment #3 Graph

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 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

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

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Lecture 25 Spanning Trees

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

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

Info 2950, Lecture 16

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

CS4800: Algorithms & Data Jonathan Ullman

Graph Search Methods. Graph Search Methods

Graph Search Methods. Graph Search Methods

Undirected Graphs. Hwansoo Han

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Minimum Spanning Trees

tree follows. Game Trees

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

Minimum Spanning Trees

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

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

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Trees Algorhyme by Radia Perlman

Lecture Notes on Spanning Trees

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

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

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

Algorithms: Lecture 10. Chalmers University of Technology

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Lecture 25 Notes Spanning Trees

Minimum-Cost Spanning Tree. Example

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees

Minimum Spanning Trees My T. UF

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

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

MST worksheet By Jim Xu

Spanning Trees. Lecture 20 CS2110 Spring 2015

Outline. Graphs. Divide and Conquer.

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

CSE 100: GRAPH ALGORITHMS

SPANNING TREES. Lecture 21 CS2110 Spring 2016

Lecture 34 Fall 2018 Wednesday November 28

CS 206 Introduction to Computer Science II

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

3.1 Basic Definitions and Applications

22 Elementary Graph Algorithms. There are two standard ways to represent a

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

22 Elementary Graph Algorithms. There are two standard ways to represent a

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

國立清華大學電機工程學系. Outline

Minimum spanning trees

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Announcements. HW4 due Friday. Summer 2016 CSE373: Data Structures & Algorithms 1

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

6.1 Minimum Spanning Trees

Graph Traversals. CS200 - Graphs 1

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

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

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Week 11: Minimum Spanning trees

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

lecture29: Shortest Path Algorithms

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

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

L22-23: Graph Algorithms

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

Introduction: (Edge-)Weighted Graph

CS 310 Advanced Data Structures and Algorithms

Week 12: Minimum Spanning trees and Shortest Paths

Graph Algorithms (part 3 of CSC 282),

Copyright 2000, Kevin Wayne 1

Priority Queues. 04/10/03 Lecture 22 1

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

(Re)Introduction to Graphs and Some Algorithms

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Minimum Spanning Trees Ch 23 Traversing graphs

Chapter 23. Minimum Spanning Trees

CSE 373: Data Structures and Algorithms

Searching in Graphs (cut points)

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Total Score /15 /20 /30 /10 /5 /20 Grader

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

Minimum Spanning Trees and Shortest Paths

Minimum Spanning Trees

Agenda. Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

1. O(log n), because at worst you will only need to downheap the height of the heap.

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Tree Construction (BFS, DFS, MST) Chapter 5. Breadth-First Spanning Tree (BFS)

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

CS/COE

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Minimum Spanning Trees. CSE 373 Data Structures

Transcription:

Spanning Trees

Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges do not have an associated direction...then a spanning tree of the graph is a connected subgraph in which there are no cycles A connected, undirected graph Four of the spanning trees of the graph 2

Finding a spanning tree To find a spanning tree of a graph, pick an initial node and call it part of the spanning tree do a search from the initial node: each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it An undirected graph One possible result of a BFS starting from top One possible result of a DFS starting from top 3

Minimizing costs Suppose you want to supply a set of houses (say, in a new subdivision) with: electric power water sewage lines telephone lines To keep costs down, you could connect these houses with a spanning tree (of, for example, power lines) However, the houses are not all equal distances apart To reduce costs even further, you could connect the houses with a minimum-cost spanning tree 4

Minimum-cost spanning trees Suppose you have a connected undirected graph with a weight (or cost) associated with each edge The cost of a spanning tree would be the sum of the costs of its edges A minimum-cost spanning tree is a spanning tree that has the lowest cost A E 16 21 11 19 5 F 33 14 18 B D 6 C 10 A connected, undirected graph A E 16 11 F 18 B D C A minimum-cost spanning tree 5 6 5

Kruskal s algorithm T = empty spanning tree; E = set of edges; N = number of nodes in graph; while T has fewer than N - 1 edges { remove an edge (v, w) of lowest cost from E if adding (v, w) to T would create a cycle then discard (v, w) else add (v, w) to T } Finding an edge of lowest cost can be done just by sorting the edges Efficient testing for a cycle requires a fairly complex algorithm (UNION-FIND) which we don t cover in this course 6

Prim s algorithm T = a spanning tree containing a single node s; E = set of edges adjacent to s; while T does not contain all the nodes { } remove an edge (v, w) of lowest cost from E if w is already in T then discard edge (v, w) else { } add edge (v, w) and node w to T add to E the edges adjacent to w An edge of lowest cost can be found with a priority queue Testing for a cycle is automatic Hence, Prim s algorithm is far simpler to implement than Kruskal s algorithm 7

Mazes Typically, Every location in a maze is reachable from the starting location There is only one path from start to finish If the cells are vertices and the open doors between cells are edges, this describes a spanning tree Since there is exactly one path between any pair of cells, any cells can be used as start and finish This describes a spanning tree 8

Mazes as spanning trees While not every maze is a spanning tree, most can be represented as such The nodes are places within the maze There is exactly one cyclefree path from any node to any other node 9

Building a maze I This algorithm requires two sets of cells the set of cells already in the spanning tree, IN the set of cells adjacent to the cells in the spanning tree (but not in it themselves), called the FRONTIER Start with all walls present Pick any cell and put it into IN (red) Put all adjacent cells, that aren t in IN, into FRONTIER (blue) 10

Building a maze II Repeatedly do the following: Remove any one cell C from FRONTIER and put it in IN Erase the wall between C and some one adjacent cell in IN Add to FRONTIER all the cells adjacent to C that aren t in IN (or in FRONTIER already) Continue until there are no more cells in FRONTIER When the maze is complete (or at any time), choose the start and finish cells 11

Depth First Path-finding getpath(cell P, Cell Q, Maze M) Stack S = new Stack() // to record the path Set V = new Set() // to record visited cells Set H = new Set() // to store neighbors S.push(P) // start from P if (P!=Q) found = false else return while ( S.size() > 0 &&!found) Cell C = S.pop() V.add(C) H = neighbors(c,m) V if (H.size() > 0) S.push(C) N = H.first() S.push(N) If (N==Q) found = true return S 12

The End 13