1 - Basics and Asymptotic Analysis
1 - Basics and Asymptotic Analysis
Text Book
Textbook: Introduction to Algorithms by Thomas H.
Cormen, Charles E. Leiserson, Ronald L. Rivest and
Clifford Stein. 3rd Edition
Reference: Algorithm Design by Jon Kleinberg and Eva
Tardos. (Published by Addison Wesley).
Introduction
What is an algorithm?
An algorithm is an effective method for solving a
problem using a finite sequence of instructions.
Examples
sort a list of numbers
find a route from one place to another (cars, packet routing,
phone routing, ...)
find the longest common substring between two strings
microchip wiring/design (VLSI)
solving sudoku
cryptography
compression (file, audio, video)
pagerank
classify a web page
Properties of interest
What properties of algorithms are we interested
in?
does it terminate?
is it correct, i.e. does it do what we think it’s supposed
to do?
what are the computational costs?
what are the memory/space costs?
what happens to the above with different inputs?
how difficult is it to implement and implement
correctly?
Motivation
Why are we interested?
For example:
15 , 9 , 2, 3, 1, 20 , 25, 14 (input)
1 , 2 , 3, 9 , 14, 15, 20, 25 (sorted output)
Insertion Sort (A)
Input: A is a set of unsorted integer values
Output: Sorted set
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Insertion Sort
runtimes
For example…
f (n) takes n2
steps
1
2
f1 ( x ) 3n
2 f 2 ( x) 1 / 2n 2 100
O(n ) 2
f 3 ( x) n 5n 40
f 4 ( x) 6n
Big O: Upper bound
O(g(n)) is the set of functions:
there exists positive constants c and n0 such that
O( g (n)) f (n) :
0 f (n) cg (n) for all n n0
2
f1 ( x) 3n
2 f 2 ( x) 1 / 2n 2 100
( n ) 2
f 3 ( x) n 5n 40
3
f 4 ( x) 6n
Theta: Upper and lower bound
Θ(g(n)) is the set of functions:
there exists positive constants c1 , c2 and n0 such that
( g (n)) f (n) :
0 c1 g (n) f (n) c2 g (n) for all n n0
Theta: Upper and lower bound
Θ(g(n)) is the set of functions:
there exists positive constants c1 , c2 and n0 such that
( g (n)) f (n) :
0 c1 g (n) f (n) c2 g (n) for all n n0
2
f1 ( x) 3n
2 f 2 ( x) 1 / 2n 2 100
( n ) 2
f 3 ( x) n 5n 40
2
f 4 ( x) 3n n log n
Visually
f(n)
Visually: upper bound
f(n)
n0
Visually: lower bound
f(n)
n0
worst-case vs. best-case vs.
average-case
worst-case: what is the worst the running time of the
algorithm can be?
best-case: what is the best the running time of the
algorithm can be?
average-case: given random data, what is the
running time of the algorithm?
Don’t confuse this with O, Ω and Θ. The cases
above are situations, asymptotic notation is about
bounding particular situations
Proving bounds: find constants
that satisfy inequalities
Show that 5n2 – 15n + 100 is Θ(n2)
Step 1: Prove O(n2) – Find constants c and n
0
such that 5n2 – 15n + 100 ≤ cn2 for all n > n0
2
cn 5n 2 15n 100
c 5 15 / n 100 / n 2
2
cn 5n 2 15n 100
c 5 15 / n 100 / n 2
2n -15n2 + n3 log n
nlog n + n2 + 15n3
n5 + n! – nn
Some examples
O(1)– constant. Fixed amount of work,
regardless of the input size
add two 32 bit numbers
determine if a number is even or odd
sum the first 20 elements of an array
delete an element from a doubly linked list
O(logn) – logarithmic. At each iteration,
discards some portion of the input (i.e. half)
binary search
Some examples
O(n)– linear. Do a constant amount of work
on each element of the input
find an item in a linked list
determine the largest element in an array
O(nlog n) log-linear. Divide and conquer
algorithms with a linear amount of work to
recombine
Sort a list of number with MergeSort
FFT
Some examples
O(n2) – quadratic. Double nested loops that
iterate over the data
Insertion sort
O(2n) – exponential
Enumerate all possible subsets
Traveling salesman using dynamic programming
O(n!)
Enumerate all permutations
determinant of a matrix with expansion by minors