P. 1
Assignment of Algorithms TB

Assignment of Algorithms TB

|Views: 68|Likes:

See more
See less

05/06/2011

pdf

text

original

Subject: Algorithms Subject Code: BSIT – 41 Assignment: TB Q1.

Hand simulate the sequential search algorithm on the data set given below and the search element being 10, 05, 76, 85, 200 Data set:: 10, 80, 03, 09, 55, 32, 100, 07, 05, 02, 12, 65, 63, 22, 92, 81, 48,76 Ans1: Search element: 10 Data set:: A[]= 1 0 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 i=from subscript 1-18 n=no. of elements in the array A[]. The algorithm for sequential search is as follows: Algorithm: Sequential search Input: A, vector of n elements K, search element Output: j-index of K Method: i=1 While (i<n) { if(A[i]=K) { write(“Search successful”); write(K is at location i) exit(); } else i++ if end while end Write(“Search unsuccessful”); algorithm ends (i)Here the search element is 10 an n=18 K=10 ← Method: i=1 (1st pass) While(i<n) // i.e 1<18 returns true 8 0 0 3 0 9 5 5 3 2 10 0 0 7 0 5 0 2 1 2 6 5 6 3 2 2 9 2 8 1 4 8 76

↑ ↑ ↑ ↑ 15 16 17 18

{ if(A[i]=K) //hereA[i]=A[1]=10 and K=10 so, it returns true { write(“search successful”) write(K is at location 1) exit(); } Result: K is found in just one pass. Q2. Hand simulate the binary search technique on the given in (Q1) for the keys. Ans: Input: A vector of n elements. Data set:: A[]= 1 8 0 0 5 3 10 0 0 0 1 6 6 2 9 8 4 76 0 0 3 9 5 2 0 7 5 2 2 5 3 2 2 1 8 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Search element: 10 Technique to search: Binary search. For this method it is necessary to have the vector in an alphabetical order or numerically increasing order. The algorithm for binary search is as follows: Algorithm: binary search Input: A, vector of n elements K, search element Output: low-index of K Method: low=1 high=n While(low<=high-1) { mid=(low+high)/2 if(K<a[mid]) high=mid else low=mid if end } while end if(K=A[low]) { write(“search successful”) write(K is at location low) exit(); }

else write(“search unsuccessful”) if ends algorithm ends Note: So, before hand simulating we need to have it in a numerically increasing order. So the new data set will look as given below. 0 2 0 3 0 5 0 7 0 9 1 0 1 2 2 2 3 2 4 5 5 5 6 3 6 5 7 6 8 0 8 1 9 2 10 0

Subscript from 1-18 (i) Search element =10 Number of elements n=18 Method: (1stpass) low=1 , high=n // i.e. high=18 While(low<=high-1) //(1<=18-1(=17)) returns true { mid=(1+18)/2 // mid=19/2~9 if(K<a[mid]) //here K=10 and a[mid]=a[9]=32 returns true high=mid //high=9 //again moving up to the method (2nd pass) low=1 , high=9 while(1<=9-1) //returns true { mid=(1+9)/2 //mid=10/2=5 //mid=a[5] if(K<a[mid]) //K=10 and a[mid]=a[5]=09. So it returns false so else part will be executed else low=mid //low=mid=5 (3rd pass) low=5 , high=9 While(5<=9-1(=8)) //returns true mid=(5+9)/2 //mid=14/2=7 if(10<a[7]) //i.e. (10<12) returns true high=mid //high=7 (4thpass) low=5 , high=7 While(5<7-1(=6)) //returns true

Mid=(5+7)/2 //mid=12/2=6 If(K<a[mid]) //k=10 and a[mid]=a[6]=10 K is not less than or greater than a[mid] but is equal to a[mid]. So, else part will be executed. Low=mid //low=mid=6 (5th pass) low=6 , high=7 While(6<7-1(=6) //i.e. while(6<6) returns false so we come out of the while loop. If(K=A[low] //here K=10 and A[low]=10].So it returns true. { write(“search successful”) write(K is at location 6) exit(); }

Result: So, the search element is found in 5 attempts.

Q3. Hand simulate the insertion sort algorithm for the following dataset 10, 45, 80, 03, 09, 55, 32, 43, 02, 07, 01, 10, 15 Ans: 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 2 0 2 4 5 4 5 0 1 0 1 0 1 0 1 0 1 1 0 0 2 0 2 0 3 0 3 8 0 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 0 3 0 7 0 7 0 3 0 3 0 3 0 3 0 3 0 3 0 3 0 3 0 3 1 5 1 0 0 9 3 9 3 9 3 9 3 9 3 9 0 2 0 2 0 2 1 0 1 0 0 9 1 0 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 1 0 1 0 5 5 5 5 5 5 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 5 1 5 3 2 3 2 3 2 3 2 3 2 3 2 0 7 0 7 0 7 0 7 3 2 3 2 4 3 4 3 4 3 4 3 0 7 0 7 3 2 3 2 3 2 3 2 3 9 3 9 0 2 0 2 0 2 0 2 0 2 3 9 3 9 3 9 3 9 3 9 4 3 4 3 0 7 0 7 0 7 0 7 4 3 4 3 4 3 4 3 4 3 4 3 4 5 4 5 0 1 0 1 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 5 5 4 5 1 0 1 0 1 0 5 5 5 5 5 5 5 5 5 5 5 5 5 5 15 i=14 80 i=13 80 i=12 80 i=11 80 i=10 80 i=9 80 i=8 80 i=8 80 i=7 80 i=6 80 i=5 80 i=4

Q4. Hand simulate the selection sort algorithm for the dataset given in (Q3). Ans4: Given dataset: 10, 45, 80, 03, 09, 55, 32, 43, 02, 07, 01, 10, 15 1 0 1 0 1 0 1 0 4 5 4 5 4 5 0 1 8 0 0 2 0 2 0 2 0 3 0 3 0 3 0 3 3 9 3 9 3 9 3 9 0 9 0 9 0 9 0 9 5 5 5 5 1 0 1 0 3 2 3 2 3 2 3 2 4 3 4 3 4 3 4 3 0 2 8 0 8 0 8 0 0 7 0 7 0 7 0 7 0 1 0 1 0 1 4 5 1 0 1 0 5 5 5 5 15

15

15

15

1 0 1 0

0 1 0 1

0 2 0 2

0 3 0 3

0 7 0 7

0 9 09

1 0 ↓ 3 2 ↓

3 2 4 3

4 3 8 0

8 0 3 9

3 9 4 5

4 5 5 5

5 5

15

15

0 1 0 1 ↓ 0 1

1 0 0 2

0 2 1 0

0 3 0 3

0 7 0 7

09 ↓ 09

3 2

4 3

1 5

3 9

4 5

5 5

80

1 5 0 2 0 3 1 0 0 7 09 1 5

4 3

3 2

3 9

4 5

5 5

80

3 2

4 3

3 9

4 5

5 5

80

↓ 0 1 0 1 0 2 0 2 0 3 0 3 0 7 0 7 1 0 0 9 09 ↓ 10 1 5 0 1 0 2 0 3 0 7 3 2 3 9 3 9 4 3 4 3 4 5 4 5 5 5 5 5 80 80 1 5 3 2 3 9 4 3 4 5 5 5 80

↓ 0 1 1 1 3 9 0 0 5 2 Selection Sort

Q5. Get to know abut few more sorting algorithm and prepare a brief not on them. Ans5: Bubble Sort: Bubble sort is one of the most popular sorting methods. Bubble sort can be viewed as a selection sort because this method is also based on successively selecting the smallest element, second smallest element, etc as in the case of any other selection sort. In order to find the successive smallest element, the method relies heavily on the exchange of adjacent elements. Because of this, the method is often classed as “Sorting by exchange”. Heap Sort: Heap is a data structure which can be used to support the operations defined for a priority queue in a different way. Heaps can be used efficiently to sort a list of elements. Heaps provide an elegant way to sort an array of words. On one hand, heap sort does not use any additional array for intermediate use. Shuttle Sort: When there are “n” elements in a given array, ”A”, shuttle sort requires (n-1) passes. By the time the i-th pass (1<i<n-1) begins, the first “I” elements i.e., elements A[0] to A[i] have been sorted and these occupy the first “I” positions of the array. To insert the (i+1) –th element, A[i], during the i-th pass, A[i] is compared with A[i-1] and if the key of the former is found to be smaller than the two elements are exchanged. If an exchange takes place then A[i-1] and A[i-2] are compared if necessary, they are exchanged . The process continues until either no exchange is found necessary or the beginning of the array is reached. Shell Sort: This internal sorting technique is due to Donald L. Shell (1959). This method makes repeated use of straight insertion sort or shuttle sort.

Shell sort operating on an array of “n” elements requires that in each pass, a number called increment be chosen. The increment must be less than “n”; they should be progressively smaller and the increment for the last pass must be equal to “1”. Q6. What is recursion? Which data structure is use for recursion? Ans6: Recursion is one of the powerful programming concepts, supported by most of the languages. At the same time, it is also a fact that mot Bergner are confused by the way it works and are unable to use it effectively. Also, some languages may not support recursion. In such case, it may become necessary to rewrite recursive functions into non-recursive ones. Recursion is an offshoot of the concept o subprograms, performs the assigned tasks and comes back to the caller programs. The data structure that is used for recursion is STACK. Q7. What are the merits and demerits of recursion? Ans: Merits of recursion: 1) Mathematical function such as factorial and Fibonacci- series generation can be easily implemented using recursion than iteration. 2) In iterative techniques looping of statement is very much necessary. Demerits of recursion: 1) Many programming languages do not support recursion; hence recursive mathematical function is implemented using iterative methods. 2) Even though mathematical functions can be easily implemented using recursion it is always at the cost of execution time and memory space. 3) A recursive procedure can be called from within or outside itself and to ensure its proper functioning it has to save in order the return addresses so that, a return to the proper location will result when the return to a calling statement is made. 4) The recursive programs needs considerably more storage and will take more time. Q8. Hand simulates the algorithm to find minimum and maximum elements in a list of n elements. Ans8: The problem is to find out the maximum values in a give list of n data elements. The recursive algorithm designed to serve this purpose is as follow: Algorithm: Max-Min Input: P, q the lower an upper limits of the dataset max, min, two variables to return the maximum and minimum value in the list Output : the maximum and minimum values in the data set

Method: If(p = q) Then Max =a(P) Min = a(q) Else If ( p- q -1) Then If a(p) > a(q) Then Max =a(p) Min =a(q) Else Max = a(q) Min = a(p) If end Else m- (p+q)/2 Max-min (p,m,max1,min1) Max-min (m+1, Q, max2, min2) Max –large (max1, max2) Min- small(min1, min2) If End Algorithm Ends Hand simulation: We have to find max-min for a dataset containing two elements. 3 15 Dataset: 30, 16 0 If(p = q) Then //here p=1 , q=2 and p! =q. So, else part will be executed. Else If ( p= q -1) Then //here p(=1)=q(=2)-1 i.e. p=q-1 returns true If a(p) > a(q) Then // here a(p)=30 and a(q)=16.So a(p) is greater than a(q) Max =a(p) // Max=a(p)=30 Min =a(q) //and Min =a(q)=16 So from this we got our max-min.

Q9. Hand simulates the quicksort algorithm to sort a list of data elements. Ans9: This is another method of sorting that uses a different methodology to arrive at the same sorted result. It “Partitions” the list into 2 parts (similar to merge sort), but not necessarily at centre, but at an arbitrarily “pivot” place and ensures that all elements to the left of the pivot element are lesser than the element itself and all those to the right of it are greater than the element. Consider the following example To facilitate ordering, we add a very large element, say 1000 at the end. We keep in mind that this is what we have added and is not a part of the list. 75 A(1) 80 A(2) 85 95 70 A(5) 65 60 55 A(8) 1000 A(9)

A(3) A(4)

A(6) A(7)

Now consider the first element. We want to move this element 75 to its correct position in the list. At the end of the operation, all elements 75 to the left of 75 should be less than 75 and those to the right should be greater than 75. Q10. Hand simulates the merge sort algorithm to sort a given list of data elements. Ans10: Sorting is a process of arranging a set of given numbers in some order. The basic concept of merge sort is like this. Consider a series of numbers, say A(1), A(2)……….A(n). Suppose individually sort the first set and also the second set. To get final sorted list, we merge the two sets into one common set. We first look into the concept of arranging two individually sorted series of numbers into a common series using an example: Let the first set be A = {3, 5, 8, 14, 27, 32}. Let the second set be B = {2, 6, 9, 15, 18, 30} The two lists need not be equal in length. For example, the first list can be 8 elements and the second 5. Now we want to merge these two list to form a common list C. Look at the elements A(1), and B(1), A(1) is 3, B(1) is 2. Since B(1) < A(1), B(1) will be the first element of C i.e., C(1)=2. now compare A(1) with B(2)=6, Since A(1) is similar then B(2), A(1) will become the second element of C. Q11. Hand Simulate the recursive binary search algorithm. Ans11:

The recursive binary search method a s explained earlier is a process of searching for the presence or absence of a key element in the sorted list. The approximate mid entry is located and its key value is examined. If the mid value is greater than X, then the list is chopped off at he (mid-1)th location. Now the list gets reduced to half the original list. gets reduced to half the original list. The middle entry of the left-reduced list is examined in a similar manner. Thus, one can always think of using a recursive algorithm the solve the same. The recursive algorithm for binary search is as follows, Algorithm : binary search Input : A, vector of n elements K , search element Low, the lower limit High, the upper limit //initially low=1 and high=n the number of elements Output : the position of the K Method : if ( low<= high) Mid=(low+high)/2 If( a[mid] == K) Retrun(mid) Else If (a[mid]<k) Binary search (A, K Low, mid) Else Binary Search (A, K, High, mid) If end If end Else Return(0) If end Algorithm ends 0 1 Low=1 , high=5 0 9 1 5 2 3 50 Given dataset: Search element: 15

if ( 1<= 5) //returns true Mid=(1+5)/2 //mid=6/2=3 If( a[mid] == K) // here a[mid]=a[3]=15 and K=15. //So, it returns true and the next statement is executed. Retrun(mid)

Q12. What is binary tree? Write a note on its properties. Ans12: A tree is minimally connected graph without a circuit. For a graph with n vertices to be minimally connected there have to n-1 edges. Tree is, therefore, a connected graph with n vertices and (n-1) edges. It can be said to be a graph with n vertices and (n-1) edges with a circuit.

A

B

C

D

E

F

G

13 Different between static allocation and dynamic allocation. Ans13: Static allocation, we have two ways of representing the binary tree. One is through the use of Adjacency matrices and the other thorough the use of Single Dimensional array representation. But, Dynamic allocation, we have Multiple ways of representing the binary tree. One is through the use of Adjacency matrices and the other thorough the use of Multiple Dimensional array representation. Q14. Comment on the space utilization in various representation schemes of a binary tree. Ans14: Space utilization in Single dimensional Array Representation: If ‘n’ is the number of nodes, then the percentage of utilization is (n-1) * 100 (2l+1-1) For a complete and full binary tree, there is 100% utilization and there is a maximum wastage if the binary tree is right skewed or left skewed, where only L+1 spaces are utilized out of the 2l+1-1 spaces possible. i.e., (n-1) * 100 (2l+1-1)

is the worst possible utilization in the single dimensional array representation. An important observation to be made here is that the organization of the data in the binary tree decides the space utilization of the representation used. Space utilization in Linked Representation: If there are ‘n’ nodes to be represented, only ‘n’ node-structures will be allocated. This means that there will be 2n link fields. Out of 2n link fields only(n-1) will be actually used to p[point to the next nodes and the remaining are wasted. Therefore the percentage of utilization is given as: n-1 = 1 * 100 =50% 2n n Q15. Construct all binary tree of level 5 and traverse them in inorder, preoprder and post order and produce the corresponding sequences. Ans15:

Pre-order: In, this traversal, the nodes are visited in the order of root, left child and then right child. The preorder traversal sequence for the binary tree shown in figure is ABDHJLMKIECFG. In-order: In, this traversal, the nodes are visited in the order of left child, root and then right child. The preorder traversal sequence for the binary tree shown in figure is LJMHKDIBEAFCG Post-order: In, this traversal, the nodes are visited in the order of left child, right child and then root. The preorder traversal sequence for the binary tree shown in figure is LMJKHIDEBFGCA. Q16. Design a conventional iterative algorithm to traverses a binary tree represented in one dimensional array in inorder. Ans16:

Algorithm: In order traversal Input: A[], one dimensional array representing the binary tree i, The root address //initially i=1 Output: In order sequence Method: If( A[i] != 0 In order Traversal (A,2i) Display (A[i]) In order Traversal (A,2i+1) If end Algorithm End Q17. Design a conventional iterative algorithm to traverses a binary tree represented in one dimensional array in preorder. Ans17: To Traversal a binary tree represented in two-dimentional array in inorder is:Here, Var=pointer to Binary tree Set var to root. Push var in to the stack. Set var to var’s left if (var !=null) then go to (2). if (array is empty()) then go to (6). Else var=pop() Print var Set var to var’s right. O to 2 Stop Q18. Design a conventional iterative algorithm to traverses a binary tree represented in one dimensional array in postorder. Ans18: To traverse a binary tree represented in two-dimensional array in preorder is:Here, var=pointer to the binary tree. Set var to root. Print var. Push var in to the stack.

Set var to var’s right. if (array is empty()) then go to (6). Else var = pop() Set var to var’s right. Go to (2). 6. Stop. Q19. Design a conventional iterative algorithm to traverses a binary tree represented in two dimensional array in preorder. Ans19: Pre-order: 1) Set num to root 2) Print num 3) Push num into the stack 4) Set num to num’s left 5) If(array is empty()) then goto 6 Else a) num Pop() b) Set num to num’s right c) goto 2 6) Stop Q20. Design a conventional iterative algorithm to traverses a binary tree represented in two dimensional array in Inorder. Ans20: In-order: 1) Set num to root 2) Push num into the stack 3) Set num to num’s left 4) If (num=null) then goto 2 5) If(array is empty()) then goto 6 Else a) num=Pop() b) Print num c) Set num to num’s right d) goto 2 6) Stop Q21. Design a conventional iterative algorithm to traverses a binary tree represented in two dimensional array in Postorder. Ans21: Post-order: 1) Set num to root 2) Push num into the stack 3) Set num to num’s left

4) If (num=null) then goto 2 5) If(array is empty()) then goto 6 Else a) Print num b) num Pop() c) Set num to num’s right d) goto 2 6) Stop Q22: Design a conventional iterative algorithm to traverse a binary tree represented in linked lists in preorder. Ans22: Pre-order: 1) 2) 3) 4) 5) 6) 7) set var to root if var=NULL, print NULL tree, goto step 9 Print var’s data, set var’s falg t01, push var structure Set var to var’ left If var !=NULL then goto step 3 Var=Pop() If (var’s falg==2) then goto step 8 Else (b) set var’s flag to2 (c) push var’s structure (d) set var to var’s right (e) goto step 5

8) If(stack is !empty) then goto step 6) 9) Stop

Q23. Design a conventional iterative algorithm to traverse a binary tree represented in linked lists in Inorder. Ans23: In-order 1) Set var to root 2) If var=NULL, print NULL tree, goto step 9 3) Set var’s flag to 1, push var structure 4) Set var to var’s left 5) iIf var !=NULL then goto step 3 6) var=Pop() 7) if(var’s flag==2) then goto step 8 else (a) Print var data (b) Set var’s flag to 2

(c) Push var strtucture (d) Ser var to var’s roght (e) Goto step 5 8) if(stack !empty) then goto step 6 9) Stop Q24. Design a conventional iterative algorithm to traverse a binary tree represented in linked lists in postorder. Ans24: Post-order 1) Set var to root 2) If( var=NULL) print NULL tree, goto step[ 9 3) Sert var’s flag to 1, push var structure 4) Set var to var’s left 5) If(var1=NULL) then goto step 3 6) var=Pop() 7) if(var’s flag ==2) then print var’s data else (b) set var’s flag to 2 (c) push var structure (d) set var to var’s right (e) goto step 5 8) if(stack not empty) then goto 6 9) stop

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->