Python Lecture 10-Efficiency
Python Lecture 10-Efficiency
Computational Complexity
What is a “good” algorithm/
program?
• Solution is simple but powerful/general
• Easily understood by reader
• Easily modifiable and maintainable
• Correct for clearly defined situations
• Efficient in space and time
• Well documented
– usable by those who do NOT understand the detailed
working
• Portable across computers
• Can be used as a sub-program
can be moved
from loop
– loop-invariant x=0
t1 = (a*a*a + c)
computation t2 = b*b
for i in range(10):
y=t1*x*x + t2*x +c
print(y)
x=x+0.01
y = 2x
y = ax2 + b
y = ax + b
y = log x
• Problem:
– Given real number x and integer n
– Write an algorithm to calculate xn
• Power
– power(x,n) = 1 for n = 0
– power(x, n) = x * power(x,n-1) for n>1
def power(x,n):
if (n==0):
return 1
else:
return x*power(x,n-1)
• Power
– T(n) = 1, if n = 0
– T(n) = T(n-1) + 1, otherwise
Solving Recurrence Relations
• By Telescoping
– substitution
T(n) = T(n-1) + 1
= T(n-2) + 2
= T(n-3) + 3
...
= T(2) + n-2
= T(1) + n-1
= T(0) + n
=1+n
= O(n)
Fast Algorithm
• Fast Power
def fpower(x,n):
if (n==0):
return 1
else:
y = fpower(x,int(n/2))
if (n%2 == 0):
return y*y
else:
return x*y*y
• Fast Power
– T(n) = 1, if n = 0
– T(n) = 1, if n = 1
– T(n) = T(n/2) + c, otherwise
Solving Recurrence Relations
• By Telescoping
– substitution
T(n) = T(n/2) + c
= T(n/22) + 2c
= T(n/23) + 3c
...
= T(n/2m-1) + (m-1)c
= T(n/2m) + mc
= O(m)
= O(log2n)
...where m = log2n
Binary Search
mid
L U
• “Divide and
Conquer” strategy # Algorithm Binary Search
– at every stage, we def binarysearch(ar, l, r, x):
reduce the size of
the problem to half while l<=r:
the earlier stage mid = l+(r-l)//2
• Strategy: Compare if ar[mid] == x:
return mid
with the middle
elif ar[mid] < x:
element of current l = mid + 1 Iterative
range, and else:
eliminate half of r = mid - 1
the range
return -1
if r >= l:
mid = l + (r - l)//2
if ar[mid] == x:
return mid
elif ar[mid] > x:
return binarysearch(ar, l, mid-1, x)
else:
return binarysearch(ar, mid+1, r, x)
else:
return -1
• Binary Search
– T(n) = 1, if n = 1
– T(n) = T(n/2) + O(1), otherwise
– Solution O(log2n)
Sorting an Array
0 1 2 3 4
A 2 3 5 6 9 How do we sort?
for in range(len(A)) :
k = position of min. element
between A [i] and A [N-1]
Swap A [i] and A [k]
for in range(len(A)) :
k = position of min. element
between A [i] and A [N-1] for j in range(i+1, len(A)):
Swap A [i] and A [k] if A[min_index] > A[j]:
min_index = j
t=A[i]
A[i]=A[k]
A[k]=t