Spanning Trees Lecture 20 CS2110 – Spring 2015.

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Design and Analysis of Algorithms Approximation algorithms for NP-complete problems Haidong Xue Summer 2012, at GSU.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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:
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
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.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
SPANNING TREES Lecture 21 CS2110 – Spring
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
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.
Lecture19: Graph III Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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 Lecture 21 CS2110 – Spring
SPANNING TREES, INTRO. TO THREADS Lecture 23 CS2110 – Fall
Minimum Spanning Trees
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Greedy Technique.
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
Minimal Spanning Trees
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Spanning Trees, greedy algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Three Graph Algorithms
Minimal Spanning Trees
Short paths and spanning trees
Chapter 2: Business Efficiency Lesson Plan
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Minimum Spanning Trees
Minimum Spanning Tree.
Graphs Chapter 13.
Shortest Path.
Graphs.
Hannah Tang and Brian Tjaden Summer Quarter 2002
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
Graph Searching.
Algorithms and data structures
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSC 380: Design and Analysis of Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Winter 2019 Lecture 10 Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
CSE 332: Minimum Spanning Trees
Spanning Trees, greedy algorithms
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
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 Lecture 20 CS2110 – Spring 2015

Spanning trees Definitions Minimum spanning trees 3 greedy algorithms (including Kruskal & Prim) Concluding comments: Greedy algorithms Travelling salesman problem

Undirected trees An undirected graph is a tree if there is exactly one simple path between any pair of vertices

Facts about trees |E| = |V| – 1 connected no cycles In fact, any two of these properties imply the third, and 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

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

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

Finding a spanning tree A subtractive method Start with the whole graph – it is connected 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

Finding a spanning tree A subtractive method Start with the whole graph – it is connected 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

Finding a spanning tree A subtractive method Start with the whole graph – it is connected 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

Finding a spanning tree An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

Finding a spanning tree An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

Finding a spanning tree An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

Finding a spanning tree An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

Finding a spanning tree An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

Finding a spanning tree An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

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

Minimum spanning trees Suppose edges are weighted (> 0), and we want a spanning tree of minimum cost (sum of edge weights) 1 14 2 3 72 Useful in network routing & other applications For example, to stream a video 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithm A greedy algorithm follows the heuristic of making a locally optimal choice at each stage, with the hope of fining 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.

3 Greedy algorithm A greedy algorithm follows the heuristic of making a locally optimal choice at each stage, with the hope of fining 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.

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

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 7 33 34 9 28 64 22 24 54 4 15 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 7 9 22 24 54 4 15 21 11 6 13 12 8 16 10 25 5

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 7 9 54 4 15 11 6 13 12 8 16 10 25 5

3 Greedy algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 7 9 54 4 11 6 12 8 16 10 25 5

3 Greedy algorithms: Kruskal B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Kruskal B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Kruskal B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Kruskal B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Kruskal B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy Algorithms: Kruskal B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy Algorithms: Kruskal B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

3 Greedy algorithms: Prim When edge weights are all distinct, or if there is exactly one minimum spanning tree, the 3 algorithms all find the identical tree 1 14 2 7 9 54 4 11 6 12 8 16 10 25 5

Prim’s algorithm prim(s) { O(m + n log n) for adj list 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(m + n log n) for adj list Use a PQ Regular PQ produces time O(n + m log m) Can improve to O(m + n log n) using a fancier heap O(n2) for adj matrix while-loop iterates n times for-loop takes O(n) time

Application of MST 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. http://en.wikipedia.org/wiki/File:MAZE_30x20_Prim.ogv

More complicated maze generation http://www.cgl.uwaterloo.ca/~csk/projects/mazes/

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

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

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…