SUNY Geneseo Department of Computer Science


Greedy Algorithms

Tuesday, April 2

CSci 242, Spring 2013
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Misc

Exam 2 is next Thursday (April 11)

Questions?

Greedy Algorithms

Example: I want to put my most favorite songs on a CD, which holds a total of C songs (oddly, they’re all the same length). I have a collection of S > C songs, each with a rating, ri, of how much I like song si. Give and prove correct a strategy for creating a CD with the maximum total rating for the songs it contains.

        while CD not full
            find highest-rated song, s, not yet on CD
            burn s to CD

Running time of above algorithm if it starts by sorting S songs by rating

    sort S songs into decreasing order by rating    // Θ(S logS) time
    i = 0
    while CD not full                               // C songs to fill CD, Θ(C) time
        burn sortedSongs[i] to CD

Now suppose songs have different lengths, si lasts mi minutes, CD holds M minutes

Could we solve this version of the CD problem by dynamic programming?

Hand out greedy algorithms problem set

Next

Minimum spanning trees

Minimum-cost set of edges between 4 vertices

Chapter 23


Next Lecture