You are on page 1of 30

Algorithms (CS-204)

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.

• Specification: Must have unambiguous and


well defined input and output.

• Desired Feature: Correctness, Termination,


Efficiency
Design of Algorithm
• Understand the problem and try to explore
solution space.

• Compare the solution space in terms of


efficiency and choose the right solution
Finding minimum element in
unordered list
• Specification:
– Input: L={l1,l2,…,ln} li ∈ Integers
– Output: s ∈ L and s <= li ∀ li ∈ L
• Approach:
• We can think about a tournament, where n teams (a
team corresponds to one number in input list)
participated and a team wins a game played between
two teams if it holds smaller number. That means if there
is a game between x and y then x will win if x is less than
or equal to y.
• So we have to design a tournament to find out the
winner.
• Tournament can be organized in number of ways.
• we will create two groups where first group
consists of (Team1, …, Team K) and second group
consists of (Team K+1, …, Team n)
• Now depending on the value of 1<=k<n many
different strategies are possible to organize the
tournament,
• However, all strategies will lead us to decide
winner of the tournament.
A recursive Solution
• Approach: Split the list into two sub-lists say L1 and
L2(of smaller size). Find the minimum of L1 and L2 in
similar way. Then combine the solution obtained
from L1 and L2 to get solution of L.

• It is a recursive definition of finding minimum.

• Are we missing anything?


• We are missing base condition.

• What is base condition?

• Progress towards base is needed.


• FindMinimum(Integer List L)
– if |L| <= 1:
• if |L| == 0 Print error message; exit;
• if |L| == 1 return (L[0]);
– else
• Split L into L1 and L2
• x1=FindMinimum(L1)
• x2=FindMinimum(L2)
• If(x1 <x2) return x1
• else return x2;
How should we split the list?
• We can split a list in a number of ways

• Will it affect correctness?

• Will it affect termination?

• Will it affect efficiency?


Impact of split size on efficiency
8 1

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

• Approach: Split the list L into two sub-lists say L1 and


L2 (of smaller size). Search element q in L1 and L2 in
similar way. Then combine the result to obtain the
result of searching q in list L.

• We missed to mention base condition, split size


• 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 split L (k:n-k) into L1 and L2
x1= Search(L1,q)
x2=Search(L2,q)
If (x1==True || x2 == True) return True;
• Correctness?

• 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.

You might also like