Analysis and Design of Algorithms - Handout
Analysis and Design of Algorithms - Handout
Analysis and Design of Algorithms - Handout
Level 400
Course outline
Analysis and Design of Algorithms
Introduction
Complexity of algorithms
Maxima finding
Linear search
Sorting algorithm
A measure of the growth of functions
Classification of complexity
Greedy Algorithms
Greedy Change-making Algorithm
Greedy Routing Algorithm
Other classification of problems
What is an algorithm
A finite set (or sequence) of precise instructions for performing a
computation.
A finite set of steps that specify a sequence of operations to be
carried out in order to solve a specific problem
Properties of Algorithms
1. Finiteness
2. Absence of Ambiguity
3. Definition of Sequence
4. Feasibility
5. Input
6. Output
Algorithms
• Properties of algorithms:
no
max <ai
yes
max = ai i: = i + 1
no
i = n?
yes
end
Complexity Analysis
• An objective way to evaluate the cost of an algorithm or
code section.
k := 1 (1 operation)
while k ≤ ndo
if x = akthen found else k: = k+1} (2n operations)
if then
Example of a sorting algorithm
start
i:=1
swap ai, aj i: = i + 1
j: = j + 1
j = n? no
yes
i = n-1?
no
yes
end
Bubble Sort
Bubble Sort
DEF. Let f and g be functions from the set of integers (or real
numbers) to the set of real numbers. Then f is O(g(x)) if there
are constants C and k, such that
1 2
A C B
If you need to reach point B from point A in the fewest number of hops,
then which route will you take? If the knowledge is local, then you are
tempted to use a greedy algorithm, and reach B in 5 hops, although it is
possible to reach B in only two hops.
Other classification of problems
• Problems that have polynomial worst-case complexity are
called tractable. Otherwise they are called intractable.
• Problems of higher complexity are called intractable.
• Problems for which no solution exists are known as
unsolvable problems (like the halting problems).
Otherwise they are called solvable.
• Many solvable problems are believed to have the property
that no polynomial time solution exists for them, but a
solution, if known, can be checked in polynomial time.
These belong to the class NP (as opposed to the class of
tractable problems that belong to class P)
The Halting Problems
The Halting problem asks the question.