Introduction to Algorithms: Greedy Algorithms (and Graphs)

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Introduction to Algorithms Jiafen Liu Sept
Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Algorithms
Topological Sort Minimum Spanning Tree
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Introduction to Algorithms`
Algorithms and Data Structures Lecture XIII
CS 3343: Analysis of Algorithms
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
CS200: Algorithm Analysis
CS200: Algorithm Analysis
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
Greedy Algorithm (17.4/16.4) Greedy Algorithm (GA)
Advanced Algorithms Analysis and Design
Analysis of Algorithms CS 477/677
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Graphs G = (V, E) V are the vertices; E are the edges.
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Finding Minimum Spanning Trees
Algorithm Course Dr. Aref Rashad
Greedy Algorithms (I) Greed, for lack of a better word, is good! Greed is right! Greed works! - Michael Douglas, U.S. actor in the role of Gordon Gecko,
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Prim’s algorithm IDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in.
Minimum Spanning Trees
Presentation transcript:

Introduction to Algorithms: Greedy Algorithms (and Graphs)

CS 421 - Introduction to Algorithms 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 | = (lg V). CS 421 - Introduction to Algorithms

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, 0 if (i, j)  E. A[i, j] = CS 421 - Introduction to Algorithms

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, 0 if (i, j)  E. A[i, j] = A 1 2 3 4 (V 2) storage  dense representation. CS 421 - Introduction to Algorithms

Adjacency-List Representation An adjacency list of a vertex v  V is the list Adj[v] of vertices adjacent to v. 2 1 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} 3 4 CS 421 - Introduction to Algorithms

Adjacency-List Representation An adjacency list of a vertex v  V is the list Adj[v] of vertices adjacent to v. 2 1 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} 3 4 For undirected graphs, | Adj[v] | = degree(v). For digraphs, | Adj[v] | = out-degree(v). CS 421 - Introduction to Algorithms

Adjacency-List Representation An adjacency list of a vertex v  V is the list Adj[v] of vertices adjacent to v. 2 1 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} 3 4 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).

Minimum Spanning Trees (MSTs) Input: A connected, undirected graph G = (V, E) with weight function w : E  ℛ. For simplicity, assume that all edge weights are distinct. CS 421 - Introduction to Algorithms

Minimum Spanning Trees (MSTs) Input: A connected, undirected graph G = (V, E) with weight function w : E  ℛ. For simplicity, assume that all edge weights are distinct. Output: A spanning tree T — a tree that connects all vertices — of minimum weight:  w(u, v) . (u,v)T w(T )  CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Example of MST 6 12 5 9 14 7 15 8 3 10 CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Example of MST 6 12 5 9 14 7 15 8 3 10 CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Optimal Substructure MST T: (Other edges of G are not shown.) CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Optimal Substructure u MST T: v Remove any edge (u, v)  T. CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Optimal Substructure u MST T: v Then, T is partitioned into two subtrees T1 and T2. CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Optimal Substructure u MST T: v Theorem. The subtree T1 is an MST of G1 = (V1, E1), the subgraph of G induced by the vertices of T1: V1 = vertices of T1, E1 = { (x, y)  E : x, y  V1 }. Similarly for T2. CS 421 - Introduction to Algorithms

Proof of Optimal Substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). If T1 were a lower-weight spanning tree than T1 for G1, then T  = {(u, v)}  T1  T2 would be a lower-weight spanning tree than T for G. CS 421 - Introduction to Algorithms

Proof of Optimal Substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). Do we also have overlapping sub-problems? •Yes. Great, then dynamic programming may work! •Yes, but MST exhibits another powerful property which leads to an even more efficient algorithm. CS 421 - Introduction to Algorithms

Hallmark for “Greedy” Algorithms Greedy-choice property A locally optimal choice is globally optimal. CS 421 - Introduction to Algorithms

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. CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Proof of Theorem Proof. Suppose (u, v)  T. Cut and paste. T: v u  A  V – A (u, v) = least-weight edge connecting A to V – A CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms Proof of Theorem Proof. Suppose (u, v)  T. Cut and paste. T: v u  A  V – A (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. CS 421 - Introduction to Algorithms

Prim’s Algorithm IDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least- weight 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   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) [v]  u ⊳ DECREASE-KEY At the end, {(v, [v])} forms the MST.

Example of Prim’s Algorithm   A  V – A  6 12 5 9 15     14 7 8     3 10  CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm   A  V – A 6 12 5 9 15     14 7 8    3 10  s CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm   A  V – A 6 12 5 9 15  7   14 7 8  15  3 10 10 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm   A  V – A 6 12 5 9 15  7   14 7 8  15  3 10 10 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 12  A  V – A 6 12 5 9 15 5 7 9  14 7 8  15  3 10 10 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 12  A  V – A 6 12 5 9 15 5 7 9  14 7 8  15  3 10 10 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 6  A  V – A 6 12 5 9 15 5 7 9  14 7 8 14 15  3 10 8 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 6  A  V – A 6 12 5 9 15 5 7 9  14 7 8 14 15  3 10 8 8 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 6  A  V – A 6 12 5 9 15 5 7 9  14 7 8 3 15  3 10 8 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 6  A  V – A 6 12 5 9 15 5 7 9  14 7 8 3 15  3 10 8 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 6  A  V – A 6 12 5 9 15 5 7 9  14 7 8 3 15  3 10 8 CS 421 - Introduction to Algorithms

Example of Prim’s Algorithm 6  A  V – A 6 12 5 9 15 5 7 9  14 7 8 3 15  3 10 8 CS 421 - Introduction to Algorithms

Analysis of Prim‘s Algorithm Q  V key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   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) [v]  u CS 421 - Introduction to Algorithms

Analysis of Prim‘s Algorithm Q  V key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   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) [v]  u (V) total |V | times degree(u) times Handshaking Lemma  (E) implicit DECREASE-KEY’s. CS 421 - Introduction to Algorithms

Analysis of Prim‘s Algorithm Q  V key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   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) [v]  u (V) total |V | times degree(u) times Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY CS 421 - Introduction to Algorithms

Analysis of Prim (cont'd) Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY TEXTRACT-MIN TDECREASE-KEY O(V) O(1) Q Total array binary heap Fibonacci heap O(V2) O(E lg V) O(E + V lg V) worst case O(lg V) O(lg V) amortized O(lg V) O(1) amortized CS 421 - Introduction to Algorithms

CS 421 - Introduction to Algorithms MST Algorithms Kruskal’s algorithm (see CLRS): Uses the disjoint-set data structure Running time = O(E lg V). Best to date: Karger, Klein, and Tarjan [1993]. Randomized algorithm. O(V + E) expected time. CS 421 - Introduction to Algorithms