08-complexity
08-complexity
08-complexity
1
Iterative algorithm
• An iterative algorithm executes steps in iterations.
• It aims to find successive approximation in sequence to
reach a solution.
• They are most commonly used in linear programs
where large numbers of variables are involved.
2
Recursive vs Iterative
Algorithms:
• Approach: In recursive approach, the function calls itself until the
condition is met, whereas, in iterative approach, a function repeats until
the condition fails.
• Time & Space Effectiveness: Recursive solutions are often less efficient
in terms of time and space, when compared to iterative solutions.
4
Logical
• An algorithm may be viewed as controlled logical
deduction. This notion may be expressed as Algorithm
= logic + control.
5
Serial or parallel or distributed
6
Deterministic or non-
deterministic:
• non-deterministic algorithm solves problems via
guessing although typical guesses are made more
accurate through the use of heuristics.
7
Complexity
Intuition
• Are the following operations “fast” or “slow”?
arra linked
y list
behavior fast/slow behavior fast/slow
add at front slow add at front fast
add at back fast add at back slow
get at index fast get at index slow
resizing slow resizing fast
binary (pretty) fast binary (really) slow
search search
stac queu
k e
behavior fast/slow behavior fast/slow
push fast enqueue fast
pop fast dequeue fast
9
Complexity
• “Complexity” is a word that has a special meaning in
computer science
13
Time Complexity: Rule of
Thumb
• rule of thumb: this often works (but sometimes
doesn’t)
1 int x = 4 * 10 / 3 + 2 - 10 * 42;
15
Optimizing Code
• Many programmers care a lot about efficiency
Premature
optimizatio
n is the
root of all
evil!
Don Knuth
• Professor Emeritus at
Stanford
• “Father” of algorithm
analysis 17
Growth Rates
• We care about n as it gets bigger
– it’s a lot like calculus, with n approaching infinity
• you all know calculus, right?
n3
n4
• And as n gets really big, this approaches 0
18
Big O Notation
• We need a way to write a growth rate of a block of code
• Additional information:
– values in the array can be negative, positive, or zero
– the subsequence must be contiguous (can’t skip
elements)
– you must compute:
• the value of the sum of this subsequence
• the starting index (inclusive) of this subsequence
• the stopping index (inclusive) of this subsequence
14 8 -23 4 6 10 -18 5 5 11
max
subsequence
max sum: 4 + 6 + 10 + -18 + 5 + 5 + 11 = 23
starting index: 3
stopping index: 9
• Psuedo-code:
// try every start index, from 0 to size - 1
// try every stop index, from start index to size - 1
// compute the sum from start index to stop index
29
Case Study: maxSum
• There is a better algorithm, but it’s harder to
understand
– and I’m not going to formally prove that it always works
31
Case Study: maxSum
• Best code:
int maxSum = list[0];
int maxStart = 0;
int maxStop = 0;
int sum = 0;
int start = 0;
for (int i = 0; i < list.length; i++) {
if (sum < 0) {
sum = 0;
start = i;
} these are the
sum += list[i]; most frequently
if (sum > maxSum) {
executed lines of
maxSum = sum;
maxStart = start;
code
maxStop = i;
}
}
32
Case Study: maxSum
• What complexity class is our best algorithm?
– O(n) (linear time)
Max = 198959
Max start = 1727
Max stop = 1972
for n = 3000, time = 7.543
these numbers are close
Max = 614711 to 8 (23) and 27 (33)
Max start = 251 respectively, so this
Max stop = 3870
for n = 4500, time = 25.427 algorithm exhibited O(n3)
growth
Double/single ratio = 7.857291666666667
Triple/single ratio = 26.486458333333335
34
MaxSum.java
• Output for an array of 30,000 ints in the O(n2)
algorithm:
How many numbers do you want to use? 30000
Which algorithm do you want to use? 2
Max = 809852
Max start = 10146
Max stop = 19139
for n = 30000, time = 0.988
Max = 2170008
Max start = 9832
Max stop = 25833
for n = 60000, time = 3.935 these numbers are close
to 4 (22) and 9 (32)
Max = 4112483 respectively, so this
Max start = 74
Max stop = 88871
algorithm exhibited O(n2)
for n = 90000, time = 8.853 growth
Max = 27670910
look at how
Max start = 1045808 fast it
Max stop = 9643590 processed
for n = 10000000, time = 0.031
5,000,000,
Max = 28178549 10,000,000,
Max start = 239081 and
Max stop = 8574748 15,000,000
these numbers are close
for n = 15000000, time = 0.044
ints!
to 2 and 3 respectively,
Double/single ratio = 1.9375 so this algorithm
Triple/single ratio = 2.75 exhibited O(n) growth 36