Introduction to Algorithms

Similar documents
Introduction to Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Introduction to Algorithms

Minimum Spanning Trees

Minimum Spanning Trees My T. UF

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

Algorithm Design and Analysis

Minimum Spanning Trees

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Week 11: Minimum Spanning trees

Minimum Spanning Trees

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Lecture Notes for Chapter 23: Minimum Spanning Trees

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Minimum Spanning Trees Ch 23 Traversing graphs

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Introduction to Algorithms. Lecture 11

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

CS 561, Lecture 9. Jared Saia University of New Mexico

2 A Template for Minimum Spanning Tree Algorithms

Depth-first Search (DFS)

Unit 2: Algorithmic Graph Theory

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Algorithm Design and Analysis

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

Minimum Spanning Trees and Prim s Algorithm

COS 226 Lecture 19: Minimal Spanning Trees (MSTs) PFS BFS and DFS examples. Classic algorithms for solving a natural problem

Shortest path problems

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

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Lecture 10: Analysis of Algorithms (CS ) 1

Algorithm Design and Analysis

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CHAPTER 23. Minimum Spanning Trees

Minimum Spanning Trees Outline: MST

Week 12: Minimum Spanning trees and Shortest Paths

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

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

Graph Algorithms Using Depth First Search

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

Minimum spanning trees

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

Analysis of Algorithms, I

Announcements Problem Set 5 is out (today)!

Chapter 23. Minimum Spanning Trees

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Partha Sarathi Manal

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Greedy Algorithms CSE 6331

Introduction: (Edge-)Weighted Graph

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

CS 4407 Algorithms Lecture 5: Graphs an Introduction

Randomized Graph Algorithms

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

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

Outline. Computer Science 331. Analysis of Prim's Algorithm

Shortest Path Algorithms

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

Algorithms for Minimum Spanning Trees

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Data Structures and Algorithms. Werner Nutt

Minimum Spanning Trees COSC 594: Graph Algorithms Spring By Kevin Chiang and Parker Tooley

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

23.2 Minimum Spanning Trees

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSci 231 Final Review

Dijkstra s Shortest Path Algorithm

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

The minimum spanning tree problem

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

COMP251: Single source shortest paths

6.1 Minimum Spanning Trees

Lecture 19. Broadcast routing

Tutorial 6-7. Dynamic Programming and Greedy

Kruskal s MST Algorithm

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Minimum Spanning Tree (undirected graph)

Greedy Algorithms and Data Compression. Curs Fall 2017

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

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

Minimum Spanning Trees Ch 23 Traversing graphs

Transcription:

Introduction to Algorithms 6.046J/18.401J LECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy choice Prim s greedy MST algorithm Prof. Charles E. Leiserson November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.1

Graphs (review) Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of a set V of vertices (singular: vertex), a set E! V " V of edges. In an undirected graph G = (V, E), the edge set E consists of unordered pairs of vertices. In either case, we have E = O(V 2 ). Moreover, if G is connected, then E # V 1, which implies that lg E = $(lgv). (Review CLRS, Appendix B.) November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.2

Adjacency-matrix representation The adjacency matrix of a graph G = (V, E), where V = {1, 2,, n}, is the matrix A[1.. n, 1.. n] given by 1 if (i, j) % E, A[i, j] = 0 if (i, j) & E. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.3

Adjacency-matrix representation The adjacency matrix of a graph G = (V, E), where V = {1, 2,, n}, is the matrix A[1.. n, 1.. n] given by 1 if (i, j) % E, A[i, j] = 0 if (i, j) & E. 22 11 33 44 A 1 2 3 4 1 0 1 1 0 2 0 0 1 0 3 0 0 0 0 4 0 0 1 0 $(V 2 ) storage ' dense representation. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.4

Adjacency-list representation An adjacency list of a vertex v % V is the list Adj[v] of vertices adjacent to v. 22 11 33 44 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.5

Adjacency-list representation An adjacency list of a vertex v % V is the list Adj[v] of vertices adjacent to v. 22 11 33 44 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} For undirected graphs, Adj[v] = degree(v). For digraphs, Adj[v] = out-degree(v). November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.6

Adjacency-list representation An adjacency list of a vertex v % V is the list Adj[v] of vertices adjacent to v. 22 11 33 44 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} For undirected graphs, Adj[v] = degree(v). For digraphs, Adj[v] = out-degree(v). Handshaking Lemma: ( v%v = 2 E for undirected graphs ' adjacency lists use $(V + E) storage a sparse representation (for either type of graph). November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.7

Minimum spanning trees Input: A connected, undirected graph G = (V, E) with weight function w : E ) R. For simplicity, assume that all edge weights are distinct. (CLRS covers the general case.) November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.8

Minimum spanning trees Input: A connected, undirected graph G = (V, E) with weight function w : E ) R. For simplicity, assume that all edge weights are distinct. (CLRS covers the general case.) Output: A spanning tree T a tree that connects all vertices of minimum weight: ( w( T ) * w( u, v). ( u, v) % T November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.9

Example of MST 6 12 5 9 14 8 7 15 3 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.10

Example of MST 6 12 5 9 14 8 7 15 3 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.11

Optimal substructure MST T: (Other edges of G are not shown.) November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.12

Optimal substructure MST T: (Other edges of G are not shown.) Remove any edge (u, v) % T. u v November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.13

Optimal substructure MST T: (Other edges of G are not shown.) Remove any edge (u, v) % T. u v November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.14

Optimal substructure MST T: (Other edges of G are not shown.) u T 2 T 1 Remove any edge (u, v) % T. Then, T is partitioned into two subtrees T 1 and T 2. v November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.15

Optimal substructure MST T: (Other edges of G are not shown.) u T 2 T 1 Remove any edge (u, v) % T. Then, T is partitioned into two subtrees T 1 and T 2. Theorem. The subtree T 1 is an MST of G 1 = (V 1, E 1 ), the subgraph of G induced by the vertices of T 1 : V 1 = vertices of T 1, E 1 = { (x, y) % E : x, y % V 1 }. Similarly for T 2. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.16 v

Proof of optimal substructure w(t) = w(u, v) + w(t 1 ) + w(t 2 ). Proof. Cut and paste: If T 1 + were a lower-weight spanning tree than T 1 for G 1, then T+ = {(u, v)}, T 1 +,T 2 would be a lower-weight spanning tree than T for G. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.17

Proof of optimal substructure Proof. Cut and paste: w(t) = w(u, v) + w(t 1 ) + w(t 2 ). If T 1 + were a lower-weight spanning tree than T 1 for G 1, then T+ = {(u, v)}, T 1 +,T 2 would be a lower-weight spanning tree than T for G. Do we also have overlapping subproblems? Yes. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.18

Proof of optimal substructure Proof. Cut and paste: w(t) = w(u, v) + w(t 1 ) + w(t 2 ). If T 1 + were a lower-weight spanning tree than T 1 for G 1, then T+ = {(u, v)}, T 1 +,T 2 would be a lower-weight spanning tree than T for G. Do we also have overlapping subproblems? Yes. Great, then dynamic programming may work! Yes, but MST exhibits another powerful property which leads to an even more efficient algorithm. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.19

Hallmark for greedy algorithms Greedy-choice property A locally optimal choice is globally optimal. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.20

Hallmark for greedy algorithms Greedy-choice property A locally optimal choice is globally optimal. Theorem. Let T be the MST of G = (V, E), and let A! V. Suppose that (u, v) % E is the least-weight edge connecting A to V A. Then, (u, v) % T. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.21

Proof of theorem Proof. Suppose (u, v) & T. Cut and paste. T: % A % V A u v (u, v) = least-weight edge connecting A to V A November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.22

Proof of theorem Proof. Suppose (u, v) & T. Cut and paste. T: v % A % V A u (u, v) = least-weight edge connecting A to V A Consider the unique simple path from u to v in T. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.23

Proof of theorem Proof. Suppose (u, v) & T. Cut and paste. T: v % A % V A u (u, v) = least-weight edge connecting A to V A Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V A. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.24

Proof of theorem Proof. Suppose (u, v) & T. Cut and paste. T+: v % A % V A u (u, v) = least-weight edge connecting A to V A Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V A. A lighter-weight spanning tree than T results. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.25

Prim s algorithm IDEA: Maintain V A as a priority queue Q. Key each vertex in Q with the weight of the leastweight edge connecting it to a vertex in A. Q - V key[v] -.for all v % V key[s] - 0 for some arbitrary s % V while Q /0 do u - EXTRACT-MIN(Q) for each v % Adj[u] do if v % Q and w(u, v) < key[v] then key[v] - w(u, v) 1[v] - u At the end, {(v, 1[v])} forms the MST.! DECREASE-KEY November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.26

Example of Prim s algorithm % A % V A 14.. 6 12 5.... 7 8.. 00 9 15.... 3.. 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.27

Example of Prim s algorithm % A % V A 14.. 6 12 5.... 7 8.. 00 9 15.... 3.. 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.28

Example of Prim s algorithm % A % V A 14.. 6 12 5.. 22 7 8.. 00 9 15.. 35 3 34 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.29

Example of Prim s algorithm % A % V A 14.. 6 12 5.. 22 7 8.. 00 9 15.. 35 3 34 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.30

Example of Prim s algorithm % A % V A 14 36 6 12 5 55 22 7 8.. 00 9 15 77 35 3 34 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.31

Example of Prim s algorithm % A % V A 14 36 6 12 5 55 22 7 8.. 00 9 15 77 35 3 34 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.32

Example of Prim s algorithm % A % V A 88 6 12 5 55 22 14 7 8 39 00 3 :: 10 9 15 77 35 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.33

Example of Prim s algorithm % A % V A 88 6 12 5 55 22 14 7 8 39 00 3 :: 10 9 15 77 35 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.34

Example of Prim s algorithm % A % V A 88 6 12 5 55 22 14 7 8 39 00 3 :: 10 9 15 77 35 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.35

Example of Prim s algorithm % A % V A 14 88 6 12 5 55 22 7 8 ;; 00 9 15 77 35 3 :: 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.36

Example of Prim s algorithm % A % V A 14 88 6 12 5 55 22 7 8 ;; 00 9 15 77 35 3 :: 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.37

Example of Prim s algorithm % A % V A 14 88 6 12 5 55 22 7 8 ;; 00 9 15 77 35 3 :: 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.38

Example of Prim s algorithm % A % V A 14 88 6 12 5 55 22 7 8 ;; 00 9 15 77 35 3 :: 10 November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.39

Analysis of Prim Q - V key[v] -.for all v % V key[s] - 0 for some arbitrary s % V while Q /0 do u - EXTRACT-MIN(Q) for each v % Adj[u] do if v % Q and w(u, v) < key[v] then key[v] - w(u, v) 1[v] - u November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.40

Analysis of Prim $(V) total Q - V key[v] -.for all v % V key[s] - 0 for some arbitrary s % V while Q /0 do u - EXTRACT-MIN(Q) for each v % Adj[u] do if v % Q and w(u, v) < key[v] then key[v] - w(u, v) 1[v] - u November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.41

Analysis of Prim V times $(V) total Q - V key[v] -.for all v % V key[s] - 0 for some arbitrary s % V while Q /0 do u - EXTRACT-MIN(Q) for each v % Adj[u] do if v % Q and w(u, v) < key[v] then key[v] - w(u, v) 1[v] - u November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.42

Analysis of Prim V times $(V) total degree(u) times Q - V key[v] -.for all v % V key[s] - 0 for some arbitrary s % V while Q /0 do u - EXTRACT-MIN(Q) for each v % Adj[u] do if v % Q and w(u, v) < key[v] then key[v] - w(u, v) 1[v] - u November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.43

V times Analysis of Prim $(V) total degree(u) times Q - V key[v] -.for all v % V key[s] - 0 for some arbitrary s % V while Q /0 do u - EXTRACT-MIN(Q) for each v % Adj[u] do if v % Q and w(u, v) < key[v] then key[v] - w(u, v) 1[v] - u Handshaking Lemma ' $(E) implicit DECREASE-KEY s. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.44

V times Analysis of Prim $(V) total degree(u) times Q - V key[v] -.for all v % V key[s] - 0 for some arbitrary s % V while Q /0 do u - EXTRACT-MIN(Q) for each v % Adj[u] do if v % Q and w(u, v) < key[v] then key[v] - w(u, v) 1[v] - u Handshaking Lemma ' $(E) implicit DECREASE-KEY s. Time = $(V) T EXTRACT -MIN + $(E) T DECREASE-KEY November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.45

Analysis of Prim (continued) Time = $(V) T EXTRACT -MIN + $(E) T DECREASE-KEY November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.46

Analysis of Prim (continued) Time = $(V) T EXTRACT -MIN + $(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.47

Analysis of Prim (continued) Time = $(V) T EXTRACT -MIN + $(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total array O(V) O(1) O(V 2 ) November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.48

Analysis of Prim (continued) Time = $(V) T EXTRACT -MIN + $(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total array O(V) O(1) O(V 2 ) binary heap O(lg V) O(lg V) O(E lg V) November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.49

Analysis of Prim (continued) Time = $(V) T EXTRACT -MIN + $(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total array O(V) O(1) O(V 2 ) binary heap Fibonacci heap O(lg V) O(lg V) O(E lg V) O(lg V) amortized O(1) amortized O(E + V lg V) worst case November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.50

MST algorithms Kruskal s algorithm (see CLRS): Uses the disjoint-set data structure (Lecture 10). Running time = O(E lg V). November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.51

MST algorithms Kruskal s algorithm (see CLRS): Uses the disjoint-set data structure (Lecture 10). Running time = O(E lg V). Best to date: Karger, Klein, and Tarjan [1993]. Randomized algorithm. O(V + E) expected time. November 9, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.52