Lecture19-greedy-minspanningtree

# Greedy takes a set c and returns optionally a set an

• Notes
• 35

This preview shows page 7 - 13 out of 35 pages.

Greedy takes a set C and returns: § Optionally: a set § An optimal value for the quantity of interest OR § A result  indicating failure

Subscribe to view the full document.

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} if feasible (S{x}) then S  S  {x} if solution (S) then return S else return  Generalizing Greedy Algs. Start off with S empty, since we haven’t added anything to the solution
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} if feasible (S{x}) then S  S  {x} if solution (S) then return S else return  Generalizing Greedy Algs. 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.)

Subscribe to view the full document.

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} if feasible (S{x}) then S  S  {x} if solution (S) then return S else return  Generalizing Greedy Algs. 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?
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} if feasible (S{x}) then S  S  {x} if solution (S) then return S else return  Generalizing Greedy Algs. After going through the loop, 1. either S is a solution or 2. there aren’t any solutions

Subscribe to view the full document.

Returning to Coins Algorithm 1.
You've reached the end of this preview.
• Spring '08
• Jones,M
• Greedy algorithm, Kruskal's algorithm

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern