SPANNING TREES Lecture 21 CS2110 – Spring 2016 1.

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.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Chapter 23 Minimum Spanning Trees
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:
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Spanning Trees.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
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.
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 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.
Lecture 23. Greedy Algorithms
Analysis of Algorithms
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
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,
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
SPANNING TREES, INTRO. TO THREADS Lecture 23 CS2110 – Fall
COMP108 Algorithmic Foundations Greedy methods
Greedy Technique.
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
Shortest Paths and Minimum Spanning Trees
C.Eng 213 Data Structures Graphs Fall Section 3.
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Spanning Trees, greedy algorithms
Three Graph Algorithms
Short paths and spanning trees
CSCE350 Algorithms and Data Structure
Advanced Analysis of Algorithms
Minimum-Cost Spanning Tree
Hannah Tang and Brian Tjaden Summer Quarter 2002
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Tree.
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSC 380: Design and Analysis of Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Winter 2019 Lecture 10 Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
Spanning Trees, greedy algorithms
Prims’ spanning tree algorithm
Minimum-Cost Spanning Tree
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

SPANNING TREES Lecture 21 CS2110 – Spring

Spanning trees What we do today:  Demo A8, Temple of Boom  Talk about modifying an existing algorithm  Calculating the shortest path in Dijkstra’s algorithm  Minimum spanning trees  3 greedy algorithms (including Kruskal & Prim)  Look at time complexity of shortest path 2

Assignment A7 available Due 2 days after prelim 2. Implement Dijkstra’s shortest-path algorithm. Start with our abstract algorithm, implement it in a specific setting. Our method: 36 lines, including extensive comments SAME THING ON A6! Start with methods in ArrayHeap, modify them to new environment. We will make our solution to A6 available after the deadline for late submissions. Last semester: median: 4.0, mean: But our abstract algorithm is much closer to the planned implementation than lat fall, and we expect a much lower median and mean. 3

Execution times for ArrayList methods, etc. Several questions on the Piazza about how fast various methods are in ArrayList, HashMap, etc. Please please look at the Java API documentation for these classes! All the information is there! For example, I will demo googling ArrayList 8 java and show you, in class. Also, look in the FAQs note for an assignment before asking a question about that assignment! 4

5 Dijkstra’s algorithm using Nodes. An object of class Node for each node of the graph. Nodes have an identification, (S, A, E, etc). Nodes contain shortest distance from Start node (red). S, 0 E, 4 C, 1 D, 3 A, 2 B,

6 Backpointers Shortest path requires not only the distance from start to a node but the shortest path itself. How to do that? In the graph, red numbers are shortest distance from S. S, 0 E, 4 C, 1 D, 3 A, 2 B, Need shortest path from S to every node. Storing that info in node S wouldn’t make sense.

7 Backpointers Shortest path requires not only the distance from start to a node but the shortest path itself. How to do that? In the graph, red numbers are shortest distance from S. S, 0 null E, 4 C, 1 D, 3 A, 2 B, In each node, store (a pointer to) previous node on the shortest path from S to that node. Backpointer

8 Backpointers S, 0 null E, 4 C, 1 D, 3 A, 2 B, When to set a backpointer? In the algorithm, processing an edge (f, w): If the shortest distance to w changes, then set w’s backpointer to f. It’s that easy!

f= node in Frontier with min spl; Remove f from Frontier; for each neighbor w of f: if w in far-off set then w.spl= f.dist + weight(f, w); Put w in the Frontier; else if f.dist + weight(f, w) < w.spl then w.dist= f.dist + weight(f, w) dist _________ backPointer_____ Each iteration of Dijkstra’s algorithm dist: shortest-path length calculated so far 9 w.backPointer= f; Node

Undirected trees An undirected graph is a tree if there is exactly one simple path between any pair of vertices 10 Root of tree? It doesn’t matter. Choose any vertex for the root

Facts about trees 1.|E| = |V| – 1 2.connected 3.no cycles Any two of these properties imply the third —and imply that the graph is a tree 11 V: set of vertices E: set of edges Consider a graph with these properties:

12 A spanning tree of a connected undirected graph (V, E) is a subgraph (V, E') that is a tree 12 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 Same set of vertices V Minimal set of edges that connect all vertices Three equivalent definitions

Spanning trees: examples 13

Finding a spanning tree A subtractive method If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?) Repeat until no more cycles Start with the whole graph – it is connected 14 Use: Maximal set of edges that contains no cycle

If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?) Repeat until no more cycles Start with the whole graph – it is connected Finding a spanning tree A subtractive method 15 Use: Maximal set of edges that contains no cycle

If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?) Repeat until no more cycles Start with the whole graph – it is connected Finding a spanning tree A subtractive method 16 Nondeterministic algorithm Use: Maximal set of edges that contains no cycle

17 Start with no edges Finding a spanning tree: Additive method While the graph is not connected: Choose an edge that connects 2 connected components and add it – the graph still has no cycle (why?) Use: Minimal set of edges that connects all vertices Tree edges will be red. Dashed lines show original edges. Left tree consists of 5 connected components, each a node nondeterministic algorithm

Minimum spanning trees Suppose edges are weighted (> 0), and 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 cost, any of which is a minimum spanning tree 18

Minimum spanning trees Suppose edges are weighted (> 0), and we want a spanning tree of minimum cost (sum of edge weights) Useful in network routing & other applications For example, to stream a video

Greedy algorithm A greedy algorithm: follow the heuristic of making a locally optimal choice at each stage, with the hope of finding a global optimum 20 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.

Greedy algorithm A greedy algorithm: follow the heuristic of making a locally optimal choice at each stage, with the hope of fining a global optimum. Doesn’t always work 21 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.

Greedy algorithm A greedy algorithm: follow the heuristic of making a locally optimal choice at each stage, with the hope of fining a global optimum. Doesn’t always work 22 Example. Make change (if possible) using the fewest number of coins. Coins have these values: 7, 5, 2 Greedy: At each step, choose the largest possible coin Consider making change for 10. The greedy choice would choose: 7, 2 –and can’t proceed! But 5, 5 works

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

Construct minimum spanning tree (greedy) As long as there is a cycle: Find a black max-weight edge – if it is on a cycle, throw it out otherwise keep it (make it red) 24 Maximal set of edges that contains no cycle We mark a node red to indicate that we have looked at it and determined it can’t be removed because removing it would unconnect the graph (the node is not on a cycle)

Construct minimum spanning tree (greedy) As long as there is a cycle: Find a black max-weight edge – if it is on a cycle, throw it out otherwise keep it (make it red) 25 Maximal set of edges that contains no cycle Throw out edge with weight Can’t throw out 5; make it red Throw out one Nondeterministic algorithm No more cyles: done

26 Construct minimum spanning tree (greedy) As long as there is a cycle: Find a black max-weight edge – if it is on a cycle, throw it out otherwise keep it (make it red) Nobody uses this algorithm because, usually, there are far more edges than nodes. If graph with n nodes is complete, O(n*n) edges have to be deleted! Maximal set of edges that contains no cycle Minimal set of edges that connect all vertices It’s better to use this property of a spanning tree and add edges to the spanning tree. For a tree with n nodes, n-1 edges have to be added

Two greedy algorithms for constructing a minimum spanning tree  Kruskal  Prim Both use this definition of a spanning tree and in a greedy fashion: Both are nondeterministic, in that at a point they may choose one of several nodes with equal weight 27 Minimal set of edges that connect all vertices

Kruskal’s algorithm: greedy 28 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 edge with weight one of the 4’s the Dashed edges: original graph Red edges: the constructed spanning tree

29 Invariant: The added edges (and their nodes) are connected Have start node edge with weight edge with weight 5 one of the 4’s the Prim’s algorithm. greedy

Tree greedy spanning tree algorithms 30 When edge weights are all distinct, or if there is exactly one minimum spanning tree, all 3 algorithms construct the same tree. 1. Algorithm that uses this property of a spanning tree: Maximal set of edges that contains no cycle 2. Algorithms that use this property of a spanning tree: Minimal set of edges that connect all vertices (a) Kruskal (b) Prim

Prim’s algorithm (n nodes, m edges)  O(m + n log n) for adj list  Use a priority queue PQ  Regular PQ produces time O(n + m log m)  Can improve to O(m + n log n) using a fancier heap prim(s) { D[s]= 0; //start vertex D[i]= ∞ for all i ≠ s; while (a vertex is unmarked) { v= unmarked vertex with smallest D; mark v; for (each w adj to v) D[w]= min(D[w], c(v,w)); } O(n 2 ) for adj matrix –while-loop iterates n times –for-loop takes O(n) time 31

Application of MST Maze generation using Prim’s algorithm 32 The generation of a maze using Prim's algorithm on a randomly weighted grid graph that is 30x20 in size.

More complicated maze generation 33

Greedy algorithms  These are Greedy Algorithms  Greedy Strategy: is an algorithm design technique Like Divide & Conquer  Greedy algorithms are used to solve optimization problems Goal: find the best solution  Works when the problem has the greedy-choice property: A global optimum can be reached by making locally optimum choices Example: Making change Given an amount of money, find smallest number of coins to make that amount Solution: Use Greedy Algorithm: Use as many large coins as you can. Produces optimum number of coins for US coin system May fail for old UK system 34

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 –best: next in priority queue –update: D[w] = min(D[w], c(v,w)) 35 c(v,w) is the v  w edge weight

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… 36