Beruflich Dokumente
Kultur Dokumente
Goal
What is an algorithm? Why do we analyze them? Introduction to analysis of algorithms Insertion sort Merge sort notation
Lecture 1-1
What is an algorithm?
A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational steps that transforms the input into the output.)
Lecture 1-2
Analysis of Algorithms
ProblemSorting
Lecture 1-3
Pseudocode notations
liberal use of English use of indentation for block structure employ any clear and concise expressive methods typically are not concerned with software engineering issues error handling data abstraction modularity. ...with the exception of adding good comments.
Lecture 1-4
Insertion Sort
Lecture 1-5
Lecture 1-6
Lecture 1-7
Running time
For a given input, we can characterize an algorithms running time. What do we mean by time? CPU? Wall-clock? doesnt it depend on implementation?
Lecture 1-8
Running time
Typically, count primitive operations Examples Indexing an array Moving an item Loop accounting Makes characterization independent of relative speed (on same machine) absolute speed (on different machines) So, for sorting that leaves input size (e.g. 6 elements vs.6000) input itself (e.g. partially sorted already) Generally want upper bound Experiments suggest performance, but analysis gives user a guarantee about performance
Lecture 1-9
Running time
One natural simplification Express running time as a function of input size, rather than of a particular input. But now there is more than one answer (one for each distinct input size). What to do?
Lecture 1-10
Kinds of analysis
Express as function T(n) of input size n (usually)Worst case T(n)=max time on any input of size n (sometimes)Average case T(n)=average time over all inputs of size n (assumes statistical distribution of inputs) (almost never)Best caseBad Give slow algorithm that is fast on some input. Useful only for showing lousy lower bound. For insertion sort, what is worst-case time?
Lecture 1-11
Asymptotic analysis
Ignore machine-dependent constants Look at growth of T(n) as n notation Drop low-order terms Ignore leading constants
Lecture 1-12
Lecture 1-13
Lecture 1-14
Lecture 1-15
Lecture 1-16
Lecture 1-17
Merge Sort
To sort n numbers
Lecture 1-18
Basic step
Merge two already sorted lists into one sorted list
Lecture 1-19
Recursive algorithm.
Lecture 1-20
Lecture 1-21
Recurrence
describes a function recursively in terms of itself. describes performance of recursive algorithms.
Lecture 1-22
Lecture 1-23
Recursion tree
Lecture 1-24
Lecture 1-25
Conclusions
Lecture 1-26