320 likes | 465 Vues
This lecture focuses on greedy algorithms and the Minimal Spanning Tree (MST) problem, exploring the concepts and methodologies used to achieve optimal solutions. Key objectives include defining greedy algorithms, applying them to the coins problem, and understanding Kruskal's algorithm for finding a minimum spanning tree. The lecture will also emphasize the method of optimization through selecting the next beneficial piece in problem-solving. Students are encouraged to participate in a hands-on activity to illustrate these principles in practice.
E N D
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. CS 312: Algorithm Analysis Lecture #19: Greedy Algorithms and Minimal Spanning Trees Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick and figures from Dasgupta et al.
Announcements • HW #12 • Due now • Mid-Term Exam • Testing Center: ends Thursday • 3 hours max – beware closing time! • 1 page of notes written/typed by you • Project #4: Intelligent Scissors • Questions?
Objectives • Define a greedy algorithm • Solve the coins problem with a greedy algorithm • Define the Minimal Spanning Tree (MST) problem • Understand Kruskal’s Algorithm • Prove correctness of Kruskal’s Algorithm
Given: unbounded supply of coins of various denominations. Given: a number Find: minimal number of coins that add up to . Learning Activity Need a volunteer to solve the problem Explain why you did what you did at every step Everyone: write an algorithm to solve this problem (on scratch paper) Volunteer to write algorithm on the board. Coins Problem
Greedy Algorithms: Main Idea • Optimize some quantity of interest • “optimize”=minimize/maximize • Build up a solution piece by piece • Always choose the next piece that offers the most obvious and immediate benefit • Without violating given constraints
function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S while (C != & !solution(S)) x select(C) C C \ {x} iffeasible(S{x}) then S S {x} ifsolution(S) thenreturn S elsereturn Generalizing Greedy Algs.
function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S while (C != & !solution(S)) x select(C) C C \ {x} iffeasible(S{x}) then S S {x} ifsolution(S) thenreturn S elsereturn Greedy takes a set C and returns: Optionally: a set An optimal value for the quantity of interest OR A result indicating failure Generalizing Greedy Algs.
function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S while (C != & !solution(S)) x select(C) C C \ {x} iffeasible(S{x}) then S S {x} ifsolution(S) thenreturn S elsereturn Start off with S empty, since we haven’t added anything to the solution Generalizing Greedy Algs.
function greedy (C) Input: set of candidates C Output: solution S (a set),optimal quantity S while (C != & !solution(S)) x select(C) C C \ {x} iffeasible(S{x}) then S S {x} ifsolution(S) thenreturn S elsereturn As long as … There are still candidates to choose from And S is not a solution… (solution() function returns true iff S is a solution.) Generalizing Greedy Algs.
function greedy (C) Input: set of candidates C Output: solution S (a set) ,optimal quantity S while (C != & !solution(S)) x select(C) C C \ {x} iffeasible(S{x}) then S S {x} ifsolution(S) thenreturn S elsereturn Select the next-best candidate from C Remove this candidate from C Add this candidate to S, if it’s feasible. select(): choose the next best candidate feasible(): if x is added, is it possible to get a solution? Generalizing Greedy Algs.
function greedy (C) Input: set of candidates C Output: solution S (a set) ,optimal quantity S while (C != & !solution(S)) x select(C) C C \ {x} iffeasible(S{x}) then S S {x} ifsolution(S) thenreturn S elsereturn After going through the loop, either S is a solution or 2. there aren’t any solutions Generalizing Greedy Algs.
Returning to Coins Algorithm • What are the candidates? • What is the solution set? • Solution test? • Select fn.? • Feasibility test? • Objective?
Problem: Minimal Spanning Trees • Given: A connected, undirected graph G=V,E • V = a finite set of vertices • E VV = a finite set of edges • w:ER0 = a cost function. 1 2 1 2 3 5 6 4 4 6 3 8 4 5 6 7 4 3 7
Problem: Minimal Spanning Trees • Given: A connected, undirected graph G=V,E • V = a finite set of vertices • E VV = a finite set of edges • w:ER0 = a cost function. • Find: a set of edges that • includes every vertex in V • forms a tree (i.e., no cycles) • has minimum possible cost 1 2 1 2 3 5 6 4 4 6 3 8 4 5 6 7 4 3 7
Problem: Minimal Spanning Trees • Two solutions: • Kruskal’s Algorithm • Prim’s Algorithm • (You’ve seen these before in CS 236) • (More depth this time) • Application: • Networking computers • Plumbing • Electrical network • Approximation to Hamiltonianpath! • … 1 2 1 2 3 5 6 4 4 6 3 8 4 5 6 7 4 3 7
Kruskal’s Algorithm • Idea: Repeatedly add the next lightest edge that doesn’t produce a cycle. • Greedy?
1 2 1 2 3 5 6 6 4 4 3 8 4 5 6 7 4 3 7 Kruskal’s Algorithm Sort edges by cost 1: {1,2} 2: {2,3} 3: {4,5} 3: {6,7} 4: {1,4} 4: {2,5} 4: {4,7} 5: {3,5}
Kruskal’s Algorithm Make each vertex a component 1 2 1: {1,2} {1}{2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm Add first edge to T 1 2 1: {1,2} {1}{2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm Merge vertices in added edges 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm Process each edge in order 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} {1,2,3,4,5}{6,7} 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm Must join separate components 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} {1,2,3,4,5}{6,7} rejected 4: {2,5} 7 4 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm Stop when all vertices connected 1 2 1: {1,2} {1,2}{3}{4}{5}{6}{7} 1 2 3 2: {2,3} {1,2,3}{4}{5}{6}{7} 5 6 6 4 {1,2,3}{4,5}{6}{7} 4 3: {4,5} {1,2,3}{4,5}{6,7} 3: {6,7} 3 8 4 5 6 4: {1,4} {1,2,3,4,5}{6,7} rejected 4: {2,5} 7 4 {1,2,3,4,5,6,7} done 3 4: {4,7} 5: {3,5} 7
Kruskal’s Algorithm Candidate Set Solution Set Selection Function Feasibility Test Solution Test break if size(X) == |V|-1 • Identify: • Important operations • Elements of a Greedy algorithm Objective: Minimize Spanning Tree Cost
Kruskal’s Algorithm break if size(X) == |V|-1 • Identify: • Important operations • Elements of a Greedy algorithm Objective: Minimize Spanning Tree Cost
3 Questions • Is it correct? • Now • How long does it take? • Next time • Can we do better? • Next time
Correctness • Depends on the idea of a “cut” • Cut Property (a Lemma): Suppose edges X are part of a minimum spanning tree of G=(V,E). Pick any subset of nodes S for which X does not cross between S and V-S, and let e be the lightest edge across this partition. Then X U {e} is part of some MST
Correctness: Kruskal’s Algorithm Theorem: Kruskal’s Algorithm finds a minimum spanning tree Basis: X = is part of a minimum spanning tree This is because: a) part of every set b) An MST must exist since G is connected
Correctness: Kruskal’s Algorithm Theorem: Kruskal’s Algorithm finds a minimum spanning tree Induction Step: Assume is part of an MST S 1 2 • E • Choose S to be a connected component on one end of the edge, , chosen by Kruskall’s • S vs. V-S is a cut • No edge in leaves S • is lightest edge that leaves • Cut Property holds • U {e} is part of an MST u 5 6 6 4 4 3 8 v 7 4 3