Minimum Spanning Trees and Shortest Paths

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
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.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Spanning Trees.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Minimum Spanning Trees
COSC 2007 Data Structures II Chapter 14 Graphs III.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
Instructor: Lilian de Greef Quarter: Summer 2017
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Cinda Heeren / Geoffrey Tien
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Graph Algorithm.
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Minimum Spanning Trees
Spanning Trees.
Minimum-Cost Spanning Tree
Graphs.
Outline This topic covers Prim’s algorithm:
Minimum-Cost Spanning Tree
Hannah Tang and Brian Tjaden Summer Quarter 2002
Chapter 11 Graphs.
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Lecture 19: Minimum Spanning Trees
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSE 417: Algorithms and Computational Complexity
Lecture 18: Implementing Graphs
CSE 373: Data Structures and Algorithms
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Lecture 23: Minimum Spanning Trees
Lecture 22: Implementing Dijkstra’s
Topological Sorting Minimum Spanning Trees Shortest Path
Chapter 9: Graphs Spanning Trees
Minimum-Cost Spanning Tree
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:

Minimum Spanning Trees and Shortest Paths Kruskal's Algorithm Prim's Algorithm Shortest Paths April 04, 2018 Cinda Heeren / Geoffrey Tien

Kruskal's algorithm Data types for implementation KruskalsAlgorithm() { set 𝐸′ = ø while ( 𝐸′ ≠ 𝑉 −1) Find minimum weight edge 𝑒∉𝐸′ such that 𝐸′ 𝑒 does not contain cycles Add 𝑒 to 𝐸′ } We need ADTs that support our required operations efficiently How do we find the minimum weight edge? Priority queue! How can we check for cycles and perform union? Disjoint sets! April 04, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Kruskal's algorithm Example A B 2 prQ MST weight: 38 6 3 𝐴,𝐵 2 𝐹,𝐺 10 16 7 C 8 17 𝐵,𝐶 3 𝐹,𝐻 11 D E 13 12 F 5 13 𝐺,𝐻 4 𝐷,𝐹 12 9 16 10 11 A 𝐸,𝐹 5 𝐵,𝐺 13 G H 4 𝐴,𝐶 6 𝐶,𝐹 13 E B C D G F H 𝐵,𝐸 7 𝐴,𝐷 16 prQ cost heap ordered array build removeMin 𝐶,𝐷 8 𝐸,𝐻 16 Overall cost? 𝐷,𝐺 9 𝐶,𝐸 17 Note: only one edge direction listed in prQ for compactness in this slide Note that no insertions performed after build April 04, 2018 Cinda Heeren / Geoffrey Tien

A slight tangent – maze construction What makes a good maze? A bunch of adjacent rooms Each room is a vertex Open wall between rooms form edge Unpredictable, not easily solved Highly branching, many dead ends Just enough walls to get from any room to any other room Especially start and finish in out April 04, 2018 Cinda Heeren / Geoffrey Tien

Maze under construction So far, a number of walls have been knocked down, while others remain Now we consider the wall between rooms A and B Should we knock it down? If A and B are otherwise connected If A and B are not otherwise connected A B Algorithm: While edges remain in 𝐸 Remove a random edge 𝑒= 𝑢,𝑣 from 𝐸 If 𝑢 and 𝑣 have not been connected Add 𝑒 to 𝐸′ Mark 𝑢 and 𝑣 as connected This is a lot like Kruskal's algorithm! Solve it using disjoint sets and random edge selection April 04, 2018 Cinda Heeren / Geoffrey Tien

Recall: BFS spanning tree Starting from vertex A A B 2 A (B,2), (C,6), (D,16) B (A,2), (C,3), (E,7), (G,13) C (A,6), (B,3), (D,8), (E,17), (F,13) D (A,16), (C,8), (F,12), (G,9) E (B,7), (C,17), (F,5), (H,16) F (C,13), (D,12), (E,5), (G,10), (H,11) G (D,9), (F,10), (H,4) H (E,16), (F,11), (G,4) B C D 6 3 E G 16 7 C F 8 17 D E 13 12 F 5 H 13 9 16 10 11 G H 4 Queue: A B C D E G F H Identified: T T T T T T T T A B C D E F G H What if we use a priority queue (with neighbours' edge weights) instead of an ordinary queue? April 04, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Prim's algorithm Greedy algorithm Builds a spanning tree from initially one vertex. Repeatedly chooses the minimum-weight edge from a vertex in the tree, to a vertex outside the tree – adds that vertex to the tree PrimsAlgorithm(v) { mark v as visited, add v to spanning tree while (graph has unvisited vertices) Find least cost edge (w, u) from a visited vertex w to unvisited vertex u Mark u as visited Add vertex u and edge (w, u) to the minimum spanning tree } April 04, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Prim's algorithm A B 2 B A (B,2), (C,6), (D,16) B (A,2), (C,3), (E,7), (G,13) C (A,6), (B,3), (D,8), (E,17), (F,13) D (A,16), (C,8), (F,12), (G,9) E (B,7), (C,17), (F,5), (H,16) F (C,13), (D,12), (E,5), (G,10), (H,11) G (D,9), (F,10), (H,4) H (E,16), (F,11), (G,4) 6 3 C E 16 7 C D 8 17 D E G 13 12 F 5 F 13 9 16 10 11 H G H 4 (A,B,2) (A,C,6) (A,D,16) (B,C,3) (B,E,7) prQ: Visited: T T T T T T T T (B,G,13) (C,D,8) (C,E,17) (C,F,13) (E,F,5) A B C D E F G H (E,H,16) (F,D,12) (F,G,10) (F,H,11) (D,G,9) (G,H,4) All vertices visited MST weight: 38 April 04, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Prim's algorithm Complexity Unlike Kruskal's algorithm, we will intersperse insertion and removal operations to the priority queue Maximum number of insertions into the priority queue? Assuming heap implementation of prQ 𝐸 in the worst case, then total cost of all insertions 𝑂 𝐸 log 𝐸 For dense graphs, 𝐸 ∈𝑂 𝑉 2 , then log 𝐸 ∈𝑂 2∙ log 𝑉 ∈𝑂 log 𝑉 Thus the complexity of Prim's algorithm is 𝑂 𝐸 log 𝑉 Actually we can describe Kruskal's algorithm the same way April 04, 2018 Cinda Heeren / Geoffrey Tien

Single-source shortest path Given a graph 𝐺= 𝑉,𝐸 and a vertex 𝑠∈𝑉, find the shortest path from 𝑠 to every vertex in 𝑉 Variations Weighted vs unweighted Cyclic vs acyclic Positive weights only vs negative weights allowed Multiple weight types to optimize April 04, 2018 Cinda Heeren / Geoffrey Tien

Single-Source Shortest Path Un/directed, weighted graphs, no negative cycles What is the least cost path from one vertex to another? For weighted graphs, this is the path that has the smallest sum of its edge weights A 1 4 B C 5 3 2 E 1 5 D 8 1 F 2 G The shortest path between B and G is: B-D-E-F-G (7) and not B-G (8) April 04, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Dijkstra's Algorithm Classic algorithm for solving shortest path in weighted graphs without negative weights A greedy algorithm Best local choice is made at each step, without considering future consequences Intuition: Shortest path from source vertex to itself is 0 Cost of going to adjacent nodes is at most edge weights Cheapest of these must be shortest path to that node Update paths for new node and continue picking shortest path April 04, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Dijkstra's Algorithm Initialize the cost of reaching each vertex to ∞ Initialize the cost of the source to 0 While there are unvisited vertices left in the graph Select the unvisited vertex with the lowest cost: 𝑢 Mark 𝑢 as visited, and note the vertex 𝑣 which was used to reach 𝑢 For each vertex 𝑤 which is adjacent to 𝑢 𝑤's cost = min(𝑤's old cost, 𝑢's cost + cost of (𝑢,𝑤)) And note the "parent" vertex which can be used to reach 𝑤 with the lowest cost April 04, 2018 Cinda Heeren / Geoffrey Tien

Dijkstra's Algorithm Example: directed graph A B C D E F G H 2 4 7 9 1 8 10 3 Source node: C Priority queue: Vertex Cost Parent A B C D E F G H Visited Cost Parent Note that we need access to arbitrary elements in this prQ in order to update April 04, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Exercise Given the following undirected, weighted graph, use Kruskal's algorithm and Prim's algorithm to find a minimum spanning tree and write its weight. Is the tree you found the unique MST for this graph? A 12 B 8 9 9 C 10 8 5 5 D 6 E F 14 G 3 4 H 12 7 4 9 I 7 J 8 K April 04, 2018 Cinda Heeren / Geoffrey Tien

Readings for this lesson Carrano & Henry Chapter 20.4.3 (Minimum spanning trees) Chapter 20.4.4 (Shortest paths) April 04, 2018 Cinda Heeren / Geoffrey Tien