You are on page 1of 9

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.

MAY 2011 Master of Computer Application (MCA) Semester 4 MC0080 Analysis and Design of Algorithms 4 Credits
(Book ID: B0891) Assignment Set 2 (40 Marks)

Answer the following:

610 = 6

1. Explain the concept of Recursion in algorithms Ans 1 :


A Recursive Algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller (or simpler) input. More generally if a problem can be solved utilizing solutions to smaller versions of the same problem, and the smaller versions reduce to easily solvable cases, then one can use a recursive algorithm to solve that problem. For example, the elements of a recursively defined set, or the value of a recursively defined function can be obtained by a recursive algorithm. If a set or a function is defined recursively, then a recursive algorithm to compute its members or values mirrors the definition. Initial steps of the recursive algorithm correspond to the basis clause of the recursive definition and they identify the basic elements. They are then followed by steps corresponding to the inductive clause, which reduce the computation for an element of one generation to that of elements of the immediately preceding generation. In general, recursive computer programs require more memory and computation compared with iterative algorithms, but they are simpler and for many cases a natural way of thinking about the problem. Example 1: Algorithm for finding the k-th even natural number Note here that this can be solved very easily by simply outputting 2*(k - 1) for a given k . The purpose here, however, is to illustrate the basic idea of recursion rather than solving the problem. Algorithm 1: Even(positive integer k) Input: k , a positive integer Output: k-th even natural number (the first even being 0) Algorithm: if k = 1, then return 0; else return Even(k-1) + 2 . Here the computation of Even(k) is reduced to that of Even for a smaller input value, that is Even(k1). Even(k) eventually becomes Even(1) which is 0 by the first line. For example, to compute Even(3), Algorithm Even(k) is called with k = 2. In the computation of Even(2), Algorithm Even(k) is called 1

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.

with k = 1. Since Even(1) = 0, 0 is returned for the computation of Even(2), and Even(2) = Even(1) + 2 = 2 is obtained. This value 2 for Even(2) is now returned to the computation of Even(3), and Even(3) = Even(2) + 2 = 4 is obtained. As can be seen by comparing this algorithm with the recursive definition of the set of nonnegative even numbers, the first line of the algorithm corresponds to the basis clause of the definition, and the second line corresponds to the inductive clause. By way of comparison, let us see how the same problem can be solved by an iterative algorithm. Algorithm 1-a: Even(positive integer k) Input: k, a positive integer Output: k-th even natural number (the first even being 0) Algorithm: int i, even; i := 1; even := 0; while( i < k ) { even := even + 2; i := i + 1; } return even .

2. What do you mean by Pushdown Automata? Ans 2:


A PDA is formally defined as a 7-tuple: where

is a finite set of states is a finite set which is called the input alphabet is a finite set which is called the stack alphabet is a mapping of to finite subsets of , the transition relation, where * means "a finite (maybe empty) list of element of " and denotes the empty string. is the start state is the initial stack symbol is the set of accepting states is a transition of M. It has the intended meaning that M, in state as topmost stack symbol, may read a, . The letter (epsilon) denotes the

An element

, with on the input and with change the state to q, pop A, replacing it by pushing

empty string and the component of the transition relation is used to formalize that the PDA can either read a letter from the input, or proceed leaving the input untouched. In many texts the transition relation is replaced by an (equivalent) formalization, where

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.


into finite subsets of

is the transition function, mapping .

Here (p,a,A) contains all possible actions in state p with A on the stack, while reading a on the input. One writes for the function precisely when relation. Note that finite in this definition is essential. for the

Example The following is the formal description of the PDA which recognizes the language by final state:

PDA for

(by final state) , where

Q = {p,q,r} = {0,1} = {A,Z} F = {r} consists of the following six instructions: (p,0,Z,p,AZ), (p,0,A,p,AA), (p,,Z,q,Z), (p,,A,q,A), (q,1,A,q,), and (q,,Z,r,Z). In words, in state p for each symbol 0 read, one A is pushed onto the stack. Pushing symbol A on top of another A is formalized as replacing top A by AA. In state q for each symbol 1 read one A is popped. At any moment the automaton may move from state p to state q, while it may move from state q to accepting state r only when the stack consists of a single Z. There seems to be no generally used representation for PDA. Here we have depicted the instruction (p,a,A,q,) by an edge from state p to state q labelled by a;A / (read a; replace A by ).

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.

3. Explain the concept of undecidable problems for context free languages. Ans 3:
Does a given Turing machine M halt on all inputs? Does Turing machine M halt for any input? Do two Turing machines M1 and M2 accept the same language? Is the language L (M) finite? Does L (M) contain any two strings of the same length? Does L (M) contain a string of length k, for some given k? If G is a unrestricted grammar. Does L (G) = ?

Does L (G) infinite? If G is a context sensitive grammar. Does L (G) = ?

Does L (G) infinite ? If L1 and L2 are any context free languages over Does Does L1 = L2? Does L1 L2? . ? .

If L is recursively enumerable language over Does L empty? Does L finite?

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.

4. If L1 and L2 are context- free languages, then L1 U L2 is a context free language. Ans 4:

5. Explain prims Algorithm. 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 (VT, 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 =

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.

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 VT 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))

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.

In the next iteration, out of the edges, not chosen earlier and not making a cycle with earlier chosen edge and having either a, b, d or e as one of its vertices, the edge with minimum weight is chosen. Thus, choice is restricted to edges dc and ac with weights respectively 3 and 5. Hence the edge dc with weight 3 is chosen. Thus, after fourth iteration, we have the given graph with chosen edges and VT and ET as follows: VT = (a, b, d, e, c) ET = ((a, b), (a, d) (d, e) (d, c))

At this stage, it can be easily seen that each of the vertices, is on some chosen edge and the chosen edges form a tree. Given below is the semiformal definition of Prims Algorithm Algorithm Spanning-Prim (G) // the algorithm constructs a minimum spanning tree // for which the input is a weighted connected graph G = (V, E) // the output is the set of edges, to be denoted by ET, which together constitute a minimum spanning tree of the given graph G // for the pair of vertices that are not adjacent in the graph to each other, can be given // the label indicating infinite distance between the pair of vertices. // the set of vertices of the required tree is initialized with the vertex v0 VT } ET // let n = number of vertices in V

MCA Semester 4 Assignment Set 2


For i = 1 to n 1 do

MC0080 Analysis and Design of Algorithms Roll No.

find a minimum-weight edge e = (v1, u1) among all the edges such that v1 is in VT and u1 is in V VT. VT ET = ET { e } Return ET

6. Give an algorithm for Greedy Knapsack problem. Analyze your algorithm? Ans 6: There are n items in a store. For i =1,2, . . . , n, item i has weight wi > 0 and worth vi > 0. Thief can carry a maximum weight of W pounds in a knapsack. In this version of a problem the items can be broken into smaller piece, so the thief may decide to carry only a fraction xi of object i, where 0 xi 1. Item i contributes xiwi to the total weight in the knapsack, and xivi to the value of the load. In Symbol, the fraction knapsack problem can be stated as follows. maximize nSi=1 xivi subject to constraint nSi=1 xiwi W It is clear that an optimal solution must fill the knapsack exactly, for otherwise we could add a fraction of one of the remaining objects and increase the value of the load. Thus in an optimal solution nSi=1 xiwi = W. Greedy-fractional-knapsack (w, v, W) FOR i =1 to n do x[i] =0 weight = 0 while weight < W do i = best remaining item IF weight + w[i] W then x[i] = 1 weight = weight + w[i] else x[i] = (w - weight) / w[i] weight = W return x Analysis If the items are already sorted into decreasing order of vi / wi, then the while-loop takes a time in O(n); Therefore, the total time including the sort is in O(n log n). If we keep the items in heap with largest vi/wi at the root. Then creating the heap takes O(n) time while-loop now takes O(log n) time (since heap property must be restored after the removal of root) Although this data structure does not alter the worst-case, it may be faster if only a small number of items are need to fill the knapsack. One variant of the 0-1 knapsack problem is when order of items are sorted by increasing weight is the same as their order when sorted by decreasing value. The optimal solution to this problem is to sort by the value of the item in decreasing order. Then pick up the most valuable item which also has a least weight. First, if its weight is less than the total weight that can be carried. Then deduct the total weight that can be carried by the weight of the 8

MCA Semester 4 Assignment Set 2

MC0080 Analysis and Design of Algorithms Roll No.

item just pick. The second item to pick is the most valuable item among those remaining. Keep follow the same strategy until thief cannot carry more item (due to weight). Proof One way to proof the correctness of the above algorithm is to prove the greedy choice property and optimal substructure property. It consist of two steps. First, prove that there exists an optimal solution begins with the greedy choice given above. The second part prove that if A is an optimal solution to the original problem S, then A - a is also an optimal solution to the problem S - s where a is the item thief picked as in the greedy choice and S - s is the subproblem after the first greedy choice has been made. The second part is easy to prove since the more valuable items have less weight. Note that if v` / w` , is not it can replace any other because w` < w, but it increases the value because v` > v. Theorem The fractional knapsack problem has the greedy-choice property. Proof Let the ratio v`/w` is maximal. This supposition implies that v`/w` v/w for any pair (v, w), so v`v / w > v for any (v, w). Now Suppose a solution does not contain the full w` weight of the best ratio. Then by replacing an amount of any other w with more w` will improve the value.

You might also like