AC Unit1
AC Unit1
Introduction
Algorithm
An algorithm is any well-defined computational
procedure that take some value, or set of values,
as input and produces some value, or set of
values, as output. An algorithm is thus a
sequence of computational steps that transform
the input into the output.
Algorithmic Solution
• With the definition, we can identify five
important characteristics of algorithms :
• Algorithms are well-ordered.
• Algorithms have unambiguous operations.
• Algorithms have effectively computable
operations.
• Algorithms produce a result.
• Algorithms halt in a finite amount of time.
Properties of an Algorithm
Main Task
Repetition
• While/Repeat
While (cond) do
end while
• Do while /Repeat
Do
while (cond)
Algorithm Specification
One procedure
example
Example: Selection Sort Algorithm
First Attempt :
Find the smallest from unsorted list and place next
to sorted list
31
Ideal Solution
32
Example
Associate a "cost" with each statement.
Find the "total cost“ by finding the total number of times
each statement is executed.
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
33
Another Example
Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2
34
Asymptotic Notation
35
Big-O Notation
36
More Examples …
37
Visualizing Orders of Growth
On a graph, as
you go to the
right, a faster
Value of function
fA(n)=30n+8
growing
function
eventually
fB(n)=n2+1
becomes
larger...
Increasing n
38
Back to Our Example
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
39
Example (cont’d)
Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2 = O(N2)
40
Review: Asymptotic Performance
Asymptotic performance: How does algorithm
behave as the problem size gets very large?
• Running time
• Memory/storage requirements
Remember that we use the RAM model:
• All memory equally expensive to access
• No concurrent operations
• All reasonable instructions take unit time
– Except, of course, function calls
• Constant word size
– Unless we are explicitly manipulating bits
Review: Running Time
Number of primitive steps that are executed
Except for time of executing a function call most
statements roughly require the same amount of
time We can be more exact if need be
RR
O( f ) ( f )
•f
( f )
Asymptotic Notations
Allow us
to analyze an algorithm’s running time by
identifying its behavior as the input size for the
algorithm increases.
This is also known as an algorithm’s growth
rate.
• Quasilinear
• the time complexity O(n log n) can describe a data structure
where each operation takes O(log n) time. example :quick sort,
a divide-and-conquer algorithm.
• Non-polynomial time complexity
• An algorithm with time complexity O(n!) often iterates through
all permutations of the input elements. example brute-force
search seen in the travelling salesman problem
• Exponential
• An exponential algorithm often also iterates through all subsets
of the input elements. It is denoted O(2n). The larger the data
set, the more steep the curve becomes. a brute-force attack.
Order of Growth classification
Asymptotic Notations- Big O Notation
{f(n) = O(g(n))}
Example:
Example:
If – then-else statement
If (Condition) { Here, either sequence 1 will execute, or
Sequences of sequence 2 will execute.
statements 1 Therefore, the worst-case time is the
Complexity
} slowest of the two possibilities:
Else { max(time(sequence 1), time(sequence
Sequences of 2))
Statement 2
}
How to Determine Complexities
for loops The loop executes N times, so the sequence
for (i = 0; i < N; i++) { of statements also executes N times.
{ Complexity which is O(N) overall.
sequence of
statements
}
}
Nested loops
The outer loop executes N times. Every
for (i = 0; i < N; i++) { time the outer loop executes, the inner
for (j = 0; j < M; j++) { loop executes M times
Complexity Thus, the complexity is O(N * M)
sequence of statements
}
}
x = 0;
A[n] = some array of The loop executes N times, so the
length n; sequence of statements also
while (x != A[i]) Complexity executes N times.
{ which is O(N) overall.
i++;
}
Asymptotic notations
O-notation
52
Examples – (‘=’ symbol readed as ‘is’ instead of ‘equal’)
The function 3n+2 = O(n) as 3n+2 ≤ 4n for all n≥2
The function 3n+3 = O(n) as 3n+3 ≤ 4n for all n≥3
The function 100n+6 = O(n) as 100n+6 ≤ 101n for all
n≥6
The function 10n2+4n+2 = O(n^2) as 10n^2+4n+2 ≤
11n2 for all n≥5
The function 1000n2+100n-6 = O(n2) as 1000n2+100n-
6 ≤ 1001n2 for all n≥100
The function 6*2n+n2 = O(2n) as 6*2n +n2≤ 7*2n for
all n≥4
The function 3n+3 = O(n2) as 3n+3 ≤ 3n2 for all n≥2
Tabular Method
n Function f(n) compare c. g(n)
10n^2+4n+2 11n^2
1 10+4+2=16 > 11
2 40+8+2=50 > 44
3 90+12+2=104 > 99
4 160+16+2=178 > 176
60
An Example: Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
61
Insertion Sort
What is the precondition
InsertionSort(A, n) {
for this loop?
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
62
Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
} How many times will
} this loop execute?
David Luebke
63
Insertion Sort
Statement Effort
InsertionSort(A, n) {
for i = 2 to n { c1 n
key = A[i] c2(n-1)
j = i - 1; c3(n-1)
while (j > 0) and (A[j] > key) { c4 T
A[j+1] = A[j] c5(T-(n-1))
j = j - 1 c6(T-(n-1))
} 0
A[j+1] = key c7(n-1)
} 0
}
T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith for loop
iteration
David Luebke
64
Analyzing Insertion Sort
T(n) = c1n + c2(n-1) + c3(n-1) + c4T + c5(T - (n-1)) + c6(T - (n-1)) + c7(n-1)
= c8T + c9n + c10
What can T be?
Best case -- inner loop body never executed
• ti = 1 T(n) is a linear function
Worst case -- inner loop body executed for all
previous elements
• ti = i T(n) is a quadratic function
Average case
• ???
David Luebke
65
Upper Bound Notation
We say InsertionSort’s run time is O(n2)
Properly we should say run time is in O(n2)
Read O as “Big-O” (you’ll also hear it as “order”)
In general a function
f(n) is O(g(n)) if there exist positive constants c
and n0 such that f(n) c g(n) for all n n0
Formally
O(g(n)) = { f(n): positive constants c and n0 such
that f(n) c g(n) n n0
David Luebke
66
Insertion Sort Is O(n2)
Proof
Suppose runtime is an2 + bn + c
• If any of a, b, and c are less than 0 replace the constant
with its absolute value
an2 + bn + c (a + b + c)n2 + (a + b + c)n + (a + b + c)
3(a + b + c)n2 for n 1
Let c’ = 3(a + b + c) and let n0 = 1
Question
Is InsertionSort O(n3)?
Is InsertionSort O(n)?
David Luebke
67
Omega notation (Ω)
69
Examples –
• The function 3n+2 = Ω(n) as 3n+2 ≥ 3n for all n≥1
• The function 3n+3 = Ω(n) as 3n+3 ≥ 3n for all n≥1
• The function 100n+6 = Ω(n) as 100n+6 ≥ 100n for all n≥1
• The function 10n2+4n+2 = Ω(n2) as 10n2+4n+2 ≥ n2 for all
n≥1
• The function 6*2n+n2 = Ω(2n) as 6*2n +n2 ≥ 2n for all n≥1
• The function 3n+3 = Ω(1)
• The function 10n2+4n+2 = Ω(n)
• The function 10n2+4n+2 = Ω(1)
Theta notation (Θ)
72
Examples
• The function 3n+2 = Θ(n) as
3n+2 ≥ 3n for all n≥2 and
3n+2 ≤4n for all n≥2
So, c1=3, c2=4 and n0=2.
• The function 3n+3 = Θ(n)
• The function 10n^2+4n+2 = Θ (n^2)
• The function 6 * 2^n +n^2 = Θ(2^n)
Next Lecture
Recurrence Relations
Recurrence Relations
Definition: Given a recursive algorithm a
recurrence relation for the algorithm is an
equation that gives the run time on an input
size in terms of the run times of smaller input
sizes.
When iterative formulas for T(n) are difficult
or impossible to obtain, one can use either
a recursion tree method,
an iteration method , or
a substitution method with Induction to get T(n) or
a bound U(n)of T(n) , where T(n)= Θ(U(n)).
Recursion Tree
A recursion tree is a tree generated by tracing
the execution of a recursive algorithm.
Recurrence Relations
A recurrence relation for the sequence { an } is
an equation that expresses an in terms of one
or more of the previous terms of the
sequence, namely, a0 , a1 ,...an for all integers
n with n ≥ n0 , where n0 is a nonnegative
integer. A sequence is called a solution of a
recurrence relation if its terms satisfy the
recurrence relation.
Let {a } be a sequence that satisfies the
n
are a2 , and a3 ?
Solution: We see from the recurrence relation
that a2 a1 a0 5 3 2
and a3 a2 a1 2 5 3
We can find a4, a5, and each successive term in
a similar way.
Creating Recurrence Relation
Fib(a) T(n)
{
if(a==1 || a==0) 1
return 1;
return Fib(a-1) + Fib(a-2); T(n-1)+T(n-2)
}
(comparison, comparison, addition) and also
calls itself recursively.
The Fibonacci sequence, f0, f1, f2, . . . , is defined by the
initial conditions f0 = 0, f1 = 1, and the recurrence
relation fn = fn−1 + fn−2 for n = 2, 3, 4, . . . .
Find the Fibonacci numbers f2, f3, f4, f5, and f6.
Solution:. Because the initial conditions tell us that
f0 = 0 and f1 = 1, using the recurrence relation in
the definition we find that
f2 = f1 + f0 = 1 + 0 = 1,
f3 = f2 + f1 = 1 + 1 = 2,
f4 = f3 + f2 = 2 + 1 = 3,
f5 = f4 + f3 = 3 + 2 = 5,
f6 = f5 + f4 = 5 + 3 = 8.
Solving Recurrence Relations
Solution
The characteristic equation of the recurrence relation
is − x 5 x 6 0
2
So, ( x 3)( x 2) 0
Hence, the roots are −
x1 3
and x2 2
The roots are real and distinct. So, this is in the form of
case 1
Hence, the solution is −
Fn ax bx
n
1
n
2
Here, F a3 b2
n n
(As x1=3 and x2=2)
Therefore,
1 F0 a30 b20 a b
4 F1 a31 b21 3a 2b
f n f n1 f n2 f 0 0, f1 1
Has solution: fn r r 1 1
n
2 2
n
1 5 1 5
Characteristic roots: r1 r2
2 2
Konstantin Busch - LSU 91
f1 f 0 r2 1
1
r1 r2 5
f 0 r1 f1 1
2
r1 r2 5
n n
1 1 5 1 1 5
5 2 2
5