












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Slides by Kevin Wayne.Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
"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
Brute force. For many non-trivial problems, there is a natural brute force search algorithm that checks every possible solution. Typically takes 2N^ time or worse for inputs of size N. Unacceptable in practice.
Desirable scaling property. When the input size doubles, the algorithm should only slow down by some constant factor C.
Def. An algorithm is poly-time if the above scaling property holds.
There exists constants c > 0 and d > 0 such that on every input of size N, its running time is bounded by c Nd^ steps.
choose C = 2d
n! for stable matching with n men and n women
Worst case running time. Obtain bound on largest possible running time of algorithm on input of a given size N. Generally captures efficiency in practice. Draconian view, but hard to find effective alternative.
Average case running time. Obtain bound on running time of algorithm on random input as a function of input size N. Hard (or impossible) to accurately model real instances by random distributions. Algorithm tuned for a certain distribution may perform poorly on other inputs.
Slight abuse of notation. T(n) = O(f(n)). Not transitive:
Meaningless statement. Any comparison-based sorting algorithm requires at least O(n log n) comparisons. Statement doesn't "type-check." Use for lower bounds.
Transitivity. If f = O(g) and g = O(h) then f = O(h). If f = (g) and g = (h) then f = (h). If f = (g) and g = (h) then f = (h).
Additivity. If f = O(h) and g = O(h) then f + g = O(h). If f = (h) and g = (h) then f + g = (h). If f = (h) and g = O(h) then f + g = (h).
Linear time. Running time is proportional to input size.
Computing the maximum. Compute maximum of n numbers a 1 , …, an.
max a 1 for i = 2 to n { if (ai > max) max ai }
O(n log n) time. Arises in divide-and-conquer algorithms.
Sorting. Mergesort and heapsort are sorting algorithms that perform O(n log n) comparisons.
Largest empty interval. Given n time-stamps x 1 , …, xn on which copies of a file arrive at a server, what is largest interval of time when no copies of the file arrive?
O(n log n) solution. Sort the time-stamps. Scan the sorted list in order, identifying the maximum gap between successive time-stamps.
also referred to as linearithmic time
Quadratic time. Enumerate all pairs of elements.
Closest pair of points. Given a list of n points in the plane (x 1 , y 1 ), …, (xn, yn), find the pair that is closest.
O(n^2 ) solution. Try all pairs of points.
Remark. (n^2 ) seems inevitable, but this is just an illusion.
min (x 1 - x 2 )^2 + (y 1 - y 2 )^2 for i = 1 to n { for j = i+1 to n { d (xi - xj)^2 + (yi - yj)^2 if (d < min) min d } }
don't need to take square roots
see chapter 5
Independent set of size k. Given a graph, are there k nodes such that no two are joined by an edge?
O(nk) solution. Enumerate all subsets of k nodes.
Check whether S is an independent set = O(k^2 ). Number of k element subsets = O(k^2 nk^ / k!) = O(nk).
foreach subset S of k nodes { check whether S is an independent set if (S is an independent set) report S is an independent set } }
n k
n^ ( n^ 1) ( n^ ^ 2)^ ( n^ ^ k^ 1) k ( k 1) ( k 2) (2) (1)
n
k k! poly-time for k=17, but not practical
k is a constant
Independent set. Given a graph, what is maximum size of an independent set?
O(n^2 2 n) solution. Enumerate all subsets.
S* foreach subset S of nodes { check whether S is an independent set if (S is largest independent set seen so far) update S* S } }