Sunday 2 December 2012

Data Structures and Algorithm-Unit 2

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

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

UNIT II – HEAP STRUCTURES

1. Define 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
i.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.
ii.The parental dominance requirement-The key at each node is greater that or equal to the keys of its children 

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

3. 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. 

4. what  is maxheap?
              If we want the elements in the more typical increasing sorted order,we can change the ordering property so that the parent has a larger key than the child.it is called max heap

5. What do you mean by structure property in a heap?
A heap is a binary tree that is completely filled with the possible exception at the bottom level, which is filled from left to right. Such a tree is known as a complete binary tree.

6. What do you mean by heap order property?
In a heap, for every node X, the key in the parent of X is smaller than (or
equal to) the key in X, with the exception of the root (which has no parent).

7. What are the applications of priority queues?
• The selection problem
• Event simulation

8. 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
i. Finding an item with the highest priority
ii.Deleting an item with highest priority
iii.Adding a new item to the set

9. 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 

10. 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
i. 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
ii.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. 

11. What are the two alternatives that are used to construct a heap?
The two alternatives to construct a heap are 
  •       Bottom-up heap construction
  •       Top-down heap construction

12. What is the algorithm to delete the root’s key from the heap?
ALGORITHM
i.Exchange the root’s key with the last key K of the heap
ii.Decrease the heap’s size by one
iii.“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.

13. What do you mean by the term “Percolate up”?
To insert an element, we have to create a hole in the next available heap location. Inserting an element in the hole would sometimes violate the heap order property, so we have to slide down the parent into the hole. This strategy is continued until the correct location for the new element is found. This general strategy is known as a percolate up; the new element is percolated up the heap until the correct location is found.

14. What do you mean by the term “Percolate down”?
When the minimum element is removed, a hole is created at the root. Since the heap now becomes one smaller, it follows that the last element X in the heap must move somewhere in the heap. If X can be placed in the hole, then we are done.. This is unlikely, so we slide the smaller of the hole’s children into the hole, thus pushing the hole down one level. We repeat this step until X can be placed in the hole. Thus, our action is to place X in its correct spot along a path from the root containing minimum children. This general strategy is known as percolate down.

15. What is meant by Implicit Heaps?

            A particularly simple and beautiful implementation of the heap structure is the implicit heap. Data is simply put into an array x[1...N] and the childrens of element x[i] are defined to be the elements x[2i] and x[2i+1]. Thus the parent of the element c can be found at c/2 (integer division).

16.What is a skew heap?
A heap data structure that is stored in a binary tree (not necessarily complete and balanced). The insertion and deletion of elements in the tree come from merging two skew heaps together in such a way that the heap property is preserved and the tree does not degrade to a linear tree.
17. How to merging 2 skew heaps?
Suppose we are merging a heap containing the elements 2, 5, and 7 with a heap containing the elements 4 and 6.
                    7  <- merge ->    6
                   / \                     /
                  5   2                             4
 i.   Identify the root, thus 7 becomes the new root and the left
   subtree of the heap with root 7 becomes the right subtree of
   the other heap:
                        7          2 <- merge -> 6   (to form the left subtree
                         \                           /     of the new skew heap)
                          5                        4
  ii.

                                           7
                                          / \
                                         6   5
                                          \
                                           4   <- merge -> 2
iii.
                                           7
                                          / \
                                         6   5
                                        / \
                                       2   4 

18. Define Fabinacci Heap.
The Fibonacci heap is a data structure that supports all the basic heap operations in O(1) amortized time, with the exception of delete_min and delete, which take O (log n) amortized time. It immediately follows that the heap operations in Dijkstra's algorithm will require a total of O(|E| + |V| log |V|) time. 

 Two heaps are merged only when it is required to do so. This is similar to lazy deletion. For lazy merging, merges are cheap, but because lazy merging does not actually combine trees, the delete_min operation could encounter lots of trees, making that operation expensive. Any one delete_min could take linear time, but it is always possible to charge the time to previous merge operations. In particular, an expensive delete_min must have been preceded by a large number of unduly cheap merges, which have been able to store up extra potential. 


20. Write the amortized analysis of Lazy Binomial Queues
To carry out the amortized analysis of lazy binomial queues, we will use the same potential function that was used for standard binomial queues. Thus, the potential of a lazy binomial queue is the number of trees.


21. Binomial heaps:
A set of binomial trees satisfying the following:
1.  Each binomial tree in H is heap-ordered:
            the key of a node is greater than or equal to the key of its parent
2. There is at most one binomial tree in H whose root has a given degree

22.Write the Dijkstra’s shortest path algorithm

Let G = (V,E) be a weighted (weights are non-negative) undirected graph, let s Î V. Want to find the distance (length of the shortest path), d(s,v) from s to every other vertex.




Data Structures and Algorithm-Unit 1

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
Short Questions and Answers
(2 Marks )
Prepared By,
Mr. S. P. Santhoshkumar M.E.,
Assistant Professor,
Department of CSE.

UNIT I – COMPLEXITY ANALYSIS & ELEMENTARY DATA STRUCTURES

1. What do asymptotic notation means?
Asymptotic notations are terminology that is introduced to enable us to
make meaningful statements about the time and space complexity of an
algorithm. The different notations are
Big – Oh notation
Omega notation
Theta notation.

2. What are the basic asymptotic efficiency classes?
The various basic efficiency classes are
Constant : 1
Logarithmic : log n
Logarithmic : log n
Linear : n
N-log-n : nlog n
Quadratic : n2
Cubic : n3
Exponential : 2n
Factorial : n!

3. Define O-notation?
A function t(n) is said to be in O(g(n)), denoted by t(n)
O(g(n)), if t(n) is bounded above by some constant multiple of g(n) for all large n,
i.e., if there exists some positive constant c and some nonnegative integer
n0 such that T(n) ” cg(n) for all n • n0

4. What are exponential growth functions?
The functions 2n and n! are exponential growth functions, because these
two functions grow so fast that their values become astronomically large even for
rather smaller values of n.

5. What is worst-case efficiency?
The worst-case efficiency of an algorithm is its efficiency for the worstcase
input of size n, which is an input or inputs of size n for which the algorithm
runs the longest among all possible inputs of that size.

6. What is best-case efficiency?
The best-case efficiency of an algorithm is its efficiency for the best-case
input of size n, which is an input or inputs for which the algorithm runs the fastest
among all possible inputs of that size.

7. What is average case efficiency?
The average case efficiency of an algorithm is its efficiency for an average
case input of size n. It provides information about an algorithm behavior on a
“typical” or “random” input.

8. What is amortized efficiency?
In some situations a single operation can be expensive, but the total time
for the entire sequence of n such operations is always significantly better that the
worst case efficiency of that single operation multiplied by n. this is called
amortized efficiency.

9. What is the formula used in Euclid’s algorithm for finding the greatest
common divisor of two numbers?
Euclid’s algorithm is based on repeatedly applying the equality
Gcd(m,n)=gcd(n,m mod n) until m mod n is equal to 0, since gcd(m,0)=m.

10. Define articulation points.
If a graph is not biconnected,the vertices whose removal would disconnect
the graph are known as articulation points.

11. Give some example of NP complete problems.
i. Hamiltonian circuit.
ii. Travelling salesmen problems
iii. Longest path problems
iv. Bin packing
v. Knapsack problem
vi. Graph colouring problem

12. What is the recurrence relation to find out the number of multiplications
and the initial condition for finding the n-th factorial number?
The recurrence relation and initial condition for the number of
multiplications is
M(n)=M(n-1)+1 for n>0 M(0)=0

13. What is the basic operation in the Tower of Hanoi problem and give the
recurrence relation for the number of moves?
The moving of disks is considered the basic operation in the Tower of
Hanoi problem and the recurrence relation for the number of moves is given as
M(n)=2M(n)+1 for n>1 M(1)=1

14. Who introduced the Fibonacci numbers and how can it be defined by a
simple recurrence?
Leonardo Fibonacci introduced the fibonacci numbers in 1202 as a
solution to a problem about the size of rabbit population. It can be defined by the
simple recurrence F(n)=F(n-1)+F(n-2) for n>1 And two initial conditions F(0)=0
and F(1)=1

15. What is the explicit formula for the nth Fibonacci number?
The formula for the nth Fibonacci number is given by
F(n)= 1/5(Φn - Φn)
Where
Φ =(1+5)/2
Φ =(1-5)/2

16. Define Data Structures
Data Structures is defined as the way of organizing all data items that
consider not only the elements stored but also stores the relationship between
the elements.

17. Define Linked Lists
Linked list consists of a series of structures, which are not necessarily
adjacent in memory. Each structure contains the element and a pointer to a
structure containing its successor. We call this theNext Pointer. The last
cell’sNext pointer points to NULL.

18. State the different types of linked lists
The different types of linked list include singly linked list, doubly linked list and
circular linked list.

19. List out the advantages of using a linked list
• It is not necessary to specify the number of elements in a linked list during its
declaration
• Linked list can grow and shrink in size depending upon the insertion and
deletion that occurs in the list
• Insertions and deletions at any place in a list can be handled easily and
efficiently
• A linked list does not waste any memory space

20. List out the applications of a linked list
Some of the important applications of linked lists are manipulation of
polynomials, sparse matrices, stacks and queues.

21. State the difference between arrays and linked lists

22. What do you mean by balanced trees?
Balanced trees have the structure of binary trees and obey binary search
tree properties. Apart from these properties, they have some special constraints,
which differ from one data structure to another. However, these constraints are
aimed only at reducing the height of the tree, because this factor determines the
time complexity.
Eg: AVL trees, Splay trees.

23. What is the use of threaded binary tree?
In threaded binary tree, the NULL pointers are replaced by some
addresses. The left pointer of the node points to its predecessor and the right
pointer of the node points to its successor.

24.Construction of expression trees?
1.convert the given infix expression into postfix notation
2. Create a stack and read each character of the expression and push into the
stack, if operands are encountered.
3.when an operator is encountered pop 2 values from the stack. From a tree
using the operator.

25. Why you need a data structure?
A data structure helps you to understand the relationship of one data
element with the other and organize it within the memory. Sometimes the
organization might be simple and can be very clearly visioned. Eg) List of names
of months in a year –Linear Data Structure, List of historical places in the world-
Non-Linear Data Structure. A data structure helps you to analyze the data, store
it and organize it in a logical and mathematical manner.

26. List the basic operations carried out in a linked list
The basic operations carried out in a linked list include:
• Creation of a list
• Insertion of a node
• Deletion of a node
• Modification of a node
• Traversal of the list