UNIT IV - SNS Courseware

UNIT IV
ALGORITHMIC TECHNIQUES
1. 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 the
methods 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.
2. What are the three variations by which a given instance of a problem is
transformed?
The three major variations are
 Transformation to a simpler or more convenient instance of the same
problem called instance simplification
 Transformation to a different representation of the same instance
called representation change
 Transformation to an instance of a different problem for which the
algorithm is already available called problem reduction.
3. What is presorting?
Presorting is the idea of sorting a list so that problems can be solved
more easier than in an unsorted list. The time efficiency of the algorithm that
involve sorting before solving the problem depends on the sorting algorithm
being used.
4. Give the algorithm for element uniqueness using presorting?
5. Compare the efficiency of solving the problem of element uniqueness
using presorting and without sorting the elements of the array?
The brute force algorithm compares pairs of the array’s elements until
either two equal elements were found or no more pairs were left. It’s worst
case efficiency was in q(n2).
The running time of the presorted algorithm depends on the time
spent on sorting and the time spent on checking consecutive elements. The
worst case efficiency of the entire presorting based algorithm will be as
follows
6. Give the algorithm for computing the mode using presorting.
7. Compare the efficiencies of the algorithms used to compute the mode
before and after sorting the array of elements.
The efficiency of computing the mode before sorting the array for the
worst case is a list with no equal elements. For such a list the ith element is
compared with i-1 elements of the auxiliary list of distinct values seen so far
before being added to the list with a frequency of 1.
The worst case number of comparisons made by the algorithm in
creating the frequency list is q(n2).
The analysis of the presorted algorithm will be dominated by the time
spent on sorting the list, since the remainder time to search the frequency list
takes only linear time. So the efficiency class for the algorithm is q(n log n).
8. 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
structure is named.
9. What are binary search trees? Mention its uses.
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.
10. What is a rotation in AVL tree used for?
If an insertion of a new node makes an AVL tree unbalanced, the tree
is transformed by a rotation. A rotation in an AVL tree is a local
transformation of its subtree rooted at a node whose balance has become
either +2 or –2; if there are several such nodes, then the tree rooted at the
unbalanced node that is closest to the newly inserted leaf is rotated.
11. What are the types of rotation?
There are four types of rotations, in which two of them are the mirror
images of the other two rotations.
The four rotations are
 Single right rotation or R-rotation
 Single left rotation or L-rotation
 Double left-right rotation or LR-rotation
 Double right-left rotation or RL-rotation
12. 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 q(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.
13. What are the drawbacks of AVL trees?
The drawbacks of AVL trees are
 Frequent rotations
 The need to maintain balances for the tree’s nodes
 Overall complexity, especially of the deletion operation.
14. 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 3nodes. 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.
15. What is a heap?
A heap is a partially ordered data structure, and can be defined as a
binary tree assigned to its nodes, one key per node, provided the following
two conditions are met
 The tree’s shape requirement-The binary tree is essentially complete,
that is all the leaves are full except possibly the last level, where only
some rightmost leaves will be missing.
 The parental dominance requirement-The key at each node is greater
that or equal to the keys of its children
16. What is the main use of heap?
Heaps are especially suitable for implementing priority queues.
Priority queue is a set of items with orderable characteristic called an item’s
priority, with the following operations
 Finding an item with the highest priority
 Deleting an item with highest priority
 Adding a new item to the set

17. Give three properties of heaps?
The properties of heap are
 There exists exactly one essentially complete binary tree with ‘n’
nodes. Its height is equal to log2n
 The root of the heap is always the largest element.
 A node of a heap considered with all its descendants is also a heap
18. Give the main property of a heap that is implemented as an array.
A heap can be implemented as an array by recording its elements in
the top-down, left-to-right fashion. It is convenient to store the heap’s
elements in positions 1 through n of such an array.
In such a representation
 The parental node keys will be in the first n/2 positions of the
array,
while the leaf keys will occupy the last n/2 positions
 The children of a key in the array’s parental position ‘i’ (1 £ i£ n/2)
will be in positions 2i and 2i+1and correspondingly, the parent of
the key in position ‘i’ (2£ i£n) will be in position i/2.
19. What are the two alternatives that are used to construct a heap?
The two alternatives to construct a heap are
o Bottom-up heap construction
o Top-down heap construction
20. Give the pseudocode for Bottom-up heap construction.
ALGORITHM HeapBottomUp(H[1..n])
21. What is the algorithm to delete the root’s key from the heap?
ALGORITHM
 Exchange the root’s key with the last key K of the heap
 Decrease the heap’s size by one
 “Heapify” the smaller tree by sifting K down the tree exactly in
the same way as bottom-up heap construction. Verify the
parental dominance for K: if it holds stop the process, if not
swap K with the larger of its children and repeat this operation
until the parental dominance holds for K in its new position.
22. Who discovered heapsort and how does it work?
Heapsort was discovered by J.W.J. Williams. This is a two stage
process that works as follows
 Stage 1 Heap construction: construct a heap for a given array.
 Stage 2 Maximum deletions: Apply the root deletion operation n-1
times to the remaining heap.
23. What is dynamic programming and who discovered it?
Dynamic programming is a technique for solving problems with
overlapping subproblems. These subproblems arise from a recurrence
relating a solution to a given problem with solutions to its smaller
subproblems only once and recording the results in a table from which the
solution to the original problem is obtained. It was invented by a prominent
U.S Mathematician, Richard Bellman in the 1950s.
24. Define transitive closure.
25. What is the formula used by Warshall’s algorithm?
26. Give the Warshall’s algorithm.
27. Give the Floyd’s algorithm
28. How many binary search trees can be formed with ‘n’ keys?
The total number of binary search trees with ‘n’ keys is equal to the
nth Catalan numberc(n)
29. Give the algorithm used to find the optimal binary search tree.
30. 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
 Feasible : It has to satisfy the problem’s constraints
 Locally optimal : It has to be the best local choice among all feasible
choices available on that step.
 Irrevocable : Once made, it cannot be changed on a subsequent step
of the algorithm
31. Mention the algorithm for Prim’s algorithm.
32. What are the labels in Prim’s algorithm used for?
Prim’s algorithm makes it necessary to provide each vertex not in the
current tree with the information about the shortest edge connecting the
vertex to a tree vertex. The information is provided by attaching two labels
to a vertex
o The name of the nearest tree vertex
o The length of the corresponding edge
33. How are the vertices not in the tree split into?
The vertices that are not in the tree are split into two sets
 Fringe : It contains the vertices that are not in the tree but are adjacent
to atleast one tree vertex.
 Unseen : All other vertices of the graph are called unseen because
they are yet to be affected by the algorithm.
34. What are the operations to be done after identifying a vertex u* to be
added to the tree?
After identifying a vertex u* to be added to the tree, the following two
operations need to be performed
Move u* from the set V-VT to the set of tree vertices VT.
For each remaining vertex u in V- VT that is connected to u* by a
shorter edge than the u’s current distance label, update its labels by u*
and the weight of the edge between u* and u, respectively.
35. What is a min-heap?
A min-heap is a mirror image of the heap structure. It is a complete
binary tree in which every element is less than or equal to its children. So the
root of the min-heap contains the smallest element.
36. What is the use of Kruskal’s algorithm and who discovered it?
Kruskal’s algorithm is one of the greedy techniques to solve the
minimum spanning tree problem. It was discovered by Joseph Kruskal when
he was a second-year graduate student.
37. Give the Kruskal’s algorithm.
38. What is a subset’s representative?
One element from each of the disjoint subsets in a collection is used
as the subset’s representative. Some implementations do not impose any
specific constraints on such a representative, others do so by requiring the
smallest element of each subset to be used as the subset’s representative.
39. What is the use of Dijksra’s algorithm?
Dijkstra’s algorithm is used to solve the single-source shortest-paths
problem: for a given vertex called the source in a weighted connected graph,
find the shortest path to all its other vertices.
The single-source shortest-paths problem asks for a family of paths,
each leading from the source to a different vertex in the graph, though some
paths may have edges in common.
40. What is encoding and mention its types?
Encoding is the process in which a text of ‘n’ characters from some
alphabet is assigned with some sequence of bits called codewords. There are
two types of encoding they are
 Fixed-length encoding
 Variable-length encoding
41. What is the problem faced by variable-length encoding and how can it
be avoided?
Variable-length encoding which assigns codewords of different
lengths to different characters introduces a problem of identifying how many
bits of an encoded text represent the first character or generally the ith
character. To avoid this prefix-free codes or prefix codes are used. In prefix
codes, no codeword is a prefix of a codeword of another character.
42. Mention the Huffman’s algorithm.
ALGOITHM Huffman
1. Initialize n one-node trees and label them with the characters of the
alphabet. Record the frequency of each character in its tree’s root to
indicate the tree’s weight.
2. Repeat the following operation until a single tree is obtained. Find two
trees with the smallest weight. Make them the left and right subtree of
a new tree and record the sum of their weights in the root of the new
tree as its weight.
A tree constructed by the above algorithm is called Huffman tree and it
defines the Huffman code