Академический Документы
Профессиональный Документы
Культура Документы
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.
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
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* .
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}
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
=(
is in
and