Pmscs 623p Lecture 1
Pmscs 623p Lecture 1
Algorithm
(PMSCS 623P)
Lecture 1
Data Structures
Data
• Data are simply values or sets of values, raw materials used as inputs.
• A data item refers to a single unit of values.
• Data items that are divided into sub items are group items. Those that are
not are called elementary items. For example, a student’s name may be
divided into three sub items – [first name, middle name and last name] but
the ID of a student would normally be treated as a single item.
Student
English
Pseudocode
key ← A[ j]
For loop body
i = j – 1;
i←j–1 while(i > 0 && A[i] > key){
while i > 0 and A[i] > key A[i+1] = A[i];
do A[i+1] ← A[i] i = i – 1;
While
Loop
body
i←i–1 }//while
A[i+1] ← key A[i+1] = key;
}//for
Indentation/spacing determines where }
the algorithm/loop/if/else-body ends
i j
Insertion Sort Simulation 1 2 3 4 5
⊳ A[1 . . n]
key 4 7 4 9 5 1
INSERTION-SORT (A, n)
for j ← 2 to n i j
1 2 3 4 5
1 4 5 7 9
Analysis of Algorithms
• What does it mean to analyze an algorithm?
– To have an estimate about how much time an algorithm may take to
finish, or in other words, to analyze its running time (aka. Time
complexity)
– Sometimes, instead of running time, we are interested in how much
memory/space the algorithm may consume while it runs (space
complexity)
– It enables us to compare between two algorithms
• What do we mean by running time analysis?
– Also referred to as time-complexity analysis
– To determine how running time increases as the size of the problem
increases.
Analysis of Algorithms
– # of elements in a matrix
(how the algorithm performs in all sort of situations.. How the internal
structure of the input)
Example: Counting
Operations
How many operations are required?
for (int i = 1; i <= n; i++) {
perform 100 operations; // A
for (int j = 1; j <= n; j++) {
perform 2 operations; // B
n n n
}
} Total Ops = A +
100 ( 2)
B i 1 i 1 j 1
n
100n 2n2 2n2 100n
100n 2n
i1
Example: Counting
Operations
How many operations are required?
for (int i = 1; i <= n; i++) {
perform 100 operations; // A
for (int j = 1; j <= n; j++) {
perform 2 operations; // B
n n n
}
} Total Ops = A +
100 ( 2)
B i 1 i 1 j 1
n
100n 2n2 2n2 100n
100n 2n
i1
Example: Counting
Operations
Knowing the number of operations required
by the algorithm, we can state that
Algorithm X takes 2n2 + 100n operations to solve
problem of size n
If the time t needed for one operation is
known, then we can state
Algorithm X takes (2n2 + 100n)t time units
Example: Counting
Operations
However, time t is directly dependent on
the factors mentioned earlier
e.g. different languages, compilers and
computers
Instead of tying the analysis to actual
time t, we can state
Algorithm X takes time that is proportional
to 2n2 + 100n for solving problem of size n
Approximation of Analysis
Results
Suppose the time complexity of
Algorithm A is 3n2 + 2n + log n + 1/(4n)
Algorithm B is 0.39n3 + n
Intuitively, we know Algorithm A will outperform B
When solving larger problem, i.e. larger n
The dominating term 3n2 and 0.39n3 can tell us
approximately how the algorithms perform
The terms n2 and n3 are even simpler and preferred
These terms can be obtained through asymptotic
analysis
Asymptotic
Analysis
Asymptotic analysis is an analysis of
algorithms that focuses on
Analyzing problems of large input size
Consider only the leading term of the formula
Ignore the coefficient of the leading term
Why Choose Leading Term?
• Growth rates of 1E+30
functions: 1E+28 Cubic
1E+26
– Linear n 1E+24 Quadratic
– Quadratic n2 1E+22
Linear
1E+20
– Cubic n3 1E+18
T (n )
1E+16
1E+14
• In a log-log chart, the 1E+12
1E+10
slope of the line 1E+8
1E+6
corresponds to the 1E+4
growth rate of the 1E+2
1E+0
function 1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
Why Choose Leading Term?
• The growth rate is 1E+26
1E+24 Quadratic
• Thus the running times (also called time complexity) of the programs of the
previous slide becomes:
• f(N)= c1N ≈ N*(some constant)
• g(N) = (c1+c2+c3)N+(c1+c2) ≈ N*(some constant)
• Thus both these functions grows linearly with N and as such both have the same
running time (specifically, linear running time). We say that both of them are
O(N) functions, which means that, the running time of each of these algorithms is
a constant multiple of N (we ignore the value of the constant).
• We compare running times of different functions in an asymptotic manner (i.e.,
we check if f(n) > g(n) for very large values of n). That’s why, the task of
computing time complexity (of an algorithm) is also called asymptotic analysis.
Intuition behind Asymptotic Analysis
• Consider the example of buying elephants and goldfish:
Cost: cost_of_elephants + cost_of_goldfish
Cost ~ cost_of_elephants (approximation)
• The low order terms, as well as constants in a function are
relatively insignificant for large n
f(n) = 6n + 4 ≈ n
g(n) = n4 + 100n2 + 10n + 50 ≈ n4
i.e., we say that n4 + 100n2 + 10n + 50 and n4 have the same
rate of growth. However, f(n) and g(n) have different rate of
growths.
The Purpose of Asymptotic Analysis
To estimate how long a program will run.
To estimate the largest input that can reasonably
be given to the program.
To compare the efficiency of different
algorithms.
To help focus on the parts of code that are
executed the largest number of times.
To choose an algorithm for an application.
Asymptotic Notations
Examples:
T(n) = 3n2+10nlgn+8 is O(n2), O(n2lgn), O(n3), O(n4), …
T’(n) = 52n2+3n2lgn+8 is O(n2lgn), O(n3), O(n4),
…
Loose upper
bounds
Asymptotic Notations
• - notation (Big Omega)
Examples:
T(n) = 3n2+10nlgn+8 is Θ(n2)
T’(n) = 52n2+3n2lgn+8 is Θ(n2lgn)
Big O
• Informally, O (g(n)) is the set of all functions
with a smaller or same order of growth as g(n),
within a constant multiple
• If we say f(n) is in O(g(n)), it means that g(n) is
an asymptotic upper bound of f(n)
– Intuitively, it is like f(n) ≤ g(n)
• What is O(n2)?
– The set of all functions that grow slower than or in
the same order as n2
Examples
• Show that 30n+8 is O(n).
– Show c,n0: 30n+8 cn, n ≥ n0 .
Value of function
• It isn’t even
less than 31n
everywhere.
• But it is less than
30n+8
n
31n everywhere to O(n)
the right of n=8.
n>n0=8
Increasing n