Lec - 1 Intro
Lec - 1 Intro
Quality
Solution = Program
Time
𝑺𝟏 𝑺𝟐 𝑺𝟑 𝑺𝟒
𝒕𝟏 𝒕𝟐 𝒕𝟑 𝒕𝟒 Storage Space
𝟐𝟑𝒔 𝟏𝟓𝒔 𝟏𝟎𝒔 𝟏𝟗𝒔
Before & After!
Before, 𝑺𝟏 𝑪𝟏 𝑺𝟐 𝑪𝟐 𝑺𝟑 𝑪𝟑 𝑺𝟒 𝑪𝟒
Algorithm Analysis
Some methods are able to make analysis without coding and without the
need for PC.
Before algorithm: Write code to solve problem Save resources
After algorithm: Write EFFICIENT code to solve problem Save time
Save money
importance
Think Design Analyze
Analysis Design
Discuss other
Sections
algorithms
Our course
Assignments
on the time,
1 no Late!
Assignments -1
5
10
Quizzes 10
Evaluations
Mid-term 15
Final 60
The running time of an algorithm on a particular input is
the number of primitive operations or “steps” executed.
𝑇 𝑛 = 1 + 𝑛 + 𝑛 + 1 + 𝑚𝑛 = 2 + 2𝑛 + 𝑚𝑛
In this analysis, we made one important assumption. We assumed that the
body of the loop doesn’t depend on i. Sometimes the runtime of the body
does depend on i. In that case, our calculation becomes a little bit difficult.
In the for loop above, the control goes inside the if condition only when i is
an even number. That means the body of if condition gets executed n/2
times. The total cost is therefore
𝑇 𝑛 = 𝑛2 × 𝑛 2 = 𝑛3 2
There are two for loops, each goes n times. So the total cost is
𝑇 𝑛 = 𝑛 × 𝑛 × 𝑛 = 𝑛3
while loops are usually harder to analyze than for loops because there is no
obvious a priori way to know how many times we shall have to go round the
loop. One way of analyzing while loops is to find a variable that goes
increasing or decreasing until the terminating condition is met.
How many times the loop repeats? In every iteration, the value of i gets
halved. If the initial value of i is 16, after 4 iterations it becomes 1
(𝐥𝐨𝐠 𝟐 𝟏𝟔 = 𝟒 ) and the loop terminates. The implies that the loop repeats
𝐥𝐨𝐠 𝟐 𝒊 times. In each iteration, it does the n work. Therefore the total cost is
𝑇 𝑛 = nlog 2 𝑖
int sum(int a, int b) {
int c = a + b; 1
return c 1
}
𝑇 𝑛 =1+1=2
int array_sum(int a, int n) {
int i; 1
(variable declaration and assignment) int sum = 0; 2 Initial 1
for (i = 0; i < n; i++) { Increment n
sum = sum + a[i] n Condition n+1
}
return sum; 1
}
𝑇 𝑛 = 1 + 2 + 1 + 𝑛 + 𝑛 + 1 + 𝑛 + 1 = 5 + 3𝑛
int sum = 0; 1
for (i = 0; i < n; i++) { n
for (j = 0; j < n; j++) { n
for (k = 0; k < n; k++) { n
if (i == j == k) {
𝒏𝟒 𝒏𝟑 for (l = 0; l < n; l++) { n
𝒏𝟐
n sum = i + j + k + l; 𝟏
}
}
}
}
}
𝑇 𝑛 = 1 + 𝑛4 = 𝑛4
max=-∞ 1
Initial 1
for i=1 to n Increment n
Condition n+1
if A(i)>max
1 1
4n Indexing 1
max=A(i)
Condition 1
end if
end for
return max 1
𝑇 𝑛 = 1 + 1 + 𝑛 + 𝑛 + 1 + 4𝑛 + 1 = 6𝑛 + 4
Initial 1
for i=2 to i<=𝒏𝟐 Increment size (𝒏𝟐 -1)
Condition size+1 (𝒏𝟐 -1) +1
(𝒏 -1)
𝟐
print “hello”
(𝒏𝟐 -1) print (i)
end for
𝑇 𝑛 = 1 + 𝑛2 − 1 + 𝑛2 − 1 + 1 + 𝑛2 − 1 + 𝑛2 − 1
𝑇 𝑛 = 4𝑛2 -2