02-05-2012, 01:01 PM
Analysis of Algorithms
Analysis of Algorithms.ppt (Size: 803.5 KB / Downloads: 36)
Introduction
What is Algorithm?
a clearly specified set of simple instructions to be followed to solve a problem
Takes a set of values, as input and
produces a value, or set of values, as output
May be specified
In English
As a computer program
As a pseudo-code
Data structures
Methods of organizing data
Program = algorithms + data structures
Why need algorithm analysis ?
writing a working program is not good enough
The program may be inefficient!
If the program is run on a large data set, then the running time becomes an issue
Example: Selection Problem
Given a list of N numbers, determine the kth largest, where k N.
Algorithm 1:
(1) Read N numbers into an array
(2) Sort the array in decreasing order by some simple algorithm
(3) Return the element in position k
Algorithm 2:
(1) Read the first k elements into an array and sort them in decreasing order
(2) Each remaining element is read one by one
If smaller than the kth element, then it is ignored
Otherwise, it is placed in its correct spot in the array, bumping one element out of the array.
(3) The element in the kth position is returned as the answer.
Example: Selection Problem…
Which algorithm is better when
N =100 and k = 100?
N =100 and k = 1?
What happens when N = 1,000,000 and k = 500,000?
There exist better algorithms
Algorithm Analysis
We only analyze correct algorithms
An algorithm is correct
If, for every input instance, it halts with the correct output
Incorrect algorithms
Might not halt at all on some input instances
Might halt with other than the desired answer
Analyzing an algorithm
Predicting the resources that the algorithm requires
Resources include
Memory
Communication bandwidth
Computational time (usually most important)
Algorithm Analysis…
Factors affecting the running time
computer
compiler
algorithm used
input to the algorithm
The content of the input affects the running time
typically, the input size (number of items in the input) is the main consideration
E.g. sorting problem the number of items to be sorted
E.g. multiply two matrices together the total number of elements in the two matrices
Machine model assumed
Instructions are executed one after another, with no concurrent operations Not parallel computers
Worst- / average- / best-case
Worst-case running time of an algorithm
The longest running time for any input of size n
An upper bound on the running time for any input
guarantee that the algorithm will never take longer
Example: Sort a set of numbers in increasing order; and the data is in decreasing order
The worst case can occur fairly often
E.g. in searching a database for a particular piece of information
Best-case running time
sort a set of numbers in increasing order; and the data is already in increasing order
Average-case running time
May be difficult to define what “average” means
Running-time of algorithms
Bounds are for the algorithms, rather than programs
programs are just implementations of an algorithm, and almost always the details of the program do not affect the bounds
Bounds are for algorithms, rather than problems
A problem can be solved with several algorithms, some are more efficient than others