Basic of Algorithms Analysis: Computational Tractability
Basic of Algorithms Analysis: Computational Tractability
As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise - By what course of calculation can these results be arrived at by the machine in the shortest time? - Charles Babbage
"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
Computational Tractability
Worst case running time. Obtain bound on largest possible running time of algorithm on input of a given size N, and see how this scales with N. Generally captures efficiency in practice. Draconian view, but hard to find effective alternative.
! !
Why It Matters
Desirable scaling property. When the input size increases by a factor of 2, the algorithm should only slow down by some constant factor C.
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.
Def. An algorithm is efficient if it has polynomial running time. Justification. It really works in practice!
3 4
Notation
Slight abuse of notation. T(n) = O(f(n)). Vacuous statement. Any comparison-based sorting algorithm requires at least O(n log n) comparisons.
Properties
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).
! ! !
10
foreach set Si { foreach other set Sj { foreach element p of Si { determine whether p also belongs to Sj } if (no element of Si belongs to Sj) report that Si and Sj are disjoint } }
Chapter 5
11
12
Exponential Time
Independent set. Given a graph, what is maximum size of an independent set? O(n2 2n) solution. Enumerate all subsets.
S* % & foreach subset S of nodes { check whether S in an independent set if (S is largest independent set seen so far) update S* % S } }
Check whether S is an independent set = O(k2). Number of k element subsets = " n % n (n ( 1) (n ( 2) L (n ( k + 1) nk = ) $ ' 2 k k O(k n / k!) = O(n ). k ( k ( 1) ( k ( 2) L (2) (1) k! #k &
assuming k is a constant
!
13 14