Algorithms Lab Manual (SCSVMV DU)
Algorithms Lab Manual (SCSVMV DU)
#epartment $% C$mputer Science & En''(, )a* Manual $% +l'$rithms )a* , -(E [CSE"
/ / / /
INDEX
Sl.No
CONTENT
Page
1.
2.
3.
4.
List of Experiments
5.
61
62
HP SERVER HP Pro ML 350 G6 (TM) / Intel QUAD CORE XEON E5504 2.0 / 8 GB DDR31333 146 GB SAS HDD DP HDD*3 DVD ROM DRIVE HP Keyboard, Mouse 19 TFF MONITOR
HP CLIENT MACHINES Intel Core 2 Duo E7500 2.93 Ghz 4 GB DDR3 Ram / 320 GB HDD, DVD RW, HP Keyboard, HP Mouse 18.5TFT monitor
Ex .No
TITLE OF PROGRAMS
1.
2.
3.
4.
5.
Write a program that implements Quick sort Write a program to find the minimum and maximum value using divide and conquer. Write a program that implements knapsack using greedy method
10
11
12
Method to solve the problem To move n disks from peg A to peg C with peg B as auxiliary First move n-1 disks recursively from peg A to peg B with peg C as auxiliary Move the largest disk from peg A to peg C Then move the n-1 disks recursively from peg B to peg C with peg A as auxiliary f n!1 simply move the single disk from source to destination
Recursive algorithm to move n disks from peg A to peg C using Peg B Algorithm TOH(n,A,B,C) //Moves n disks from Peg A to Peg C using Peg B as auxiliary if n=1 Move AC return else { TOH(n-1,A,C,B) Move AC TOH(n-1,B,A,C) } Stop
Analysis: The recursive relation for getting the num"er of moves is given "y# M$n%!&M$n-1%'1 for n(1 M$1%!1 )olving the recurrence relation gives M$n%!&n-1
6( <2-7.+CC2 [email protected]
Problem Statement: Compute the nth Fibonacci number. The Fibonacci sequence usually refers to a set of numbers that starts as {0, 1, 1, 2, 3, 5, 8, 13, 21}. This sequence is created by following two rules: The first two numbers are 0 and 1. The next number is the sum of the two most recent numbers.
Method to solve the problem Mathematically the n+1th Fibonacci number is given by the formula x[n+1] = x[n] + x[n-1]. Algorithms Recursive algorithm Algorithm Fib(n) //Computes the nth Fibonacci number recursively by using its definition //Input: A nonnegative integer n //Output: The nth Fibonacci number if n 1 return n else return F(n 1) + F(n 2) Non Recursive algorithm Algorithm Fib(n) //Computes the nth Fibonacci number iteratively by using its definition //Input: A nonnegative integer n //Output: The nth Fibonacci number F00; F11 for i 2 to n do {FF0+ F1 F0 F1 F1 F} return F Analysis: The time complexity of the recursive algorithm is exponential in n The non recursive algorithm makes n 1 additions. Hence, it is linear as a function of n
10
<2-7.+CC2 [email protected]
11
12
13
3( 2.SE1T27. S71T
Problem Statement: To sort an array using insertion sort. Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly-sorted lists, and often is used as part of more sophisticated algorithms. It works by taking elements from the list one by one and inserting them in their correct position into new sorted list. The insertion sort works just like its name suggests - it inserts each item into its proper place in the final list. Method to solve the problem: To sort an array a[1:n], the basic idea of insertion sort is to place the element a[j] in correct position in the sorted set a[1:j-1], for j=2. .n.To accommodate a[j] the elements in a[1:j-1] have to moved. Example: Following figure shows the operation of INSERTION-SORT on the array A= (5, 2, 4, 6, 1, 3). Each part shows what happens for a particular iteration with the value of j
Algorithm: Algorithm INSERTION_SORT (A,n) FOR j 2 TO n DO {key A[j] //Put A[j] into the sorted sequence A[1 . . j 1] ij1 WHILE i > 0 and A[i] > key DO {A[i +1] A[i] ii1 } A[i + 1] key} Stop Analysis: The worst case time of this sorting is (n2 ) and best case time is (n )
14
2.SE1T27. S71T
15
16
17
4( SE)ECT27. S71T
Problem Statement To sort an array using selection sort. Selection sort is a simple sorting algorithm that is relatively in-efficient for large lists. It works by selecting the smallest/largest element from the unsorted list and placing it in the correct position of the sorted list. The selection sort works just like its name suggests - it selects the correct item and places it in its proper place in the final list. Method to solve the problem Effectively, the list is divided into two parts: the set of items already sorted which is built up from left to right/right to left, and the set of items remaining to be sorted, occupying the remainder of the array +l'$rithm Algorithm Selectionsort(A,n) { FOR j 2 TO length[A] i := n while i > 1 do { m 1 max A[1] j2 while j < i do { if max < A[j] then {mj max A[j]} jj+1 } A[m] A[i] A[i] max ii-1} }
Analysis Selection sort does not depend on the data in the array. Selecting the lowest element requires scanning all n elements (this takes n 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n 1 elements and so on. Hence the number of comparisons is (n 1) + (n 2) + ... + 2 + 1 = n(n 1) / 2 (n2)
18
SE)ECT27. S71T
19
20
21
!( -2.+1; SE+1C:
Problem Statement: This is a an efficient algorithm for searching for an element K in a sorted array using divide and conquer technique Method to solve the problem The algorithm checks for the middle element A(m) If K = A[m], stop (successful search); otherwise, continue searching by the same method in A[0..m-1] if K < A[m] and in A[m+1..n-1] if K > A[m] Recursive algorithm Algorithm Binsearch(A,low,high,K) if (low=high) then //Small P {if K=A[low] then return low else return -1} else { mid (low+high)/2 if K = A[mid] return mid else if K < A[mid] return Binsearch(A,low,mid-1,K) else return Binsearch(A,mid+1,high,K) } Non Recursive algorithm Algorithm Binsearch(A,n,K) l 0; r n-1 while l r do m (l+r)/2 if K = A[m] return m else if K < A[m] r m-1 else l m+1 return -1 Analysis: The time complexity of Binary search is given by Best case (1) ; Average case (log(n)) and worst case (log(n))
22
-2.+1; SE+1C:
23
24
25
6( ME19E S71T
Problem Statement To sort an array using Merge sort which uses divide and conquer technique. It takes advantage of the ease of merging already sorted lists into a new sorted list. It starts by comparing every two elements (i.e. 1 with 2, then 3 with 4...) and swapping them if the first should come after the second. It then merges each of the resulting lists of two into lists of four, then merges those lists of four, and so on; until at last two lists are merged into the final sorted list. Of the algorithms described here, this is the first that scales well to very large lists. Method to solve the problem Merge sort works as follows: 1. Divide the unsorted list into two sub lists of about half the size 2. Sort each of the two sub lists 3. Merge the two sorted sub lists back into one sorted list.
Algorithm Algorithm MergeSort(low,high) //a[low:high] is a global array to be sorted //Small(P) is true if there is only one element //to sort. In this case the list is already sorted. { if (low<high) then //if there are more than one element {
26
//Divide P into subproblems //find where to split the set mid = [(low+high)/2]; //solve the subproblems. mergesort (low,mid); mergesort(mid+1,high); //combine the solutions . merge(low,mid,high); } } Algorithm: Merging 2 sorted subarrays using auxiliary storage. Algorithm merge(low,mid,high) //a[low:high] is a global array containing //two sorted subsets in a[low:mid] and in a[mid+1:high].The goal is to merge these 2 sets into //a single set residing in a[low:high].b[] is an auxiliary global array. { h=low; I=low; j=mid+1; while ((h<=mid) and (j<=high)) do { if (a[h]<=a[j]) then { b[I]=a[h]; h = h+1; } else { b[I]= a[j]; j=j+1; } I=I+1; } if (h>mid) then for k=j to high do { b[I]=a[k]; I=I+1; } else for k=h to mid do { b[I]=a[k]; I=I+1; } for k=low to high do a[k] = b[k]; }
27
ANALYSIS The straightforward version of function merge requires at most 2n steps (n steps for copying the sequence to the intermediate array arr b, and at most n steps for copying it back to array a). The time complexity of mergesort is therefore T(n) 2n + 2 T(n/2) and
T(1) = 0 The solution of this recursion yields T(n) 2n log(n) O(n log(n))
A drawback of Mergesort is that it needs an additional space of (n) for the temporary array b.
28
29
30
31
A( @82CK S71T
Problem Statement: Quick sort is a divide and conquer algorithm which relies on a partition operation: to partition an array, using an element, called a pivot. pivot All ll smaller elements are moved before the pivot, and all greater elements are moved after it. This can be done efficiently in linear time and inin place. Then recursively sorting can be done for the lesser and greater sub lists. lists. Method to solve the problem Select a pivot (partitioning element) here, the first element Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions ns are larger than or equal to the pivot Exchange the pivot with the last element in the first (i.e., ) subarray the pivot is now in its final position Sort the two sub arrays recursively
32
Algorithm to Partition the array Algorithm Partition(A, left, right) //Partition the sub array A[l..r] using its first element as pivot pA[l] il; jr; do do ii+1 until A[i]p do jj-1 until A[j]p swap (A[i],A[j]) until ij swap (A[i],A[j]) swap (A[l],A[j]) return j Algorithm to invoke quick sort Algorithm quicksort(A, left, right) if right > left j partition(A, left, right+1) quicksort(A, left,j-1) quicksort(A, j+1, right) end
ANALYSIS The partition routine examines every item in the array at most once, so complexity is clearly O(n). Usually, the partition routine will divide the problem into two roughly equal sized partitions. The array of n items can be divided in to half in log2n times. This makes quicksort a O(nlogn) algorithm - equivalent to heapsort. The most complex issue in quick sort is choosing a good pivot element; consistently poor choices of pivots can result in drastically slower (O(n2)) performance, but if at each step we choose the median as the pivot then it works in O(n log n).
33
@82CK S71T
34
35
36
37
38
39
40
5r$*lem Statement *iven n o"+ects and a knapsack where o"+ect i has a weight wi and profit pi and the knapsack has a capacity m f a fraction xi of o"+ect i placed into knapsack1 a profit pixi is earned The o"+ective is to o"tain a filling of knapsack maximi,ing the total profit
Method to solve the problem -ro"lem formulation Maximi,e .pixi )u"+ect to .wixi / M and 0 / xi / 11 1 /i /n Greedy approximation algorithm The greedy approximation algorithm to solve the knapsack problem sorts the objects in increasing order of ratio of profit to weight (pi/wi) and then proceeds to insert them into the sack, starting from the first element (the greatest) until there is no longer space in the sack for more
Algorithm Greedy Knapsack void GreedyKnapsack(float m, int n) // p[1:n] and w[1:n] contain the profits and weights / respectively of the n objects ordered such that p[i]/w[i] >= p[i+1]/w[i+1]. m is the knapsack size and x[1:n] is the solution vector. { for (i1 to n) do x[i] = 0.0; // Initialize ize x. U m;
41
for (i1 to n) do { if (w[i] > U) break; x[i] 1.0 U U - w[i] } if (i <= n) x[i] U/w[i] } Analysis: Sorting: O(n log n) using fast sorting algorithm like merge sort GreedyKnapsack: O(n) So, total time is O(n log n)
42
43
44
45
46
Algorithm Function TSP(G,n) for all S, subset of {2,3,n} with ||S||=s do { for s=0 to n-2 do { for all i not in S do { if s=0 then g(i,S})=ci1 else g(i,S)=min ij, j in S {ci,j +g(j,S-{j})} } } } opt=min 2k n { c1k +g(k,V-{1,k})} Return (opt)
Analysis
If we try to determine the solution of this problem systematically, we would end up with (n 1)! Possible solutions. For example if there were 21 cities the steps required are (n - 1)! = (21 - 1)! = 20! Steps
47
48
49
50
The Floyd-Warshall Algorithm Floyd-Warshall's algorithm is based upon the observation that a path linking any two vertices u and v may have zero or more intermediate vertices. The algorithm begins by disallowing all intermediate vertices. In this case, the partial solution is simply the initial weights of the graph or infinity if there is no edge. The algorithm proceeds by allowing an additional intermediate vertex at each step. For each introduction of a new intermediate vertex x, the shortest path between any pair of vertices u and v, x,u,vV, is the minimum of the previous best estimate of (u,v), or the combination of the paths from ux and xv. (u,v)min((u,v),(u,x)+(x,v)) Algorithm Algorithm FLOYD-WARSHALL (W) 1 n rows [W] 2 D(0) W 3 for k 1 to n 4 do for i 1 to n 5 do for j 1 to n 6 do dij (k) MIN ( dij (k-1) , dik (k-1) + dkj (k-1) ) 7 return D(n) Analysis The time complexity of the algorithm is O( n3 ).
51
52
53
54
6( .,@8EE. 517-)EM
5r$*lem Statement To place N queens on a chessboard in such a way that no queen can attack any other using backtracking method. Method to solve the problem
A queen can attack another queen if it exists in the same row, column or diagonal as the queen. This problem can be solved by trying to place the first queen, then the second queen so that it cannot attack the first, and then the third so that it is not conflicting with previously placed queens The solution for N=8 is a vector of length 8 (x(1), x(2), x(3), ...., x(8)). x(i) corresponds to the column in the ith row where we should place the i-th queen. All the x(i)s should be distinct since no two queens should be placed in the same column The solution is to build a partial solution element by element until it is complete. We should backtrack in case we reach to a partial solution of length k, that we couldn't expand any more
Algorithm Algorithm place (k,I) //return true if a queen can be placed in k th row and I th column. otherwise it returns false . //X[] is a global array whose first k-1 values have been set. Abs returns the absolute value of r. { For j=1 to k-1 do If ((X [j]=I) //two in same column. Or (abs (X [j]-I)=Abs (j-k))) Then return false; Return true; }
55
Algorithm Nqueen (k,n) //using backtracking it prints all possible positions of n queens in n*n chessboard. { For I=1 to n do { If place (k,I) then { X [k]=I; If (k=n) then write (X [1:n]); Else nquenns(k+1,n) ; } } } Analysis For 8X8 chess board there are 64C8 possible ways to place 8 pieces. However by allowing only placements of queens on distinct rows and columns we require the examination of at most 8! 8-tuples.
56
. @8EE. 517-)EM
57
58
59
60
61
LABORATORY RECORD
Name Reg. No Class Subject : : : : II YEAR BE[CSE] EBC4AP121 Algorithms Lab
62
BONAFIDE CERTIFICATE This is to Certify that this is the bonafide record of work done by Mr/Ms.________________________________________________________, with Reg.No.___________________ of Algorithms during the year 2013-2014. II Year B.E [CSE] in the
Station Date
:_____________ :_____________
Staff-in-charge
Internal Examiner
External Examiner
63
INDEX
Exp. No. Date TITLE Page No Sign
64