Monday, 3 December 2012

Data Structures and Algorithm_ Unit 4


 P. A. COLLEGE OF ENGINEERING AND TECHNOLOGY
POLLACHI, COIMBATORE – 642 002.


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


ME – DEGREE COMPUTER SCIENCE AND ENGINEERING


DATA STRUCTURES AND ALGORITHMS_Unit 4



Short Questions and Answers
(2 Marks )
Prepared By,
Mr. S. P. Santhoshkumar M.E.,
Assistant Professor,
Department of CSE.

UNIT IV – GREEDY & DIVIDE AND CONQUER


1. Give the general plan for divide-and-conquer algorithms.
The general plan is as follows 
i.A problems instance is divided into several smaller instances of the same problem, ideally about the same size
ii.The smaller instances are solved, typically recursively
iii.If necessary the solutions obtained are combined to get the solution of the original problem

2. State the Master theorem and its use.
If f(n) εθ(nd) where d ≥ 0 in recurrence equation T(n) = aT(n/b)+f(n), then 
    
θ(nd)  if a<bd
  T(n)
  
θ(ndlog n) if a=bd
     
θ(nlogba) if a>bd

The efficiency analysis of many divide-and-conquer algorithms are greatly simplified by the use of Master theorem.

3. What is the general divide-and-conquer recurrence relation?
 An instance of size ‘n’ can be divided into several instances of size n/b, with ‘a’ of them needing to be solved. Assuming that size ‘n’ is a power of ‘b’, to simplify the  analysis, the following recurrence for the running time is obtained:  T(n) = aT(n/b)+f(n)  Where f(n) is a function that accounts for the time spent on dividing the problem into smaller ones and on combining their solutions.

4. What is decrease and conquer approach and mention its variations?
 The decrease and conquer technique based on exploiting the relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem. The three major variations are
  •   Decrease by a constant
  •  Decrease by a constant-factor
  • Variable size decrease

5. What is a tree edge and back edge?
 In the depth first search forest, whenever a new unvisited vertex is reached for the first time, it is attached as a child to the vertex from which it is being reached. Such an edge is called tree edge because the set of all such edges forms a forest. The algorithm encounters an edge leading to a previously visited vertex other than its immediate predecessor. Such an edge is called a back edge because it connects a vertex to its ancestor, other than the parent, in the depth first search forest.

 6. What is a tree edge and cross edge?
 In the breadth first search forest, whenever a new unvisited vertex is reached for the first time, it is attached as a child to the vertex from which it is being reached. Such an edge is called tree edge. If an edge is leading to a previously visited vertex other than its immediate predecessor, that edge is noted as cross edge.

7. What is transform and conquer technique?
The group of design techniques that are based on the idea of transformation is called transform and conquer technique because themethods work as two stage procedures. First in the transformation stage, the problem’s instance is modified to be more amenable (agreeable) to the solution. Then in the second or conquering stage, it is solved.

8. What is greedy technique?
 Greedy technique suggests a greedy grab of the best alternative available in the hope that a sequence of locally optimal choices will yield a globally optimal solution to the entire problem. The choice must be made as follows
i.Feasible : It has to satisfy the problem’s constraints
ii.Locally optimal : It has to be the best local choice among all feasible choices available on that step.
iii.Irrevocable : Once made, it cannot be changed on a subsequent step of the algorithm  

9. What is a state space tree?
The processing of backtracking is implemented by constructing a tree of choices being made. This is called the state-space tree. Its root represents a initial state before the search for a solution begins. The nodes of the first level in the tree represent the choices made for the first component of the solution, the nodes in the second level represent the choices for the second component and so on.

 10. What is a promising node in the state-space tree?
A node in a state-space tree is said to be promising if it corresponds to a partially constructed solution that may still lead to a complete solution. 

11. What is a non-promising node in the state-space tree?
A node in a state-space tree is said to be promising if it corresponds to a partially constructed solution that may still lead to a complete solution; otherwise it is called non-promising. 

12. What do leaves in the state space tree represent?
 Leaves in the state-space tree represent either non-promising dead ends or complete solutions found by the algorithm.

13. What is the manner in which the state-space tree for a backtracking algorithm is constructed?
In the majority of cases, a state-space tree for backtracking algorithm is constructed in the manner of depth-first search. If the current node is promising, its child is generated by adding the first remaining legitimate option for the next component of a solution, and the processing moves to this child. If the current node turns out to be non-promising, the algorithm backtracks to the node’s parent to consider the next possible solution to the problem, it either stops or backtracks to continue searching for other possible solutions.

14. What is a feasible solution and what is an optimal solution?
In optimization problems, a feasible solution is a point in the problem’s search space that satisfies all the problem’s constraints, while an optimal solution is a feasible solution with the best value of the objective function.

15. Define Divide and Conquer algorithm? 
Divide and Conquer algorithm is based on dividing the problem to be solved into several, smaller sub instances, solving them independently and then combining the sub instances solutions so as to yield a solution for the original instance.

16. Mention some application of Divide and Conquer algorithm? 
a. Quick Sort  b. Merge Sort  c. Binary search

17. What are the two stages for heap sort?
Stage 1 : Construction of heap Stage 2 : Root deletion N-1 times 

18. What is divide and conquer strategy ?
In divide and conquer strategy the given  problem is divided into smaller                                                  
Problems and solved  recursively. The conquering phase consists of patching together the answers .  Divide – and – conquer is a very  powerful use of recursion that we will see many times.

19. What do you mean by separate chaining?
Separate chaining is a collision resolution technique to keep the list of all elements that hash to the same value. This is called separate chaining because each hash table element is a separate chain (linked list). Each linked list contains all the elements whose keys hash to the same index.

20. Write the advantage  and Disadvantages of separate chaining.
Adv:
• More number of elements can be inserted as it uses linked lists.

Dis Adv.
• The elements are evenly distributed. Some elements may have more
elements and some may not have anything.
• It requires pointers. This leads to slow the algorithm down a bit because of
the time required to allocate new cells, and also essentially requires the
implementation of a second data structure.

21. What do you mean by open addressing?
Open addressing is a collision resolving strategy in which, if collision occurs alternative cells are tried until an empty cell is found. The cells h0(x), h1(x), h2(x),…. are tried in succession, where hi(x)=(Hash(x)+F(i))mod Tablesize with F(0)=0. The function F is the collision resolution strategy.

22. What do you mean by Probing?
Probing is the process of getting next available hash table array cell.

23. What do you mean by linear probing?
Linear probing is an open addressing collision resolution strategy in which F is a linear function of i, F(i)=i. This amounts to trying sequentially in search of an empty cell. If the table is big enough, a free cell can always be found, but the time to do so can get quite large.

24. What do you mean by primary clustering?
In linear probing collision resolution strategy, even if the table is relatively
empty, blocks of occupied cells start forming. This effect is known as primary
clustering means that any key hashes into the cluster will require several attempts to resolve the collision and then it will add to the cluster.

25. What do you mean by quadratic probing?
Quadratic probing is an open addressing collision resolution strategy in which F(i)=i2. There is no guarantee of finding an empty cell once the table gets half full if the table size is not prime. This is because at most half of the table can be used as alternative locations to resolve collisions.

26. What do you mean by secondary clustering?
Although quadratic probing eliminates primary clustering, elements that hash to the same position will probe the same alternative cells. This is known as secondary clustering.

27. List the limitations of linear probing.
• Time taken for finding the next available cell is large.
• In linear probing, we come across a problem known as clustering.

28. Mention one advantage and disadvantage of using quadratic probing.
Advantage: The problem of primary clustering is eliminated.
Disadvantage: There is no guarantee of finding an unoccupied cell once the table
is nearly half full.

Data Structures and Algorithm-Unit 3

P. A. COLLEGE OF ENGINEERING AND TECHNOLOGY
POLLACHI, COIMBATORE – 642 002.


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


ME – DEGREE COMPUTER SCIENCE AND ENGINEERING


DATA STRUCTURES AND ALGORITHMS_Unit3



Short Questions and Answers
(2 Marks )
Prepared By,
Mr. S. P. Santhoshkumar M.E.,
Assistant Professor,
Department of CSE.

UNIT III – SEARCH STRUCTERS

1. What is binary search?
Binary search is a remarkably efficient algorithm for searching in a sorted array. It works by comparing a search key K with the arrays middle element A[m]. if they match the algorithm stops; otherwise the same operation is repeated recursively for the first half of the array if K < A[m] and the second half if K > A[m].

                                        K  
A[0]………A[m-1] A[m] A[m+1]………A[n-1]
                      search here if K<A[m]                           search here if K>A[m]

2. What is a binary tree extension and what is its use?
 The binary tree extension can be drawn by replacing the empty subtrees by special nodes in a binary tree. The extra nodes shown as little squares are called external & the original nodes shown as little circles called internal. The extension of a empty binary tree is a single external node. The binary tree extension helps in analysis of tree algorithms.

3. What are the classic traversals of a binary tree?
 The classic traversals are as follows
i. Preorder traversal: the root is visited before left & right subtrees 
ii. Inorder traversal: the root is visited after visiting left subtree and before visiting right subtree
iii. Postorder traversal: the root is visited after visiting the left and right subtrees

4. Mention an algorithm to find out the height of a binary tree.
ALGORITHM
Height(T) //Compares recursively the height of a binary tree
//Input: A binary tree T //Output: The height of T
            if T = Φ
return –1
else
return max{Height(TL),
 Height(TR)}+1

5. What are binary search trees and what is it mainly used for?
 Binary search trees is one of the principal data structures for implementing dictionaries. It is a binary tree whose nodes contain elements of a set of orderable items, one element per node, so that all elements in the left subtree are smaller than the element in the subtree’s root and all elements in the right subtree are greater than it.

6. Define AVL trees and who was it invented by?
An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of the node’s left and right subtrees, is either 0 or +1 or –1. the height of an empty subtree is defined as –1. AVL trees were invented in 1962 by two Russian scientists, G.M.Adelson-Velsky and E.M.Landis, after whom the data struture is named. 

7.  Define AVL Tree.
An empty tree is height balanced. If T is a non-empty binary tree with TL and
TR as its left and right subtrees, then T is height balanced if
i) TL and TR are height balanced and
ii) │hL - hR│≤ 1
Where hL and hR are the heights of TL and TR respectively.

8. What are the various transformation performed in AVL tree?  
                   1.single rotation   - single L rotation  - single R rotation 
                   2.double rotation  -LR rotation          -RL rotation

9. What are the categories of AVL rotations?
Let A be the nearest ancestor of the newly inserted nod which has the balancing factor ±2. Then the rotations can be classified into the following four categories:
Left-Left: The newly inserted node is in the left subtree of the left child of A.
Right-Right: The newly inserted node is in the right subtree of the right child of  A.
Left-Right: The newly inserted node is in the right subtree of the left child of A.
Right-Left: The newly inserted node is in the left subtree of the right child of A.

10. What do you mean by balance factor of a node in AVL tree?
The height of left subtree minus height of right subtree is called balance factor of a node in AVL tree.The balance factor may be either 0 or +1 or -1.The height of an empty tree is -1.

11. Write about the efficiency of AVL trees?
As with any search tree , the critical characteristic is the tree’s height. The tree’s height is bounded above and below by logarithmic functions. The height ‘h’ of any AVL tree with ‘n’ nodes satisfies the inequalities
log2 n ≤ h < 1.4405 log2(n+2) – 1.3277
The inequalities imply that the operations of searching and insertion are θ(log n) in the worst case. The operation of key deletion in an AVL tree is more difficult than insertion, but it turns out to have the same efficiency class as insertion i.e., logarithmic

12. What is the minimum number of nodes in an AVL tree of height h?
The minimum number of nodes S(h), in an AVL tree of height h is given
by S(h)=S(h-1)+S(h-2)+1. For h=0, S(h)=1.

13. What are 2-3 trees and who invented them?
A 2-3 tree is a tree that can have nodes of two kinds:2-nodes and 3-nodes. A 2-node contains a single key K and has two children, the left child serves as the root of a subtree whose keys are less than K and the right child serves as the root of a subtree with keys greater than K.  A 3-node contains two ordered keys K1 & K2 (K1<K2). The leftmost child serves as the root of a subtree with keys less than K1, the middle child serves as the root of a subtree with keys between K1 & K2 and the rightmost child serves as the root of a subtree with keys greater than K2. The last requirement of 2-3 trees is that all its leaves must be on the same level, a 2-3 tree is always height balanced. 2-3 trees were introduced by John Hopcroft in 1970.

14. What do you mean by 2-3-4 tree?
A B-tree of order 4 is called 2-3-4 tree. A B-tree of order 4 is a tree that is not
binary with the following structural properties:
• The root is either a leaf or has between 2 and 4 children.
• All non-leaf nodes (except the root) have between 2 and 4 children.
• All leaves are at the same depth.

14. Define B-tree of order M.
A B-tree of order M is a tree that is not binary with the following structural
properties:
• The root is either a leaf or has between 2 and M children.
• All non-leaf nodes (except the root) have between ┌M/2┐ and M children.
• All leaves are at the same depth.


15. What are the applications of B-tree?
• Database implementation
• Indexing on non primary key fields

 

16. Definition of a red-black tree

A red-black tree is a binary search tree which has the following red-black properties:
  1. Every node is either red or black.
  2. Every leaf (NULL) is black.
  3. If a node is red, then both its children are black.
  4. Every simple path from a node to a descendant leaf contains the same number of black nodes.



A basic red-black tree
Basic red-black tree with the sentinel nodes added. Implementations of the red-black tree algorithms will usually include the sentinel nodes as a convenient means of flagging that you have reached a leaf node.
They are the NULL black nodes of property 2.

17. Define splay tree.
A splay tree is a binary search tree in which restructuring is done using a scheme called splay. The splay is a heuristic method which moves a given vertex v to the root of the splay tree using a sequence of rotations.

18. What is the idea behind splaying?
Splaying reduces the total accessing time if the most frequently accessed node is moved towards the root. It does not require to maintain any information regarding the height or balance factor and hence saves space and simplifies the code to some extent.

19. List the types of rotations available in Splay tree.
Let us assume that the splay is performed at vertex v, whose parent and
grandparent are p and g respectively. Then, the three rotations are named as:
i. Zig: If p is the root and v is the left child of p, then left-left rotation at p would
suffice. This case always terminates the splay as v reaches the root after this
rotation.
ii. Zig-Zig: If p is not the root, p is the left child and v is also a left child, then a left-
left rotation at g followed by a left-left rotation at p, brings v as an ancestor of g
as well as p.
iii. Zig-Zag: If p is not the root, p is the left child and v is a right child, perform a
left-right rotation at g and bring v as an ancestor of p as well as g.

20. Define brute force string matching.
The brute force string matching has a given string of n characters called the text and a string of m characters called the pattern, find a substring of the text that matches the pattern. And find the index I of the leftmost character of the first matching substring in the text.

21. What are the advantages of brute force technique?
The various advantages of brute force technique are
i. Brute force applicable to a very wide variety of problems. It is used for many elementary but important algorithmic tasks 
ii.For some important problems this approach yields reasonable algorithms of at least some practical value with no limitation on instance size
iii.The expense to design a more efficient algorithm may be unjustifiable if only a few instances of problems need to be solved and a brute force algorithm can solve those instances with acceptable speed
iv. Even if inefficient in general it can still be used for solving small-size instances of a problem
v. It can serve as a yardstick with which to judge more efficient alternatives for solving a problem

22. What are the properties of binary heap?
i) Structure Property
ii) Heap Order Property