Spanning Trees, greedy algorithms

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
Graphs - II CS 2110, Spring Where did I leave that book?
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
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:
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.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
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.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
COSC 2007 Data Structures II Chapter 14 Graphs III.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Minimum Spanning Tree: Prim’s & Kruskal’s Algorithms Presenter: Michal Karpinski.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
SPANNING TREES Lecture 21 CS2110 – Spring
SPANNING TREES, INTRO. TO THREADS Lecture 23 CS2110 – Fall
Graph Search Applications, Minimum Spanning Tree
Graphs Lecture 19 CS2110 – Spring 2013.
Graphs Representation, BFS, DFS
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
Minimum Spanning Trees
Minimal Spanning Trees
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Minimum Spanning Trees and Shortest Paths
CISC 235: Topic 10 Graph Algorithms.
Spanning Trees, greedy algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
Short paths and spanning trees
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Algorithm.
Graph Search Lecture 17 CS 2110 Spring 2018.
CSCE350 Algorithms and Data Structure
Graphs Representation, BFS, DFS
Minimum Spanning Tree.
Graphs Chapter 13.
Spanning Trees.
Minimum-Cost Spanning Tree
Graphs.
Chapter 11 Graphs.
Minimum Spanning Trees
Minimum Spanning Tree.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Prims’ spanning tree algorithm
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:

Spanning Trees, greedy algorithms 1 HEY, SIGN UP FOR LUNCH. LOTS OF OPENINGS Spanning Trees, greedy algorithms Lecture 21 CS2110 – Spring 2019

About A6, Prelim 2 Prelim 2: Thursday, 23 April. Visit exams page of course website and read carefully to find out when you take it (5:30 or 7:30) and what to do if you have a conflict. Time assignments the opposite of Prelim 1! HEY, SIGN UP FOR LUNCH. LOTS OF OPENINGS

JavaHyperText material Look at entries spanning This one gives you a .zip file that contains a Java app to create random mazes. greedy We go over two algorithms for generating a spanning tree of a graph. You have to know them only at a high level of abstraction.

Amortized time Visit JavaHyperText, put “amort” into Filter Field, read the first ---one-page--- pdf file. In A5 –Heap.java, insert takes amortized time O(1). WHAT DOES THAT MEAN?

Undirected trees An undirected graph is a tree if there is exactly one simple path between any pair of vertices What’s the root? It doesn’t matter! Any vertex can be root.

Facts about trees #E = #V – 1 connected no cycles Tree with #V = 1, #E = 0 #E = #V – 1 connected no cycles Tree with #V = 3, #E = 2 Any two of these properties imply the third and thus imply that the graph is a tree

Facts about trees #E = #V – 1 connected no cycles Any two of these properties imply the third and thus imply that the graph is a tree

Spanning trees A spanning tree of a connected undirected graph (V, E) is a subgraph (V, E') that is a tree Same set of vertices V E' ⊆ E (V, E') is a tree Same set of vertices V Maximal set of edges that contains no cycle There are three equivalent conditions. The second and third ones each leads to a class of spanning-tree algorithms. Same set of vertices V Minimal set of edges that connect all vertices Three equivalent definitions

Spanning trees: examples http://mathworld.wolfram.com/SpanningTree.html

Finding a spanning tree: Subtractive method Start with the whole graph – it is connected Maximal set of edges that contains no cycle While there is a cycle: Pick an edge of a cycle and throw it out – the graph is still connected (why?) nondeterministic algorithm One step of the algorithm

Aside: Test whether an undirected graph has a cycle /** Visit all nodes reachable along unvisited paths from u. * Pre: u is unvisited. */ public static void dfs(int u) { Stack s= (u); // inv: All nodes to be visited are reachable along an // unvisited path from a node in s. 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); } We modify iterative dfs to calculate whether the graph has a cycle

Aside: Test whether an undirected graph has a cycle /** Return true if the nodes reachable from u have a cycle. */ public static boolean hasCycle(int u) { Stack s= (u); // inv: All nodes to be visited are reachable along an // unvisited path from a node in s. while (s is not empty) { u= s.pop(); if (u has been visited) visit u; for each edge (u, v) leaving u { s.push(v); } return true; return false;

Finding a spanning tree: Subtractive method Start with the whole graph – it is connected Maximal set of edges that contains no cycle While there is a cycle: Pick an edge of a cycle and throw it out – the graph is still connected (why?) How many edges have to be discarded? One step of the algorithm Starts with e edges Get down to n-1 edge. Throw out e – (n-1) edges. Dense graph? Throw out O(n*n) edges! In general, too many to throw out! Don’t use!

Finding a spanning tree: Additive method Start with no edges Minimal set of edges that connect all vertices While the graph is not connected: Choose an edge that connects 2 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 5 connected components, each a node Add n-1 edges

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 < 100 (i.e. < $1) Greedy: At each step, choose the largest possible coin If n >= 50 choose a half dollar and reduce n by 50; If n >= 25 choose a quarter and reduce n by 25; As long as n >= 10, choose a dime and reduce n by 10; If n >= 5, choose a nickel and reduce n by 5; Choose n pennies.

Greediness works here You’re standing at point x. 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. That is a local optimum choice, not a global one. Greediness works in this case. x

Greediness doesn’t work here 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

Greedy algorithm —doesn’t always work! 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, 5, 1 Greedy: At each step, choose the largest possible coin Consider making change for 10. The greedy choice would choose: 7, 1, 1, 1. But 5, 5 is only 2 coins.

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 abstract, at a high level. They are versions of the basic additive method we have already seen: at each step add an edge that does not create a cycle. 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

MST using Kruskal’s algorithm Minimal set of edges that connect all vertices At each step, add an edge (that does not form a cycle) with minimum weight edge with weight 2 3 2 5 4 6 3 2 5 4 6 edge with weight 3 One of the 4’s The 5 3 2 5 4 6 3 2 5 4 6 3 2 5 4 6 Red edges need not form tree (until end)

Kruskal Minimal set of edges that connect all vertices 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. We now investigate Prim’s algorithm

MST using “Prim’s algorithm” (should be called “JPD algorithm”) Developed in 1930 by Czech mathematician Vojtěch Jarník. Práce Moravské Přírodovědecké Společnosti, 6, 1930, pp. 57–63. (in Czech) Developed in 1957 by computer scientist Robert C. Prim. Bell System Technical Journal, 36 (1957), pp. 1389–1401 Developed about 1956 by Edsger Dijkstra and published in in 1959. Numerische Mathematik 1, 269–271 (1959) 🙏

Kruskal Prim’s algorithm Minimal set of edges that connect all vertices 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 3 3 2 5 4 6 3 2 5 4 6 edge with weight 5 One of the 4’s 3 2 5 4 6 The 2 3 2 5 4 6 3 2 5 4 6

Difference between Prim and Kruskal Minimal set of edges that connect all vertices 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, 2) Kruskal chooses (3, 4) Here, Prim chooses (0, 1) Kruskal chooses (3, 4) 1 2 3 4 1 2 3 4 5 6 2 5 4 6 4 3

Difference between Prim and Kruskal Minimal set of edges that connect all vertices 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, 2) Kruskal chooses (3, 4) Here, Prim chooses (0, 1) Kruskal chooses (3, 4) 1 2 3 4 1 2 3 4 5 6 2 5 4 6 4 3

Difference between Prim and Kruskal Minimal set of edges that connect all vertices Prim requires that the constructed red tree always be connected. Kruskal doesn’t But: Both algorithms find a minimal spanning tree If the edge weights are all different, the Prim and Kruskal algorithms construct the same tree.

Implement JPD to create a maze The graph is a grid. Each node is connected by an edge to its neighbors. We now use the JPD algorithm to construct a random maze. Only difference, instead of choosing an edge with minimum weight, choose an edge at random.

Implement JPD to create a maze The graph is a grid. Each node is connected by an edge to its neighbors. There is no need to implement edges. Each node has an edge to its North, East, West, and South neighbors: N, E, W, S If being drawn in a GUI, draw a black line between adjacent nodes.

Implement JPD to create a maze The graph is a grid. Each node is connected by an edge to its neighbors. b There is no need to implement edges. Automatically, each node has an edge to its North, East, West, and South neighbors: N, E, W, S Choose beginning node b randomly, make it white

Implement JPD to create a maze The graph is a grid. Each node is connected by an edge to its neighbors. b All nodes that have been added to the spanning tree are white. They have in them a letter b, N, E, W, or S ---as will be seen later.

Implement JPD to create a maze The graph is a grid. Each node is connected by an edge to its neighbors. b All nodes that have been added to the spanning tree are white. They have in them the letter b, N, E, W, or S ---as will be seen later. The frontier set are nodes that are adjacent to white nodes. They are in an ArrayList F. We make them blue.

Implement JPD to create a maze Here’s ONE step of the algorithm. Choose a random Frontier (blue) node, Make it white (put in spanning tree), remove it from Frontier. Put in it a letter to indicate edge to the white node to which it was adjacent. Remove black line between the the two nodes. Add adjacent nodes to the Frontier b N (Remember, don’t add a cycle)

Implement JPD to create a maze b You know about backpointers from the shortest path algorithm. Think of start node b as the root of the tree. The N is a backpointer for the path from root b of the tree to this node. It point to the node’s parent. N ↑

Implement JPD to create a maze Here’s ONE step of the algorithm. Choose a random Frontier (blue) node, Make it white (put in spanning tree), remove it from Frontier. Put in it a letter to indicate edge to the white node to which it was adjacent. Remove black line between the the two nodes. Add adjacent nodes to the Frontier b N N

Implement JPD to create a maze Here’s ONE step of the algorithm. Choose a random Frontier (blue) node, Make it white (put in spanning tree), remove it from Frontier. Put in it a letter to indicate edge to the white node to which it was adjacent. Remove black line between the the two nodes Add adjacent nodes to the Frontier b N W N

Implement JPD to create a maze Important pts about implementation No implementation of edges. They are in our head Nodes with a letter in them are in the spanning tree. b N indicates an edge of spanning tree –to node to North (similar for E, W, S) N W N Frontier set (blue nodes) is an ArrayList. Efficient to choose one at random and to remove it from set

Maze generation using Prim’s algorithm The generation of a maze using Prim's algorithm on a randomly weighted grid graph that is 30x20 in size. https://en.wikipedia.org/wiki/Maze_generation_algorithm jonathanzong.com/blog/2012/11/06/maze-generation-with-prims-algorithm

Greedy algorithms Suppose the weights are all 1. Then Dijkstra’s shortest-path algorithm does a breath-first search! 1 1 1 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!

Similar code structures while (a vertex is unmarked) { v= best unmarked vertex mark v; for (each w adj to v) update D[w]; } Breadth-first-search (bfs) best: next in queue update: D[w] = D[v]+1 Dijkstra’s algorithm best: next in priority queue update: D[w] = min(D[w], D[v]+c(v,w)) Prim’s algorithm update: D[w] = min(D[w], c(v,w)) c(v,w) is the vw edge weight

Graph Algorithms Search Shortest paths Minimum spanning trees Depth-first search Breadth-first search Shortest paths Dijkstra's algorithm Minimum spanning trees Prim's algorithm Kruskal's algorithm