Design and Analysis of Algorithm Course Code: 5009
Design and Analysis of Algorithm Course Code: 5009
MIDTERM
9. Dynamic Programming I
10. Dynamic Programming II
11. Backtracking I
12. Backtracking II Quiz 03, Assignment 02
13. Branch And Bound Techniques I
14. Branch And Bound Techniques II
15. NP Complete Problems I
16. NP Complete Problems II Quiz 04
FINAL EXAMS
Course Information
• Office hours
– 9:00-11:00 Wednesday and Friday
• Grading policy
– Assign and Quiz. 20%, Midterm: 30%, Final: 50%
More Information
• Textbook
– Anany Levitin-Introduction to the Design and Analysis of Algorithms -
Addison Wesley (Pearson Education Inc.) (2011).pdf
• Others
– Introduction to Design & Analysis Computer Algorithm 3rd, Sara
Baase, Allen Van Gelder, Adison-Wesley, 2000.
– Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice Hall,
2004.
– Introduction to The Design and Analysis of Algorithms 2nd Edition,
Anany Levitin, Adison-Wesley, 2007.
An Algorithm
• An Algorithm is a finite sequence of instructions, each of which has a
clear meaning and can be performed with a finite amount of effort in a
finite length of time. No matter what the input values may be, an
algorithm terminates after executing a finite number of instructions.
• Input: there are zero or more quantities, which are externally supplied;
Output: at least one quantity is produced;
• Definiteness: each instruction must be clear and unambiguous;
9
APPROXIMATE ALGORITHM
10
What is a program?
• A program is the expression of an algorithm in
a programming language
• a set of instructions which the computer will
follow to solve a problem
Let us write down the algorithm for a
problem that is familiar to us
1001011
Algorithm for Decimal-to-Binary Conversion
Better Definition:
A precise sequence of a limited
number of unambiguous, executable
steps that terminates in the form of a
solution
Analysis of Algorithms
• Analysis in the context of algorithms is concerned with
predicting the resources that are requires:
– Computational time
– Memory
• However, Time – generally measured in terms of the
number of steps required to execute an algorithm - is the
resource of most interest
1. Best Case : The minimum possible value of f(n) is called the best case.
3. Worst Case : The maximum value of f(n) for any key possible input.
The field of computer science, which studies efficiency of algorithms, is known as analysis
of algorithms.
1. Big–OH (O) 1,
2. Big–OMEGA (Ω),
3. Big–THETA () and
4. Little–OH (o)
Big–OH O (Upper Bound)
f(n) = O(g(n)), (pronounced order of or big oh), says that the growth rate of f(n) is less
than or equal (<) that of g(n).
Little–OH (o)
Growth Rate of Running Time
Wednesday, August
Analysis 21, 2019
of Algorithm 34
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
35
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
36
Rate of Growth
• Consider the example of buying elephants and
goldfish:
Cost: cost_of_elephants + cost_of_goldfish
Cost ~ cost_of_elephants (approximation)
• The low order terms in a function are relatively
insignificant for large n
n4 + 100n2 + 10n + 50 ~ n4
Wednesday,Analysis of Algorithm
August 21, 2019 38
A Simple Example – Linear Search
INPUT: a sequence of n numbers, key to search for.
OUTPUT: true if key occurs in the sequence, false otherwise.
LinearSearch(A, key) cost times
1 i1 c1
n
i 2
1 1
2 while i ≤ n and A[i] != key c2 n+1
3 do i++ c3 n
if i n c4 1
4 then return true c5 1
5 else return false c6 1
x ranges between 1 and n+1.
So, the running time ranges between
c1+ c2+ c4 + c5 – best case
and
c1+ c2(n+1)+ c3n + c4 + c6 – worst case
Comp 122,
A Simple Example – Linear Search
INPUT: a sequence of n numbers, key to search for.
OUTPUT: true if key occurs in the sequence, false otherwise.
LinearSearch(A, key) cost times
1 i1 1 n
i 2
1 1
2 while i ≤ n and A[i] != key 1 n+1
3 do i++ 1 n
4 if i n 1 1
5 then return true 1 1
6 else return false 1 1
Assign a cost of 1 to all statement executions.
Now, the running time ranges between
1+ 1+ 1 + 1 = 4 – best case
and
1+ (n+1)+ n + 1 + 1 = 2n+4 – worst case
Comp 122,
A Simple Example – Linear Search
INPUT: a sequence of n numbers, key to search for.
OUTPUT: true if key occurs in the sequence, false otherwise.
LinearSearch(A, key) cost times
1 i1 1 n
i 2
1 1
2 while i ≤ n and A[i] != key 1 n
3 do i++ 1 n-1
4 if i n 1 1
5 then return true 1 1
6 else return false 1 1
If we assume that we search for a random item in the list,
on an average, Statements 2 and 3 will be executed n/2 times.
Running times of other statements are independent of input.
Hence, average-case complexity is
1+ n/2+ n/2 + 1 + 1 = n+3
Comp 122,
Order of growth
• Principal interest is to determine
– how running time grows with input size – Order of growth.
– the running time for large inputs – Asymptotic complexity.
• In determining the above,
– Lower-order terms and coefficient of the highest-order term are
insignificant.
– Ex: In 7n5+6n3+n+10, which term dominates the running time for very
large n?
• Complexity of an algorithm is denoted by the highest-order
term in the expression for running time.
– Ex: Ο(n), Θ(1), Ω(n2), etc.
– Constant complexity when running time is independent of the input size
– denoted Ο(1).
– Linear Search: Best case Θ(1), Worst and Average cases: Θ(n).
• More on Ο, Θ, and Ω in next class. Use Θ for the present.
Comp 122,
Important Summations
43
Important Summations that should be
Committed to Memory.
44
Analysis: A Harder Example
45
Solution
46
Analysis: A Harder Example
47
Analysis: A Harder Example
48
Analysis: A Harder Example
49
Order of Magnitude Analysis
Measure speed with respect to the part of the sum
that grows quickest
50 N 2 31N 3 24 N 15
3N 2 N 21 4 3N
Ordering:
1 log 2 N N N log 2 N N N 2 3
2 3 N N