Lecture19-greedy-minspanningtree

Greedy takes a set c and returns optionally a set an

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
Image of page 7

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
Image of page 8
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.)
Image of page 9

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?
Image of page 10
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
Image of page 11

Subscribe to view the full document.

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern