Professional Documents
Culture Documents
Introduction
• Course Page:
https://www.iitp.ac.in/~sourav/CS204_2021/
• Instructor:
– Dr. Sourav Kumar Dandapat(sourav@iitp.ac.in)
• TA:
• Class Timing:
– Monday (11am – 11.55am)
– Tuesday (11am – 11.55 am)
– Wednesday (11 am – 11.55 am)
• Textbook: Introduction to Algorithm by Thomas H.
Cormen, Charles E. Leiserson, Ronald Rivest, Clifford
Stein
Evaluation breakup
• End Sem: 25
• Mid Sem: 25
• Bi-weekly assessment: 40
• (best n-1 quiz performance will be considered,
no compensatory quiz )
• Daily performance: 10
– Attendance
– Home work
Regarding course structure
• Recursion
– Divide and Conquer
– Backtracking
– Recurrence Solving
• Sorting Algorithm
– Merge sort, Heap-sort, Quick-sort, Linear-sort
• Selection Algorithm
• Dynamic Programming
• Greedy Programming
• Graph algorithms
• Hardness of the problems – NP
• String algorithm
Introduction to Algorithm
• What is algorithm: Set of steps for performing
a particular task.
1 7 1
1,n-1
Total Cost = 7
1 6 1
1 5 1
1 4 1
1 3 1
1 2 1
1 1
Impact of split size on efficiency
8 1
2 1 6 1 2,n-2
Total Cost = 7
2 1 4 1
1 1
2 1 2 1
1 1
1 1 1 1
Impact of split size on efficiency
8 1
3 1 5 1 3,n-3
Total Cost = 7
3 1 2 1
1 2 1
2 1 1 1
1 1
1
1 1
Impact of split size on efficiency
8 1
4 1
4 1 4,n-4
Total Cost = 7
2 1 2 1 2 1 2 1
1 1 1 1 1 1 1 1
Cost Analysis
T(1)=0
T(n)= 1+T(n-1)
T(8)=1+T(7)=1+1+T(6)=3+T(5)=4+T(4)=7+T(1)=7=
n-1
T(n) = T(n/2)+T(n-n/2)+1
T(8) =T(4)+T(4)+1 = 2(2T(2)+1)+1=4(2T(1)+1)+3=7=n-1
T(n) = O(n)
What Can We Conclude?
Searching element in a ordered integer list
• Termination?
• Efficiency?
Do we really need to check both sub-lists?
• Search(Integer List L, integer q)
• if |L| == 0 print error message and exit
• if |L|== 1
– if L[0] == q then return True;
– else return False;
• else
– Find some k for splitting
– split L into L1(0,k-1) and L2(k,n-1)
– If L1[k-1]<=q return Search(L1,q);
– else return Search(L2,q)
Does all the split cost same?
Impact of split size on efficiency
8 1
1 1 7 1
K=1
WorstCost = 8
1 1 6 1
1 1 5 1
1 1 4 1
1 1 3 1
1 1 2 1
1 1 1 1
Impact of split size on efficiency
8 1
2 1 6 1 K=2
Worst Cost = 5
1 2 1 1
1 1 4
1
1 1 1 2 1 2 1
1
1 1 1 1 1 1
1 1
Impact of split size on efficiency
8 1
5 1 K=3
3 1
WorstCost = 5
3 1 2 1
2 1 1 1
2 1 1 1 1 1 1
1 1 1 1 1
1 1 1 1
Impact of split size on efficiency
8 1
4 1
4 1 Worst Cost = 4
2 1 2 1 2 1 2 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
1
Cost Analysis
• T(1)=1
• T(n)=T(n/2)+1=T(n/4)+2=T(n/2i)+i
• n/2i=1 => i=log2n
• T(n)=T(1)+log2n=1+log2n = O(log2n)
Homework
• Given a list L of n unordered integers. You are asked
to find out minimum as well as maximum of n
elements. Analyze the cost of your algorithm in
terms of number of comparison made.