Professional Documents
Culture Documents
Introduction To Complexity: Prof. Sin-Min Lee Department of Computer Science San Jose State University
Introduction To Complexity: Prof. Sin-Min Lee Department of Computer Science San Jose State University
3
4
5
6
Analysis of Algorithms is a field in
computer science whose overall goal
is an understanding of the complexity
of algorithms. While an extremely
large amount of research is devoted
to worst-case evaluations,
The subject was founded by Knuth (who coined the term "analysis
of algorithms" in the mid-sixties) and is well illustrated by his
monumental series, The Art of Computer Programming
The field entertains close ties with a number of areas like discrete
mathematics, combinatorial analysis, probability theory, analytic
number theory, asymptotic analysis, complexity theory, and
sometimes statistical physics.
7
8
Donald Knuth
9
10
11
12
Data Structures and Algorithms
13
14
15
16
17
Recursion
18
Mathematical Examples
• factorial function
factorial(0) = 1
factorial(n) = n * factorial(n-1) [for n>0]
• Let's compute factorial(3). factorial(3)
= 3 * factorial(2)
= 3 * ( 2 * factorial(1) )
= 3 * ( 2 * ( 1 * factorial(0) ))
= 3 * ( 2 * ( 1 * 1 ) )) = 6
19
Fibonacci function:
• fibonacci(0) = 1
• fibonacci(1) = 1
• fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
[for n>1]
• This definition is a little different than the
previous ones because It has two base cases, not
just one; in fact, you can have as many as you like.
• In the recursive case, there are two recursive
calls, not just one. There can be as many as you
like.
20
Recursion
Internal External
The algorithm’s efficiency, • Size of the input to the
in terms of: algorithm
• Time required to run • Speed of the computer
• Space (memory storage) on which it is run
required to run • Quality of the compiler
23
Introduction to complexity
26
Common growth rates
27
Growth rates
O(N2)
O(Nlog N)
Number of Inputs
28
Calculating the actual time taken by
a program (example)
29
Best, average, worst-case complexity
30
How do we calculate big-O?
1 Loops
2 Nested loops
3 Consecutive statements
4 If-then-else statements
5 Logarithmic complexity
31
Guideline 1: Loops
32
Guideline 2: Nested loops
33
Guideline 3: Consecutive statements
34
Guideline 4: If-then-else statements
35
Guideline 5: Logarithmic complexity
36
Performance isn’t everything!
37