CS51A - Spring 2019 - Class 36
Lecture notes
nim tournament
"For me, great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing." -- Francis Sullivan
What is an algorithm?
- way for solving a problem
- method of steps to accomplish a task
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
- add two numbers
- microchip wiring/design (VLSI)
- solving sudoku
- cryptography
- compression (file, audio, video)
- spell checking
- pagerank
- classify a web page
- ...
Main parts to algorithm analysis
- developing algorithms that work
- making them faster
- analyzing/understanding the efficiency/run-time
What questions might we want to ask/measure about an algorithm?
- how long does it take to run? How efficient is it?
- how much memory does it use?
- does it finish?
- is it right?
- how hard is it to code?
asymptotic analysis
- Key idea: how does the run-time grow as we increase the input size?
- for example, if we double the input, what will happen to the run-time?
- unchanged?
- double?
- triple?
- quadruple?
- Big-O notation: an upper bound on the run-time
- describe how the run-time changes as the input time changes
- this gives us groups of methods/functions that behave similarly
- O(f(n)): as n increases, the run-time increases wrt f(n)
- O(n) = linear
- double the input size, double the run-time
- O(n^2) = quadratic
- double the input size, quadruple the run-time
- ..
- runtimes table
Revisit contains function on a list (i.e., "in")
- What is the Big-O for contains?
- O(n)
- the runtime increases linearly with the size of the list