07-greedy - Algorithms Lecture 7 Greedy Algorithms[Fa'10...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Algorithms Lecture 7: Greedy Algorithms [ Fa’10 ] The point is, ladies and gentleman, greed is good. Greed works, greed is right. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit. Greed in all its forms, greed for life, money, love, knowledge has marked the upward surge in mankind. And greed—mark my words—will save not only Teldar Paper but the other malfunctioning corporation called the USA. — Michael Douglas as Gordon Gekko, Wall Street (1987) There is always an easy solution to every human problem— neat, plausible, and wrong. — H. L. Mencken, “The Divine Afflatus”, New York Evening Mail (November 16, 1917) 7 Greedy Algorithms 7.1 Storing Files on Tape Suppose we have a set of n files that we want to store on a tape. In the future, users will want to read those files from the tape. Reading a file from tape isn’t like reading a file from disk; first we have to fast-forward past all the other files, and that takes a significant amount of time. Let L [ 1 .. n ] be an array listing the lengths of each file; specifically, file i has length L [ i ] . If the files are stored in order from 1 to n , then the cost of accessing the k th file is cost ( k ) = k X i = 1 L [ i ] . The cost reflects the fact that before we read file k we must first scan past all the earlier files on the tape. If we assume for the moment that each file is equally likely to be accessed, then the expected cost of searching for a random file is E [ cost ] = n X k = 1 cost ( k ) n = n X k = 1 k X i = 1 L [ i ] n . If we change the order of the files on the tape, we change the cost of accessing the files; some files become more expensive to read, but others become cheaper. Different file orders are likely to result in different expected costs. Specifically, let π ( i ) denote the index of the file stored at position i on the tape. Then the expected cost of the permutation π is E [ cost ( π )] = n X k = 1 k X i = 1 L [ π ( i )] n . Which order should we use if we want the expected cost to be as small as possible? The answer is intuitively clear; we should store the files in order from shortest to longest. So let’s prove this. Lemma 1. E [ cost ( π )] is minimized when L [ π ( i )] L [ π ( i + 1 )] for all i . Proof: Suppose L [ π ( i )] > L [ π ( i + 1 )] for some i . To simplify notation, let a = π ( i ) and b = π ( i + 1 ) . If we swap files a and b , then the cost of accessing a increases by L [ b ] , and the cost of accessing b decreases by L [ a ] . Overall, the swap changes the expected cost by ( L [ b ] - L [ a ]) / n . But this change is an improvement, because L [ b ] < L [ a ] . Thus, if the files are out of order, we can improve the expected cost by swapping some mis-ordered adjacent pair. c Copyright 2011 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License ( http://creativecommons.org/licenses/by-nc-sa/3.0/ ). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ 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