01 Introduction Old
01 Introduction Old
Algorithm Definition
• An algorithm is a step-by-step procedure for solving a particular
problem in a finite amount of time.
Input ALGORITHM
Some mysterious Output
X processing Y=
F: X→Y
F(X)
Algorithm -- Examples
• Repairing a lamp
• A cooking recipe
• Calling a friend on the phone
• The rules of how to play a game
• Directions for driving from A to B
• A car repair manual
• Human Brain Project
• Internet & Communication Links (Graph)
• Matrix Multiplication
Algorithm vs. Program
• A computer program is an instance, or concrete representation, for
an algorithm in some programming language
High Level
Language
Program
Solving Problems (1)
When faced with a problem:
1. First clearly define the problem
3. Select the one that seems the best under the prevailing
circumstances
• It must be correct.
• It must terminate.
Syntax & Semantics
An algorithm is “correct” if its: WARNINGS:
• Semantics are correct
• Syntax is correct 1. An algorithm can be
syntactically correct, yet
semantically incorrect –
Semantics: dangerous situation!
• The concept embedded in an
algorithm (the soul!) 2. Syntactic correctness is
easier to check as
compared to semantic
Syntax: correctness
• The actual representation of
an algorithm (the body!)
Algorithm Summary
• Problem Statement
• Relationship b/w input and output
• Algorithm
• Procedure to achieve the relationship
• Definition
• A sequence of steps that transform the input to output
• Instance
• The input needed to compute solution
• Correct Algorithm
• for every input it halts with correct output
Al-Khwarizmi’s Golden Principle
All complex problems can be and must be solved
using the following simple steps:
• Platform dependent
• Execution time differ on different
architectures
• Data dependent
• Execution time is sensitive to
amount and type of data
manipulated.
• Language dependent
• Execution time differ for same
code, coded in different
languages
∴ absolute measure for an algorithm is not appropriate
Theoretical Analysis
• Data independent
• Takes into account all possible inputs
• Platform independent
• Language independent
• Implementation independent
• not dependent on skill of programmer
• can save time of programming an inefficient solution
• Correctness
• Does the input/output relation match algorithm requirement?
• Amount of work done
• Basic operations to do task
• Amount of space used
• Memory used
• Simplicity, clarity
• Verification and implementation.
• Optimality
• Is it impossible to do better?
Problem Solving Process
• Problem
• Strategy
• Algorithm
• Input
• Output
• Steps
• Analysis
• Correctness
• Time & Space Optimality
• Implementation
• Verification
Computation Model for Analysis
• To analyze an algorithm is to determine the amount of resources
necessary to execute it. These resources include computational
time, memory and communication bandwidth.
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0];
2. for i = 1 to n-1 do
3. if A[i] > currentMax then
4. currentMax
A[i]
5. return currentMax;
Pseudocode
• Indentation indicates block structure. e.g body of loop
• Looping Constructs while, for and the conditional if-then-else
• The symbol // indicates that the reminder of the line is a comment.
• Arithmetic & logical expressions: (+, -,*,/, ) (and, or and not)
• Assignment & swap statements: a b , ab c, a b
• Return/Exit/End: termination of an algorithm or block
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0];
2. for i = 1 to n-1 do
3. if A[i] > currentMax then
4. currentMax
A[i]
5. return currentMax;
Pseudocode
• Local variables mostly used unless global variable explicitly defined
• If A is a structure then |A| is size of structure. If A is an Array then
n =length[A], upper bound of array. All Array elements are accessed
by name followed by index in square brackets A[i].
• Parameters are passed to a procedure by values
• Semicolons used for multiple short statement written on one line
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0]
2. for i = 1 to n-1 do
3. if A[i] > currentMax then
4. currentMax
A[i]
5. return currentMax
Elementary Operations
• An elementary operation is an operation which takes constant time
regardless of problem size.
• The running time of an algorithm on a particular input is determined
by the number of “Elementary Operations” executed.
• Theoretical analysis on paper from a description of an algorithm
Block #1 t1
T(n) = max(t1,t2)
Block #2 t2
for i = 1 to n do
P(i);
T(n) = nt
for i = 0 to n do
for j = 0 to m do
P(j);
T(n) = nmt
Repetition (Loops)
Analysing Nested Loops
for i = 0 to n do
for j = 0 to i do
P(j);
• Assume that P(j) takes time t, where t is independent of i and j
• How do we analyze the running time of an algorithm that has
complex nested loop?
• The answer is we write out the loops as summations and then
solve the summations. To convert loops into summations, we
work from inside-out.
T(n) = n + +t
= n + n(n+1)/2 + tn(n+1)/2
Analysis Example
Algorithm: Number of times executed
1. n = read input from user 1
2. sum = 0 1
3. i = 0 1
4. while i < n n
5. number = read input from user n or
6. sum = sum + number n or
7. i=i+1 n or
8. mean = sum / n 1
The computing time for this algorithm in terms on input size n is:
T(n) = 1 + 1 + 1 + n + n + n + n + 1
T(n) = 4n + 4
Asymptotic Growth Rate
• Changing the hardware/software environment
• Affects T(n) by constant factor, but does not alter the growth rate of T(n)
0.05 N2 = O(N2)
3N = O(N)
Time (steps)
Input (size)
N = 60
Important Functions
These functions often appear in algorithm analysis:
A comparison of Growth-Rate Functions
Size does Matter:
What happens if we increase the input size N?
Performance Classification
f(n) Classification
1
:
Constant run time is fixed, and does not depend upon n. Most instructions are
executed once, or only a few times, regardless of the amount of information being processed
log n Logarithmic: when n increases, so does run time, but much slower. Common in
programs which solve large problems by transforming them into smaller problems.
n Linear: run time varies directly with n. Typically, a small amount of processing is done on
each element.
n log n When n doubles, run time slightly more than doubles. Common in programs which
break a problem down into smaller sub-problems, solves them independently, then combines solutions
n2 Quadratic: when n doubles, runtime increases fourfold. Practical only for small
problems; typically the program processes all pairs of input (e.g. in a double nested loop).
2n Exponential: when n doubles, run time squares. This is often the result of a natural, “brute
force” solution.
Running Time vs. Time Complexity
• Running time is how long it takes a program to run.
Comparisons: 2n + 2
Time complexity is O(n).
Example: Linear Search Algorithm
• Given a linear array A containing n elements, locate the position of an
Item ‘x’ or indicate that ‘x’ does not appear in A.
• The linear search algorithm solves this problem by comparing ‘x’, one
by one, with each element in A. That is, we compare ITEM with A[1],
then A[2], and so on, until we find the location of ‘x’.
Worst case: ‘x’ is located in last location of the array or is not there
at all.
T(n) = 1 + n + n + 1 +1
= 2n +3
= O(n)
Average case
Average Case: Assume that it is equally likely for ‘x’ to appear at any
position in array A,. Accordingly, the number of comparisons can be
any of the numbers 1,2,3,..., n, and each number occurs with
probability p = 1/n.
This agrees with our intuitive feeling that the average number of
comparisons needed to find the location of ‘x’ is approximately equal
to half the number of elements in the A list.