0% found this document useful (0 votes)
15 views

Lecture 2.3 - Calculating Complexity

Uploaded by

smitstudying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 2.3 - Calculating Complexity

Uploaded by

smitstudying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Calculating complexity — Examples

Madhavan Mukund
https://www.cmi.ac.in/~madhavan

Programming, Data Structures and Algorithms using Python


Week 2
Calculating complexity

Iterative programs
Recursive programs

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 2/9
Example 1

Find the maximum element in a list

Input size is length of the list def maxElement(L):


maxval = L[0]
Single loop scans all elements
for i in range(len(L)):
Always takes n steps if L[i] > maxval:
maxval = L[i]
Overall time is O(n) return(maxval)

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 3/9
Example 2

Check whether a list contains duplicates

Input size is length of the list def noDuplicates(L):


for i in range(len(L)):
Nested loop scans all pairs of elements
for j in range(i+1,len(L)):
A duplicate may be found in the very if L[i] == L[j]:
first iteration return(False)
return(True)
Worst case — no duplicates, both loops
run fully
Time is
(n − 1) + (n − 2) + . . . + 1 = n(n − 1)/2
Overall time is O(n2 )

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 4/9
Example 3

Matrix multiplication

Matrix is represented as list of lists def matrixMultiply(A,B):


  (m,n,p) = (len(A),len(B),len(B[0]))
1 2 3
4 5 6 C = [[ 0 for i in range(p) ]
for j in range(m) ]
[[1,2,3],[4,5,6]]
for i in range(m):
Input matrices have size m × n, n × p for j in range(p):
for k in range(n):
Output matrix is m × p C[i][j] = C[i][j] + A[i][k]*B[k][j]
Three nested loops return(C)
Overall time is O(mnp) — O(n3 ) if
both are n × n

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 5/9
Example 4

Number of bits in binary representation of n

log n steps for n to reach 1 def numberOfBits(n):


For number theoretic problems, input
count = 1
size is number of digits
This algorithm is linear in input size while n > 1:
count = count + 1
n = n // 2

return(count)

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 6/9
Example 5
Towers of Hanoi

Three pegs A,B,C


Move n disks from A to B, use C as
transit peg
Never put a larger disk on a smaller one

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 7/9
Example 5
Towers of Hanoi

Three pegs A,B,C


Move n disks from A to B, use C as
transit peg
Never put a larger disk on a smaller one

Recursive solution

Move n − 1 disks from A to C, use B as


transit peg
Move larges disk from A to B
Move n − 1 disks from C to B, use A as
transit peg
Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 7/9
Example 5
Recurrence

M(n) — number of moves to transfer n disks


M(1) = 1
M(n) = M(n − 1) + 1 + M(n − 1) = 2M(n − 1) + 1

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 8/9
Example 5
Recurrence

M(n) — number of moves to transfer n disks


M(1) = 1
M(n) = M(n − 1) + 1 + M(n − 1) = 2M(n − 1) + 1

Unwind and solve


M(n) = 2M(n − 1) + 1

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 8/9
Example 5
Recurrence

M(n) — number of moves to transfer n disks


M(1) = 1
M(n) = M(n − 1) + 1 + M(n − 1) = 2M(n − 1) + 1

Unwind and solve


M(n) = 2M(n − 1) + 1
= 2(2M(n − 2) + 1) + 1 = 22 M(n − 2) + (2 + 1)

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 8/9
Example 5
Recurrence

M(n) — number of moves to transfer n disks


M(1) = 1
M(n) = M(n − 1) + 1 + M(n − 1) = 2M(n − 1) + 1

Unwind and solve


M(n) = 2M(n − 1) + 1
= 2(2M(n − 2) + 1) + 1 = 22 M(n − 2) + (2 + 1)
= 22 (2M(n − 3) + 1) + (2 + 1) = 23 M(n − 3) + (4 + 2 + 1)

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 8/9
Example 5
Recurrence

M(n) — number of moves to transfer n disks


M(1) = 1
M(n) = M(n − 1) + 1 + M(n − 1) = 2M(n − 1) + 1

Unwind and solve


M(n) = 2M(n − 1) + 1
= 2(2M(n − 2) + 1) + 1 = 22 M(n − 2) + (2 + 1)
= 22 (2M(n − 3) + 1) + (2 + 1) = 23 M(n − 3) + (4 + 2 + 1)
···
= 2k M(n − k) + (2k − 1))

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 8/9
Example 5
Recurrence

M(n) — number of moves to transfer n disks


M(1) = 1
M(n) = M(n − 1) + 1 + M(n − 1) = 2M(n − 1) + 1

Unwind and solve


M(n) = 2M(n − 1) + 1
= 2(2M(n − 2) + 1) + 1 = 22 M(n − 2) + (2 + 1)
= 22 (2M(n − 3) + 1) + (2 + 1) = 23 M(n − 3) + (4 + 2 + 1)
···
= 2k M(n − k) + (2k − 1))
···
= 2n−1 M(1) + (2n−1 − 1)

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 8/9
Example 5
Recurrence

M(n) — number of moves to transfer n disks


M(1) = 1
M(n) = M(n − 1) + 1 + M(n − 1) = 2M(n − 1) + 1

Unwind and solve


M(n) = 2M(n − 1) + 1
= 2(2M(n − 2) + 1) + 1 = 22 M(n − 2) + (2 + 1)
= 22 (2M(n − 3) + 1) + (2 + 1) = 23 M(n − 3) + (4 + 2 + 1)
···
= 2k M(n − k) + (2k − 1))
···
= 2n−1 M(1) + (2n−1 − 1)
= 2n−1 + 2n−1 − 1 = 2n − 1
Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 8/9
Summary

Iterative programs
Focus on loops

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 9/9
Summary

Iterative programs
Focus on loops

Recursive programs
Write and solve a recurrence

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 9/9
Summary

Iterative programs
Focus on loops

Recursive programs
Write and solve a recurrence

Need to be clear about accounting for “basic” operations

Madhavan Mukund Calculating complexity — Examples PDSA using Python Week 2 9/9

You might also like