Chap1-2 (IA) Complexity - Examples
Chap1-2 (IA) Complexity - Examples
Time complexity:
𝑂(1)
def int_sum(n):
sum = 0
while n > 0: Time complexity:
sum += n 𝑂(𝑛)
n -= 1
return sum
4 1 2 3
8 5 6 6
𝑛
1 9 3 7
7 3 2 8
def print_matrix(mat):
for row in mat:
for cell in row:
print(cell)
def print_matrix(mat):
𝑛
for row in mat:
for cell in row:
print(cell)
def print_matrix(mat):
𝑛
for row in mat:
𝑛
for cell in row:
print(cell)
def print_matrix(mat):
𝑛
for row in mat:
𝑛
for cell in row:
1print(cell)
𝑇 𝑛 =𝑛∗𝑛∗1
𝑇 𝑛 = 𝑛²
𝑇 𝑛 =𝑛∗𝑛∗1
𝑇 𝑛 = 𝑛²
Time complexity:
𝑂(𝑛²)
𝑚
4 1 2 3 7
8 5 6 6 4
𝑛
1 9 3 7 3
7 3 2 8 9
𝑇 𝑛 =𝑛∗𝑚∗1
𝑇 𝑛 = 𝑛𝑚
Time complexity:
𝑂(𝑛𝑚)
def print_matrix(mat):
𝑛
for row in mat:
𝑚
for cell in row:
1print(cell)
def foo(n):
for i in range(10000):
print(i)
def foo(n):
10000
for i in range(10000):
print(i)
def foo(n):
10000
for i in range(10000):
print(i)
1
𝑇 𝑛 = 10000 ∗ 1
𝑇 𝑛 = 10000
𝑇 𝑛 = 10000 ∗ 1
𝑇 𝑛 = 10000
Time complexity:
𝑂(1)
def foo(n):
sum = 0
for i in range(n):
for j in range(2*n):
sum += j
for i in range(100*n):
sum += i
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
for i in range(n):
for j in range(2*n):
sum += j
for i in range(100*n):
sum += i
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
for j in range(2*n):
sum += j
for i in range(100*n):
sum += i
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum += j
for i in range(100*n):
sum += i
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum
1 += j
for i in range(100*n):
sum += i
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum
1 += j
for
100𝑛i in range(100*n):
sum += i
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum
1 += j
for
100𝑛i in range(100*n):
1
sum += i
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum
1 += j
for
100𝑛i in range(100*n):
1
sum += i
4
for i in range(4):
for j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum
1 += j
for
100𝑛i in range(100*n):
1
sum += i
4
for i in range(4):
for
3 j in range(3):
sum += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum
1 += j
for
100𝑛i in range(100*n):
1
sum += i
4
for i in range(4):
for
3 j in range(3):
sum
1 += j
return sum
def foo(n):
1 = 0
sum
𝑛 i in range(n):
for
2𝑛 j in range(2*n):
for
sum
1 += j
for
100𝑛i in range(100*n):
1
sum += i
4
for i in range(4):
for
3 j in range(3):
sum
1 += j
return
1 sum
𝑇 𝑛 = 1 + 𝑛 ∗ 2𝑛 ∗ 1 + 100𝑛 ∗ 1 + 4 ∗ 3 ∗ 1 + 1
2
𝑇 𝑛 = 1 + 2𝑛 + 100𝑛 + 12 + 1
2
𝑇 𝑛 = 2𝑛 + 100𝑛 + 14
𝑇 𝑛 = 1 + 𝑛 ∗ 2𝑛 ∗ 1 + 100𝑛 ∗ 1 + 4 ∗ 3 ∗ 1 + 1
2
𝑇 𝑛 = 1 + 2𝑛 + 100𝑛 + 12 + 1
2
𝑇 𝑛 = 2𝑛 + 100𝑛 + 14
𝑇 𝑛 = 1 + 𝑛 ∗ 2𝑛 ∗ 1 + 100𝑛 ∗ 1 + 4 ∗ 3 ∗ 1 + 1
2
𝑇 𝑛 = 1 + 2𝑛 + 100𝑛 + 12 + 1
2
𝑇 𝑛 = 2𝑛 + 100𝑛 + 14
𝑇 𝑛 = 1 + 𝑛 ∗ 2𝑛 ∗ 1 + 100𝑛 ∗ 1 + 4 ∗ 3 ∗ 1 + 1
2
𝑇 𝑛 = 1 + 2𝑛 + 100𝑛 + 12 + 1
2
𝑇 𝑛 = 2𝑛 + 100𝑛 + 14
Time complexity:
𝑂(𝑛²)
def foo(n):
for i in range(1, n+1):
for j in range(i):
print(j)
def foo(n):
𝑛for i in range(1, n+1):
for j in range(i):
print(j)
def foo(n):
𝑛for i in range(1, n+1):
𝑖
for j in range(i):
print(j)
1
𝑇 𝑛 = 1 + 2 + 3 +⋯𝑛
𝑛 𝑛+1 2
𝑛 +𝑛
𝑇 𝑛 = =
2 2
1 1
𝑇 𝑛 = 𝑛² + 𝑛
2 2
𝑇 𝑛 = 1 + 2 + 3 +⋯𝑛
𝑛 𝑛+1 2
𝑛 +𝑛
𝑇 𝑛 = =
2 2
1 1
𝑇 𝑛 = 𝑛² + 𝑛
2 2
𝑇 𝑛 = 1 + 2 + 3 +⋯𝑛
𝑛 𝑛+1 2
𝑛 +𝑛
𝑇 𝑛 = =
2 2
1 1
𝑇 𝑛 = 𝑛² + 𝑛
2 2
𝑇 𝑛 = 1 + 2 + 3 +⋯𝑛
𝑛 𝑛+1 2
𝑛 +𝑛
𝑇 𝑛 = =
2 2
1 1
𝑇 𝑛 = 𝑛² + 𝑛
2 2
𝑇 𝑛 = 1 + 2 + 3 +⋯𝑛
𝑛 𝑛+1 2
𝑛 +𝑛
𝑇 𝑛 = =
2 2
1 1
𝑇 𝑛 = 𝑛² + 𝑛
2 2
𝑇 𝑛 = 1 + 2 + 3 +⋯𝑛
𝑛 𝑛+1 2
𝑛 +𝑛
𝑇 𝑛 = =
2 2
1 1
𝑇 𝑛 = 𝑛² + 𝑛
2 2
𝑇 𝑛 = 1 + 2 + 3 +⋯𝑛
𝑛 𝑛+1 2
𝑛 +𝑛
𝑇 𝑛 = =
2 2
1 1
𝑇 𝑛 = 𝑛² + 𝑛
2 2
Time complexity:
𝑂(𝑛²)
def foo(n):
for
𝑛 i in range(n):
print(j)
1
def foo(n):
for
𝑛 i in range(n):
print(j)
1
𝑇 𝑛 = 1 + 1 + 1 + ⋯+ 1
𝑇 𝑛 =𝑛∗1=𝑛
def intersection(arr1, arr2):
for elem in arr1:
if elem in arr2:
print(elem)
def intersection(arr1, arr2):
𝑛for elem in arr1:
if elem in arr2:
print(elem)
def intersection(arr1, arr2):
𝑛for elem in arr1:
if
1 elem in arr2:
print(elem)
def intersection(arr1, arr2):
𝑛for elem in arr1:
if
1 elem in arr2:
print(elem)
arr2 𝑚
def intersection(arr1, arr2):
𝑛for elem in arr1:
if
𝑚 elem in arr2:
print(elem)
def intersection(arr1, arr2):
𝑛for elem in arr1:
if
𝑚 elem in arr2:
1 print(elem)
𝑇 𝑛, 𝑚 = 𝑛 𝑚 + 1
𝑇 𝑛, 𝑚 = 𝑛𝑚 + 𝑚
𝑇 𝑛, 𝑚 = 𝑛 𝑚 + 1
𝑇 𝑛, 𝑚 = 𝑛𝑚 + 𝑚
𝑇 𝑛, 𝑚 = 𝑛 𝑚 + 1
𝑇 𝑛, 𝑚 = 𝑛𝑚 + 𝑚
Time complexity:
𝑂(𝑛𝑚)
arr1
arr2
def both_contain(arr1, arr2, k):
return k in arr1 and k in arr2
def both_contain(arr1, arr2, k):
𝑛 in arr1 and k in arr2
return k
def both_contain(arr1, arr2, k):
𝑛 in arr1 and k𝑚in arr2
return k
def both_contain(arr1, arr2, k):
𝑛 in arr1 1and k𝑚in arr2
return k
def both_contain(arr1, arr2, k):
1 𝑛 in arr1 1and k𝑚in arr2
return k
𝑇 𝑛, 𝑚 = 𝑛 + 𝑚 + 1 + 1
𝑇 𝑛, 𝑚 = 𝑛 + 𝑚 + 2
𝑇 𝑛, 𝑚 = 𝑛 + 𝑚 + 1 + 1
𝑇 𝑛, 𝑚 = 𝑛 + 𝑚 + 2
𝑇 𝑛, 𝑚 = 𝑛 + 𝑚 + 1 + 1
𝑇 𝑛, 𝑚 = 𝑛 + 𝑚 + 2
Time complexity:
𝑂(𝑛 + 𝑚)
def both_contain(arr1, arr2, k):
1 1 in arr1 1and k1 in arr2
return k
𝑇 𝑛, 𝑚 = 1 + 1 + 1 + 1
𝑇 𝑛, 𝑚 = 4
𝑇 𝑛, 𝑚 = 1 + 1 + 1 + 1
𝑇 𝑛, 𝑚 = 4
Time complexity:
𝑂(1)
def foo(n):
i = 1
while i < n:
i *= 2
1 ∗ 2 ∗ 2 ∗ ⋯∗ 2 = 𝑛
𝑘
1 ∗ 2 ∗ 2 ∗ ⋯∗ 2 = 𝑛
𝑘
𝑘
2 = 𝑛
𝑘
2 = 𝑛
𝑏
𝑎 = 𝑐 ⇒ 𝑏 = log 𝑎 𝑐
𝑘
2 = 𝑛
𝑏
𝑎 = 𝑐 ⇒ 𝑏 = log 𝑎 𝑐
𝑘
2 = 𝑛 ⇒ 𝑘 = log 2 𝑛
def foo(n):
1i = 1
while i < n:
log 2 𝑛
1i *= 2
𝑇 𝑛 = (log 2 𝑛 ∗ 1) + 1
𝑇 𝑛 = log 2 𝑛 + 1
Time complexity:
𝑂(𝑙𝑜𝑔𝑛)
def where_equal(str1, str2):
indexes = []
i = 0
while i < len(str1) and i < len(str2):
if str1[i] == str2[i]:
indexes.append(i)
i += 1
return indexes
def where_equal(str1, str2):
1
indexes = []
i = 0
while i < len(str1) and i < len(str2):
if str1[i] == str2[i]:
indexes.append(i)
i += 1
return indexes
def where_equal(str1, str2):
1
indexes = []
1 = 0
i
while i < len(str1) and i < len(str2):
if str1[i] == str2[i]:
indexes.append(i)
i += 1
return indexes
def where_equal(str1, str2):
1
indexes = []
1 = 0
i
while i < len(str1) and i < len(str2):
1 str1[i] == str2[i]:
if
indexes.append(i)
i += 1
return indexes
def where_equal(str1, str2):
1
indexes = []
1 = 0
i
while i < len(str1) and i < len(str2):
1 str1[i] == str2[i]:
if
1 indexes.append(i)
i += 1
return indexes
def where_equal(str1, str2):
1
indexes = []
1 = 0
i
while i < len(str1) and i < len(str2):
1 str1[i] == str2[i]:
if
1 indexes.append(i)
i
1 += 1
return indexes
def where_equal(str1, str2):
1
indexes = []
1 = 0
i
while i < len(str1) and i < len(str2):
1 str1[i] == str2[i]:
if
1 indexes.append(i)
i
1 += 1
return
1 indexes
def where_equal(str1, str2):
1
indexes = []
1 = 0
i
min(𝑛, 𝑚)
while i < len(str1) and i < len(str2):
1 str1[i] == str2[i]:
if
1 indexes.append(i)
i
1 += 1
return
1 indexes
𝑇 𝑛, 𝑚 = 1 + 1 + min 𝑛, 𝑚 1+1+1 +1
𝑇 𝑛, 𝑚 = 3 min 𝑛, 𝑚 + 3
𝑇 𝑛, 𝑚 = 1 + 1 + min 𝑛, 𝑚 1+1+1 +1
𝑇 𝑛, 𝑚 = 3 min 𝑛, 𝑚 + 3
𝑇 𝑛, 𝑚 = 1 + 1 + min 𝑛, 𝑚 1+1+1 +1
𝑇 𝑛, 𝑚 = 3 min 𝑛, 𝑚 + 3
𝑇 𝑛, 𝑚 = 1 + 1 + min 𝑛, 𝑚 1+1+1 +1
𝑇 𝑛, 𝑚 = 3 min 𝑛, 𝑚 + 3
Time complexity:
𝑂(min(𝑛, 𝑚))
while i < len(str1) or i < len(str2):
while i < len(str1) or i < len(str2):
Time complexity:
𝑂(max(𝑛, 𝑚))
def where_equal(str1, str2):
indexes = []
i = 0
while i < len(str1) and i < len(str2):
if str1[i] == str2[i]:
1 indexes.append(i)
i += 1
return indexes
def where_equal(str1, str2):
indexes = []
1
i = 0
while i < len(str1) and i < len(str2):
if str1[i] == str2[i]:
indexes.append(i)
i += 1
return indexes
def where_equal(str1, str2):
min(𝑛,
indexes𝑚)= []
1
i = 0
while i < len(str1) and i < len(str2):
if str1[i] == str2[i]:
indexes.append(i)
i += 1
return indexes
𝑇 𝑛, 𝑚 = min 𝑛, 𝑚 + 1
Space complexity:
𝑂(min(𝑛, 𝑚))
def where_equal(str1, str2):
1
indexes = []
1
i = 0
while i < len(str1) and i < len(str2):
if str1[i] == str2[i]:
indexes.append(i)
i += 1
return indexes
𝑇 𝑛, 𝑚 = 1 + 1
𝑇 𝑛, 𝑚 = 2
Space complexity:
𝑂(1)
def get_even_values(arr):
even_values = []
for elem in arr:
if (elem % 2) == 0:
even_values.append(elem)
return even_values
def get_even_values(arr):
𝑛
even_values = []
for elem in arr:
if (elem % 2) == 0:
even_values.append(elem)
return even_values
Space complexity:
𝑂(𝑛)
def copy_arr(arr):
copy = arr
return copy
arr
def copy_arr(arr):
copy = arr
return copy
copy
def copy_arr(arr):
copy = arr
return copy
Space complexity:
𝑂(1)
>>> arr = [1, 2, 3]
>>> copy = arr
>>> copy.append(4)
>>> arr
<<< [1, 2, 3, 4]
def copy_arr(arr):
copy = arr.copy()
return copy
Space complexity:
𝑂(𝑛)
def concat(str1, str2):
return str1 + str2
def concat(str1, str2):
return str1 + str2
str1: "inside" str2: "code"
𝑛 𝑚
str1+str2: "insidecode"
𝑛+𝑚
def concat(str1, str2):
return 𝑛str1
+ 𝑚 + str2
Space complexity:
𝑂(𝑛 + 𝑚)
def get_prefixes(s):
prefixes = []
for i in range(len(s)):
prefixes.append(s[0:i])
return prefixes
Proper prefixes of "abcdef":
• ""
• "a"
• "ab"
• "abc"
• "abcd"
• "abcde"
def get_prefixes(s):
prefixes = []
for i in range(len(s)):
prefixes.append(s[0:i])
return prefixes
def get_prefixes(s):
prefixes
𝑛 = []
for i in range(len(s)):
prefixes.append(s[0:i])
return prefixes
𝑚
["green",
"three",
𝑛 "sugar",
"earth",
"house"]
Proper prefixes of "abcdef":
• ""
• "a"
• "ab"
• "abc"
• "abcd"
• "abcde"
Proper prefixes of "abcdef":
• "" 0
• "a"
• "ab"
• "abc"
• "abcd"
• "abcde"
Proper prefixes of "abcdef":
• "" 0
• "a" 1
• "ab"
• "abc"
• "abcd"
• "abcde"
Proper prefixes of "abcdef":
• "" 0
• "a" 1
• "ab" 2
• "abc"
• "abcd"
• "abcde"
Proper prefixes of "abcdef":
• "" 0
• "a" 1
• "ab" 2
• "abc"
• "abcd"
• "abcde" 𝑛 − 1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1
𝑛 𝑛+1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 + 𝑛 =
2
𝑛 𝑛+1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 = −𝑛
2
2𝑛𝑛 + 𝑛
𝑛+1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 = −𝑛
22
1 2 1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 =
2
𝑛 + 𝑛
2
−𝑛
1 2 1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 = 𝑛 − 𝑛
2 2
1
𝑛 2 1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 = 𝑛 − 𝑛
2 2
1
𝑛 2 1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 = 𝑛 − 𝑛
2 2
1
𝑛 2 1
0 + 1 + 2 + 3 + ⋯+ 𝑛 − 1 = 𝑛 − 𝑛
2 2
Space complexity:
𝑂(𝑛²)
𝑇 𝑛 = 0 + 1+ 2+ 3+ ⋯+ 𝑛 − 1
𝑛−1
2
𝑛−1 𝑛 𝑛 −𝑛
𝑇 𝑛 = 𝑘 = =
2 2
𝑘=0
1 2 1 2
𝑇 𝑛 = 𝑛 − 𝑛 = 𝑂(𝑛 )
2 2
Time complexity: 𝑚
𝑂(𝑛𝑚)
𝑛
Time complexity: 𝑚
𝑂(𝑛 + 𝑚)
𝑛
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Time complexity: Abir
𝑂(𝑛) Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
Dalal
Dora
𝑛 Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
𝑛 Alia
Balkis
2 Dalal
Dora
Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Abir
Alia
Balkis
𝑛 Dalal
Dora
4 Faouzi
Houyem
Montassar
Roua
Samir
Wahida
Time complexity:
𝑂(𝑙𝑜𝑔𝑛)
Time complexity:
𝑂(𝑙𝑜𝑔𝑛)
Write a program which for any integer n > 1,
provides its GCD (Greatest Common Divisor)
other than itself.
▪ gcd(n)=1, if n is a prime
▪ 1 < gcd(n) < n-1, else
1 k n-1
To examine Examined
def gcd_1 (n:integer):
i:integer
i = n-1
while n mod i ≠ 0:
i = i-1
end while
gcd = i
return gcd
2 k n
Examined To examine
The result we are looking for is equal to n/p, where p is the Least Common
Divisor (LCD) that must be greater than 2.
def gcd_2 (n:integer):
i:integer
i = 2
while n mod i ≠ 0:
i = i+1
end while
gcd = n/i
return gcd
n is not a prime: 2 < lcd(n) < gcd(n) < n-1 ;
n is not a prime: (lcd(n))2 < n
2 k 𝒏 n
Examined To examine No scan needed
if (n mod i = 0)
gcd = i
else
gcd = 1
return gcd
Sequence of the 3 functions
I1 t1
while C do tc
Corresponding
I2 t2
exécution times
end while
I3 t3
Exécution times for
tBC
(un)conditional
tBI branches
Sequence of the 3 functions
✓ Detailing the practical complexity consists in calculating a, b, a' and b' specific to a
given machine.