Weak 1
Weak 1
Week 1, Module 1
Efficiency
Modelling
Techniques
Background in programming
Arrays, lists
Topics to be covered
Asymptotic complexity
Data structures
Miscellaneous topics
Intractability, …
Tentative schedule January
18 19 20 21 22 23 24
Week 3: Graphs and basic graph algorithms
25 26 27 28 29 30 31
Week 4: More graph algorithms, disjoint set
February
Week 5: Divide and conquer, heaps 1 2 3 4 5 6 7
8 Weekly quizzes
6 programming assignments
Certification exam
Algorithm Design
Algorithms
Varanasi
Ahmedabad
Kolkata
Mumbai
Hyderabad
Visakhapatnam
Bangalore
Chennai
Trivandrum
Throw away the map and record the network
Earliest deadline?
Plagiarism detection
Edit distance
Impossibly inefficient!
Decomposing the problem
Make the first character in both documents the
same
Sequence is 1,1,2,3,5,8,13,21,….
Computing recursively
Focus on words
Ignore constants
Ignore constants
Ignore constants
t(n) ≤ cg(n)
for every n ≥ n0
Examples: Big O
100n + 5 is O(n2)
100n + 5
≤ 100n + n, for n ≥ 5
2
= 101n ≤ 101n , so n0 = 5, c = 101
Alternatively
100n + 5
≤ 100n + 5n, for n ≥1
= 105n ≤ 105n2, so n0 = 1, c = 105
n3 is not O(n2)
No matter what c we choose, cn2 will be
dominated by n3 for n ≥ c
Useful properties
If
f1(n) is O(g1(n))
f2(n) is O(g2(n))
t(n) ≥ cg(n)
for every n ≥ n0
Lower bounds
n3 is Ω(n2)
n3 ≥ n2 for all n
n0 = 0 and c = 1
Upper bound
2 2
n(n-1)/2 = n /2 - n/2 ≤ n /2, for n ≥ 0
Lower bound
Iterative programs
Recursive programs
Example 1
Maximum value in an array
function maxElement(A):
maxval = A[0]
for i = 1 to n-1:
if A[i] > maxval:
maxval = A[i]
return(maxval)
Example 2
Check if all elements in an array are distinct
function noDuplicates(A):
for i = 0 to n-1:
for j = i+1 to n-1:
if A[i] == A[j]:
return(False)
return(True)
Example 3
Matrix multiplication
function matrixMultiply(A,B):
for i = 0 to n-1:
for j = 0 to n-1:
C[i][j] = 0
for k = 0 to n-1:
C[i][j] = C[i][j] + A[i][k]*B[k][j]
return(C)
Example 4
Number of bits in binary representation of n
function numberOfBits(n):
count = 1
while n > 1:
count = count + 1
n = n div 2
return(count)
Example 5
Towers of Hanoi
Three pegs,
A, B, C
Move n disks
from A to B
C is transit peg
Example 5
Recursive solution
M(1) = 1
Recurrence
M(n) = 2M(n-1) + 1
= 2(2M(n-2)+1) + 1 = 22M(n-2) + (2+1)
= 22(2M(n-3)+1) + 2 + 1 = 23M(n-3) + (4+2+1)
=…
= 2kM(n-k) + (2k - 1)
=…
= 2n-1M(1) + (2n-1 - 1)
= 2n-1 + 2n-1 - 1 =
= 2n - 1
Summary
Iterative programs
Focus on loops
Recursive programs