Sunteți pe pagina 1din 8

Describe the following: o Well known Sorting Algorithms o Divide and Conquer Techniques Ans:- Well Known Sorting

Algorithms Insertion sort The insertion sort, algorithm for sorting a list L of n numbers represented by an array A [ 1 n] proceeds by picking up the numbers in the array from left one by one and each newly picked up number is placed at its relative position, w.r.t. the sorting order, among the earlier ordered ones. Selection sort Selection Sort for sorting a list L of n numbers, represented by an array A [ 1.. n], proceeds by finding the maximum element of the array and placing it in the last position of the array representing the list. Then repeat the process on the sub array representing the sublist obtained from the list excluding the current maximum element. Shell Sort The sorting algorithm is named so in honour of D.L Short (1959), who suggested the algorithm. Shell sort is also called diminishing increment sort. The essential idea behind Shell Sort is to apply any of the other sorting algorithm (generally Insertion Sort) to each of the several interleaving sublists of the given list of numbers to be sorted. In successive iteration, the sublists are formed by stepping through the file with an increment INCi taken from some pre defined decreasing sequence of step sizes INC1 > INC2 > .. INCi > > 1, which must terminate in 1. Heap Sort In order to discuss Heap Sort algorithm, we recall the following definitions; where we assume that the concept of a tree is already known: Binary Tree: A tree is called a binary tree, if it is either empty, or it consists of a node called the root together with two binary trees called the left subtree and a right subtree. Divide and Conquer Technique The Divide and Conquer is a technique of solving problems from various domains and will be discussed in details later on. Here, we briefly discuss how to use the technique in solving sorting problems. A sorting algorithms based on Divide and Conquer technique has the following outline. Procedure Sort (list) If the list has length 1 then return the list

Else { i.e., when length of the list is greater than 1} begin partition the list into two sublists say L and H, Sort (L) Sort (H) Combine (sort (L), Sort (H)) {during the combine operation, the sublists are merged in sorted order} End Explain in your own words the different asymptotic functions and notations. Ans:- Well Known Asymptotic Functions & Notations We often want to know a quantity only approximately and not necessarily exactly, just to compare with another quantity. And, in many situations, correct comparison may be possible even with approximate values of the quantities. The advantage of the possibility of correct comparisons through even approximate values of quantities, is that the time required to find approximate values may be much less than the times required to find exact values. We will introduce five approximation functions and their notations. The purpose of these asymptotic growth rate functions to be introduced, is to facilitate the recognition of essential character of a complexity function through some simpler functions delivered by these notations. For examples, a complexity function f(n) = 5004 n3 + 83 n2 + 19 n + 408, has essentially same behavior as that of g(n) = n3 as the problem size n becomes larger and larger. But g(n) = n3 is much more comprehensible and its value easier to compute than the function f(n) Enumerate the five well known approximation functions and how these are pronounced i) of n2) ii) iii) is pronounced as big oh of n2 or sometimes just as oh

is pronounced as big omega of n2 or sometimes just as omega of n2) is pronounced as theta of n2)

o : (o (n2) is pronounced as little oh n2) iv) is pronounced as little omega of n2

These approximations denote relations from functions to functions.

For example, if functions f, g: N N are given by

f (n) = n2 5n and g(n) = n2 then O (f (n)) = g(n) or O (n2 5n) = n2 To be more precise, each of these notations is a mapping that associates a set of functions to each function under consideration. For example, if f(n) is a polynomial of degree k then the set O(f(n) included all polynomials of degree less than or equal to k. Remark In the discussion of any one of the five notations, generally two functions say f and g are involved. The functions have their domains and N, the set of natural numbers, i.e., f:N g:N N N

These functions may also be considered as having domain and co domain as R. Describe the following: o Fibonacci Heaps o Binomial Heaps Ans:- Fibonacci Heaps Structure of Fibonacci heaps Like a binomial heap, a Fibonacci heap is a collection of min-heap-ordered trees. The trees in a Fibonacci heap are not constrained to be binomial trees, however. Figure 4.3(a) shows an example of a Fibonacci heap. Unlike trees within binomial heaps, which are ordered, trees within Fibonacci heaps are rooted but unordered. As Figure 4.3(b) shows, each node x contains a pointer p [x] to its parent and a pointer child [x] to any one of its children. The children of x are linked together in a circular, doubly linked list, which we call the child list of x. Each child y in a child list has pointers left [y] and right [y] that point to ys left and right siblings, respectively. If node y is an only child, then left [y] = right [y] = y. The order in which siblings appear in a child list is arbitrary.

A Fibonacci heap consisting of five min-heap-ordered trees and 14 nodes. The dashed line indicates the root list. The minimum node of the heap is the node containing the key 3. The three marked nodes are blackened. The potential of this particular Fibonacci heap is 5+2.3=11. (b) A more complete representation showing pointers p (up arrows), child (down arrows), and left and right (sideways arrows). Binomial Heaps A binomial heap H is a set of binomial trees that satisfies the following binomial heap properties. 1. Each binomial tree in H obeys the min-heap property: the key of a node is greater than or equal to the key of its parent. We say that each such tree is min-heap-ordered. 2. For any nonnegative integer k, there is at most one binomial tree in H whose root has degree k. The first property tells us that the root of a min-heap-ordered tree contains the smallest key in the tree. The second property implies that an n-node binomial heap H consists of at most [lg n] + 1 binomial trees. To see why, observe that the binary representation of n has [lg n] + 1 bits, say , so that . By property 1 of 4.4.2, therefore, binomial tree Bi appears in H if and only if bit bI = 1. Thus, binomial heap H contains at most [lg n] + 1 binomial trees. Discuss the process of flow of Strassens Algorithm and also its limitations. Ans:- Strassens Algorithm:-Strassens recursive algorithm for multiplying n n matrices runs in the time. For sufficiently large value of n, therefore, it outperforms matrix-multiplication algorithm.

The idea behind the Strassens algorithm is to multiply multiplications (instead of 8). Consider the matrices

matrices with only 7 scalar

The seven sub matrix products used are P1 = a .(g-h) P2 = (a+b) h P3 = (c+d) e P4 = d. (fe) P5 = (a + d). (e + h) P6 = (b d). (f + h) P7 = (a c) . (e+g) Using these sub matrix products the matrix products are obtained by r = P5 + P4 P2 + P6 s = P1 + P2 t = P3 + P4 u = P5 + P1 P3 P1 This method works as it can be easily seen that s=(agah)+(ah+bh)=ag+bh. In this method there are 7 multiplications and 18 additions. For (nn) matrices, it can be worth replacing one multiplication by 18 additions, since multiplication costs are much more than addition costs. The recursive algorithm for multiplying nn matrices is given below:

1. Partition the two matrices A, B into

matrices.

2. Conquer: Perform 7 multiplications recursively. 3. Combine: Form using + and . The running time of above recurrence is given by the recurrence given below:

The current best upper bound for multiplying matrices is approximately Limitations of Strassens Algorithm From a practical point of view, Strassens algorithm is often not the method of choice for matrix multiplication, for the following four reasons: 1. The constant factor hidden in the running time of Strassens algorithm is larger than the constant factor in the method.

2. When the matrices are sparse, methods tailored for sparse matrices are faster. 3. Strassens algorithm is not quite as numerically stable as the nave method. 4. The sub matrices formed at the levels of recursion consume space. How do you formulize a greedy technique? Discuss the different steps one by one? Ans:- Formalization of Greedy Technique In order to develop an algorithm based on the greedy technique to solve a general optimization problem, we need the following data structures and functions: i) A set or list of give / candidate values from which choices are made, to reach a solution. For example, in the case of Minimum Number of Notes problem, the list of candidate values (in rupees) of notes is ii) Set (rather multi-set) of considered and chosen values: This structure contains those candidate values, which are considered and chosen by the algorithm based on greedy technique to reach a solution. Let us call this structure asCV : Structure of Chosen Values iii) Set of Considered and Rejected Values: As the name suggests, this is the set of all those values, which are considered but rejected. Let us call this set as RV : Set of considered and Rejected Values iv) A function say SolF that checks whether a solution is reached or not. However, the function does not check for the optimality of the obtained solution. In the case of Minimum Number of Notes problem, the function SolF finds the sum of all values in the multi-set CV

and compares with the desired amount, say Rs. 289. For example, if at one stage CV = {100, 100} then sum of values in CV is 200 which does not equal 289, then the function SolF returns. Solution not reached. v) Selection Function say SelF finds out the most promising candidate value out of the values not yet rejected, i.e., which are not in RV. In the case of Minimum Number of Notes problem, for collecting Rs. 289, at the stage when RV = {1000, 500} and CV = {100, 100} then first the function SelF attempts the denomination 100. But, through function SolF, when it is found that by addition of 100 to the values already in CV, the total value becomes 300 which exceeds 289, the value 100 is rejected and put in RV. Next, the function SelF attempts the next lower denomination 50. vi) The Feasibility-Test Function, say FeaF. When a new value say v is chosen by the function SelF, then the function FeaF checks whether the new set, obtained by adding v to the set CV of already selected values, is a possible part of the final solution. Thus in the case of Minimum Number of Notes problem, if amount to be collected is Rs. 289 and at some stage, CV = {100, 100}, then the function SelF returns 50. At this stage, the function FeaF takes the control. It adds 50 to the sum of the values in CV, and on finding that the sum 250 is less than the required value 289 informs the main/calling program that {100, 100, 50} can be a part of some final solution, and needs to be explored further. vii) The Objective Function, say ObjF, gives the value of the solution. For example, in the case of the problem of collecting Rs. 289; as CV = {100, 100, 50, 20, 10, 5, 2, 2} is such that sum of values in CV equals the required value 289, the function ObjF returns the number of notes in CV, i.e., the number 8. Briefly explain the Prims algorithm. Ans:- Prims Algorithm The algorithm due to Prim builds up a minimum spanning tree by adding edges to form a sequence of expanding subtrees. The sequence of subtrees is represented by the pair (V T, ET), where VT and ET respectively represent the set of vertices and the set of edges of a subtree in the sequence. Initially, the subtree, in the sequence, consists of just a single vertex which is selected arbitrarily from the set V of vertices of the given graph. The subtree is built-up iteratively by adding an edge that has minimum weight among the remaining edges (i.e., edge selected greedily) and, which at the same time, does not form a cycle with the earlier selected edges. We illustrate the Prims algorithm through an example before giving a semi-formal definition of the algorithm. Example (of Prims Algorithm): Let us explain through the following example how Primes algorithm finds a minimal spanning tree of a given graph. Let us consider the following graph: Initially VT = (a)

ET = In the first iteration, the edge having weight which is the minimum of the weights of the edges having a as one of its vertices, is chosen. In this case, the edge ab with weight 1 is chosen out of the edges ab, ac and ad of weights respectively 1, 5 and 2. Thus, after First iteration, we have the given graph with chosen edges in bold and VT and ET as follows: VT = (a, b) ET = ( (a, b)) In the next iteration, out of the edges, not chosen earlier and not making a cycle with earlier chosen edge and having either a or b as one of its vertices, the edge with minimum weight is chosen. In this case the vertex b does not have any edge originating out of it. In such cases, if required, weight of a non-existent edge may be taken as . Thus choice is restricted to two edges viz., ad and ac respectively of weights 2 and 5. Hence, in the next iteration the edge ad is chosen. Hence, after second iteration, we have the given graph with chosen edges and V T and ET as follows: VT = (a, b, d) ET = ((a, b), (a, d)) In the next iteration, out of the edges, not chosen earlier and not making a cycle with earlier chosen edges and having either a, b or d as one of its vertices, the edge with minimum weight is chosen. Thus choice is restricted to edges ac, dc and de with weights respectively 5, 3, 1.5. The edge de with weight 1.5 is selected. Hence, after third iteration we have the given graph with chosen edges and VT and ET as follows: VT = (a, b, d, e) ET = ((a, b), (a, d); (d, e))

S-ar putea să vă placă și