Unit II D&C and Greedy 2022I
Unit II D&C and Greedy 2022I
Where:
• T(n) is the time for D&C on any input of size n
• g(n) is the time to compute the answer directly for small
inputs.
• The function f(n) is the time for dividing P and combining
the solutions to sub problems
• For D&C based algorithms that produce sub problems of
the same type as the original problem, it is very natural to
first describe such algorithms using recursion
D&C: Example
Min/Max Problem
D&C: Example Min/Max Problem Naïve approach
Find maximum
value in an array of
size n
Time complexity
• Naïve algo: O(n)
• D & C algo: O(nlog2 n)
35 Maximum(start_index_of_array, end_index_of_array)
30
25
20 n
15 log(n)
10 nlog(n)
5
0
1 2 3 4 5
6 7 8 9 10
D&C: Example Min/Max Problem
Maximum(x , y)
A[0] =10
A[1] =12
A[2] = 8
A[3] = 2
A[4] = 3
A[5] = 1
max1= maximum (0,1) (12), max2 = return max( a[2], a[2]) ≡ 8, return max( 12, 8) 12
max1= maximum (0,2) (12), max2 = return max( a[3], a[5]) ≡ 2 , return max( 12, 2) 12
ans= maximum ( 0 , 5)
A[0] =10
D&C: Example Min/Max Problem
A[1] =12
Maximum(x , y)
A[2] = 8
A[3] = 2
A[4] = 3
A[5] = 1
max1= maximum (3,4) (3) , max2 = maximum (5, 5) (1) , return max( max1, max2) (3)
max1= maximum (0,2) (12), max2 = (return max( a[3], a[5]) ≡ 3, return max( 12, 3) 12
ans= maximum ( 0 , 5) 12
Recursive Calls Example: Algorithm maximum()
Find Maximum value for given array
Element 5 10 15 2 -1 -50 100 7 3
Index 1 2 3 4 5 6 7 8 9
(1,9)
(1,5) (6,9)
(1,2) (3,3)
Maximum (1,9) Maximum (1,5)
If ( 9-1 <= 1 ) If ( 5-1 <= 1 )
…….. ……..
else else
max1 = maximum(1,5) => 15 max1 = maximum(1,3) => 15
max2 = maximum(6,9) => 100 max2 = maximum(4,5) => 2
return(max1,max2) =>
max(15,100) return(max1,max2) =>
return(100) max(15,100)
return(15)
return(max1,max2) =>
Recurrence Equation for MinMax Algorithm
D&C: Example
Binary Search
Iterative Binary Search
Examples of Binary Search
Recursive Binary Search
Binary Decision Tree for Binary Search
n=14
Binary Search: Recurrence
Input: Sorted array A of size n, an element x to be searched
Question: Is x ∈ A
Recurrence relation is
T(n) = T(n/2) + 1 ………Otherwise
T(1) = 1 ……..n=1
where T(n) is the time required for binary search in an array of size n.
Binary Search: Recurrence
T(n) = T(n/2) + 1 ………Otherwise
T(1) = 1 ……..n=1
where T(n) is the time required for binary search in an array of size n.
So how many times we need to divide by 2 until we have only one element left
Since T(1) = 1,when n = 2k
⇒ log n=k [taken log(base 2) on both sides ] Put k= log n in final eq
T(n) = T(1) + k = 1 + log2 (n).
T(n) = Θ(log2 (n))
Time Complexity of Binary Search
D&C: Example
Merge Sort
Merge Sort
Examples of Merge Sort
When n is a power of 2, n = 2k
Pivot
Pivot 40 20 10 80 60 50 7 30 100
index
=0 0 1 2 3 4 5 6 7 8
Low High
Pivot 7 20 10 30 40 50 60 80 100
index
0 1 2 3 4 5 6 7 8
=4
23
Greedy algorithm: example 4: Minimum Spanning
Tree(MST) Kruskal’s algorithm: cycle
• A tree is a acyclic data structure
• While buildingMST from graph, algorithm
cycle, is required to check for cycle
• At each step of Kruskal’s algorithm, composite
partial solution (V, A) is a forest
1. If nodes u and v are in the same tree, then adding
edge (u, v) to A creates a cycle
2. If nodes u and v arenotin the same tree,
then adding edge (u, v) does not create cycle
24
Greedy algorithm: example 4: Minimum Spanning
Tree(MST) Kruskal’s algorithm:
Union/ add
• A tree is a acyclic data structure
• While buildingMST from graph, algorithm
cycle, is required to check for cycle
• At each step of Kruskal’s algorithm,
composite partial solution (V, A) is a forest
1. If nodes u and v are in the same tree, then adding
edge (u, v) to A creates a cycle
2. If nodes u and v arenotin the same tree,
then adding edge (u, v) does not create cycle
25
Thank You