C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 C TEL SOLUTION Subject: Design and analysis of algorithm( DAA ) Notes prepared By: Ahmad Sir 9867597554 All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 1 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 UNIT -III Divide and Conquer Strategies & Greedy Approach All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 2 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 1. Divide and Conquer strategies: 1. Binary Search : Algorithm bsearch (a[i], key, low, high,) { mid =( low + high) /2 if ( low > high ) then return(1) else if ( a[ mid] = key ) then return(mid) else if( a[ mid] < key ) return(bsearch (a[i], key, mid+1, high,)) else return((bsearch (a[i], key, low, mid-1)) } Complexity: O(log2n) This analysis is to determine average no of comparison for successful and unsuccessful search. All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 3 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 2. Quick Sort : Algorithm Quick Sort(a, low, high) { low = 0 ,high = n-1 if (low < high ) mid = partition ( a, low, high) Quick Sort ( a, low, mid-1) Quick Sort ( a, mid+1, high) } Algorithm partition ( a, low, high) { i = low, j = high while ( a[i] < = key && I < high ) i++ while ( a[j] > key && j < low ) j- if( i< j) then interchange a[i] & a[j] else interchange a[j] & key } Complexity: Best case & Average case: n log2n All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 4 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 Worst case O (n2) 3. Merge Sort: Algorithm Mergesort( a, low, high) { if ( low < high) { mid =( low + high) /2 mergesort( a, low, mid) mergesort( a, mid+1, high) mergesort( a, low, high) } } Complexity: n log2n Where ‘n’ is the no of comparisons and log2n is the hight of tree. All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 5 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 4. Min_max : Algorithm min_max( low, high, min, max) { if ( low = high) then { min = max = a [low] else if ( low = high -1) then else if ( a[low] < a [high]) then min = a[low] max = a[high] else min = a[high] max = a[low] } else mid =( low + high) /2 min_max( low, mid, min, max) min_max(mid+1, high, min1, max1) if ( min1 < min) then min = min1 if( max1> max) then max = max1 } All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 6 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 Complexity: 2 log2n 5.Heap sort: Algorithm Heapsort(A) { Build_ heap (A) For i = n to 1 do swap ( A[1], A[i] ) n- heapify(A, 1) Build Max_heap(A) Heap-size(A) = length [A] For i int(length[A]/2) to 1 do Max_heap(A, i) } Complexity: O(n logn) All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 7 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 6.Multiplication Algorithm: Stasseens matrix multiplication: A A = ( ππ Aππ Aππ ) Aππ B= ( Bππ Bππ Bππ ) Bππ S1 = B12 – B22 S2 = A11 + A12 S3 = A21 + A22 S4 = B21 – B11 S5 = A11 + A22 S6 = B11 + B22 S7 = A12 – A22 S8 = B21 + B22 S9 = A11 – A21 S10 = B11 + B12 P1 = S1 βA11 P2 = S2 βB22 P3 = S3 βB11 P4 = S4 βA22 C11 = - P2 + P4 + P5 + P6 C12 = P1 + P2 C21 = P3 + P4 C11 = P1 – P3 + P5 - P7 All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 8 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 A ( ππ Aππ Aππ B ) . ( ππ Bππ Aππ Bππ C ) = ( ππ Bππ Cππ Cππ ) Cππ Greedy Methods: 1. Knapsack Problem: Assumption: 1. P[1….n] profit array 2. W[1…n] weight array Capacity = total capacity Weight = 0 x[1…n] is the array that contains three types of values 1 = complete 0 = reject Fraction = partially All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 9 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 Algorithm knapsack(P, W, capacity, weight) Step1. For i = 1 to n do x[i] = 0 weight = 0 Step2. while ( weight < capacity) do { Select the best object with max (Pi/ Wi) ratio, let its index = i if (weight + W[i] <= capacity ) then { x[i] = 1 weight = weight +W[i] } else { x[i] = πππππππ‘π¦−π€πππβπ‘ π[π] weight = capacity } Step3. Profit = 0 For i = 1 to n do Profit = profit + [x[i]×P[i]] All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 10 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 } Complexity: O( nlog n) 2.Application to job sequencing with deadlines problem: job_scheduling(d[0….n] : j[0…n]) Algorithm { d[0] = 0 j[0] = 0 k =1 , j[1] = 1 for i = 2 to n do { r=k while( (d[ j[r]) > max (d[i],r)) r = r-1 if( d[i] > r ) then for m = k to r + 1 do j[r +1] = i k ++ } } All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 11 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 3.Minimum cost spanning trees: 1.Kruskal’s algorithm: Assumption: 1. G is a graph which represented in the form of cost matrix of size n×n 2. minimum cost = 0 3. there are two functions used in algorithm 1. Find: These function will find the component in which the vertex are present. 2. Merge: These function will merge the two component if they are different. All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 12 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 Algorithm kruskal(cost, G, n, mincost: t) { Step1. Create component as per no of vertices in the graph // n: component Step2. i=1 mincost = 0 while( i < = n-1) do select the best edge and it be (u,v) x = Find(u) y = Find(v) if (x ≠ y) t[i, 1] = u t[i, 2] = v t[i, 3] = cost[u,v] mincost = cost[u,v] + mincost Merge[x,y] i = i+1 } All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 13 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 Complexity: Elog V 2. Prim’s algorithm: Algorithm prims(G, n, cost, near, mincost:t) { Step1. Consider t Let cost[k, l] be the min value in cost matrix t[1,1] = k t[1, 2] = l t[1,3] = cost[k, l] // consider near array For i = 1 to n do { if( cost [k, i] < cost[l, i] ) near[i] = k else near[i] = l } near[k] = 0 All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 14 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 near[l] = 0 step2. // refer t for i = 2 to n-1 { Let j be index in near array such that near[j] ≠0 & cost[ near[j], j] is min t[i, 1] = near[j] t[i, 2] = j t[i, 3] = cost[near[j], j] mincost = mincost + cost[near[j], j] near[j] = 0 for k = 1 to n do { if(near[k] ≠ 0) and ( cost[k, j] < cost[k.near[k]) near[k] = j } } Complexity: E + log V All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 15 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 Difference between Kruskal’s and Prim’s algorithm: Kruskal’s S.N. Prim’s 1. In this, edge is selected with minimum value. In this, edge is selected with minimum value and connectivity. 2. Graph generated with isolated and will required connecting edge Graph generated will be connected but no inter path require. 3. Complexity is E log V Complexity is E+ log V 4. Single Source Shortest path(SSSP): Algorithm SSSP( cost, n, dist) Step1. For i = 1 to n do { S[i] = 0 dist[i] = cost[v,i] } Step2. S[v] = 0 dist[v] = 0 Step3. for i =2 to n do All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 16 C TEL SOLUTIONS Opp. Moil Office, Babbu Galaxy, Chhaoni, Sadar, Nagpur Contact no: 86000006680 { Determine n-1 path from vertex ‘v’ and let the vertices ‘u’ select a vertex such that S[i] ≠ 1 and dist[u] = min for ( each ‘w’ adjacent to vertex ‘u’) { if( dist[w] > dist[u] + cost[u, w]) then dist[w] = dist[u] + cost[u,w] } } Complexity: O(n2) All the best… Notes Prepared by- Ahmad Sir : 9867597554 Page 17