Escolar Documentos
Profissional Documentos
Cultura Documentos
Algorithm
A finite set of instructions which if followed accomplish a
particular task. In addition every algorithm must satisfy
following criteria:
1. Input: zero or more quantities externally supplied
2. Output: at least one quantity is produced
3. Definiteness: Each instruction must be clear and
unambiguous.
4. Finiteness: In all cases algorithm must terminate after
finite number of steps.
5. Effectiveness: each instruction must be sufficiently
basic.
Algorithm A1
Algorithm A2
A2
A1
50 n lg n
2 n2
Super computer
108 ins/sec
Time taken by Super Computer
= 2.(106)2/ 108 = 20,000 sec
P.C
106 ins /sec
Complexity
Some questions to answer:
How fast can we solve a problem?
There may be many algorithms for a given problem.
Which algorithm to use?
What are the classical algorithm design techniques ?
Are there problems inherently difficult to solve?
Pseudocode
Pseudocode is an English language like representation of
Example
Algorithm: deviation
Pre: nothing
1
2
3
4
5
6
7
8
i= 0
Loop(all data are read)
1
I=I+1
2
read numbers into array[i]
3
sum = sum + number
Average = sum / I
Print (average)
J=0
Loop (j < i)
1
j = j+ 1
2
dev = array[j] average
3
print (array [ j] . Dev)
Return
End deviation
Summary
Confidence in algorithms from testing and
correctness proof
Correctness
How do we know that an algorithm works?
Correctness of Recursive
algorithms
To prove correctness of recursive algorithm:
Prove it by induction on the size of the problem being
solved
Base of recursion is base of induction
Need to prove the recursive calls are given sub-problems,
i.e., no infinite recursion
Inductive step: assume that recursive calls work
correctly, and use this assumption to prove that the
current call works correctly
Recursive Maximum
Recursive maximum
Recursive Multiplication
Recursive Multiplication
Recursive Multiplication
Analysis
Summary
Constant Multiples
Analyze the resource usage of an algorithm to within a constant
multiple.
Why? Because other constant multiples creep in when translating
from an algorithm to executable code:
Programmer ability
Programmer effectiveness
Compiler
Computer hardware
Recall: Measure resource usage as a function of input size
Big oh
Example
Most big-Os can be proved by induction.
First Example: log n = O(n).
Second example
Big Omega
Big Theta
True or false
Continue
Types of Analysis
Example
Time Complexity
Multiplication
Bubble sort
Analysis Trick
Example
Algorithms Analysis 2
The Heap
Contd
Contd
Contd
Contd
Contd
Contd
Contd
Contd
Contd
Contd
Implementing a Heap
Analysis of l(n)
Contd
Example
Heapsort
Contd
Contd
Continue
Continue
Continue
Example
Continue
Analysis of Multiplication
Repeated Substitution
Warning
Reality Check
Merge Sorting
Continue
Continue
A General Theorem
Proof Sketch
Continue
Geometric Progressions
Continue
Messy Details
Continue
Example