Dsa 18
Dsa 18
CS_GATE@ANKUSH_SAKLECHA 2
CS_GATE@ANKUSH_SAKLECHA 3
CS_GATE@ANKUSH_SAKLECHA 4
CS_GATE@ANKUSH_SAKLECHA 5
CS_GATE@ANKUSH_SAKLECHA 6
Radix Sort:
171, 258,365,198,879,266,721,98,911
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
CS_GATE@ANKUSH_SAKLECHA 7
Radix Sort:
171, 258,365,198,879,266,721,98,911
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
CS_GATE@ANKUSH_SAKLECHA 8
Complexity of Radix sort:
CS_GATE@ANKUSH_SAKLECHA 9
A sorting algorithm is said to be stable if two objects with equal keys appear
in the same order in the sorted output as they appear in the unsorted input.
Whereas a sorting algorithm is said to be unstable if there are two or more
objects with equal keys which don't appear in same order before and after
sorting.
Bubble sort, Insertion Sort, Merge Sort, Radix sort are the stable sorting
algorithm.
QuickSort, Heap Sort, and Selection sort are the unstable sorting algorithm
An in-place algorithm is an algorithm that does not need an extra space and
produces an output in the same memory that contains the data by
transforming the input ‘in-place’. However, a small constant extra space
used for variables is allowed.
In Place : Bubble sort, Selection sort, Insertion sort, Heap sort, Quick Sort.
Not In-Place : Merge sort, Radix sort. Note that merge sort requires O(n)
extra space.
CS_GATE@ANKUSH_SAKLECHA 10
CS_GATE@ANKUSH_SAKLECHA 11
Straightforward method for Matrix multiplication :
Complexity = ?
CS_GATE@ANKUSH_SAKLECHA 12
Divide and Conquer
Following is simple Divide and Conquer method to multiply two
square matrices.
1) Divide matrices A and B in 4 sub-matrices of size N/2 x N/2 as
shown in the below diagram.
2) Calculate following values recursively. ae + bg, af + bh, ce + dg
and cf + dh.
Complexity = ?
1 2 3 4 1 2 1 1
4 3 2 1 2 1 1 2
1 2 3 1 2 1 1 2
2 1 2 3 1 3 1 2
CS_GATE@ANKUSH_SAKLECHA 13
CS_GATE@ANKUSH_SAKLECHA 14
Strassen’s Method for Matrix Multiplication :
Complexity = ?
CS_GATE@ANKUSH_SAKLECHA 15
Straightforward Method : O(n3 )
Divide and Conquer : O(n3 )
CS_GATE@ANKUSH_SAKLECHA 16
Thank You
Tell me and I forget. Teach me and I remember. Involve me and I learn.
CS_GATE@ANKUSH_SAKLECHA 17
Greedy Method
By Ankush Saklecha
CS_GATE@ANKUSH_SAKLECHA 18
Greedy Method
The greedy method is a design technique applied to those
problems having n inputs and requires us to obtain a
subset that satisfies some constraints.
Any subset that satisfy the constrains are called feasible
solution.
We are require to find a feasible solution that either
maximizes or minimizes a given objective function.
A feasible solution that does this is called an optimal
solution.
Greedy Method does not gives the guarantee of optimal
solution but gives guarantee of feasible solution.
CS_GATE@ANKUSH_SAKLECHA 19
Algorithm Greedy (A,n)
{
// A[1….n] contains the n inputs solution
// initialize solution to empty
for i =1 to n do
x = SELECT(A);
if feasible(solution,x) then
solution = Union(solution,x);
end if
repeat
return (solution);
}
➢ The function SELECT() selects an input from A and assign its value to x.
➢ feasible() is a boolean valued function which determine if x can be included
into the solution vector.
➢ Union() combine x with solution and updates the objective function.
CS_GATE@ANKUSH_SAKLECHA 20
Problems where we can apply greedy approach :
1. Knapsack problem
2. Job sequencing with deadline
3. Optimal merge pattern
4. Huffman code
5. Minimum cost spanning tree
(a) Kruskal’s Algorithm
(b) Prim’s Algorithm
(c) Boruvka’s algorithm
6. Single source shortest path algorithm(Dijkstra's algorithm).etc
CS_GATE@ANKUSH_SAKLECHA 21
1. Knapsack Problem:
Total n objects are there and each object has some weight and profit
corresponding to it and one knapsack of size M.
Problem Statement: we need to choose objects and fill it into knapsack such that
overall weight should be less than or equal to size of knapsack and need to earn
maximum profit.
Hence,
The objective is maximize
𝒏
𝒙𝒊 ∗ 𝒑𝒊
𝒏=𝟏
subject to constraint,
𝒏
𝒙𝒊 ∗ 𝒘𝒊 ≤ 𝑴
𝒏=𝟏
and 0 <= xi <= 1
pi = profit of object i
wi = weight of object i
xi = portion of object i
22
CS_GATE@ANKUSH_SAKLECHA
Find maximum profit. Size of knapsack is 60.
CS_GATE@ANKUSH_SAKLECHA 23
Algorithm Greedy(m,n)
{ // Assume all objects arrange according to highest profit/weight ratio first.
for(i=1 to n do x[i] = 0.0 )
U = m;
for i = 1 to n do
{
if (w[i]>U) then break;
x[i] = 1.0; U = U- w[i];
}
if(i<=n) then x[i] = U/w[i];
}
CS_GATE@ANKUSH_SAKLECHA 24
Time Complexity-
• The main time taking step is the sorting of all items in decreasing order of their
value / weight ratio.
• If the items are already arranged in the required order, then while loop takes
O(n) time.
• The average time complexity of Quick Sort is O(nlogn).
• Therefore, total time taken including the sort is O(nlogn)
CS_GATE@ANKUSH_SAKLECHA 25
CS_GATE@ANKUSH_SAKLECHA 26