Week 1
Week 1
Algorithms
Prerequisites
Topics
Topics
Topics
Topics
Evaluation
Why Study Algorithms?
● Important for all other branches of computer science
● Routing protocols in communication networks piggyback on classical
shortest path algorithms.
● Public-key cryptography relies on efficient number-theoretic
algorithms.
● Computer graphics requires the computational primitives supplied by
geometric algorithms.
● Database indices rely on balanced search tree data structures.
● Computational biology uses dynamic programming algorithms to
measure genome similarity
Why Study Algorithms?
Good for the brain! Fun
Integer Multiplication
● Input: Two n digit numbers x and y
● Output: product of x and y
● Primitive operations: add or multiply
The Grade-School Algorithm
The Grade-School Algorithm
Can we do better?
Karatsuba Multiplication
Karatsuba Multiplication
Karatsuba Multiplication
Karatsuba Multiplication
Which multiplication is better?
Karatsuba Multiplication
Analysis of algorithms
Measuring efficiency of an algorithm
● Time: How long the algorithm takes (running time)
● Space: Memory requirement
Time and space
● Time depends on processing speed
○ Impossible to change for given hardware
● Space is a function of available memory
○ Easier to reconfigure, augment Typically,
● We will focus on time, not space
Measuring running time
● Analysis independent of underlying hardware
● Don’t use actual time
● Measure in terms of “basic operations”
● Typical basic operations?
RAM Model
● Algorithms can be studied in a machine/language independent way.
Because we use the RAM model of computation for all our analysis.
RAM Model
● Algorithms can be studied in a machine/language independent way.
Because we use the RAM model of computation for all our analysis.
● Each ”simple” operation (+, −, =, if) takes 1 step.
● Loops and subroutine calls ?
RAM Model
● Algorithms can be studied in a machine/language independent way.
Because we use the RAM model of computation for all our analysis.
● Each ”simple” operation (+, −, =, if ) takes 1 step.
● Loops and subroutine calls are not simple operations. They depend
upon the size of the data and the contents of a subroutine.
● Run time of an algorithm is measured by counting number of steps
Search for K in an unsorted array A
● Running time on input of size n varies across inputs
Best, Average, Worst Case Time
Complexity
● Best case complexity of an algorithm is the function defined by the
minimum number of steps taken on input data of n elements
● Average-case complexity of the algorithm is the function defined by
an average number of steps taken on input data of n elements
● Worst-case complexity indicates the maximum number of steps
performed on input data of n elements
Worst case complexity
● For each n, worst case input forces algorithm to take the maximum
amount of time
○ If K not in A, search scans all elements
● Upper bound for the overall running time
○ Here worst case is proportional to n for the array size n
Average case complexity
● Worst case may be very pessimistic
● Compute average time taken over all inputs
● Difficult to compute
○ Average over what?
○ Are all inputs equally likely?
○ Need probability distribution over inputs
Worst case vs average case
● Worst case can be unrealistic …
● … but average case is hard, if not impossible, to compute
Worst case vs average case
● Worst case can be unrealistic …
● … but average case is hard, if not impossible, to compute