Académique Documents
Professionnel Documents
Culture Documents
Analysis of Algorithms
How good is the algorithm?
Correctness Time efficiency Space efficiency Simplicity
Analysis of Algorithms
Issues:
Correctness (Is it guaranteed to produce a correct correspondence between inputs and outputs?) Time efficiency (How fast does it run?) Space efficiency (How much extra space does it require?) Optimality (Is this provably the best possible solution?)
Approaches:
Theoretical analysis Empirical analysis Visualisation
Efficiency
Can analyse efficiency with respect to either:
running time
how fast easier to improve I often say when you can measure what you are speaking about and express it in numbers you know something about it; but when you cannot measure it, when you cannot express it in numbers, your knowledge of it is of a meagre and unsatisfactory kind: it may be the beginning of knowledge but you have scarcely, in your thoughts, advanced to the stage of science, whatever the matter may be. .
memory space
how big less important than before
Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size Basic operation: the operation that contributes most towards the running time of the algorithm
input size
T(n) copC(n)
running time execution time for basic operation number of times basic operation is executed
Classical Problems
Problem
Search for key in list of n items Matrix Multiplication Compute an Graph problem
Basic operation
Key comparison Floating point multiplication Floating point multiplication Visiting a vertex or traversing an edge
Objectives
To outline a general analytic framework To introduce the conceptual tools of:
Asymptotic Notations Base Efficiency Classes
Best case:
B(n) min over inputs of size n
Average case:
A(n) avg over inputs of size n Number of times the basic operation will be executed on typical input NOT the average of worst and best case INSTEAD expected number of ops considered as a random variable under some assumption about the probability distribution of all possible inputs of size n most difficult to estimate, as need to make assumptions about the possible inputs of size n
8
Amortized Efficiency
From a data structure perspective, the total time of a sequence of operations is important
Real-time apps are an exception R. E. Tarjan - recipient of 1986 Turing Award [TARJAN87]
Though in some situations a single operation can be expensive, the total time for a sequence n of such operations is always significantly better than the worst-case efficiency multiplied by n Amortized efficiency:
amortized time defined as the time of an operation averaged over a worst-case sequence of operations
9
Algorithm:
Scan the list and compare successive elements with K until either a matching element is found (successful search) or the list is exhausted (unsuccessful search)
Calculate the:
Worst case? Best case? Average case?
10
11
Order of Growth
Most important: Order of growth within a constant multiple as n Example:
How much faster will algorithm run on a computer that is twice as fast? How much longer does it take to solve problem of double input size?
12
Asymptotic Notations
A way of comparing functions that ignores constant factors and small input sizes O(g(n)):
Class of functions f(n) that grow no faster than g(n) f(n) c g(n) for all n n0
(g(n)):
Class of functions f(n) that grow at least as fast as g(n) f(n) c g(n) for all n n0
(g(n)):
Class of functions f(n) that grow at the same rate as g(n) c2 g(n) f(n) c1 g(n) for all n n0
13
14
15
16
17
Example:
10n vs. 2n2 limn 10n/2n2 = 5 limn 1/n = 0
Exercises:
n(n+1)/2 logb n vs. vs. n2 logc n
18
LHpitals rule
If
limn f(n) = limn g(n) = and the derivatives f ', g' exist,
Then
limn f(n) / g(n) = limn f '(n) / g'(n)
Example:
log n vs. n limn log n / n = limn (1/n)/1 = 0 So, order log n < order n (actually little-o)
19
20
Principal change in the second edition is the chapter on iterative improvement will do the simplex method also included more puzzles
21
21
Objectives
To outline a general analytic framework To introduce the conceptual tools of:
Asymptotic Notations Base Efficiency Classes
22
24 11
24
Selection sort
Find smallest element in remainder of list and swap with current element
Insertion sort
Assume sub-list is sorted and insert current element
Mystery Algorithm
Exercise
27
Matrix Multiplication
=
1. n = matrix order 2. Basic op = multiplication or addition 3. Best case = worst case = average case
28
Selection Sort
-
n = number of list elements basic op = comparison best case = worst case = average case
29
Insertion Sort
-
n = number of list elements basic op = comparision best case != worst case != average case best case: A[j] > v executed only once on each iteration
30
32
32
How many one-by-one squares are generated by the algorithm that starts with a single square and on each of its n iterations adds squares all around the outside? von Neumann neighborhoods for ranges r==0, 1, 2, and 3 are illustrated above.
33
33
34
34
Recurrence Relations
Definition:
An equation or inequality that describes a function in terms of its value on smaller inputs
Recurrence:
The recursive step E.g., M(n) = M(n-1) + 1 for n > 0
Initial Condition:
The terminating step E.g., M(0) = 0 [call stops when n = 0, no mults when n = 0]
Must differentiate the recurrence relation for the factorial calculation from the recurrence relation for the number of basic operations
36
Pattern: Ultimately:
37
38
Towers of Hanoi
Recursive solution:
to move n>1 disks from Peg 1 to Peg 3 (using Peg 2 as holder):
first move n-1 disks from Peg 1 to Peg 2 (using Peg 3 as holder) then move largest directly to Peg 3 then move n-1 from Peg 2 to Peg 3, (using Peg 1 as holder).
39
39
40
40
41
41
Algorithms Business by numbers Sep 13th 2007 From The Economist print edition Consumers and companies increasingly depend on a hidden mathematical world Illustration by Gillian Blease ALGORITHMS sound scary, of interest only to dome-headed mathematicians. In fact they have become the instruction manuals for a host of routine consumer transactions. Browse for a book on Amazon.com and algorithms generate recommendations for other titles to buy. Buy a copy and they help a logistics rm to decide on the best delivery route. Ring to check your order's progress and more algorithms spring into action to determine the quickest connection to and through a call-centre. From analysing credit-card transactions to deciding how to stack supermarket shelves, algorithms now underpin a large amount of everyday life. Their pervasiveness reects the application of novel computing power to the age-old complexities of business. No human being can work fast enough to process all the data available at a certain scale, says Mike Lynch, boss of Autonomy, a computing rm that uses algorithms to make sense of unstructured data. Algorithms can. As the amount of data on everything from shopping habits to media consumption increases and as customers choose more personalisation, algorithms will only become more important. Algorithms can take many forms. At its core, an algorithm is a step-by-step method for doing a job. These can be prosaica recipe is an algorithm for preparing a mealor they can be anything but: the decision-tree posters that hang on hospital walls and which help doctors work out what is wrong with a patient from his symptoms are called medical algorithms. This formulaic style of thinking can itself be a useful tool for businesses, much like the rigour of good project-management. But computers have made algorithms far more valuable to companies. A computer program is a written encoding of an algorithm, 42 explains Andrew Herbert, who runs Microsoft Research in Cambridge, Britain. The speed and processing power of computers mean that algorithms can execute tasks with blinding speed using vast amounts of data. 42
Describes the growth pattern of Rabbits. Which is exponential, just ask Australia! Fibonacci recurrence:
F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1
44
45
46
4. Implement the Algorithm 5. Generate a Sample Set of Inputs 6. Run the Algorithm and Record the Empirical Results 7. Analyze the Data
Regression Analysis is often used to fit a function to the scatterplot of results
47
Algorithm Visualisation
Definition:
Algorithm Visualisation seeks to convey useful algorithm information through the use of images
Flavours:
Static algorithm visualisation Algorithm animation
Some new insights: e.g., odd and even disks in Towers of Hanoi Attributes of Information Visualisation apply overview, zoom and filter, detail on demand,
48