Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Data Structure

Syllabus :-
Time and space complexity, Data Structures – Introduction to Data Structures, abstract data types,
Linear list – singly linked list implementation, insertion, deletion and searching operations on linear
list, circular linked list implementation, Double linked list implementation, insertion, deletion and
searching operations. Applications of linked lists.

Stacks-Operations, array and linked representations of stacks, stack applications -infix to postfix
conversion, postfix expression evaluation, recursion implementation.

Queues-operations, array and linked representations. Circular Queue operations, Dequeues,


applications of queues.

Searching and Sorting – Sorting- selection sort, bubble sort, insertion sort, quick sort, merge sort, shell sort,
radix sort, Searching-linear and binary search methods, comparison of sorting and searching methods

Trees – Definitions, tree representation, properties of trees, Binary tree, Binary tree representation, binary tree
properties, binary tree traversals, binary tree implementation, applications of trees.
A data structure is a storage that is used to store and organize data. It is a way of arranging data on
a computer so that it can be accessed and updated efficiently.

● Linear data structure: Data structure in which data elements are arranged sequentially or
linearly, where each element is attached to its previous and next adjacent elements, is called a
linear data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
○ Static data structure: Static data structure has a fixed memory size. It is easier to
access the elements in a static data structure.
An example of this data structure is an array.
○ Dynamic data structure: In dynamic data structure, the size is not fixed. It can be
randomly updated during the runtime which may be considered efficient concerning
the memory (space) complexity of the code.
Examples of this data structure are queue, stack, etc.
● Non-linear data structure: Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all
the elements in a single run only.
Examples of non-linear data structures are trees and graphs.
Popular types of Data Structures:

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of
the same type together. This makes it easier to calculate the position of each element by simply adding an offset
to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the
array).

1. It is a group of variables of similar data types referred to by a single element.


2. Its elements are stored in a contiguous memory location.
3. The size of the array should be mentioned while declaring it.
4. Array elements are always counted from zero (0) onward.
5. Array elements can be accessed using the position of the element in the array.
6. The array can have one or more dimensions.

Advantages:-
● Code Optimization: we can retrieve or sort the data efficiently.
● Random access: We can get any data located at an index position.
Disadvantages:-

● Size Limit: We can store only the fixed size of elements in the array. It doesn’t grow its size at runtime.

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.
a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the
list.

Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the
end. A circular linked list can be a singly circular linked list or doubly circular linked list.

Advantages of Circular Linked Lists:

1) Any node can be a starting point. We can traverse the whole list by starting from any point. We just need to
stop when the first visited node is visited again.
2) Useful for implementation of queue. Unlike this implementation, we don’t need to maintain two pointers for
front and rear if we use circular linked list. We can maintain a pointer to the last inserted node and front can
always be obtained as next of last.

Circular Doubly Linked Lists are used for implementation of advanced data structures like Fibonacci Heap.

Stack Data Structure

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be
LIFO(Last In First Out) or FILO(First In Last Out).

A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In
First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came
first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most
recently added; in a queue, we remove the item the least recently added.

Queue is used when things don’t have to be processed immediately, but have to be processed in First
In First Out order like Breadth First Search.

This property of Queue makes it also useful in following kind of scenarios.

1. When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk
Scheduling.
2. When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes. Examples include IO Buffers, pipes, file IO, etc.
3. In Operating systems:
○ Semaphores
○ FCFS ( first come first serve) scheduling, example: FIFO queue
○ Spooling in printers
○ Buffer for devices like keyboard
4. In Networks:
○ Queues in routers/ switches
○ Mail Queues
Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated
with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in
which they are removed). If in any case the elements have same priority, they are served as per their ordering in the
queue.

A Priority Queue is different from a normal queue, because instead of being a “first-in-first-out”, values come out in order
by priority. It is an abstract data type that captures the idea of a container whose elements have “priorities” attached to
them.

Dijkstra’s Shortest Path Algorithm using priority queue: When the graph is stored in the form of adjacency list or
matrix, priority queue can be used to extract minimum efficiently when implementing Dijkstra’s algorithm.

Prim’s algorithm: It is used to implement Prim’s Algorithm to store keys of nodes and extract minimum key node at every
step.

Data compression : It is used in Huffman codes which is used to compresses data.

Heap Sort : Heap sort is typically implemented using Heap which is an implementation of Priority Queue.

Applications of Deque: Since Deque supports both stack and queue operations, it can be used as both.
The Deque data structure supports clockwise and anticlockwise rotations in O(1) time which can be
useful in certain applications.

A Circular Queue is a special version of queue where the last element of the queue is connected to the first
element of the queue forming a circle. The operations are performed based on FIFO (First In First Out) principle. It is
also called ‘Ring Buffer’.

Operations on Circular Queue:

● Front: Get the front item from queue.


● Rear: Get the last item from queue.
● enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is
always inserted at Rear position.
1. Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).
2. If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is
true then set rear=0 and insert element.

Binary Tree
tree is a popular data structure that is non-linear in nature. Unlike other data structures like array, stack,
queue, and linked list which are linear in nature, a tree represents a hierarchical structure. The ordering
information of a tree is not important. A tree contains nodes and 2 pointers. These two pointers are the left
child and the right child of the parent node. Let us understand the terms of tree in detail.

● Root: The root of a tree is the topmost node of the tree that has no parent node. There is only one
root node in every tree.
● Edge: Edge acts as a link between the parent node and the child node.
● Leaf: A node that has no child is known as the leaf node. It is the last node of the tree. There can
be multiple leaf nodes in a tree.
● Depth: The depth of the node is the distance from the root node to that particular node.
● Height: The height of the node is the distance from that node to the deepest node of the tree.
● Height of tree: The Height of the tree is the maximum height of any node.
Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary
tree can have only 2 children, we typically name them the left and right child.

Applications of Binary Tree:

● In compilers, Expression Trees are used which is an application of binary tree.


● Huffman coding trees are used in data compression algorithms.
● Priority Queue is another application of binary tree that is used for searching maximum or minimum in O(1)
time complexity.
Binary Tree Traversals:

● PreOrder Traversal: Here, the traversal is: root – left child – right child. It means that the root node is
traversed first then its left child and finally the right child.
● InOrder Traversal: Here, the traversal is: left child – root – right child. It means that the left child is
traversed first then its root node and finally the right child.
● PostOrder Traversal: Here, the traversal is: left child – right child – root. It means that the left child is
traversed first then the right child and finally its root node.

Tree

1 //Root Node
/\
2 3
/\ /\
4 5 6 7 //Leaf Nodes

PreOrder Traversal of the above tree: 1-2-4-5-3-6-7


InOrder Traversal of the above tree: 4-2-5-1-6-3-7
PostOrder Traversal of the above tree: 4-5-2-6-7-3-1

Big Oh Notation, Ο :- The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time.
It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete.
For example, for a function f(n) Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }

Omega Notation:- Ω The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It
measures the best case. For example, for a function f(n) Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤
c.f(n) for all n > n0. }

Theta Notation,:- θ The notation θ(n) is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows − θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all
n > n0. }

Which of these best describes an array?

a) A data structure that shows a hierarchical behavior


b) Container of objects of similar types
c) Arrays are immutable once initialised
d) Array is not a data structure

How do you initialize an array in C?


a) int arr[3] = (1,2,3);
b) int arr(3) = {1,2,3};
c) int arr[3] = {1,2,3};
d) int arr(3) = (1,2,3);

Which of the following is the correct way to declare a multidimensional array in Java?

a) int[] arr;
b) int arr[[]];
c) int[][]arr;
d) int[[]] arr;

Assuming int is of 4bytes, what is the size of int arr[15];?


a) 15
b) 19
c) 11
d) 60

KEY POINTS:-
● ArrayIndexOutOfBoundsException is a run-time exception and the compilation is error-free.
● Whenever a particular memory location is referred to, it is likely that the locations nearby are also referred, arrays are
stored as contiguous blocks in memory, so if you want to access array elements, spatial locality makes it to access
quickly.
● Arrays are of fixed size. If we insert elements less than the allocated size, unoccupied positions can’t be used again.
Wastage will occur in memory.

Process of inserting an element in stack is called:- Push


Process of removing an element from stack is called:- Pop
In a stack, if a user tries to remove an element from an empty stack it is called :-Underflow
The data structure required for Breadth First Traversal on a graph is:- Queue

Which of the following is not the application of stack?


a) A parentheses balancing program
b) Tracking of local variables at run time
c) Compiler Syntax Analyzer
d) Data Transfer between two asynchronous process

Exp:- Data Transfer between two asynchronous process is an application of queue.

What is the value of the postfix expression 6 3 2 4 + – *?

a) 1
b) 40
c) 74
d) -18

Exp:- Postfix Expression is (6*(3-(2+4))) which results -18 as output.

The postfix form of the expression (A+ B)*(C*D- E)*F / G is?

a) AB+ CD*E – FG /**


b) AB + CD* E – F **G /
c) AB + CD* E – *F *G /
d) AB + CDE * – * F *G /

Explanation: (((A+ B)*(C*D- E)*F) / G) is converted to postfix expression as


=(AB+(*(C*D- E)*F )/ G)
=(AB+CD*E-*F) / G
=(AB+CD*E-*F * G/). Thus Postfix expression is AB+CD*E-*F*G/

The data structure required to check whether an expression contains a balanced


parenthesis is?
a) Stack
b) Queue
c) Array
d) Tree

What data structure would you mostly likely see in non recursive implementation of a
recursive algorithm?
a) Linked List
b) Stack
c) Queue
d) Tree

The postfix form of A*B+C/D is?

a) *AB/CD+
b) AB*CD/+
c) A*BC+/D
d) ABCD+/*

Explanation: Infix expression is (A*B)+(C/D)


AB*+(C/D)
AB*CD/+. Thus postfix expression is AB*CD/+.

Which of the following statement(s) about stack data structure is/are NOT correct?
a) Linked List are used for implementing Stacks
b) Top of the Stack always contain the new node
c) Stack is the FIFO data structure
d) Null link is present in the last node at the bottom of the stack

Explanation: Stack follows LIFO.

Which of the following is not an inherent application of stack?

a) Reversing a string
b) Evaluation of postfix expression
c) Implementation of recursion
d) Job scheduling

Explanation: Job Scheduling performed using Queue

Binary Search Tree is a node-based binary tree data structure which has the following properties:

● The left subtree of a node contains only nodes with keys lesser than the node’s key.
● The right subtree of a node contains only nodes with keys greater than the node’s key.
● The left and right subtree each must also be a binary search tree.

Heap :-- A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Generally,
Heaps can be of two types:
1. Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present
at all of it’s children. The same property must be recursively true for all sub-trees in that Binary
Tree.
2. Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present
at all of it’s children. The same property must be recursively true for all sub-trees in that Binary
Tree.

A normal queue, if implemented using an array of size MAX_SIZE, gets full when?

a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front

A linear collection of data elements where the linear node is given by means of pointer is
Called?
a) Linked list
b) Node list
c) Primitive list
d) Unordered list

The concatenation of two lists can be performed in O(1) time. Which of the following
variation of the linked list can be used?

a) Singly linked list


b) Doubly linked list
c) Circular doubly linked list
d) Array implementation of list

Linked lists are not suitable for the implementation of ___________


a) Insertion sort
b) Radix sort
c) Polynomial manipulation
d) Binary search

Linked list is considered as an example of dynamic memory allocation.

In Linked List implementation, a node carries information regarding ___________

a) Data
b) Link
c) Data and Link
d) Node

Which of the following points is/are not true about Linked List data structure when it is
compared with an array?

a) Arrays have better cache locality that can make them better in terms of performance
b) It is easy to insert and delete elements in Linked List
c) Random access is not allowed in a typical implementation of Linked Lists
d) Access of elements in linked list takes less time than compared to arrays

Which of the following sorting algorithms can be used to sort a random linked list with
minimum time complexity?

a) Insertion Sort
b) Quick Sort
c) Heap Sort
d) Merge Sort

Which of the following is false about a doubly linked list?

a) We can navigate in both the directions


b) It requires more space than a singly linked list
c) The insertion and deletion of a node take a bit longer
d) Implementing a doubly linked list is easier than singly linked list

Which of the following application makes use of a circular linked list?


a) Undo operation in a text editor
b) Recursive function calls
c) Allocating CPU to resources
d) Implement Hash Tables

Which of the following is false about a circular linked list?

a) Every node has a successor


b) Time complexity of inserting a new node at the head of the list is O(1)
c) Time complexity for deleting the last node is O(n)
d) We can traverse the whole circular linked list by starting from any point

Explanation: Time complexity of inserting a new node at the head of the list is O(n) because you have to traverse
through the list to find the tail node.

What is the best case time complexity of deleting a node in a Singly Linked list?

a) O (n)
b) O (n2)
c) O (nlogn)
d) O (1)

If the array is not full, then we can directly add the new element through the index. In this case, the time complexity
would be constant, i.e., O(1). If the array is full, we first need to copy the array into another array and add a new
element. In this case, the time complexity would be O(n).

In a circular queue, how do you increment the rear end of the queue?

a) rear++
b) (rear+1) % CAPACITY
c) (rear % CAPACITY)+1
d) rear–

In case of insertion into a linked queue, a node borrowed from the __________ list is
inserted in the queue.

a) AVAIL
b) FRONT
c) REAR
d) NULL

Of the following choices, which operator has the lowest precedence?

a) ^
b) +
c) /
d) #

Reverse polish Notation is the other name for a postfix expression whereas Polish
Notation, Warsaw notation are the other names for a prefix expression.
Which of these operators have the highest order of precedence?

a) ‘(‘ and ‘)’


b) ‘*’ and ‘/’
c) ‘~’ and ‘^’
d) ‘+’ and ‘-‘

What would be the Prefix notation for the given equation?


A+(B*C)

a) +A*CB
b) *B+AC
c) +A*BC
d) *A+CB

How many children does a binary tree have?


a) 2
b) any number of children
c) 0 or 1 or 2
d) 0 or 1

Level order traversal of a tree is formed with the help of

a) breadth first search


b) depth first search
c) dijkstra’s algorithm
d) prims algorithm

A binary tree is a rooted tree and also an ordered tree (i.e) every node in a binary tree has at most two children.

Breadth first traversal, also known as level order traversal is the traversal strategy used in a binary tree. It involves
visiting all the nodes at a given level.

Which of the following pair’s traversals on a binary tree can build the tree uniquely?

a) post-order and pre-order


b) post-order and in-order
c) post-order and level order
d) level order and preorder

Which of the following tree data structures is not a balanced binary tree?

a) AVL tree
b) Red-black tree
c) Splay tree
d) B-tree

All the tree data structures given in options are balanced, but B-tree can have more than two children. AVL , red black
and splay tree are balanced binary tree.

Which of the following is not the self balancing binary search tree?

a) AVL Tree
b) 2-3-4 Tree
c) Red – Black Tree
d) Splay Tree

Explanation: 2-3-4 Tree is balanced search trees. But it is not a binary tree. So, it is not a
self balancing binary tree. AVL tree, Red-Black Tree and Splay tree are self balancing
binary search tree.

In AVL tree is a self – balancing binary search tree, in which the heights of the two child sub
trees of any node differ by _________

a) At least one
b) At most one
c) Two
d) At most two

Explanation: In an AVL tree, the difference between heights of the two child sub trees of any node is at most one. If
the height differs by more than one, AVL tree performs rotations to balance the tree.

Types of Tree data structures

The different types of tree data structures are as follows:

1. General tree

A general tree data structure has no restriction on the number of nodes. It means that a parent node can have any
number of child nodes.

2. Binary tree

A node of a binary tree can have a maximum of two child nodes. In the given tree diagram, node B, D, and F are left
children, while E, C, and G are the right children.

3. Balanced tree

If the height of the left sub-tree and the right sub-tree is equal or differs at most by 1, the tree is known as a balanced tree.

4. Binary search tree

As the name implies, binary search trees are used for various searching and sorting algorithms. The examples include
AVL tree and red-black tree. It is a non-linear data structure. It shows that the value of the left node is less than its parent,
while the value of the right node is greater than its parent.

Applications of Tree data structure:

The applications of tree data structures are as follows:

1. Spanning trees: It is the shortest path tree used in the routers to direct the packets to the destination.

2. Binary Search Tree: It is a type of tree data structure that helps in maintaining a sorted stream of data.
Trie: It is a fast and efficient way for dynamic spell checking. It is also used for locating specific keys from
within a set.
Heap: It is also a tree data structure that can be represented in a form of an array. It is used to implement
priority queues.

Which of the following is a self – balancing binary search tree?

a) 2-3 tree
b) Threaded binary tree
c) AA tree
d) Treap

A self – balancing binary search tree can be used to implement ________

a) Priority queue
b) Hash table
c) Heap sort
d) Priority queue and Heap sort

Explanation: Self-balancing binary search trees can be used to construct and maintain ordered lists,
to achieve the optimal worst case performance. So, self – balancing binary search tree can be used to
implement a priority queue, which is ordered list.

In which of the following self – balancing binary search tree the recently accessed element
can be accessed quickly?

a) AVL tree
b) AA tree
c) Splay tree
d) Red – Black tree

Splay tree:- In a Splay tree, the recently accessed element can be accessed quickly. In Splay tree, the
frequently accessed nodes are moved towards the root so they are quick to access again.

Treap, also known as random binary search tree, Random binary tree and Uniform spanning tree are all
random tree. Random tree is a tree formed by a random process of addition and deletion of nodes.

AA Trees are implemented using?

a) Colors
b) Levels
c) Node size
d) Heaps
AA Tree :- AA trees are the variation of the red-black trees, a form of binary search tree.
AA trees use the concept of levels to aid in balancing binary trees. The level of node (instead of colour) is used for
balancing information. A link where child and parent’s levels are same, is called a horizontal link, and is analogous to a
red link in the red-black tree.

● The level of every leaf node is one.


● The level of red nodes are same as the level of their parent nodes and the links are called horizontal links.
● The level of black nodes are one less than the level of their parent node.

for tree to be AA tree, it must satisfy the following five invariants:

1.) The level of leaf node is 1.


2.) The level of left child is exactly one less than of its parent.
3.) The level of every right child is equal to or one less than of its parent.
4.) The level of every right grandchild is strictly less than that of its grandparent.
5.) Every node of level greater than one has two children.

Red-Black Tree:- A red-black tree is a kind of self-balancing binary search tree where each node has an extra
bit, and that bit is often interpreted as the color (red or black). These colors are used to ensure that the tree
remains balanced during insertions and deletions.
Rules That Every Red-Black Tree Follows:
1. Every node has a color either red or black.
2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4. Every path from a node (including root) to any of its descendants NULL nodes has the same number of black
nodes.
5. All leaf nodes are black nodes.

The time complexity of red-black tree is :- nlogn

What are the two different operations done in an AA-Tree?


a) shift and color
b) skew and split
c) zig and zag
d) enqueue and dequeue

Explanation: A skew removes a left horizontal link by right rotation and a split removes a right horizontal link by left
rotation

What is the prime condition of AA-tree which makes it simpler than a red-black tree?

a) Only right children can be red


b) Only left children can be red
c) Right children should strictly be black
d) There should be no left children

Comparing the speed of execution of Red-Black trees and AA-trees, which one has the
faster search time?

a) AA-tree
b) Red-Black tree
c) Both have an equal search time
d) It depends

What is an AVL tree?


a) a tree which is balanced and is a height balanced tree
b) a tree which is unbalanced and is a height balanced tree
c) a tree with three children
d) a tree with atmost 3 children

AVL tree :- AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left
and right subtrees cannot be more than one for all nodes.

Why to prefer red-black trees over AVL trees?


a) Because red-black is more rigidly balanced
b) AVL tree store balance factor in every node which costs space
c) AVL tree fails at scale
d) Red black is more efficient

Which of the below statements are true?


i. Cartesian tree is not a height balanced tree
ii. Cartesian tree of a sequence of unique numbers can be unique generated
a) both statements are true
b) only i. is true
c) only ii. is true
d) both are false

A Cartesian tree is a tree data structure created from a set of data that obeys the following structural
invariants:

1. The tree obeys in the min (or max) heap property – each node is less (or greater) than its children.
2. An inorder traversal of the nodes yields the values in the same order in which they appear in the
initial sequence.

Why do we impose restrictions like


. root property is black
. every leaf is black
. children of red node are black
. all leaves have same black
a) to get logarithm time complexity
b) to get linear time complexity
c) to get exponential time complexity
d) to get constant time complexity

Which of the following is an application of Red-black trees and why?


a) used to store strings efficiently
b) used to store integers efficiently
c) can be used in process schedulers, maps, sets
d) for efficient sorting

When to choose Red-Black tree, AVL tree and B-trees?


a) many inserts, many searches and when managing more items respectively
b) many searches, when managing more items respectively and many inserts respectively
c) sorting, sorting and retrieval respectively
d) retrieval, sorting and retrieval respectively

Which data structure is used to maintain a dynamic forest using a link or cut operation?
a) Top Tree
b) Array
c) Linked List
d) Stack

Top tree:-Top tree is a type of data structure which is based on unrooted dynamic binary tree and is used to solve path
related problems. It allows an algorithm called divide and conquer.

What are splay trees?


a) self adjusting binary search trees
b) self adjusting binary trees
c) a tree with strings
d) a tree with probability distributions

Like AVL and Red-Black Trees, Splay tree is also self-balancing BST. The main idea of splay tree is to bring the
recently accessed item to root of the tree, this makes the recently searched item to be accessible in O(1) time if
accessed again. Splay trees have excellent locality properties. Frequently accessed items are easy to find.
Infrequent items are out of way. Unlike AVL tree, a splay tree can change even with read-only operations like
search.

Which of the following options is an application of splay trees?


a) cache Implementation
b) networks
c) send values
d) receive values

What is the disadvantage of using splay trees?


a) height of a splay tree can be linear when accessing elements in non decreasing
order.
b) splay operations are difficult
c) no significant disadvantage
d) splay tree performs unnecessary splay when a node is only being read
A treap is a combination of a tree and a heap. The structure of a treap is determined by the fact that it is
heap-ordered.

B-tree of order n is a order-n multiway tree in which each non-root node contains
__________
a) at most (n – 1)/2 keys
b) exact (n – 1)/2 keys
c) at least 2n keys
d) at least (n – 1)/2 keys

Which of the following is true?


a) larger the order of B-tree, less frequently the split occurs
b) larger the order of B-tree, more frequently the split occurs
c) smaller the order of B-tree, more frequently the split occurs
d) smaller the order of B-tree, less frequently the split occurs

Which of the following is false?


a) A B+ -tree grows downwards
b) A B+ -tree is balanced
c) In a B+ -tree, the sibling pointers allow sequential searching
d) B+ -tree is shallower than B-tree
It grows upwards

Which of the following is false?


a) Compared to B-tree, B+ -tree has larger fanout
b) Deletion in B-tree is more complicated than in B+ -tree
c) B+ -tree has greater depth than corresponding B-tree
d) Both B-tree and B+ -tree have same search and insertion efficiencies

Explanation: A B+ -tree has larger fanout and therefore have a depth smaller than that of corresponding B-tree

Which one of the following data structures are preferred in database-system implementation?
a) AVL tree
b) B-tree
c) B+ -tree
d) Splay tree

In binary search trees we have seen the average-case time for operations like search/insert/delete is O(log N) and the
worst-case time is O(N) where N is the number of nodes in the tree.
Like other Trees include AVL trees, Red Black Tree, B tree, 2-3 Tree is also a height balanced tree.
The time complexity of search/insert/delete is O(log N) .
A 2-3 tree is a B-tree of order 3.

Properties of 2-3 tree:

Nodes with two children are called 2-nodes. The 2-nodes have one data value and two children
Nodes with three children are called 3-nodes. The 3-nodes have two data values and three children.
Data is stored in sorted order.
It is a balanced tree.
All the leaf nodes are at same level.
Each node can either be leaf, 2 node, or 3 node.
Always insertion is done at leaf.

Which of the following the BST is isometric with the 2-3 tree?
a) Splay tree
b) AA tree
c) Heap
d) Red – Black tree

Explanation: AA tree is isometric of the 2-3 trees. In an AA tree, we store each node a level, which is the height of
the corresponding 2-3 tree node. So, we can convert a 2-3 tree to an AA tree.

Heap can be used as ________________


a) Priority queue
b) Stack
c) A decreasing order array
d) Normal Array

Heap exhibits the property of a binary tree.

What is the process of building a ternary heap called?


a) Heapify
b) Hashing
c) Linking
d) Merging

Suffix tree is a compressed trie of all the suffixes of a given string. Suffix trees help in solving a lot of string related
problems like pattern matching, finding distinct substrings in a given string, finding longest palindrome etc. suffix tree is
also known as PAT tree or position tree. It is a compressed search tree or prefix tree in which keys contain the suffix of
text values as the text position In this tutorial following points will be covered:

Compressed Trie
Suffix Tree Construction (Brute Force)
Brief description of Ukkonen's Algorithm

Which of the following statements for a simple graph is correct?


a) Every path is a trail
b) Every trail is a path
c) Every trail is a path as well as every path is a trail
d) Path and trail have no relation

A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as
vertices and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph can be defined
as,

A Graph consists of a finite set of vertices(or nodes) and a set of Edges that connect a pair of nodes. Graphs are
used to solve many real-life problems. Graphs are used to represent networks. The networks may include
paths in a city or telephone network or circuit network.

What is the number of edges present in a complete graph having n vertices?


a) (n*(n+1))/2
b) (n*(n-1))/2
c) n
d) Information given is insufficient

A connected planar graph having 6 vertices, 7 edges contains _____________ regions.


a) 15
b) 3
c) 1
d) 11

Explanation: By euler’s formula the relation between vertices(n), edges(q) and regions(r) is given by n-q+r=2.

A graph G = (V, E) consists of a set of vertices V = { V1, V2, . . . } and set of edges E = { E1, E2, . . . }. The set of
unordered pairs of distinct vertices whose elements are called edges of graph G such that each edge is identified with
an unordered pair (Vi, Vj) of vertices.

Finite Graphs: A graph is said to be finite if it has finite number of vertices and finite number of edges.
Infinite Graph: A graph is said to be infinite if it has infinite number of vertices as well as infinite number of edges.
Trivial Graph: A graph is said to be trivial if a finite graph contains only one vertex and no edges.
Simple Graph: A simple graph is a graph which does not contains more than one edge between the pair of vertices. A
simple railway tracks connecting different cities is an example of simple graph.
Regular Graph: A simple graph is said to be regular if all vertices of a graph G are of equal degree. All complete
graphs are regular but vice versa is not possible.
Bipartite Graph: A graph G = (V, E) is said to be bipartite graph if its vertex set V(G) can be partitioned into two
non-empty disjoint subsets.
Connected or Disconnected Graph: A graph G is said to be connected if for any pair of vertices (Vi, Vj) of a graph G
are reachable from one another. Or a graph is said to be connected if there exists at least one path between each and
every pair of vertices in graph G, otherwise it is disconnected. A null graph with n vertices is disconnected graph
consisting of n components. Each component consists of one vertex and no edge.

Advantage:
They are useful for implementing algorithms like DFS and BFS.
Graph provides data accuracy.
Graphs are useful for finding the shortest path.
To find the minimum spanning tree.
It is used in complex problems.
It is useful for organizing data.

Disadvantages:
Graphs make data structures complex as they use pointers.
A graph can have a large memory complexity.
The graph is represented by an adjacency matrix that does not allow parallel edges.
Multiplication in a graph is complex.

For a given graph G having v vertices and e edges which is connected and has no cycles, which of the following
statements is true?
a) v=e
b) v = e+1
c) v + 1 = e
d) v = e-1

What are the dimensions of an incidence matrix?


a) Number of edges*number of edges
b) Number of edges*number of vertices
c) Number of vertices*number of vertices
d) Number of edges * (1⁄2 * number of vertices)

For the given conditions, which of the following is in the correct order of increasing space requirement?
i) Undirected, no weight
ii) Directed, no weight
iii) Directed, weighted
iv) Undirected, weighted
a) ii iii i iv
b) i iii ii iv
c) iv iii i ii
d) i ii iii iv

In which case adjacency list is preferred in front of an adjacency matrix?


a) Dense graph
b) Sparse graph
c) Adjacency list is always preferred
d) Complete graph

a dense graph is a graph in which the number of edges is close to the maximal number of edges (where every pair of
vertices is connected by one edge). The opposite, a graph with only a few edges, is a sparse graph.

Space complexity for an adjacency list of an undirected graph having large values of V
(vertices) and E (edges) is ___________
a) O(E)
b) O(V*V)
c) O(E+V)
d) O(V)

Sorting algorithm:-

A Sorting Algorithm is used to rearrange a given array or list elements according to acomparison operator on the
elements. The comparison operator is used to decide the new order of element in the respective data structure.

Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The
array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the
correct position in the sorted part.
This algorithm is one of the simplest algorithm with simple implementation
Basically, Insertion sort is efficient for small data values
Insertion sort is adaptive in nature, i.e. it is appropriate for data sets which are already partially sorted.
Insertion Sort algorithm follows incremental approach. insertion sort is an in-place sorting algorithm. insertion sort is a
stable sorting algorithm. Insertion sort is used when number of elements is small. It can also be useful when input array
is almost sorted, only few elements are misplaced in complete big array.
An insertion algorithm consists of N-1 passes when an array of N elements is given
● Insertion sort is similar to that of a binary heap algorithm because of the use of
temporary variable to swap.
● The total number of pairs in a list L is N(N-1)/2. Thus, an average list has half this
amount, or N(N-1)/4 inversions.
● To perform an insertion sort, we use two basic loops- an outer for loop and an
inner while loop
● Binary search can be used in an insertion sort algorithm to reduce the number of
comparisons. This is called a Binary insertion sort.
● An insertion sort is stable, adaptive, in-place and incremental in nature.
● Insertion sort is an example of an incremental algorithm.
● The insertion sort is good for sorting small arrays. It sorts smaller arrays faster than any other sorting algorithm.
● Insertion sort is not an exchange sort

What will be the number of passes to sort the elements using insertion sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1
Explanation: The number of passes is given by N-1. Here, N=6. Therefore,
6-1=5 passes.

Which of the following real time examples is based on insertion sort?


a) arranging a pack of playing cards
b) database scenarios and distributes scenarios
c) arranging books on a library shelf
d) real-time systems

Which of the following sorting algorithms is the fastest for sorting small arrays?
a) Quick sort
b) Insertion sort
c) Shell sort
d) Heap sort

Which of the following sorting algorithm is best suited if the elements are already sorted?
a) Heap Sort
b) Quick Sort
c) Insertion Sort
d) Merge Sort

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending
order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

The subarray which is already sorted.


Remaining subarray which is unsorted.

Time Complexity: The time complexity of Selection Sort is O(N2). Auxiliary Space: O(1) as the only extra memory
used is for temporary variable while swapping two values in Array. Stability : The default implementation is not stable.
However it can be made stable.

Selection is based on keys, hence a file with large values and small keys can be efficiently sorted with selection sort.

selection sort is an in-place sorting algorithm, it does not require additional storage. Selection sort is not scalable.

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in
the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite
high.
Worst and Average Case Time Complexity: O(N2). The worst case occurs when an array is reverse sorted.
Best Case Time Complexity: O(N). The best case occurs when an array is already sorted.
Auxiliary Space: O(1)

external sorting algorithm uses external memory like tape or disk.


● internal sorting algorithm uses internal main memory.
● Bubble sort works by starting from the first element and swapping the elements if required in each iteration even in
the average case.● Sorting In Place: Yes, ● Stable: Yes
The Merge Sort algorithm is a sorting algorithm that is considered an example of the divide and conquer strategy.
So, in this algorithm, the array is initially divided into two equal halves and then they are combined in a sorted manner.
It is Used in External Sorting.

Auxiliary Space: O(n)


● Algorithmic Paradigm: Divide and Conquer
● Sorting In Place: No in a typical implementation
● Stable: Yes

Merge sort is preferred for linked list over arrays. It is because in a linked list the insert operation takes only O(1)
time and space which implies that we can implement merge operation in constant time.
● Tim sort is a hybrid sorting algorithm as it uses more than one sorting algorithm internally. It makes use of merge
sort and insertion sort.
● Bottom up merge sort uses the iterative method in order to implement sorting. Bottom up merge sort does not use
recursion
● Merge Sort is useful for sorting linked lists in O(nLogn) time.

Choose the incorrect statement about merge sort from the following?
a) it is a comparison based sort
b) it is an adaptive algorithm
c) it is not an in place algorithm
d) it is stable algorithm

Explanation: Merge sort is not an adaptive sorting algorithm. This is because it takes O(nlog n) time complexity
irrespective of any case.

Which of the following stable sorting algorithm takes the least time when applied to an almost sorted array?
a) Quick sort
b) Insertion sort
c) Selection sort
d) Merge sort

QuickSort
QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array
around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

Always pick the first element as a pivot.


Always pick the last element as a pivot (implemented below)
Pick a random element as a pivot.
Pick median as the pivot.

The default implementation of quick sort is not stable. However any sorting algorithm can be made stable by
considering indexes as comparison parameter. As per the broad definition of in-place algorithm it qualifies as
an in-place sorting algorithm as it uses extra space only for storing recursive function calls but not for
manipulating the input.

Quick sort is the fastest known sorting algorithm because of its highly optimized inner loop.
● In quick sort, the array is divided into sub-arrays and then it is sorted (divide-and-conquer strategy).
● Median-of-three partitioning is the best method for choosing an appropriate pivot element. Picking a first,
last or random element as a pivot is not much effective.
● Insertion sort is used along with quick sort to sort the sub arrays. It is used only at the end.
● Quick sort uses join operation since join is a faster operation than merge.
● Is QuickSort stable? :- The default implementation is not stable. However any sorting algorithm can be made stable
by considering indexes as comparison parameter.
● In stable sorting algorithm the records with equal keys appear in the same order in the sorted sequence as they
appear in the input unsorted sequence.
Quick sort does not preserve the relative order of equal sort items. Therefore, Quick sort is not
a stable sort.
● Quick sort is a space-optimised version of the binary tree sort

Which one of the following sorting algorithm is best suited to sort an array of 1 million
elements?
a) Bubble sort
b) Insertion sort
c) Merge sort
d) Quick sort
Explanation: The Quick sort is best suited to sort the array of 1 million elements. The practical
implementations of Quick sort use randomised version. In practice randomised Quick sort algorithms rarely
shows worst case behaviour and is almost always O(nlogn). And Quick sort requires little additional space and
exhibits good cache locality..

Which of the following is not true about QuickSort?


a) in-place algorithm
b) pivot position can be changed
c) adaptive sorting algorithm
d) can be implemented as a stable sort

Explanation: Once a pivot is chosen, its position is finalized in the sorted array, it cannot be modified.

ShellSort :- Shell sort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position
ahead. When an element has to be moved far ahead, many movements are involved. The idea of ShellSort is to allow
the exchange of far items.

Time Complexity: Time complexity of the above implementation of Shell sort is O(n2). In the above implementation, the
gap is reduced by half in every iteration. There are many other ways to reduce gaps which leads to better time
complexity. See this for more details.

Worst Case Complexity


The worst-case complexity for shell sort is O(n2)
Best Case Complexity
When the given array list is already sorted the total count of comparisons of each interval is equal to the size of the
given array.
So best case complexity is Ω(n log(n))
Average Case Complexity
The shell sort Average Case Complexity depends on the interval selected by the programmer.
θ(n log(n)2).

● The other name for a shell sort algorithm is diminishing decrement sort as the distance between comparisons
decreases as the algorithm runs until the last phase.
● Shell sort broke the quadratic time barrier as it works by comparing elements that are distant

● Shell sort is an example of internal sorting because sorting of elements is done internally using an array.
● Shell sort uses an increment sequence h1, h2, h3… and this sequence will work as long as h1=1.

● Shell sort is an in-place sorting algorithm. ● Shell sort is not an stable sorting algorithm.
● Shell sort is an improvement on insertion sort that allows the exchange of elements that are far apart.
● Insertion sort is more efficient than Shell sort if the length of array is small because insertion sort is quite simple to
program and involve very few actions other than comparisons and replacements on each pass.
● Shell sort’s passes completely sort the elements before going on to the next-smallest gap while Comb sort’s passes
do not completely sort the elements.

Which of the following statements is the basic for loop for a shell sort algorithm?
a) for(increment=N/2;increment>0;increment/=2)
b) for(i=1;i<n;i++)
c) for(i=n/2;i>=0;i- -)
d) for(i=0;i< n;i++;numelements- -)

Statement 1: Shell sort is a stable sorting algorithm.


Statement 2: Shell sort is an in-place sorting algorithm.
a) Both statements are true
b) Statement 2 is true but statement 1 is false
c) Statement 2 is false but statement 1 is true
d) Both statements are false

Explanation: In Shell sort, the relative order of elements with equal values may change. Therefore, it is not a stable
sorting algorithm. Shell sort is an in-place sorting algorithm as it requires O(1) auxiliary space.

Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection
sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same
process for the remaining elements.

● Heap sort is based on the algorithm of priority queue and it gives the best sorting time.
● Heap sort is slower than Shell sort because Shell sort uses Sedgewick’s increment sequence.
● Heap sort is a comparison based sorting algorithm and has time complexity O(nlogn) in the average case.
Heap sort is an in-place algorithm as it needs O(1) of auxiliary space. Heap sort uses heap and operations on heap can
change the relative order of items with the same key values. Therefore, Heap sort is not a stable sort.
● Heap sort is an implementation of selection sort using the input array as a heap representing a descending
priority queue.
● Heap sort is an in-place algorithm.
● Smooth sort is a variation of Heap sort.
● Introsort is a hybrid sorting algorithm that combines Quick sort and Heap sort to retain advantages of both.

Which one of the following is false?


a) Heap sort is an in-place algorithm
b) Heap sort has O(nlogn) average case time complexity
c) Heap sort is stable sort
d) Heap sort is a comparison-based sorting algorithm

Which of the following sorting algorithm is used by C++ internally?


a) quicksort
b) introsort
c) merge sort
d) heap sort
Explanation: Introsort is the in built sorting algorithm used by C++. It is an example of a hybrid sorting algorithm which
means it uses more than one sorting algorithm as a routine.

TimSort is a sorting algorithm based on Insertion Sort and Merge Sort.

Used in Java’s Arrays.sort() as well as Python’s sorted() and sort().


First sort small pieces using Insertion Sort, then merges the pieces using a merge of merge sort.
Tim sort has been python’s standard sorting algorithm
● Tim sort is a hybrid sorting algorithm which means it uses more than one sorting
algorithm as a routine.
● Merge sort and insertion sort are comparison based sorts. Thus overall Tim sort also
becomes a comparison based sort.
Cube sort :- cube sort is a parallel sorting algorithm. It builds self balancing multi dimensional arrays from the input
keys. The worst case performance of cube sort is O(n log n). This is the fastest possible complexity with a comparison
based sort. cube sort is stable algorithm.

Which of the following is a disadvantage of cube sort?


a) high memory overhead for small data
b) high memory overhead for any data
c) balancing is slow
d) Iteration is slow

Which of the following sorting algorithm uses the method of insertion?


a) cube sort
b) bubble sort
c) quick sort
d) selection sort

Which of the following sorting algorithms can be considered as improvement to the binary tree sort?
a) Heap sort
b) Quick sort
c) Selection sort
d) Insertion sort
Explanation: Heap sort is basically improvement to the binary tree sort. Heap sort builds a heap on the input
element by adjusting the position of the elements within the original array, rather than creating nodes as in
binary tree sort.

Cycle sort :- Cycle sort is an in-place sorting Algorithm, unstable sorting algorithm, a comparison sort that is
theoretically optimal in terms of the total number of writes to the original array.

Which of the following is an example of an unstable sorting algorithm?


a) cycle sort
b) insertion sort
c) bubble sort
d) merge sort
cycle sort algorithm is not adaptive algorithm
● Cycle sort has an auxiliary space complexity of O(1). So it qualifies to be an in-place sort
● Cycle sort is a comparison based sort.

Which of the following is an advantage of cycle sort?


a) it can sort large arrays efficiently
b) it has a low time complexity
c) it requires minimal write operations
d) it is an adaptive sorting algorithm

Explanation: Cycle sort is a slow sorting algorithm as compared to quick sort, merge sort etc. and is also not
adaptive. But it offers an advantage that it performs minimal write operations which can be very useful in a
situation where the write operation is expensive.

Which of the following algorithm is best suited for the case where swap operation is expensive?
a) bubble sort
b) cycle sort
c) cocktail sort
d) merge sort

Explanation: Cycle sort is a slow sorting algorithm but it requires a minimum number of write operations in order to sort
a given array. So it is useful when the write/swap operation is expensive.

Comb sort is an improved version of _______


a) Selection sort
b) Bubble sort
c) Insertion sort
d) Merge sort

Bogosort is also known by names like stupid sort, monkey sort, permutation sort, slow sort and shotgun sort.These
names are particularly chosen due to its inefficient algorithm. Bogosort algorithm successively generates permutations
of its input. This process is repeated until the sorted version of the array is found. bogosort is not stable. bogosort is an
in-place sorting algorithm
Which of the following sorting techniques is most efficient if the range of input data is not significantly greater than a
number of elements to be sorted?
a) selection sort
b) bubble sort
c) counting sort
d) insertion sort

bucket sort:- bucket sort is an example of a non-comparison sort so it is able to sort an array without making any
comparison.
Which of the following is not true about bucket sort?
a) It is a non comparison based integer sort
b) It is a distribution sort
c) It can also be considered as comparison based sort
d) It is in place sorting algorithm

Explanation: Bucket sort is a non comparison based integer sort. It sorts the given data by distributing the array
elements into a number of buckets. It is not an in place sorting technique.

Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is,
unlike trees, graphs may contain cycles (a node may be visited twice). To avoid processing a node more than once, use
a boolean visited array. it is equivalent to the pre-order traversal of a Binary Tree. The Depth First Search explores
every node once and every edge once (in worst case), so it’s time complexity is O(V + E). The Depth First Search is
implemented using recursion. So, stack can be used as data structure to implement depth first search.

A person wants to visit some places. He starts from a vertex and then wants to visit every vertex till it finishes from one
vertex, backtracks and then explore other vertex from same vertex. What algorithm he should use?
a) Depth First Search
b) Breadth First Search
c) Trim’s algorithm
d) Kruskal’s Algorithm
Which of the following data structure is used to implement DFS?
a) linked list
b) tree
c) stack
d) queue
Explanation: Stack is used in the standard implementation of depth first search. It is used to store the elements which
are to be explored

Breadth-First Traversal (or Search) for a graph is similar to Breadth-First Traversal of a tree . The only catch here
is, that, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node
more than once, we use a boolean visited array. For simplicity, it is assumed that all vertices are reachable from the
starting vertex. BFS uses a queue data structure for traversal.

Choose the incorrect statement about DFS and BFS from the following?
a) BFS is equivalent to level order traversal in trees
b) DFS is equivalent to post order traversal in trees
c) DFS and BFS code has the same time complexity
d) BFS is implemented using queue

Explanation: DFS is equivalent to pre order traversal in trees, not post order traversal. It is so because in DFS we
keep on exploring as far as possible along each branch before backtracking. So it should be equivalent to pre order
traversal.

Which of the following is not an application of Breadth First Search?


a) Finding shortest path between two nodes
b) Finding bipartiteness of a graph
c) GPS navigation system
d) Path Finding

Explanation: Breadth First Search can be applied to Bipartite a graph, to find the shortest path between two nodes,
in GPS Navigation. In Path finding, Depth First Search is used.
● Best First Search is a searching algorithm used in graphs. It explores it by choosing a node by heuristic
evaluation rule. It is used in solving searching for related problems.

● The greedy best first search algorithm was used to predict the closeness of the end of the path and its solution by
some of the computer scientists.
● Stack is the data structure is used for implementing LIFO branch and bound strategy.

● Queue is the data structure is used for implementing FIFO branch and bound strategy.
● Priority Queue is the data structure is used for implementing best first branch and bound strategy. Dijkstra’s
algorithm is an example of best first search algorithm.
● FIFO branch and bound leads to breadth first search. Whereas backtracking leads to depth first search.
● Both backtrackings as well as branch and bound are problem solving algorithms. Both LIFO branch and
bound strategy and backtracking leads to depth first search.

Which of the following can traverse the state space tree only in DFS manner?
a) branch and bound
b) dynamic programming
c) greedy algorithm
d) backtracking

A Spanning tree is a subset to a connected graph G, where all the edges are connected, i.e, we can traverse to any
edge from a particular edge with or without intermediates. Also, a spanning tree must not have any cycle in it. Thus we
can say that if there are n vertices in a connected graph then the no. of edges that a spanning tree may have is n-1.

A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, undirected graph is a
spanning tree with a weight less than or equal to the weight of every other spanning tree.

A minimum spanning tree has (V – 1) edges where V is the number of vertices in the given graph. Time Complexity:
O(ElogE) or O(ElogV).

Which of the following is false in the case of a spanning tree of a graph G?


a) It is tree that spans G
b) It is a subgraph of the G
c) It includes every vertex of the G
d) It can be either cyclic or acyclic

Explanation: A graph can have many spanning trees. Each spanning tree of a graph G is a subgraph of the
graph G, and spanning trees include every vertex of the gram. Spanning trees are always acyclic.

The travelling salesman problem can be solved using _________


a) A spanning tree
b) A minimum spanning tree
c) Bellman – Ford algorithm
d) DFS traversal

Explanation: In the travelling salesman problem we have to find the shortest possible route that visits every city
exactly once and returns to the starting point for the given a set of cities. So, travelling salesman problem can be
solved by contracting the minimum spanning tree.

Which of the following is not the algorithm to find the minimum spanning tree of the given
graph?
a) Boruvka’s algorithm
b) Prim’s algorithm
c) Kruskal’s algorithm
d) Bellman–Ford algorithm

The Boruvka’s algorithm, Prim’s algorithm and Kruskal’s algorithm are the algorithms that can be used to find the
minimum spanning tree of the given graph. The Bellman-Ford algorithm is used to find the shortest path from
the single source to all other vertices.

Which of the following is false?


a) The spanning trees do not have any cycles
b) MST have n – 1 edges if the graph has n edges
c) Edge e belonging to a cut of the graph if has the weight smaller than any other edge in the same cut, then the edge e
is present in all the MSTs of the graph
d) Removing one edge from the spanning tree will not make the graph disconnected
Explanation: Every spanning tree has n – 1 edges if the graph has n edges and has no cycles. The MST follows
the cut property, Edge e belonging to a cut of the graph if has the weight smaller than any other edge in the
same cut, then the edge e is present in all the MSTs of the graph.

Kruskal’s algorithm is used to ______


a) find minimum spanning tree
b) find single source shortest path
c) find all pair shortest path algorithm
d) traverse the graph

Explanation: The Kruskal’s algorithm is used to find the minimum spanning tree of the connected graph. It
construct the MST by finding the edge having the least possible weight that connects two trees in the forest.

Kruskal’s algorithm uses a greedy algorithm approach to find the MST of the connected weighted graph
● the time complexity of Kruskal’s algorithm is O(E log V)
● Kruskal’s algorithm can also run on the disconnected graphs.
● Prim’s algorithm iterates from one node to another, so it can not be applied for disconnected graph.
● Kruskal’s algorithm is comparatively easier and simpler than prim’s algorithm.
● Kruskal algorithm does not contain any cycle.
● Kruskal’s algorithm can efficiently implemented using the disjoint-set data structure.
Prim’s algorithm uses a greedy algorithm approach to find the MST of the connected weighted graph. In greedy
method, we attempt to find an optimal solution in stages.

Which of the following is true?


a) Prim’s algorithm initialises with a vertex
b) Prim’s algorithm initialises with a edge
c) Prim’s algorithm initialises with a vertex which has smallest edge
d) Prim’s algorithm initialises with a forest

Explanation: Steps in Prim’s algorithm: (I) Select any vertex of given graph and add it to
MST (II) Add the edge of minimum weight from a vertex not in MST to the vertex in MST;
(III) It MST is complete the stop, otherwise go to step (II).
● Prim’s algorithm resembles Dijkstra’s algorithm.
● Prim’s algorithm and Kruskal’s algorithm perform equally in case of the sparse
graphs
● Prim’s algorithm can be implemented using Fibonacci heap and it never accepts
Cycles

Prim’s algorithm is also known as __________


a) Dijkstra–Scholten algorithm
b) Borůvka’s algorithm
c) Floyd–Warshall algorithm
d) DJP Algorithm

Explanation: The Prim’s algorithm was developed by Vojtěch Jarník and it was latter discovered by the duo
Prim and Dijkstra. Therefore, Prim’s algorithm is also known as DJP Algorithm.

Which of the following is false about Prim’s algorithm?


a) It is a greedy algorithm
b) It constructs MST by selecting edges in increasing order of their weights
c) It never accepts cycles in the MST
d) It can be implemented using the Fibonacci heap
Explanation: Prim’s algorithm can be implemented using Fibonacci heap and it never accepts cycles. And Prim’s
algorithm follows greedy approach. Prim’s algorithms span from one vertex to another.

Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like Prim’s MST, we
generate a SPT (shortest path tree) with a given source as a root. We maintain two sets, one set contains
vertices included in the shortest-path tree, other set includes vertices not yet included in the shortest-path
tree. At every step of the algorithm, we find a vertex that is in the other set (set of not yet included) and has a
minimum distance from the source.

Dijkstra’s Algorithm is used for solving single source shortest path problems. In this algorithm,
a single node is fixed as a source node and shortest paths from this node to all other nodes in graph is found.

Which of the following is the most commonly used data structure for implementing Dijkstra’s
Algorithm?
a) Max priority queue
b) Stack
c) Circular queue
d) Min priority queue

Dijkstra’s Algorithm cannot be applied on ______________


a) Directed and weighted graphs
b) Graphs having negative weight function
c) Unweighted graphs
d) Undirected and unweighted graphs

How many priority queue operations are involved in Dijkstra’s Algorithm?


a) 1
b) 3
c) 2
d) 4
Explanation: The number of priority queue operations involved is 3. They are insert, extract-min and
decrease key.

Dijkstra’s algorithm is a Greedy algorithm and time complexity is O((V+E)LogV) (with the use of
Fibonacci heap). Dijkstra doesn’t work for Graphs with negative weights, Bellman-Ford works
for such graphs. Bellman-Ford is also simpler than Dijkstra and suites well for distributed systems. But
time complexity of Bellman-Ford is O(VE), which is more than Dijkstra.

Negative weights are found in various applications of graphs. For example, instead of paying cost for a path,
we may get some advantage if we follow the path.
Bellman-Ford works better (better than Dijkstra’s) for distributed systems. Unlike Dijkstra’s where we need
to find the minimum value of all vertices, in Bellman-Ford, edges are considered one by one.
Bellman-Ford does not work with undirected graph with negative edges as it will declared as
negative cycle.

Bellmann Ford algorithm is used to indicate whether the graph has negative weight cycles or not.
Dijikstra’s Algorithm is more efficient than Bellmann Ford Algorithm.
Relaxation is the basic principle behind Bellmann Ford Algorithm.
Bellmann Ford Algorithm can be applied for all directed and weighted graphs.
Bellmann Ford Algorithm is an example for Dynamic programming.

The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to
find shortest distances between every pair of vertices in a given edge weighted directed Graph.

Floyd Warshall Algorithm follows dynamic programming approach. ● Floyd Warshall Algorithm
can be used for finding Transitive closure.

The Stable Marriage Problem states that given N men and N women, where each person has ranked all
members of the opposite sex in order of preference, marry the men and women together such that there are
no two people of opposite sex who would both rather have each other than their current partners. If there are
no such people, all the marriages are “stable”. Stable marriage problem is an example for recursive algorithm
because it recursively uses backtracking algorithm to find an optimal solution.

What is the simplest method to prove that a graph is bipartite?


a) It has a cycle of an odd length
b) It does not have cycles
c) It does not have a cycle of an odd length
d) Both odd and even cycles are formed

Which is the correct technique for finding a maximum matching in a graph?


a) DFS traversal
b) BFS traversal
c) Shortest path traversal
d) Heap order traversal

Explanation: The correct technique for finding a maximum matching in a bipartite graph is by using a
Breadth First Search(BFS).

How many edges will a tree consisting of N nodes have?


a) Log(N)
b) N
c) N – 1
d) N + 1
Explanation: In order to have a fully connected tree it must have N-1 edges. So the correct answer will be N-1.

Recursion is a method in which the solution of a problem depends on ____________


a) Larger instances of different problems
b) Larger instances of the same problem
c) Smaller instances of the same problem
d) Smaller instances of different problems
Explanation: In recursion, the solution of a problem depends on the solution of smaller instances of the same
problem.

In Fractional Knapsack, we can break items for maximizing the total value of knapsack. This
problem in which we can break an item is also called the fractional knapsack problem. Fractional
knapsack problem is also called continuous knapsack problem. Fractional knapsack is solved
using dynamic programming. Greedy algorithm is used to solve this problem. The objective is to fill the
knapsack of some given volume with different materials such that the value of selected items is maximized.

In 0/1 knapsack problem items are indivisible and in fractional knapsack items are divisible.

Which of the following algorithms is the best approach for solving Huffman codes?
a) exhaustive search
b) greedy algorithm
c) brute force algorithm
d) divide and conquer algorithm

Explanation: Greedy algorithm is the best approach for solving the Huffman codes problem since it greedily
searches for an optimal solution. In Huffman encoding, data is always stored at the leaves of a tree inorder
to compute the codeword effectively. Backtracking approach is used to solve complex combinatorial
problems which cannot be solved by exhaustive search algorithms.

Which of the problems cannot be solved by backtracking method?


a) n-queen problem
b) subset sum problem
c) hamiltonian circuit problem
d) travelling salesman problem

Explanation: N-queen problem, subset sum problem, Hamiltonian circuit problems can be solved
by backtracking method whereas travelling salesman problem is solved by Branch and bound
method.

The problem of finding a list of integers in a given specific range that meets certain conditions is called?
a) Subset sum problem
b) Constraint satisfaction problem
c) Hamiltonian circuit problem
d) Travelling salesman problem

Constraint satisfaction problem is the problem of finding a list of integers under given constraints.
Constraint satisfaction problem is solved using a backtracking approach.

Which of the following problems is NOT solved using dynamic programming?


a) 0/1 knapsack problem
b) Matrix chain multiplication problem
c) Edit distance problem
d) Fractional knapsack problem

Explanation: The fractional knapsack problem is solved using a greedy algorithm. The 0-1
Knapsack problem can not be solved using Greedy algorithm.

The most common hamming codes are a generalized version of?


a) Hamming(7, 4) code
b) Hamming(8, 4) code
c) Hamming(6, 3) code
d) Hamming(5, 7) code

Explanation: The most common hamming codes generalize to form hamming(7, 4) code. It encodes four bits
of data into seven bits by adding three parity bits.

NP Problem:
The NP problems set of problems whose solutions are hard to find but easy to verify and are solved by
Non-Deterministic Machine in polynomial time.

NP-Hard Problem:
A Problem X is NP-Hard if there is an NP-Complete problem Y, such that Y is reducible to X in polynomial
time. NP-Hard problems are as hard as NP-Complete problems. NP-Hard Problem need not be in NP class.

NP-Complete Problem:
A problem X is NP-Complete if there is an NP problem Y, such that Y is reducible to X in polynomial time.
NP-Complete problems are as hard as NP problems. A problem is NP-Complete if it is a part of both NP and
NP-Hard Problem. A non-deterministic Turing machine can solve NP-Complete problem in polynomial
time.
Hamiltonian Path :-- A Hamiltonian path is defined as the path in a directed or undirected graph which
visits each and every vertex of the graph exactly once.

Which of the following algorithm can be used to solve the Hamiltonian path problem efficiently?
a) branch and bound
b) iterative improvement
c) divide and conquer
d) greedy algorithm
Explanation: The Hamiltonian path problem can be solved efficiently using branch and bound
approach. It can also be solved using a backtracking approach.

Hamiltonian path problem is Np complete problem.


● Hamiltonian path problem is similar to that of a travelling salesman problem since both the problem
traverses all the nodes in a graph exactly once.
● In subset sum problem check for the presence of a subset that has sum of elements equal to a given
number. If such a subset is present then we print true otherwise false.

You might also like