0% found this document useful (0 votes)
214 views25 pages

Sikkim Manipal University: July 2011

The document contains an assignment submission for a course on Analysis and Design of Algorithms. It includes answers to 5 questions about algorithms topics like Fibonacci heaps, binomial heaps, bubble sort, B-trees, and breadth-first search. The submission contains the student's name, roll number, and center code at the top and bottom of each page.

Uploaded by

Pankaj Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views25 pages

Sikkim Manipal University: July 2011

The document contains an assignment submission for a course on Analysis and Design of Algorithms. It includes answers to 5 questions about algorithms topics like Fibonacci heaps, binomial heaps, bubble sort, B-trees, and breadth-first search. The submission contains the student's name, roll number, and center code at the top and bottom of each page.

Uploaded by

Pankaj Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 25

SIKKIM MANIPAL UNIVERSITY

July 2011
Master of Computer Application (MCA) Semester 4 MC0080 Analysis and Design of Algorithms (Book ID: B0891) Assignment Set 1

SUBMITTED BY:

NAME ROLL NO

: PANKAJ KUMAR : 521048592

CENTRE CODE : 2769

NAME: PANKAJ KUMAR

ROLL NO: 521048592

CENTRE CODE: 2769

Page |1

Assignment: MC0080 Analysis and Design of Algorithms

Q 1.

Describe the following: Fibonacci Heaps Binomial Heaps

Ans.

Fibonacci Heaps: 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) 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). Two other fields in each node will be of use. The number of children in the child list of node x is stored in degree[x]. The Boolean-valued field mark[x] indicates whether node x has lost a child since the last time x was made the child of another node. Newly created nodes are unmarked, and a node x becomes unmarked whenever it is made the child of another node.
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

Page |2

Assignment: MC0080 Analysis and Design of Algorithms


A given Fibonacci heap H is accessed by a pointer min [H] to the root of a tree containing a minimum key; this node is called the minimum node of the Fibonacci heap. If a Fibonacci heap H is empty, then min [H] = NIL. The roots of all the trees in a Fibonacci heap are linked together using their left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. The pointer min [H] thus points to the node in the root list whose key is minimum. The order of the trees within a root list is arbitrary. We rely on one other attribute for a Fibonacci heap H : the number of nodes currently in H is kept in n[H].

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

Q 2. Ans. 78, g(n) < 1 f(n), it follows that g O( f ). We could have come to the same conclusion from:

. Since for n

We can show that f O( g ) by observing that the limit of f / g = . Here is an alternative method. We assume that f O( g ) and derive a contradiction. If f O( g ), then there exist constants c and n0 such that for all n n0,

Since c is a constant and n may be arbitrarily large, it is impossible to have n/2 174c for all n n0.

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

Page |3

Assignment: MC0080 Analysis and Design of Algorithms


Q 3. Ans. Explain the concept of bubble sort and also write the algorithm for bubble sort. The Bubble Sort algorithm for sorting of n numbers, represented by an array A [1..n], proceeds by scanning the array from left to right. At each stage, compares adjacent pairs of numbers at positions A[i] and A [i +1] and whenever a pair of adjacent numbers is found to be out of order, then the positions of the numbers are exchanged. The algorithm repeats the process for numbers at positions A [i + 1] and A [i + 2] Thus in the first pass after scanning once all the numbers in the given list, the largest number will reach its destination, but other numbers in the array, may not be in order. In each subsequent pass, one more number reaches its destination. Example: In the following, in each line, pairs of adjacent numbers, shown in bold, are compared. And if the pair of numbers are not found in proper order, then the positions of these numbers are exchanged. The list to be sorted has n = 6 as shown in the first row below:

In the first pass traced above, the maximum number 110 of the list reaches the rightmost position (i.e., 6th position). In the next pass, only the list of remaining (n 1) = 5 elements, as shown in the first row below, is taken into consideration. Again pairs of numbers in bold, in a row, are compared and exchanged, if required.

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

Page |4

Assignment: MC0080 Analysis and Design of Algorithms


In the second pass, the next to maximum element of the list viz., 81, reaches the 5th position from left. In the next pass, the list of remaining (n 2) = 4 elements are taken into consideration.

In the next iteration, only (n 3) = 3 elements are taken into consideration.

In the next iteration, only n 4 = 2 elements are considered 31 32 These elements are compared and found in proper order. The process terminates. Procedure bubblesort (A[ 1n]) begin for i for j 1 to n 1 do 1 to (n i)

{In each new iteration, as explained above, one less number of elements is taken into consideration. This is why j varies upto only (n i)} If A [ j ] > A [ j + 1] then interchange A [ j ] and A [ j + 1] end { A [1..n ] is in increasing order}

Q 4.

Prove that if n 1, then for any n-key, B-tree T of height hand minimum degree t 2,

Ans. If a B tree has height h, the root contains at least one key and all other nodes contain at least
t 1 keys. Thus, there are at least 2 nodes at depth 1, at least 2t nodes at depth 2, at least 2t 2 nodes at depth 3, and so on, until at depth h there are at least 2t h 1 nodes. Figure illustrates such a tree for h = 3. Thus, the number n of keys satisfies the inequality.

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

Page |5

Assignment: MC0080 Analysis and Design of Algorithms

B tree of height 3 containing a minimum possible number of keys. Shown inside each node x is n[x].

. By simple algebra, we get . Taking base t logarithms of both sides proves the theorem. Here we see the power of B trees, as compared to red-black trees. Although the height of the tree grows as O(lg n) in both cases (that t is a constant), for B trees the base of the logarithm can be many times larger. Thus, B trees save a factor of about lg t over red-black trees in the number of nodes examined for most tree operations. Since examining an arbitrary node in a tree usually requires a disk access, the number of disk accesses is substantially reduced.

Q 5. Ans.

Explain briefly the concept of breadth-First search(BFS) Breadth first search as the name suggests first discovers all vertices adjacent to a given vertex before moving to the vertices far ahead in the search graph. If G(V, E) is a graph having vertex set V and edge set E and a particular source vertex s, breadth first search find or discovers every vertex that is reachable from s. First it discovers every vertex adjacent to s, then systematically for each of those vertices find all the vertices adjacent to them and so on. In doing so, it computes the distance and the shortest path in terms of fewest numbers of edges from the source node s to each of the reachable vertex. Breadth-first Search also produces a breadth-first tree with root vertex the process of searching or traversing the graph. For recording the status of each vertex, whether it is still unknown, whether it has been discovered (or found) and whether all of its adjacent vertices have also been discovered. The vertices are termed as unknown, discovered and visited respectively. So if (u, v) E and u is visited then v will be either discovered or visited i.e., either v has just been discovered or vertices adjacent to v have also been found or visited.

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

Page |6

Assignment: MC0080 Analysis and Design of Algorithms


As breadth first search forms a breadth first tree, so if in the edge (u, v) vertex v is discovered in adjacency list of an already discovered vertex u then we say that u is the parent or predecessor vertex of V. Each vertex is discovered once only. The data structure we use in this algorithm is a queue to hold vertices. In this algorithm we assume that the graph is represented using adjacency list representation. front [u] us used to represent the element at the front of the queue. Empty() procedure returns true if queue is empty otherwise it returns false. Queue is represented as Q. Procedure enqueue() and dequeue() are used to insert and delete an element from the queue respectively. The data structure Status[ ] is used to store the status of each vertex as unknown or discovered or visite. Algorithm of Breadth First Search 1. for each vertex u V {s} 2. status[u] = unknown 3. status[s] = discovered 4. enqueue (Q, s) 5. while (empty[Q]! = false) 6. u = front[Q] 7. for each vertex v Adjacent to u 8. if status[v] = unknown 9. status[v] = discovered 10. parent (v) = u 11. end for 12. enqueue (Q, v); 13. dequeue (Q) 14. status[u] = visited 15. print u is visited 16. end while Example: In the figure given below, we can see the graph given initially, in which only sources is discovered.

(A): Initial Input Graph


NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

Page |7

Assignment: MC0080 Analysis and Design of Algorithms

(B): After we visited s We take unknown (i.e., undiscovered) adjacent vertex of a s and insert them in queue, first a and then b. The values of the data structures are modified as given below: Next, after completing the visit of a we get the figure and the data structures as given below:

(C): After we visit a

(D): After we visit b

(E): After we visit c


NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

Page |8

Assignment: MC0080 Analysis and Design of Algorithms

(F): After we visit d

(G): After we visit e

(H): After we visit f

(I): After we visit g


NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

Page |9

Assignment: MC0080 Analysis and Design of Algorithms


(A) : Initial Input Graph (B): We take unknown (i.e., undiscovered) adjacent vertices of s and insert them in the queue. (C): Now the gray vertices in the adjacency list of u are b, c and d, and we can visit any of them depending upon which vertex is inserted in the queue first. As in this example, we have inserted b first which is now at the front of the queue, so next we will visit b. (D): As there is no undiscovered vertex adjacent to b, so no new vertex will be inserted in the queue, only the vertex b will be removed from the queue. (E): Vertices e and f are discovered as adjacent vertices of c, so they are inserted in the queue and then c is removed from the queue and is visited. (F): Vertex g is discovered as the adjacent vertex of d an after that d is removed from the queue and its status is changed to visit. (G): No undiscovered vertex adjacent to e is found so e is removed from the queue and its status is changed to visit. (H): No undiscovered vertex adjacent to f is found so f is removed from the queue and its status is changed to visit. (I): No undiscovered vertex adjacent to g is found so g is removed from the queue and its status is changed to visit. Now as queue becomes empty so the while loop stops. Q 6. Ans. Explain Kruskals Algorithm. Kruskals Algorithm: We illustrate the Kruskals algorithm through the following: Let us consider the following graph, for which the minimal spanning tree is required.

Figure 6.1 Let Eg denote the set of edges of the graph that are chosen upto some stage. According to the step (i) above, the weights of the edges are arranged in increasing order as the set {1, 3, 4.2, 5, 6} In the first iteration, the edge (a, b) is chosen which is of weight 1, the minimum of all the weights of the edges of the graph. As single edge do not form a cycle, therefore, the edge (a, b) is selected, so that Eg = ((a, b)) After first iteration, the graph with selected edges in bold is as shown below:
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 10

Assignment: MC0080 Analysis and Design of Algorithms

Figure 6.2 Second Iteration: Next the edge (c, d) is of weight 3, minimum for the remaining edges. Also edges (a, b) and (c, d) do not form a cycle, as shown below. Therefore, (c, d) is selected so that, Eg = ((a, b), (c, d)) Thus, after second iteration, the graph with selected edges in bold is as shown below:

Figure 6.3 It may be observed that the selected edges do not form a connected subgraph or subtree of the given graph. Third Iteration: Next, the edge (a, d) is of weight 4.2, the minimum for the remaining edges. Also the edges in Eg along with the edge (a, d) do not form a cycle. Therefore, (a, d) is selected so that new Eg = ((a, b), (c, d), (a, d)). Thus after third iteration, the graph with selected edges in bold is as shown below:

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

P a g e | 11

Assignment: MC0080 Analysis and Design of Algorithms

Figure 6.4 Fourth Iteration: Next, the edge (a, c) is of weight 5, the minimum for the remaining edge. However, the edge (a, c) forms a cycles with two edges in Eg, viz., (a, d) and (c, d). Hence (a, c) is not selected and hence not considered as a part of the to-be-found spanning tree.

Figure 6.5 At the end of fourth iteration, the graph with selected edges in bold remains the same as at the end of the third iteration, as shown below:

Figure 6.6
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 12

Assignment: MC0080 Analysis and Design of Algorithms


Fifth Iteration: Next, the edge (e, d), the only remaining edge that can be considered, is considered. As (e, d) does not form a cycle with any of the edges in Eg. Hence the edge (e, d) is put in Eg. The graph at this stage, with selected edge in bold is as follows:

Figure 6.7 At this stage we find each of the vertices of the given graph is a vertex of some edge in Eg. Further we observe that the edges in Eg form a tree, and hence, form the required spanning tree. Also, from the choice of the edges in Eg, it is clear that the spanning tree is of minimum weight. Next, we consider semi-formal definition of Kruskals algorithm. ALGORITHM Spanning-Kruskal (G) // The algorithm constructs a minimum spanning tree by choosing successively edges // of minimum weights out of the remaining edges. // The input to the algorithm is a connected graph G = (V, E), in which V is the set of // vertices and E, the set of edges and weight of each edge is also given. // The output is the set of edges, denoted by ET, which constitutes a minimum // spanning tree of G // the variable edge-counter is used to count the number of selected edges so far. // variable t is used to count the number of edges considered so far. Arrange the edges in E in nondecreasing order of the weights of edges. After the arrangement, the edges in order are labeled as e1, e2, ..eE ET // initialize the set of tree edges as empty edge-counter 0 // initialize the encounter to zero t 0 // initialize the number of processed edges as zero // let n = number of edges in V While edge-counter < n 1 t t + 1 // increment the counter for number of edges considered so far If the edges e t does not form a cycle with any subset of edges in ET then begin // if, e t along with edges earlier in ET do not form a cycle
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 13

Assignment: MC0080 Analysis and Design of Algorithms


// then add et to ET and increase edge counter ET ET {e t}; edge-counter edge-counter + 1 end if return ET Summary of Kruskals Algorithm The algorithm is always successful in finding a minimal spanning tree

(Sub) tree structure may not be maintained, however, finally, we get a minimal spanning tree Computing Complexity of Kruskalss Let a be the number of edges and n be the number of nodes, initially given. Then

i) ii)

(a log a) time is required for sorting the edges in increasing orders of lengths An efficient Union-Find operation takes (2a) find operations and (n 1) merge operations. Thus Complexity of Kruskals algorithm is O(a log a)

END of Assignment Set 1

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

P a g e | 14

Assignment: MC0080 Analysis and Design of Algorithms

SIKKIM MANIPAL UNIVERSITY


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

SUBMITTED BY:

NAME ROLL NO

: PANKAJ KUMAR : 521048592

CENTRE CODE : 2769

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

P a g e | 15

Assignment: MC0080 Analysis and Design of Algorithms


Q 1. Describe the following with suitable examples for each: Ans. Binary Search Trees Red Black Trees

Binary Search Trees: A binary search tree is organized, as the name suggests, in a binary tree, as shown in Figure 3.5. Such a tree can be represented by a linked data structure in which each node is an object. In addition to a key field and satellite data, each node contains fields

For any node x, the keys in the left subtree of x are at most key [x], and the keys in the right subtree of x are at least key [x]. Different binary search trees can represent the same set of values. The worst-case running time for most search-tree operations is proportional to the height of the tree. (a) A binary search tree on 6 nodes with height 2. (b) A less efficient binary search tree with height 4 that contains the same keys. left, right, and p that point to the nodes corresponding to its left child, its right child, and its parent, respectively. If a child or the parent is missing, the appropriate field contains the value NIL. The root node is the only node in the tree whose parent field is NIL. The keys in a binary search tree are always stored in such a way as to satisfy the binarysearch-tree property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key [y] < key [x]. If y is a node in the right subtree of x, then key [x] < key [y]. Thus, in the above Figure (a), the key of the root is 5, the keys 2, 3, and 5 in its left subtree are no larger than 5, and the keys 7 and 8 in its right subtree are no smaller than 5. The same property holds for every node in the tree. For example, the key 3 in Figure (a) is no smaller than the key 2 in its left subtree and no larger than the key 5 in its right subtree. The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk. This algorithm is so named because the key of the root of a subtree is printed between the values in its left subtree and those in its right subtree. (Similarly, a preorder tree walk prints the root before the values in either subtree, and a postorder tree walk prints the root after the values in its subtrees.) To use the following procedure to print all the elements in a binary search tree T, we call INORDER-TREE-WALK (root[T]).
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 16

Assignment: MC0080 Analysis and Design of Algorithms


INORDER TREE WALK (x)

1. if x

NIL

2. then INORDER TREE WALK (left[x]) 3. print key [x]


4. INORDER TREE WALK (right [x]) As an example, the inorder tree walk prints the keys in each of the two binary search trees from above Figure in the order 2, 3, 5, 5, 7, 8. The correctness of the algorithm follows by induction directly from the binary-search-tree property. It takes time to walk an n-node binary search tree, since after the initial call, the procedure is called recursively exactly twice for each node in the treeonce for its left child and once for its right child. The following theorem gives a more formal proof that it takes linear time to perform an inorder tree walk.

Red Black Trees: A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. By constraining the way nodes can be colored on any path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other, so that the tree is approximately balanced. Each node of the tree now contains the fields color, key, left, right, and p. If a child or the parent of a node does not exist, the corresponding pointer field of the node contains the value NIL. We shall regard these NILs as being pointers to external nodes (leaves) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree. A binary search tree is a red-black tree if it satisfies the following red-black properties: 1. Every node is either red or black. 2. The root is black. 3. Every leaf (NIL) is black. 4. If a node is red, then both its children are black. 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes. As a matter of convenience in dealing with boundary conditions in red-black tree code, we use a single sentinel to represent NIL. For a red-black tree T, the sentinel nil [T] is an object with the same fields as an ordinary node in the tree. Its color field is BLACK, and its other fields p, left, right, and keycan be set to arbitrary values. As Figure (b) show, all pointers to NIL are replaced by pointers to the sentinel nil [T]. We use the sentinel so that we can treat a NIL child of a node x as an ordinary node whose parent is x. Although we instead could add a distinct sentinel node for each NIL in the tree, so that the parent of each NIL is well defined, that approach would waste space. Instead, we use the one sentinel nil [T] to represent all the NILs all leaves and the roots parent. The values of the fields p, left, right, and key of the sentinel are immaterial, although we may set them during the course of a procedure for our convenience. We generally confine our interest to the internal nodes of a red-black tree, since they hold the key values. In the remainder of this chapter, we omit the leaves when we draw red-black trees, as shown in Figure (c).
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 17

Assignment: MC0080 Analysis and Design of Algorithms


We call the number of black nodes on any path from, but not including, a node x down to a leaf the black-height of the node, denoted bh (x). By property 5, the notion of black-height is well defined, since all descending paths from the node have the same number of black nodes. We define the black-height of a red-black tree to be the black-height of its root. Following Figure shows an example of a red-black tree..

Figure: A red-black tree with black nodes darkened and red nodes shaded. Every node in a red-black tree is either red or black, the children of a red node are both black, or every simple path from a node to a descendant leaf contains the same number of black nodes. (a) Every leaf, shown as a NIL, is black. Each non-NIL node is marked with its black-height; NILs have black-height 0. (b) The same red-black tree but with each NIL replaced by the single sentinel nil [T], which is always black, and with black-heights omitted. The roots parent is also sentinel. (c) The same redblack tree but with leaves and the roots parent omitted entirely. Q 2.
Show that R + RS*S = a*bS*, where R = b + aa*b and S is any regular expression: Ans.
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 18

Assignment: MC0080 Analysis and Design of Algorithms R+ RS*S = R + RS*S (property 2) = R( + S*S) (property3) = R( + SS*) (property 6) = RS* (property 5) = (b+ aa*b)S*(definition of R) = ( + aa*)bS* (properties 2 and 3) = a*bS* (property 5)

Q 3.

Explain the concept of transition diagrams with examples wherever necessary. Ans. In some situations, graphical representation of the next-move (partial) function of a Turing Machine may give better idea of the behaviour of a TM in comparison to the tabular representation of . A Transition Diagram of the next-move functions d of a TM is a graphical representation consisting of a finite number of nodes and (directed) labelled arcs between the nodes. Each node represents a state of the TM and a label on an arc from one state (say p) to a state (say q) represents the information about the required input symbol say x for the transition from p to q to take place and the action on the part of the control of TM. The action part consists of (i) the symbol say y to be written in the current cell and (ii) the movement of the tape Head. Then the label of an arc is generally written as x/(y, M) where M is L, R or N. Example

Where Q = {q0, q1, q2, h3} = {0, 1} = {0, 1, #} and d be given by the following table. 0 q0 q1 q2 h3 (q2, 0, R) (q2, 0, L) 1 (q1, #, R) (q1, 1, R) # (q2, #, R) (h, #, N) (h, #, N) -

Then, the above Turing Machine may be denoted by the Transition Diagram shown below, where we assume that q0 is the initial state and h is the final state.

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

P a g e | 19

Assignment: MC0080 Analysis and Design of Algorithms

Q 4.

If L1 and L2 are context- free languages, then L1 U L2 is a context free language. Ans. If L1 and L2 are context-free languages, then each of them has a context-free grammar; call the grammars G1 and G2. Our proof requires that the grammars have no non-terminals in common. So we shall subscript all of G1s non-terminals with a 1 and subscript all of G2s non-terminals with a 2. Now, we combine the two grammars into one grammar that will generate the union of the two languages. To do this, we add one new non-terminal, S, and two new productions.

S is the starting non-terminal for the new union grammar and can be replaced either by the starting non-terminal for G1 or for G2, thereby generating either a string from L1 or from L2. Since the non-terminals of the two original languages are completely different, and once we begin using one of the original grammars, we must complete the derivation using only the rules from that original grammar. Note that there is no need for the alphabets of the two languages to be the same. Q 5. Explain prims Algorithm. Ans. 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
ROLL NO : 521048592 CENTRE CODE : 2769

NAME : PANKAJ KUMAR

P a g e | 20

Assignment: MC0080 Analysis and Design of Algorithms


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 =

Figure 5.1 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))

Figure 5.2 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
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 21

Assignment: MC0080 Analysis and Design of Algorithms


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

Figure 5.3 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))

Figure 5.4 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:
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 22

Assignment: MC0080 Analysis and Design of Algorithms


VT = (a, b, d, e, c) ET = ((a, b), (a, d) (d, e) (d, c))

Figure 5.5 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 {v0} ET // initially ET is empty // let n = number of vertices in V For i = 1 to n 1 do find a minimum-weight edge = (v1, u1) among all the edges such that v1 is in VT and u1 is in V VT. VT VT { u1} ET = ET { } Return ET

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

P a g e | 23

Assignment: MC0080 Analysis and Design of Algorithms


Q 6. Give an algorithm for Greedy Knapsack problem. Analyse your algorithm? Ans. 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 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).
NAME : PANKAJ KUMAR ROLL NO : 521048592 CENTRE CODE : 2769

P a g e | 24

Assignment: MC0080 Analysis and Design of Algorithms

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.

END of Assignment Set 2

NAME : PANKAJ KUMAR

ROLL NO : 521048592

CENTRE CODE : 2769

P a g e | 25

You might also like