Vous êtes sur la page 1sur 66

Sri Chandrasekharendra Saraswathi Viswa Mahavidyalaya

[Enathur, Kanchipuram, Tamilnadu 63 !6 "

#epartment $% C$mputer Science & En''(, )a* Manual $% +l'$rithms )a* , -(E [CSE"

.ame 1e'( .$ Class Su*3ect

/ / / /

0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 22 -(E( [CSE" E-C4+5 6 +l'$rithms )a*

INDEX

Sl.No

CONTENT

Page

1.

Introduction to the Algorithm Analysis and Design

2.

Platform used in the Lab

3.

Hardware available in the lab

4.

List of Experiments

5.

Sample viva questions

61

Format of the lab record to be prepared by the students.

62

( 2.T17#8CT27. T7 +)9712T:M +.+);S2S +.# #ES29.


An algorithm, named after the ninth century scholar Abu Jafar Muhammad Ibn Musu AlKhowarizmi, is a set of rules for carrying out calculation either by hand or on a machine. 1. Algorithmic is a Branch of Computer Science that consists of designing and analyzing Computer algorithms The design pertains to i. The description of algorithm at an abstract level by means of a pseudo language, and ii. Proof of correctness that is, the algorithm solves the given problem in all cases. 2. The analysis deals with performance evaluation (complexity analysis). The complexity of an algorithm is a function g(n) that gives the upper bound of the number of operation (or running time) performed by an algorithm when the input size is n. There are two interpretations of upper bound. Worst-case Complexity The running time for any given size input will be lower than the upper bound except possibly for some values of the input where the maximum is reached. Average-case Complexity The running time for any given size input will be the average number of operations over all problem instances for a given size. An algorithm has to solve a problem. An algorithmic problem is specified by describing the set of instances it must work on and what desired properties the output must have. We need some way to express the sequence of steps comprising an algorithm. In order of increasing precision, we have English, pseudo code, and real programming languages. Unfortunately, ease of expression moves in the reverse order. In the manual to describe the ideas of an algorithm pseudo codes, algorithms and functions are used. In the algorithm analysis and design lab various strategies such as Divide and conquer technique, greedy technique and dynamic programming techniques are used. Many sorting algorithms are implemented to analyze the time complexities.

6( 5)+T<71M 8SE# 2. T:E )+Windows XP/ Windows 7 operating system.

S$%tware %$r 2mplementati$n

8sin' Tur*$ C== C$mpiler


Your IDE will look like this..

3( :+1#>+1E +V+2)+-)E 2. T:E )+-

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

4( )2ST 7< 51791+MS

Ex .No

TITLE OF PROGRAMS

1.

Write a program that implements Tower of Hanoi.

2.

Write a program that implements Fibonacci series.

3.

Write a program that implements insertion sort.

4.

Write a program that implements Selection sort.

5.

Write a program that implements Binary search.

Write a program that implements Merge sort

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

Write a program that implements travelling sales person problem.

11

Write a program that implements All pair Shortest path

12

Write a program that implements N-Queen Problem

( T7>E1 7< :+.72


Problem Statement: The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower,[1] and sometimes pluralized) is a mathematical game or puzzle. It consists of three pegs, and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks in a neat stack in ascending order of size on one peg, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another peg, obeying the following simple rules: 1. Only one disk may be moved at a time. 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack. 3. No disk may be placed on top of a smaller disk. With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks.

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

E?p .$/ #ate

T7>E1 7< :+.72

6( <2-7.+CC2 SE@8E.CE
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

E?p .$/ 6 #ate

<2-7.+CC2 SE@8E.CE

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

E?p .$/ 3 #ate

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

E?p .$/ 4 #ate

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

E?p .$/ ! #ate

-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

E?p .$/ 6 #ate

2M5)EME.TS ME19E S71T

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

E?p .$/ A #ate

@82CK S71T

34

35

36

B( M+C2M8M +.# M2.2M8M E)EME.TS 8S2.9 #2V2#E +.# C7.@8E1


5r$*lem Statement To find the maximum and minimum of an array of elements using divide and conquer method. Method to solve the problem If the array has more than two elements the midpoint is determined and two new sub problems are generated. When maxima and minima of these two sub problems are determined, the two are compared to achieve the solution. The situation of array of size one and two are dealt separately. Algorithm to find the maximum and minimum items in a set of (n) elements. Algorithm MaxMin(i, j, max, min) if (i=j) then max=min=a(i) else if (i=j-1) then if (a(i)<a(j)) then max= a(j) min= a(i) else max= a(i) min= a(j) else mid= (i+j)/2 maxmin(i, mid, max, min) maxmin(mid+1, j, max1, min1) if (max<max1) then max = max1 if (min>min1) then min = min1 end Analysis If T(n) represents the number of comparisons then T(n)= T(n/2)+T(n/2)+2 n>2 = 1 for n=2 = 0 for n=1 By solving the above recurrence relation we get, T(n)=3n/2 which is less than 2n-2 in case of normal method. However MaxMin is worse than straight forward method in terms of storage because it requires stack space for i,j,max,min,max1,min1 and return address for every recursive call.

37

E?p .$/ B #ate

M+C2M8M +.# M2.2M8M E)EME.TS 8S2.9 #2V2#E +.# C7.@8E1

38

39

40

D( K.+5S+CK 517-)EM 8S2.9 91EE#; MET:7#

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

E?p .$/ D #ate

K.+5S+CK 8S2.9 91EE#; MET:7#

43

44

45

E( T1+VE))2.9 S+)ES 5E1S7. 517-)EM FTS5G


Problem Statement: Given a set of cities and the distance between each possible pair, the Travelling Salesman Problem is to find the best possible way of visiting all the cities exactly once and returning to the starting point. This problem is solved using Dynamic Programming technique. Method to solve the problem First, find out all (n -1)! Possible solutions, where n is the number of cities. Next, determine the minimum cost by finding out the cost of everyone of these (n -1)! solutions. Finally, keep the one with the minimum cost. Let g(i,S) be the length of the shortest path starting from vertex i going through all vertices in S and terminating at vertex 1 g(1,V-{1}) is the length of the optimal sales person tour. From the principle of optimality g(1,V-{1})=min{ c1k +g(k,V-{1,k})} for 2k n In general, g(i,S)=min{ ci,j +g(j,S-{j})} for j in S

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

E?p .$/ E #ate

T1+VE))2.9 S+)ES 5E1S7. 517-)EM

48

49

50

( +)) 5+21 S:71TEST 5+T:


Problem Statement: Given a directed, connected weighted graph G(V,E), for each edge u,vE, a weight w(u,v) is associated with the edge. The all pairs of shortest paths problem (APSP) is to find a shortest path from u to v for every pair of vertices u and v in V. This problem is solved using Dynamic Programming technique. Method to solve the problem The input is an nn matrix W=( wij ). w(i,j)={ 0 if i=j the weight of the directed edge i,j if ij and i,jE if ij and i,jE

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

E?p .$/ #ate

+)) 5+21 S:71TEST 5+T:

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

E?p .$/ 6 #ate

. @8EE. 517-)EM

57

58

59

!( <71M+T 7< T:E )+- 1EC71#S T7 -E 51E5+1E# -; T:E ST8#E.TS


The students are required to maintain the lab records as per the instructions: 1. All the record files should have a cover page as per the format. 2. All the record files should have an index as per the format. 3. All the records should have the following : I. II. III. IV. V. VI. VII. Experiment No Date Program Name Aim Algorithm or the Procedure to be followed. Program Output

60

6( S+M5)E V2V+ @8EST27.S


1. What is an algorithm? 2. What is a randomized algorithm? 3. Define Omega notation. 4. Define Big-O notation. 5. Define Theta notation. 6. What are loop invariants? 7. What is a Pseudocode? 8. What are the worst case and average case running time of insertion sort? 9. Which technique is used to sort elements in merge sort? 10. What is the running time of merge sort? 11. How merge sort is different from quick sort? 12. What is the worst case running time of quick sort? 13. What are the differences between dynamic and greedy algorithms? 14. Dijkstra algorithm can take into account the negative edge weigthts.'Is the statement true? 15. Define minimum spanning tree.' 16. Name any algorithm for finding the minimum spanning tree. 17. Compare Prim's and Kruskal's algorithm. 18. What are NP-complete problems. 19. Name some NPC problems. 20. Define Travelling salesman problem.

61

SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA


( University Established under section 3 of UGC Act 1956) ENATHUR, KANCHIPURAM 631 561

LABORATORY RECORD
Name Reg. No Class Subject : : : : II YEAR BE[CSE] EBC4AP121 Algorithms Lab

62

SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA


( University Established under section 3 of UGC Act 1956) ENATHUR, KANCHIPURAM 631 561

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

Head of the Department

Submitted for the Practical Examination held on______________

Internal Examiner

External Examiner

63

INDEX
Exp. No. Date TITLE Page No Sign

64