Вы находитесь на странице: 1из 13

1.

Describe the following:

Fibonacci Heaps Binomial Heaps

3 Explain the concept of binary search and also write the algorithm for binary search.
Binary Search: Binary Search algorithm searches a given value or element in an already sorted array by repeatedly dividing the search interval in half. It first compares the value to be searched with the item in the middle of the array. If there is a match, then search is successful and we can return the required result immediately. But if the value does not match with the item in middle of the array, then it finds whether the given value is less than or greater than the value in the middle of array. If the given value is less than the middle value, then the value of the item sought must lie in the lower half of the array. However, if the given value is greater than the item sought, must lie in the upper half of the array. So we repeat the procedure on the lower or upper half of the array according as the given value is respectively less than or greater than the middle value. The following C++ function gives the Binary Search Algorithm. Ex: int Binary Search (int * A, int low, int high, int value) { int mid: While (low < high) { mid = (low + high) / 2; If (value = = A [mid]) return mid; else if (value < A [mid]) high = mid 1; else low = mid + 1; } return 1; } Explanation of the Binary Search Algorithm: It takes as parameter the array A, in which the value is to be searched. It also takes the lower and upper bounds of the array as parameters viz., low and high respectively. At each step of the integration of the while loop, the algorithm reduces the number of the elements of the array to be searched by half. If the value is found then its index is returned. However, if the value is not found by the algorithm, then the loop terminates if the value of the low exceeds the value of high, there will be no more items to be searched and hence the function returns a negative value to indicate that item is not found.

4 Prove that If n 1, then for any n-key, B-tree T of height minimum degree t 2,
hlog t

h and

( n+ 1 ) 2

In computer science, a B-tree is a tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic amortized time. The B-tree is a generalization of a binary search tree in that more than two paths diverge from a single node. Unlike self-balancing binary search trees, the B-tree is optimized for systems that read and write large blocks of data. It is commonly used in databases and filesystems.

Proof: 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 raise to 2 nodes at depth 3, and so on, until at depth h there are at least 2t raise to h 1 nodes. n >= 1 + (t-1) Summation of 2t raise to i-1 from i=1 to h = 1 + 2(t-1) (t raise to h - 1 / t-1) = 2t raise to h - 1. By simple algebra, we get t raise to h <= (n+1) / 2.Taking base - t logarithms of both sides proves the theorem. Therefore, theorem is proved.

5. 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 treewith 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. 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

6. Explain Kruskals Algorithm.

1. Describe the following with suitable examples for each:

Binary Search Trees Red Black Trees

Binary Search Trees A binary search tree is organized, as the name suggests, in a binary tree. 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 binary-search-tree property. 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.

2. Show that R+RS*S = a*bS*, where R=b+aa*b and S is any regular expression.
In computing, regular expressions, also referred to as regex or regexp, provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. The examples illustrate a few specifications that could be expressed in a regular expression: The sequence of characters "car" in any context, such as "car", "cartoon", or "bicarbonate" The word "car" when it appears as an isolated word The word "car" when preceded by the word "blue" or "red" A dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits Proof: R+RS*S = R^ + RS*S (since R^ = ^R = R) = R (^ + S*S) (since R(S+T) = RS + RT) = R (^ + SS*) (since RR* = R*R = R* = ^+RR*) = RS* (since RR* = R*R = R* = ^+RR*) = (b + aa*b)S* (R = b + aa*b) = (^ + aa*)bS* (since R^ = ^R = R and since R(S+T) = RS + RT) = a*bS*. (since RR* = R*R = R* = ^+RR*) Therefore, hence proved. Therefore, R+RS*S = a*bS* .

3 Explain the concept of transition diagrams with examples wherever necessary.


Transition Diagrams: In some situations, graphical representation of the next-move (partial) Function of a Turing Machine may give better idea of the behavior of a TM in comparison to . of a TM is a graphical representation the tabular representation of

A Transition Diagram of the next-move functions

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. Ex:Let M = {Q, = {0, 1} = {0, 1, #} and be given by the following table. 0 1 # q0 (q2, #, R) q1 (q2, 0, R) (q1, #, R) (h, #, N) q2 (q2, 0, L) (q1, 1, R) (h, #, N) h3 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. , , , q0, h} Where Q = {q0, q1, q2, h3}

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


In formal language theory, a context-free language is a language generated by some context-free grammar. The set of all context-free languages is identical to the set of languages accepted by pushdown automata. Proof: 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 -> S1 | S2 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. Therefore, it is proved that if L1 and L2 are context free languages, then L1 Union L2 is also a context free language.

5 Explain the concept of Prims algorithm, with examples wherever necessary?


Prims Algorithm: The algorithm due to Prim builds up a minimum spanning tree by adding edges to form a sequence of expanding sub trees. The sequence of sub trees is represented by the pair ( ), where Ex: The example shows how Prims algorithm finds a minimal spanning tree of a given graph. and sub tree in the sequence. , respectively represent the set of vertices and the set of edges of a

Initially,

= (a) and

After first iteration, we have = (a, b) and = (a, b, d) and = (a, b, d, e) and = (a, b, d, e, c) and = ((a, b)) = ((a, b), (a, d)) = ((a, b), (a, d), (d, e)) = ((a, b), (a, d), (d, e), (d, c)) After second iteration, we have After third iteration, we have After fourth iteration, we have Given below is the semi formal definition of Prims Algorithm Algorithm Spanning Prim (G) <- { <} // initially is empty

// let n = number of vertices in V For i = 1 to |n| - 1 do

Find a minimum weight edge is in V <= Return . u{ u{ } . }

=(

) among all the edges such that

is in

and

6 Give an algorithm for Greedy Knapsack problem. Analyze your algorithm?


The knapsack problem comes up when there is resource allocation with various constraints. For example, given a fixed budget, how do you select what things you should buy? Everything has a cost and value, so we want the most value for a given cost. The term "knapsack problem" invokes the image of the backpacker who is constrained by a fixed-size knapsack and must fill it only with the most useful items. A typical form is the 0/1 knapsack problem, where each item must be put entirely in the knapsack or not included at all. Objects cannot be broken in pieces arbitrarily. This 0/1 property makes the knapsack problem difficult. It's easy to find a greedy algorithm for the optimal selection when we are allowed to break objects up. For each item, we could compute its "price per pound", and take as much of the most expensive item until we have it all or the knapsack is full. Then do the same with the next most expensive item, etc., until the knapsack is full. Unfortunately, the 0/1 constraint is usually a requirement. So lets say you are a thief with a knapsack of a certain capacity. You look around the place you just broke into and see items of different weights and values. You would like the items you choose for your knapsack to have the greatest value possible yet still fit in the knapsack. This is the 0-1 Knapsack Problem because you, the thief, can either take (1) or leave (0) each item. Given a set of items, each with a cost and a value, determine the number of each item to include in a collection so that the total cost is less than some given cost and the total value is as large as possible. The 0/1 knapsack problem restricts the number of each items to zero or one. We must: Maximize sum (include_item(i) * value(i)) where include_item(i) = 0 or 1 Subject to sum (include_item(i) * weight(i)) <= C There are several greedy approaches to try: Greedy by Profit: At each step select from the remaining items the one with the highest profit. This approach tries to maximize the profit by choosing the most profitable items first. Greedy by Weight: At each step select from the remaining items the one with the least weight. This approach tries to maximize the profit by putting as many items into the knapsack as possible. Greedy by Profit Density: At each step select from the remaining items the one with the largest profit density, that is, pi/wi. This approach tries to maximize the profit by choosing items with the largest profit per unit of weight. Moral: Greedy algorithm sometimes gives optimal solution, sometimes not, depending on the problem. Dynamic programming, when applicable, will typically give optimal solutions, but are usually trickier to come up with and sometimes trickier to implement.

Вам также может понравиться