Chapter 1_ Introduction to Algorithms
Chapter 1_ Introduction to Algorithms
Introduction to Algorithms
● Introduction to algorithms
Contents ● Characteristics of an algorithm
● Asymptotic notations
Algorithms
The word ‘Algorithm’ comes from the name of a Persian author, Abu Ja’ar
Mohammed ibn Musa al Khwarizmi (c. 825 A.D.), Latinized Algoritmi.
The Latin name algoritmi was altered to algorismus, algorithmus, algorithme (in
French), and finally to algorithm (in English).
Algorithms
● Any well-defined computational procedure that takes some value, or set of
values, as input and produces some value, or set of values, as output.
● All algorithms must satisfy the following criteria:
1. Input: Zero or more externally supplied quantities
2. Output: Produce at least one quantity
3. Definiteness: Clear and unambiguous instructions
4. Finiteness: Terminate after a finite number of steps
5. Effectiveness: Feasible instructions
Algorithms
● Any well-defined computational procedure that takes some value, or set of
values, as input and produces some value, or set of values, as output.
● All algorithms must satisfy the following criteria:
1. Input: Zero or more externally supplied quantities
2. Output: Produce at least one quantity
3. Definiteness: Clear and unambiguous instructions
4. Finiteness: Terminate after a finite number of steps
5. Effectiveness: Feasible instructions
● Algorithm vs program:
Program = The expression of an algorithm in a programming language
Program does not need to satisfy criterion #4.
Algorithms
Study of algorithms includes
Steps:
Steps:
1. Result ← A[0]
2. i ← 1
3. Repeat
a. If A[i] > Result
i. Result ← A[i]
b. Endif
4. Until i >= n
5. Return Result
Example
Algorithm: Find the maximum element in an array
Input: Array A of size n
Output: The maximum element in A
Steps:
1. Result ← A[0]
2. For i ← 1 to n-1 do
a. If A[i] > Result
i. Result ← A[i]
b. Endif
3. Endfor
4. Return Result
Pseudocode conventions
● // or # for comments
● Braces { } for blocks such as a collection of simple statements
● Assignments of values to variables using =, ≔ or ⟵
● Loops:
while <condition> do
{
<statement 1>
⠇
<statement 2>
}
Pseudocode conventions
● Loops:
for variable = value1 to valuen step <step> do
{
<statement 1>
⠇
<statement 2>
}
Pseudocode conventions
● Loops:
repeat
<statement 1>
⠇
<statement 2>
until <condition>
Example
1. Algorithm Max(A, n)
2. // A is an array of size n
3. {
4. Result = A[0];
5. for i = 1 to n - 1 do
6. if A[i] > Result then Result = A[i];
7. return Result;
8. }
Exercise
1. Devise an algorithm to add two matrices, A and B.
Why analyze an algorithm?
● Classify problems and algorithms by difficulty
● Predict performance, compare algorithms, tune parameters
● Better understand and improve implementations and algorithms
● Intellectual challenge
1. Memory
2. Time
Fixed space requirements, c, include space needed to store the code, simple
variables, fixed-sized structured variables, and constants.
Compile time does not depend on the instance characteristics. So, we are
concerned only with the program’s execution time, TP
Instance characteristics include the number, size, and values of the inputs and
outputs associated with I.
Program step:
A syntactically or semantically meaningful program segment whose execution time
is independent of the instance characteristics (i.e. the number, size, and values of
inputs and outputs associated with a program instance)
Example
Count program steps
1. float sum(float a, float b) {
2. {
3. return a+b;
4. }
Example
Count program steps
1. Algorithm Sum(A, B, m, n) {
2. // Algorithm to add two matrices
// A and B of size m x n.
3. {
4. C = A
5. for i = 0 to m-1 do
6. for j = 0 to n-1 do
7. C[i,j] += B[i,j]
8. return C
9. }
Order of growth
Algorithm analysis is about understanding the growth in resource consumption as
the amount of data increases
As the amount of data gets bigger, how much more resource will my algorithm
require?
The idea is that we ignore low-order terms and constant factors, focusing instead
on the shape of the running time curve.
Typical notation:
o(g(n)) = {f(n) : there exist positive constants c and n0 such that 0 ≤ f(n) < c.g(n) for all
n ≥ n0}.
⍵(g(n)) = {f(n) : there exist positive constants c and n0 such that 0 ≤ c.g(n) < f(n) for all
n ≥ n0}.
Readings
Chapter 1 from Horowitz et al.