Monday, November 28, 2011

Lecture 1 Algorithms



LECTURE 1
Analysis of Algorithms
Insertion sort
Asymptotic analysis
Merge sort
Recurrences
Prof. Charles E. Leiserson
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
Course information
1.Staff8.Course website
2.Distance learning9.Extra help
3.Prerequisites10.Registration
4.Lectures11.Problem sets
5.Recitations12.Describing algorithms
6.Handouts13.Grading policy
7.Textbook14.Collaboration policy
September 7, 2005Introduction to AlgorithmsL1.2
Analysis of algorithms
The theoretical study of computer-program performance and resource usage.
What’s more important than performance?
• modularity• user-friendliness
• correctness• programmer time
• maintainability• simplicity
• functionality• extensibility
• robustness• reliability
 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 
September 7, 2005Introduction to AlgorithmsL1.3
Why study algorithms and performance?
Algorithms help us to understand scalability.
Performance often draws the line between what is feasible and what is impossible.
Algorithmic mathematics provides a languagefor talking about program behavior.
Performance is the currency of computing.
The lessons of program performance generalize to other computing resources.
Speed is fun!
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.4
 The problem of sorting
Input: sequence <a1a2, …, anof numbers.
Output: permutation <a'1, a'2, a'n>such that a'a'… a'.
Example:
Input: 8 2 4 9 3 6
Output: 2 3 4 6 8 9
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.5
Insertion sort 
 INSERTION-SORT (An)A[1 . . n]
 
 for ←2 to n 
 do key ←Aj] 
 i ←j – 1 
“pseudocode” 
while i > and A[i] > key
 
 do A[i+1] ←A[i]
 i ←i – 1
 A[i+1] = key 
  
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.6
Insertion sort 
   INSERTION-SORT (An)A[1 . . n]
   
    for ←2 to n 
    do key ←Aj] 
    i ←j – 1 
“pseudocode”  
 while i > and A[i] > key
    
    do A[i+1] ←A[i]
    i ←i – 1
    A[i+1] = key 
     
1i jn
A:      
       
  sortedkey 
    
  Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 
September 7, 2005Introduction to AlgorithmsL1.7
 Example of insertion sort
8 2 4 9 3 6
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.8
 Example of insertion sort
8 2 4 9 3 6
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.9
 Example of insertion sort
824936
284936
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.10
 Example of insertion sort
824936
284936
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.11
 Example of insertion sort
824936
284936
248936
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.12
 Example of insertion sort
824936
284936
248936
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.13
 Example of insertion sort
824936
284936
248936
248936
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.14
 Example of insertion sort
824936
284936
248936
248936
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.15
 Example of insertion sort
824936
284936
248936
248936
234896
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.16
 Example of insertion sort
824936
284936
248936
248936
234896
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.17
 Example of insertion sort
824936 
284936 
248936 
248936 
234896 
234689done
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.18
 Running time
The running time depends on the input: an already sorted sequence is easier to sort.
Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones.
Generally, we seek upper bounds on the running time, because everybody likes a guarantee.
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.19
 Kinds of analyses
Worst-case: (usually)
T(n) = maximum time of algorithm on any input of size n.
Average-case: (sometimes)
T(n) = expected time of algorithm over all inputs of size n.
Need assumption of statistical distribution of inputs.
Best-case: (bogus)
• Cheat with a slow algorithm that works fast on some input.
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.20
 Machine-independent time
What is insertion sort’s worst-case time?
It depends on the speed of our computer:
relative speed (on the same machine),
absolute speed (on different machines).
BIG IDEA:
Ignore machine-dependent constants.
Look at growth of T(nas →8 .
“Asymptotic Analysis”
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.21
 T-notation
Math:
T(g(n)) = (n) : there exist positive constants c1c2, and
nsuch that cg(n(ncg(n)for all n}
Engineering:
Drop low-order terms; ignore leading constants.
Example: 3n+ 90n– 5+ 6046 = T(n3)
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.22
 Asymptotic performance
When gets large enough, a T(n2algorithmalways beats a T(n3algorithm.
  • We shouldn’t ignore
  asymptotically slower
  algorithms, however.
  • Real-world design
T(n) situations often call for a
 careful balancing of
  engineering objectives.
  • Asymptotic analysis is a
nn0useful tool to help to
structure our thinking.
 
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.23
 Insertion sort analysis
Worst case: Input reverse sorted.
n
2 
(n= Tj= T()[arithmetic series]
 
j=2 
Average case: All permutations equally likely.(nT/ 2) = T(n)
j=2
Is insertion sort a fast sorting algorithm?
Moderately so, for small n.
Not at all, for large n.
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.24
 Merge sort
MERGE-SORT A[1 . . n]
1.If = 1, done.
2.Recursively sort A[ 1 . . n/2 ]and An/2 +1 . . .
3.Merge” the sorted lists.
Key subroutine: MERGE
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.25
 Merging two sorted arrays
20 12
13 11
7 9
2 1
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.26
 Merging two sorted arrays
20 12
13 11
7 9
2 1
1
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.27
 Merging two sorted arrays
20122012
13111311
7979
212 
1
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.28
 Merging two sorted arrays
20122012
13111311
7979
212 
12
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.29
 Merging two sorted arrays
201220122012
131113111311
797979
212   
12
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.30
 Merging two sorted arrays
201220122012
131113111311
797979
212   
127
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.31
 Merging two sorted arrays
2012201220122012
1311131113111311
797979 9
212     
127
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.32
 Merging two sorted arrays
2012201220122012
1311131113111311
797979 9
212     
1279
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.33
 Merging two sorted arrays
20122012201220122012
13111311131113111311
797979 9  
212       
1279
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.34
 Merging two sorted arrays
20122012201220122012
13111311131113111311
797979 9  
212       
127911
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.35
 Merging two sorted arrays
201220122012201220122012
1311131113111311131113 
797979 9    
212         
127911
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.36
 Merging two sorted arrays
201220122012201220122012
1311131113111311131113 
797979 9    
212         
12791112
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.37
 Merging two sorted arrays
201220122012201220122012
1311131113111311131113 
797979 9    
212         
12791112
Time T(nto merge a total of elements (linear time).
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.38
 Analyzing merge sort
T(n)MERGE-SORT A[1 . . n]  
T(1)1.If = 1, done.  
2T(n/2)  
    
Abuse2.Recursively sort A[ 1 . . n/2 ]
 and An/2 +1 . . .  
T(n)   
3.“Merge” the sorted lists  
   
Sloppiness: Should be Tn/2 ) + Tn/2 ) , but it turns out not to matter asymptotically.
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.39
 Recurrence for merge sort
T(n) =
T(1) if = 1;
2T(n/2) + T(nif > 1.
We shall usually omit stating the base case when T(n) = T(1) for sufficiently small n, but only when it has no effect on the asymptotic solution to the recurrence.
CLRS and Lecture 2 provide several ways to find a good upper bound on T(n).
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.40
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.41
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
T(n)
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.42
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
cn
T(n/2)T(n/2)
 
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.43
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
  cn 
cn/2 cn/2
  
T(n/4)T(n/4)T(n/4)T(n/4)
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.44
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
cn
 cn/2cn/2 
   
cn/4cn/4cn/4cn/4
   
T(1)   
 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 
September 7, 2005Introduction to AlgorithmsL1.45
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
cn
  cn/2cn/2 
    
= lg ncn/4cn/4cn/4cn/4
    
T(1)   
  Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 
September 7, 2005Introduction to AlgorithmsL1.46
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
cn  cn
  cn/2cn/2 
    
= lg ncn/4cn/4cn/4cn/4
    
T(1)   
  Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 
September 7, 2005Introduction to AlgorithmsL1.47
 Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.
cn  cn
  cn/2cn/2cn
    
= lg ncn/4cn/4cn/4cn/4
    
T(1)   
  Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 
September 7, 2005Introduction to AlgorithmsL1.48
Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.cn  cn
  cn/2 cn/2cn
     
= lg ncn/4cn/4cn/4cn/4cn
    
     
T(1)    
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.49
Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.cn  cn
  cn/2  cn/2cn
      
= lg ncn/4cn/4cn/4cn/4cn
      
       
      
T(1) #leaves n  T(n)
    
   
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.50
Recursion tree
Solve T(n) = 2T(n/2) + cn, where > 0 is constant.cn  cn
  cn/2  cn/2cn
      
= lg ncn/4cn/4cn/4cn/4cn
      
       
      
T(1) #leaves n  T(n)
    
        
      Total = T(lg n)
  Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 
September 7, 2005Introduction to AlgorithmsL1.51
 Conclusions
T(lg ngrows more slowly than T(n2).
Therefore, merge sort asymptotically beats insertion sort in the worst case.
In practice, merge sort beats insertion sort for > 30 or so.
Go test it out for yourself!
Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson
September 7, 2005Introduction to AlgorithmsL1.52