07-greedy - Algorithms Lecture 7: Greedy Algorithms [ Fa10...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Algorithms Lecture 7: Greedy Algorithms [ Fa10 ] 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 greedmark my wordswill 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 isnt 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 lets 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....
View Full Document

This note was uploaded on 10/14/2011 for the course ECON 101 taught by Professor Smith during the Spring '11 term at West Virginia University Institute of Technology.

Page1 / 12

07-greedy - Algorithms Lecture 7: Greedy Algorithms [ Fa10...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online