Download SCSX1005_SEMIII_DS

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
DATA STRUCTURES AND ALGORITHMS
SCSX1005
Question Bank
UNIT 1 - INTRODUCTION TO ALGORITHMS & LINEAR DATA STRUCTURE
Part – A
1. State the differences between linear and non-linear data structures.
Linear data structures are those in which the elements form a sequence whereas non-linear
data structures are those in which the elements do not form a sequence.
2. Define an Array.
The collection of similar data items grouped together which shares the common name
3. Define Linked List.
A Linked list is a collection of elements called nodes, each of which stores two items called info
and link. Info is an element of the list and a link is a pointer to the next element.
4. What is cursor implementation of linked list
Some languages do not support pointers. So, there need to be find a solution that can replace
pointers and this solution is to write pointers as indexes of arrays.
5. Define Stack
A stack is a data structure in which addition of new element or deletion of an existing element
always takes place at the same end. This end is often known as top of stack. Stack is also called
as Last-In-First-Out (LIFO) list because the element which was placed most recently will be
deleted first
6. Define Queue.
Queue is a linear data structure that permits insertion of new element at one end and deletion of
an element at the other end. The end at which the deletion of an element take place is called
front, and the end at which insertion of a new element can take place is called rear. The deletion
or insertion of elements can take place only at the front or rear end of the list respectively.
7. State some application of Stacks
1. Conversion of infix expression to postfix expression
2. Evaluation of expression entered in postfix form
3. Towers of Hanoi
8. What is a priority queue?
A priority queue is different from a "normal" queue, because instead of being a "first-in-firstout" data structure, values come out in order by priority.
9. Define Algorithm.
A formula or set of steps for solving a particular problem. It is independent of any programming
language.
10. What is time and space complexity of an algorithm?
Time complexity: Time complexity of the program is defined as the amount of computer time it
needs to run to completion
Space complexity: The Space complexity of a program is defined as the amount of memory it
needs to run to completion
Part – B
1. Explain in detail about performance analysis of an algorithm
2. Describe the creation, insertion and deletion operations in singly linked lists.
3. Explain the operations on the array implementation of a stack
4. Explain the operations on the array implementation of a linear queue.
5. Explain in detail about the array implementation of Circular Queue
UNIT II -TREES
Part – A
1. Define AVL trees.
The purpose of going for a binary search tree is to make the searching efficient. But when the
elements are added to the binary search tree in such a way that one side of the tree becomes
heavier, then the searching becomes inefficient. The very purpose of going for a binary search
tree is not served. Hence we try to adjust this unbalanced tree to have nodes equally
distributed on both sides. This is achieved by rotating the tree using standard algorithms called
the AVL rotations. After applying AVL rotation, the tree becomes balanced and is called the AVL
tree or the height balanced tree.
2. What is a Binary search tree?
A Binary tree T is a Binary Search Tree (BST), if each node N of T has the following property : The
value at N is greater than every value in the left subtree of N and is less than every value in the
right subtree of N.
3. What are the various traversals in a Binary Tree?
There are three standard ways of traversing a binary tree T with root R. They are:
( i ) Preorder Traversal
(ii ) Inorder Traversal
(iii) Postorder Traversal
4. What is a B tree?
B – tree is a m-way search tree of order n that satisfies the following conditions
(i) All non-leaf nodes (except root node) have at least n/2 children and maximum n children
(ii) The non-leaf root node may have at least 2 children and maximum n children
(iii) B-Tree can exist with only one node. i.e. the root node containing no child
(iv) If a node has n children then it must have n-1 values
(v) All the values that appear on the left most child of a node are smaller than the first value
of that node. All values that appears on the right most child of a node are greater that
the last values of that node
5. Define level of a node
Level of a node is defined by letting root at level one. If a node is at level L, then its children are
at level L +1.
6. What is Balance Factor of a node?
The balance factor of a node is defined to be the difference between the height of the node’s
left subtree and the height of the node’s right subtree
7. Define leaf node and siblings of a node
Leaf: A node with no children is called a leaf
Siblings: Children of the same parent are said to be siblings
8. What are Tries?
A trie is a data structure that stores the information about the contents of each node in the path
from the root to the node, rather than the node itself.
9. Explain array representation of a Binary tree
The root element is always stored in position 1. The left child of node i is stored in position 2i
and right child of node is stored in position 2i + 1. Hence the following formulae can be used to
identify the parent, left child and right child of a particular node.
10. Explain the linked list representation of a Binary tree.
In linked representation of binary trees, instead of arrays, pointers are used to connect the
various nodes of the tree. Hence each node of the binary tree consists of three parts namely,
the info, left and right. The info part stores the data, left part stores the address of the left child
and the right part stores the address of the right child. Logically the binary tree in linked form
can be represented as shown.
Part – B
1. Explain the various tree traversal techniques in detail
2. Explain the creation and insertion of a node in a Binary search tree
3. Explain the deletion operation in Binary search tree
4. Explain in detail about B-trees
5. Explain AVL trees in detail.
UNIT III - GRAPHS
Part – A
1. Define graph.
Graph: A graph G is a defined as a set of objects called nodes and edges.
G = (V, E)
A graph G consists of two things:
1. A set V of elements called nodes (or points or vertices)
2. A set E of edges such that each edge e in E is identified with a unique pair [u,v] of nodes in V,
denoted by e = [u,v]
2. Define indegree and outdegree of a node
Indegree: The number of arcs entering the node is called indegree of that node
Outdegree : The number of arcs exiting from the node is called outdegree of that node.
3. What is a source node?
Source node: A node where the indegree is 0 but has a positive value for outdegree is called a
source node. That is there are only outgoing arcs to the node and no incoming arcs to the node.
4. What is a sink node?
Sink node: A node where the outdegree is 0 and has a positive value for indegree is called the
sink node. That is there is only incoming arcs to the node and no outgoing arcs the node.
5. Define directed and undirected graph?
A Directed graph or a Digraph is a graph in which the edges are directionally oriented towards
any node.
An undirected graph is a graph in which the edges are not directionally oriented towards a node.
6. Define degree of a node.
The degree of the node B in the undirected graph shown above is 3.
7. Define cyclic graph.
A cycle in a directed graph is a directed path that originates and terminates at the same node
8. What are the two graph traversing techniques?
There are two methods for traversing through the nodes of the graph. They are:
(1)
Breadth First Search Traversal (BFS)
(2)
Depth First Search Traversal (DFS)
9. Define minimum spanning tree.
Spanning Tree: Any tree consisting of edges in the graph G and including all vertices in G is
called a spanning tree. If the edges are choosen in such a way that the over all cost is minimum
then it is called as a minimum spanning tree.
10. Define adjacency matrix of a graph.
Adjacency matrix representation
The graphs can be represented using Adjacency matrix or otherwise called the incidence
matrix. Since the matrix is so sparse it is also called as sparse matrix.
The adjacency matrix is a N X N matrix where N is the number of nodes in the graph. Each entry
(I, J) in the matrix has either 1 or 0. An entry 1 indicates that there is a direct connection from I to
J. An entry 0 indicates that there is no direct connection from I to J.
Part-B
1. Explain Kruskal’s algorithm with an example.
2. Explain Prim’s algorithm with an example.
3. Explain dijkstra’s algorithm for determining single source shortest path.
4. Explain Floyd’s algorithm with an example.
5. Explain graph traversal algorithms.
UNIT IV -SORTING & SEARCHING
PART-A
1. What is the difference between external sorting and Internal sorting
Internal sorting methods are the methods that can be used when the list to be sorted is small
enough so that the entire sort can be carried out in main memory
External sorting methods are the methods to be used when the list to be sorted is large and
cannot be accommodated entirely in the main memory. In this case some of the data is present
in the main memory and some is kept in auxiliary memory such as hard disk, floppy disk, tape,
etc.
2. What id the difference between Binary search and Linear search
Linear Search is simple and straight forward method.
Binary search is a bit complicated
Linear search can be applied on both sorted and unsorted list.
Binary search applied to only sorted list
3. What is the time complexities of different sorting algorithms?
4. What is the working principle of a Binary search
The data item to be searched is compared with the approximate middle entry of the list. If it
matches with the middle entry, then the position is returned. If the data item to be searched is
lesser than the middle entry, then it is compared with the middle entry of the first half of the list
and procedure is repeated on the first half until the required item is found. If the data item is
greater than the middle entry, then it is compared with the middle entry of the second half of the
list and procedure is repeated on the second half until the required item is found. This process
continues until the desired number is found or the search interval becomes empty
5. What is the working principle of a Insertion sort
In Insertion Sort algorithm, each element A[K] in the list is compared with all the elements before
it ( A[1] to A[K-1]). If any element A[I] is found to be greater than A[K] then A[K] is inserted in the
place of A[I}. This process is repeated till all the elements are sorted.
6. What is the working principle of a Quick sort
A pivotal item near the middle of the list is chosen, and then items on either side are moved so
that the data items on one side of the pivot element are smaller than the pivot element, whereas
those on the other side are larger. The middle or the pivot element is now in its correct position.
This procedure is then applied recursively to the 2 parts of the list, on either side of the pivot
element, until the whole list is sorted
7. What is the working principle of a Merge sort
The given list is divided into two roughly equal parts called the left and the right subfiles. These
subfiles are sorted using the algorithm recursively and then the two subfiles are merged together
to obtain the sorted file
8. What is a Heap?
A Heap is a compete binary tree with the property that the value at each node is at least as large
as ( or as small as ) the values at its children (if they exist). If the value at the parent node is
larger than the values on its children then it is called a Max heap and if the value at the parent
node is smaller than the values on its children then it is called the Min heap
9. What is a disk sort and Tape sort
In disk sort, the large lists present in the disk drive is loaded to the memory segment by segment
and then sorted. The term block refers to the unit of data that is read from or written to a disk at
one time. A block generally consists of several records. For a disk, there are three factors
contributing to the read/ write time. They are seek time, latency time and transmission time
The basic strategy of tape sort is to distribute ordered initial runs of predetermined size on the
available tapes and then to repeatedly merge these runs in multiple phases in which each phase
has a predetermined number of merges before a new target tape is selected.
10. What are the advantages and disadvantages of merge sort
Advantages:
Very useful for sorting bigger lists.
Applicable for external sorting also.
Disadvantages:
Needs a temporary array every time, for storing the new sorted list.
Part-B
1. Explain Merge sort algorithm with an example.
2. Explain Quick sort algorithm with an example.
3.
Explain Heap sort algorithm with an example
4. Write a detailed note on two of the external sorting techniques
5. What is Hashing? Write some hash functions. Explain about the different collision resolution
strategies.
UNIT V ALGORITHM DESIGN TECHNIQUES
PART-A
1. Define divide and conquer?
In Divide and conquer method , a given problem is
i)
Divided into smaller sub problems
ii)
These sub problems are solved independently
iii)
The solutions to the sub problems are combined to the get the solution to the original
problem
2. State the greedy algorithm
In Greedy algorithm,
(i)
(ii)
(iii)
(iv)
(v)
We attempt to construct an optimal solution in stages.
At each stage we make decision that appears to be the best at that time.
A decision made in one stage is not changed in a later stage.
The criterion used to make the greedy decision at each stage is called the
Greedy criterion.
At each stage, a decision is made regarding whether a particular input is an
optimal solution
3. List any two applications of greedy algorithm
Following are the problems that can be solved using greedy method
1. Job sequencing
2. Finding minimum spanning tree
3. Knapsack problem
4. Travelling salesman Problem
4. What is Dynamic programming? Give two examples
Dynamic programming is an algorithm that can be used when the solution to a problem can
be viewed as the result of a sequence of decisions
Various applications of dynamic programming are:
1. Travelling sales man problem
2. 0/1 knap sack Problem
3. Optimal Binary search tree
5. State the principle of optimality
A problem is said to satisfy the Principle of Optimality if the sub solutions of an optimal
solution of the problem are themselves optimal solutions for their sub problems.
6. Differentiate between greedy method and Dynamic programming
Greedy
Method
No
Overlapping of
1 subproblems
Dynamic
Programming
Overlapping
subproblems exist
Decision taken
once cannot
be reverted
2 back
Decision taken once
can be reverted back
Locally optimal
3 solution obtain
Globally optimal
solution obtained
7. Differentiate divide and conquer and branch and bound
Divide and
Conquer
Branch and bound
Problem
divided into
1 subproblems
Problem solved using
BFS and DFS
Applications:
Quick sort,
2 binary search
Applications:
Travelling sales man
Problem
Knap sack problem
8. Differentiate between Branch and bound and Backtracking
i) Branch-and-Bound algorithms can be applied only to optimization problems.
Branch and Bound uses BFS t o generate the tree
Backtracking is more often not applied to non-optimization.
(ii) Branch-and-bound generates nodes by Best-first rule.
Backtracking generates tree by depth-first rule.
9. Define Np hard and Np complete
Np hard problems: A problem D is called NP hard if a polynomial time algorithm for D would
imply a polynomial time algorithm for every problem in NP
Np complete: A problem D is called Np complete if
i)
It belongs to class NP
ii)
Every problem in NP can also be solved in polynomial time
10. What are the drawbacks of greedy algorithm
In greedy algorithm only locally optimal solutions are obtained. Greedy always chooses the
best solution at that point of time. It may lead to worst long term outcome.
PART-B
1. Explain the greedy method with an example.
In Greedy algorithm,
(i)
(ii)
(iii)
We attempt to construct an optimal solution in stages.
At each stage we make decision that appears to be the best at that time.
A decision made in one stage is not changed in a later stage.
(1) KNAPSACK PROBLEM
In the knapsack problem, you are given with a sack of capacity m. There is n number of objects
provided. The weights of each object is given in the form of an array w[ ]. The profit of each object is
provided using the array p[ ]. Now you have to fill the sack with these objects, in such a way that the total
weight of the objects put into the sack should be lesser than or equal to the capacity of the sack. We
should select the objects in such a way that we get maximum profit.
Given:




n  Number of objects
m  Total capacity of the sack
w[ ]  weights of the objects
p[ ]  profits of the objects
Aim of the problem:

Fill the sack up to maximum capacity such that the total profit of selected objects is
maximized.
i n
x p
i 1
i
i
is maximized
Constraint:

Total weight of the selected objects should not exceed the maximum capacity of the
sack.
in
xw m
i 1
i
i
where x = { 0.. 1} or x = {0, 1} 0/1 Knapsack problem.
Solution:
Consider for example the following inputs for the Knapsack problem.
n =3
m = 20
w = {5, 10, 15}
p = {50, 30, 30}
Please note that the inputs for the w[ ] and p[ ] are given in such a way that p/w is in descending order.
That is the object with maximum profit per unit weight will appear first. Since this problem used a greedy
approach, the objects are taken in the order of their profit per unit weight, so that maximum profit can be
obtained.
Now select the objects one by one.
Iteration 1
Initially the total weight TW and the total profit TP are zero. Now select the first object. Compare
its weight with the capacity of the sack.
5 < 20
Since the weight of the object does not exceed the capacity of the sack, this can be put inside the sack.
To indicate this set the first position of X[ ] as 1.
X = { 1, 0, 0 }
Now the remaining capacity of the sack becomes m = 15.
Iteration 2
Take the second object. Compare its weight with the available capacity of the sack.
10 < 15
Since the weight of the object does not exceed the capacity of the sack, this can be put inside the sack.
To indicate this set the second position of X[ ] as 1.
X = { 1, 1, 0 }
Now the remaining capacity of the sack becomes m = 5.
Iteration 3
Take the third object. Compare its weight with the available capacity of the sack.
15 > 5
Since the weight of the object is exceeding the available capacity of the sack, we cannot put the whole
object inside the sack. But a fraction of that which fits into the sack exactly can be taken. That is 5 / 15
 1/3 of the third object can be put in the sack. Hence the final solution is,
X = { 1, 1, 0.33 }
Total profit TP = 90, Total weight = 20
Some Knapsack problems does not allow objects to be taken as fraction. The values of X allowed are
only 0 and 1. These type of knapsack problems are called the 0/1 Knapsack problem. For these the
solution for the above problem may appear like this.
X = { 1, 1, 0 }
Total profit TP = 80, Total weight = 15
Algorithm
GREEDYKNAPSACK( n, m, w[ ], p[ ], x[ ] )
For i = 1 to n
x[i] = 0
End for
u=m
For i = 1 to n
If w[i] > u
Break
Else
x[i] = 1
u = u – w[i]
End if
End for
If i ≤ n
x[i] = u / w[i]
End if
End GREEDYKNAPSACK
2. Explain Divide and conquer strategy with an example
3. Explain dynamic programming by considering a travelling sales man problem
4. Explain N-queens problem and write the algorithm
5. Explain the Branch and Bound technique with an example.
*********************************END***************************