DS Notes
DS Notes
DS Notes
Subject Code Subject Name Lectures (Periods) Tutorials (Periods) Practical (Periods)
IT-T33 Data Structures 3 1 0
Course Objectives:
To introduce the primary data structures and the associated operations
To understand the applications of data structures with case studies
To learn the implementation issues of the data structures introduced
Course Outcomes:
On successful completion of this course students will be able to:
Use appropriate data structures in programming
Learn various ways of implementing the data structures
Unit-I (10 Periods)
Basics : Abstract Data Type(ADT) – introduction to data structures – representation -
implementation
Stack and list: representing stack – implementation – application – balancing symbols – conversion
of infix to postfix expression – evaluating a postfix expression – recursive function call – Linked list
ADT – implementation using arrays – limitations - linked list using dynamic variables- linked
implementation of stacks – circular list – doubly linked lists
(Total: 54 Periods)
Content beyond Syllabus:
1. Advanced data structures and their implementation.
2. Implementation of the data structures in different language platforms.
Text Books:
th
1. Mark Allen Weiss, Data structures and algorithm analysis in C++, Pearson Education, 6
edition, 2011
2. YedidyahLangsam, Moshe J Augenstein and Aaron M Tanenbaum, Data Structures using C
nd
and C++, 2 edition, Prentice Hall of India, 2009.
Reference Books:
1. G.A.V.Pai, Data Structures and Algorithms – Concepts, Techniques and Applications, Tata
McGraw Hill Publishing Company Limited, New Delhi, 2008.
nd
2. Ellis Horowitz and SartajSahni, Fundamentals of Data structures, Galgotia Publications, 2
Edition, New Delhi, 2001.
3. Alfred V. Aho, Jeffrey D. Ullman, John E. Hopcroft. Data Structures and Algorithms. Addison
Wesley, 1983
Websites:
http://www.cs.sunysb.edu/~skiena/214/lectures/
http://opendatastructures.org/
http://www.cplusplus.com/doc/tutorial/structures/
IT T33- Data Structures
UNIT I
Basics: Abstract Data Type (ADT) - Introduction to Data Structures – Representation –
Implementation
Stack and List: Representation of Stack – Implementation – Application – Balancing Symbols –
Conversion of infix to postfix expression – Evaluating a postfix expression - Recursive function call
– Linked list ADT – Implementation using arrays – Limitations – Linked list using dynamic
Variables – Linked implementation of stacks – Circular list – Doubly Linked List
Objective
To introduce the primary data structures and their associated operations
Understand The concepts of linear data structure stack and its applications
To introduce the concept of different types of linked lists
Outcome:
Able to use the different types of data structures , stack and linked list
concepts
Built-in data type-The data type which is defined by the programming language itself
is called built-in data type.
Example: int, float, char, etc.
User defined data type-User defined data type can be defined using built in data
types. In other words, using the set of built-in data type user can define their own data
type.
Example:
Array Structure
int regno[10]; typedef struct Stud
float marks[10]; {
char result[20]; int a;
-----
-----;
}s;
Primary data structure is the basic data structure that directly operates upon the
machine instruction. They have different representation on different computers.
Example:
8085 8086 Motorola Dual Core
int 2 byte 3 byte 1 byte 4 byte
float 3 byte 4 byte 5 byte 6 byte
Secondary data structure are more complicated data structures derived from
primary data structures.
Example: array, structure
Tree Graph
A tree is a non-linear dynamic data A graph is similar to the tree except that
structure that may point to one or more it has no hierarchical relationship
nodes at a time. between its adjacent elements.
Operations:
1.2.2 LISTADT
In a linked list, each element is called a node. Every node in the list points to the
next node inthe list.
Operations:
Operations Axioms
createlist() Create an initialized list object.
destroylist() Free the memory allocated for the list.
getdata() Returns the data stored in the current node or index.
getcount() Returns the number of elements in the list.
inserrtfirst() Insert a new element in the first position of the list.
insertintermediate() Insert a new element in any intermediate position of the
list.
insertlast() Insert a new element in the last position of the list.
Note:The list ADT can be used as the building block for many other types of abstract
data type such as STACK ADT and QUEUE ADT.
The primary operations that can be carried out on the stack are:
1. PUSH (Insertion)
2. POP (Deletion)
3. PEEK(Display the top of the stack)
4. DISPLAY
SMVEC- Department of Information Technology 5
IT T33- Data Structures
PUSH
The procedure of inserting a new element to the top of the stack is known as Push
Operation.
While implementing push the overflow condition is checked to prevent pushing an
element ,when the stack is full
POP
The procedure of removing element from the top of the stack is called Pop
Operation
While implementing the pop operation, the underflow condition is checked to
prevent pop out an element ,when the stack is empty.
SMVEC- Department of Information Technology 6
IT T33- Data Structures
Algorithm pop(stack, top, stacksize)
{
if top==-1
{
write "Stack underflow. Pop operation not possible.";
return;
}
write "The element to be deleted.",stack[top];
rollno=stack[top];
top=top-1;
write rollno;
return;
}
PEEK
Peek operation is used to display the element from the stack pointed by the
point by top.
While implementing the peek operation, the underflow condition is checked to
prevent peek, when the stack is empty.
Algorithm peek(stack,top)
{
if top==-1
{
write "Stack underflow. Peek operation not possible.";
return;
}
write stack[top];
return;
}
DISPLAY:To read or display the element from the stack, traverse in the stack from top to
0th position.The underflow condition of the stack is to be checked to avoid retrieving an
element from empty stack.
Algorithm display(stack,top)
{
if top=-1
{
write "Stack is in underflow condition. No element to display”
return;
}
for i=top to step-1
{
write stack[top];
}
return;
}
Push Operation
Algorithm Push(struct node *top)
{ struct node *newnode;
newnode=new node;
read rollno;
newnode->data=rollno;
newnode->link=NULL;
if top==NULL
{
top=newnode;
}
else
{ newnode->link=top;
top=newnode;
}
return *top;
}
Pop Operation
Algorithm pop(struct node *top)
{
struct node *temp;
if top==NULL
{
write "Stack is in underflow condition. Pop operation is not possible.";
return;
}
temp=top;
top=top->link;
write "Delete rollno.";
delete temp;
}
Peek Operation
Algorithm peek(struct node *top)
{
struct node *temp;
if top==NULL
{
write "Stack is in underflow condition. Pop operation is not possible.";
return;
}
write top->data;
return;
}
Display Operation
Algorithm display(struct node *top)
{
struct node *temp;
if top==NULL
{
write "Stack is in underflow condition. Pop operation is not possible.";
return;
}
temp=top;
while temp!=NULL
{ write temp->data;
temp=temp->link;
}
return;
}
Balanced Parenthesis
Conversion from infix to postfix
Evaluation of arithmetic expression
Recursion
Example Expression: { 5+ ( 4 [ 3 + 2 ) * 1 ] }, { ( [ ) ] }
Unary operator- It includes only single operand (+,-) Eg: +a, -b, b--,++a
Binary Operator: It includes two or more operands(+,-,*,/) Eg: a+b, a-b*c;
Types of Expression
1. Infix expression: operand1 operator operand2 : a+b
2. Postfix expression: opeand1 operand2 operator: ab+
3. Prefix expression: operator operand1 operand2+ab
Algorithm infix_to_Postfix(instring,poststring)
{
Initially stack is empty;
Initialize the stack;
while(instring!=NULL)
{
ch=get the character from instring;
if(ch==operand)
{
append ch into poststring;
}
else if(ch=='(')
{
pop the data from the stack and append the data into post string
until we get '(' from the stack;
}
else if(ch==operator)
{
while(precedence(top of stack)>=precedence of ch)
{
pop the data from the stack and append into poststring;
}
push ch into stack;
}
}
pop all data from the stack and append into poststring until stack is empty;
}
When it comes to the evaluation of the expression, following rules are used.
pop the data from the stack and return popped data;
}
Recursive function: A recursive function is defined as a function that calls itself to solve a
smaller version of its task until a final call is made which doesn‟t require a call to itself.
From Fig 1.6 Every program to be executed the main is loaded into a program stack at
the beginning of execution. It remains there until it completes, at which time it is
popped off of the stack.
If main calls a function, that function is loaded onto the top of the stack and remains
there until it is complete at which point it is popped off of the stack.
A recursive function calls itself. That means another instance of the function will be
placed on the stack and will remain there until the function completes.
Array
The number of elements in the array is fixed and it is not possible to change the
number of elements in the array because insertion and deletion are not allowed in array.
The size of the list varies continuously when the elements are deleted or inserted.
The list is stored in a part of array. So, an array can be declared large enough to
hold maximum number of elements of a list.
SMVEC- Department of Information Technology 16
IT T33- Data Structures
During the execution of the program, the size of the list can be varied within the
space reserved for it.
One end of the array is fixed and other end of the array is constantly changed
depending upon the element inserted and deleted in the list.
Algorithm insertlast()
{
if top==listsize-1 then
{
write "List is full. Insertion is not possible";
return;
}
read rollno;
top=top+1;
roll[top]=rollno;
return;
}
Algorithm intermediate()
{
if top==listsize-1 then
{
write "List is full. Insertion is not possible.";
return;
}
read key;
for i=0 to top
{
if roll[i]==key
{ for j=top to i
{
roll[j+1]=roll[j];
}
}
read rollno;
roll[i]=rollno;
}
top=top+1;
return;
}
Algorithm deletefirst(roll,top)
{
if top==-1 then
{
write "List is empty. Deletion is not possible.";
return;
}
del data=roll[0];
for j=1 to top
{
roll[j-1]=roll[j];
}
top=top-1;
write "The data",deldata, "was deleted";
}
A linked list is a linear collection of data elements. These data elements are called
nodes.
Every node in the list points to the next node in the list.
SMVEC- Department of Information Technology 20
IT T33- Data Structures
Therefore, in a linked list every node contains two types of information:
o Data field - Data field contains actual data of an element to be stored in a
list.
o Link/Address field-It is also referred to as next address field which contains
the address of next node in the list.It is a pointer or link to the next node in
the list.
External Address- The address of the first node in the list is the external address. This
address is stored in the header pointer which points to the first node in the list. The
entire link can be accessed only with the help of the header pointer.
Internal Address- Internal address is the address stored in each and every node of the
linked list except the last node.
NULL Address- NULL address is the address stored by the NULL pointer from the last
node of the list which indicates the end point of the list.
struct node
{ int data;
struct node *next;
} *start =NULL;
Start node
This operation is used to insert a new node in the linked list at the specified position
1. Insertion at beginning of the list
2. Insertion at middle of the list
3. Insertion at end of the list
1.8.4.1 Insertion at beginning:
- From Fig 1.9 Obtain space for new node
- Assign data to the data field of the new node
- Set the next field of the new node to the beginning of the linked list
- Change the reference pointer of the linked list to point to the new node
Start
X Next
p
X Next
P
Algorithm:
Node insertmiddle(node *start, int x)
{
p=new node;
q = start;
while(key != q->data && q != null)
q=q->next;
p->next = q->next;
q->next=p
}
1.8.4.3Insertion at end:
Start q
X NULL
Fig 1.11 Insertion at End P
1.8.5 Deletion:
This operation is used to delete an item from the linked list. Here we have three situations.
Start
P Before Deletion
Start
4 Next 7 NULL
After Deletion
Fig 1.11 Deletion at First
1. From Fig 1.12 Store the address of the preceding node in a pointer variable q. Node
to be deleted is marked as key node.
Start node
q p
If(start->next == NULL)
free(start)
else
q=start;
while(q-> next != NULL)
q=q->next
p=q->next
free(p)
Start node
q
Data NULL
p
Fig 1.13 Deletion at last
q=start;
while(q!=NULL)
write q->data
q=q->next
Basic Operations:
1. Insertion
2. Deletion
3. Traversal / Display
A queue data structure can be implemented using a circular linked list with a single pointer
“rear” as the front node can be accessed through the rear node
struct node
{
int data;
struct node *next;
} *start=NULL;
1.9.1 Insertion at end:
Insertion of a node at the start or end of a circular linked list identified by a pointer to the
last node of the linked list takes a constant amount of time.
Start node
Address of the
first node
rear P
Data Next
p
Fig 1.15 Insertion at middle
Algorithm:
1.9.3 Deletion:
q start
p
Fig 1.16 Deletion based on position
Algorithm:
For some applications, especially for those where it is necessary to traverse the list in
both direction, doubly linked list works much better than singly linked list. Doubly linked list
is an advanced form of singly linked list in which each node contains three fields:
Previous Address Field
Data Field
Next Address Field
The previous link field contains the address of its previous node. This field is also
referred as backward link field. The data field stores the information part of the node.
The next field contains address of its next node in the list. This field is also referred to
as forward link field.
Basic operations
1. Insert
2. Delete
3. Traverse
One of the most striking disadvantages of these lists is that the inability to traverse the list
in the backward.
In most of the real world applications it is necessary to reverse the list both in
forward direction and backward direction. The most appropriate data structure for such an
application is a doubly linked list.
A double linked list is one in which all nodes are linked together by multiple number
of links which help in accessing both the successor node and predecessor node form the
given node position. It provides bi-directional traversing.
Each node in a double linked list has two linked fields. These are used to point to
the successor node and predecessor nodes. Prev field holds the address of the previous
node and next field holds the address of the nest node.
struct node
{
int data;
struct node *prev, *next;
} *start=NULL;
Start node
1.10.2 Insertion at middle: From Fig 1.19 Insertion and deletion are two basic operations
on such lists. Consider that a new node pointed to by p is to be inserted after the node
pointed to by q in a doubly linked list. Links of two nodes are altered during insertion.
Prev 3 next
Start q
P
Previous link
insertbeg()
{
p=(node*)malloc(sizeof(node));
p->data=x
p->prev=NULL
p->next=start;
if( start!=NULL)
start ->prev=p;
}
insertend()
{
p=(node*)malloc(sizeof(node));
p->data=x
p->next=NULL;
q=start;
while(q->next!=NULL)
q=q->next;
p->prev=q;
q->next=p
}
1.10.5 Deletion of node:
When a node, pointed to by P is to be deleted then its predecessor node x and its
successor node y will be affected.
P
Fig 1.20 Deletion based on position
p->prev->next=p->next;
p->next->prev=p->prev;
free(p);
Polynomials:
One classic example of an ordered list is a polynomial.
Definition:
A polynomial is the sum of terms where each term consists of variable, coefficient and
exponent.
Various operations which can be performed on the polynomial are,
Addition of two polynomials
Multiplication of two polynomials
Evaluation of polynomials
The Polynomial Expression can be represented using Linked list by the following
illustration,
struct list
{
int coef;
int pow;
struct list *next;
};
struct list *p1=NULL,*p2=NULL,*p=NULL;
while(p1->next || p2->next)
{
if(p1->next)
{
p->pow=p1->pow;
p->coef=p1->coef;
p1=p1->next;
}
if(p2->next)
{
p->pow=p2->pow;
p->coef=p2->coef;
p2=p2->next;
}
p->next=(struct list *)malloc(sizeof(struct list));
p=p->next;
p->next=NULL;
}
}
Definition 1:A data structure is logical way of storing and organizing data either in
computer's memory or on the disk storage so that it can be used efficiently.
Definition 2:A data structure is defined as collection of elements and all possible
operations which are required for those set of elements.
Linked list consists of a series of structures, which are not necessarily adjacent
inmemory. Each structure contains the element and a pointer to a structure containing
itssuccessor. We call this theNext Pointer. The last cell‟sNext pointer points to NULL.
It is not necessary to specify the number of elements in a linked list during its
declaration
SMVEC- Department of Information Technology 35
IT T33- Data Structures
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
11. State the difference between arrays and linked lists (November 2013)
13. List out the basic operations that can be performed on a stack (December 2014)
• Push operation
• Pop operation
• Peek operation
• Empty check
• Fully occupied check
• Infix Notation
• Prefix Notation
• Postfix Notation
16. State the rules to be followed during infix to postfix conversions (December
2014)
Rules which has to be followed while converting infix expression to postfix form :
• The expression is to be read from left to right.
• Read one character at a time from infix expression.
• Make use of stack to store the operators.
• There should not be any parenthesis in the postfix form
The difference between stacks and linked lists is that insertions and deletions may
occur anywhere in a linked list, but only at the top of the stack
18. Mention the advantages of representing stacks using linked lists than arrays
• Linked list representation of stacks can grow and shrink in size withoutwasting
memory space, depending upon the insertion and deletion that occursin the list
• Multiple stacks can be represented efficiently using a chain for each stack
Stack. Because of its LIFO (Last In First Out) property it remembers its „caller‟ so
knows whom to return when the function has to return. Recursion makes use of system
stack for storing the return addresses of the function calls. Every recursive function has its
equivalent iterative (non-recursive) function. Even when such equivalent iterative
procedures are written, explicit stack is to be used.
20. Define an Abstract Data Type (ADT) (Apr 2015)(Nov 2015)(May 2014)(December
2014)
To identify and create useful mathematical entities and operations todetermine what
classes of problems can be solved using these entities andoperations
To determine the representation of these abstract entities and to implement the
abstract operations on these concrete representation
• Towers of Hanoi
• Reversing a string
• Balanced parenthesis
• Recursion using stack
• Evaluation of arithmetic expressions
Many languages such as BASIC and FORTRAN do not support pointers. If linkedlists
are required and pointers are not available, then an alternative implementation mustbe
used known as cursor implementation.
Assignment Questions
Objective:
To introduce and understand the concept of queue and its operations
To understand the application of queues
Outcome:
Able to apply the queue data structures in programming
Enqueue (Insert)-adds a new element in the queue, this process is carried out
by incrementing the rear end and adding the new element in the rear position.
Dequeue(Delete)-delete an element from the queue, this process is carried out
by incrementing the front end and deleting the element a front end position.
Types of dequeue
1. Linear dequeue
2. Circular dequeue
Linear Dequeue
It is similar to the linear queue except the following conditions:
If the front pointer is in the first position (zeroth position), we can’t insert data at
front end.
If the rear is in the last position (queuesize-1) we can’t insert data at rear end.
Circular Dequeue
Circular dequeue is similar to circular queue except the following conditions:
Insertion and deletion are made at front and rear end of the queue.Irrespective of
the position of the front and rear we can insert and delete
Basic operations
1. Enqueue
2. Dequeue
3. Display
Initially the front end and the rear end are at the same position (-1).
The rear pointer is in the last position (size-1) then queue is said to be full, if front
pointer is (-1) then the queue is empty.
When you insert elements, rear pointer moves one by one until the last position
is reached.
When we delete elements, the front pointer moves one by one until the rear
pointer is reached.
2.2.1 Enqueue
Algorithm Enqueue(queue,rear,front,queuesize)
{
if rear==queuesize-1
{
write "queue is in overflow condition. Thus enqueue operation is not possible.";
return;
}
if rear==-1 and front==-1
{
rear=front=0;
}
else
{
rear=rear+1;
}
read rollno;
queue[rear]=rollno;
}
2.2.3 Dequeue
Algorithm Dequeue(queue,rear,front,queuesize)
{
if front==-1 || front>rear
{
write "queue is in underflow condition. Thus dequeue operation is not
possble.";
return;
}
if rear==front
{
rear=front=-1;
}
else
{
front=front+1;
}
}
2.3.3 Display
Algorithm display(struct node *front, struct node *rear)
{
struct node *temp;
if front==NULL and rear==NULL
{
write "Queue is in underflow condition. Dequeueoperaation is not possible.";
return;
}
temp=front;
while(temp!=front)
{
write temp->data;
temp=temp->link;
}
return;
}
Applications of Queue:
o When a resource is shared among multiple consumers. Examples include CPU scheduling,
Disk Scheduling.
o 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.
2.4.2 Dequeue
Algorithm dequeue(queue, front, rear, queuesize)
{
if front==-1 and rear==-1
{
write "Queue is in underflow condition. Dequeue operation is not possible.";
return;
}
write "The element to be deleted",queue[front];
if front==rear
{
front=rear=-1;
}
else
{
front=(front+1)%queuesize;
}
return;
}
2.4.3 Display
Algorithm display(queue, front, rear, queuesize)
{
if front=-1 and rear=-1
{
write "Queue is in underflow condition. Dequeue operation is not possible."
return;
}
i=front;
while(i!=rear)
{
write queue[i];
i=(i+1)%queuesize;
}
write queue[rear];
return;
}
The priority queue elements are arranged in any order and out of which only the
smallest or largest element allowed to be deleted each time.
The implementation of priority can be done by using array and linked list. The data
structure heap is used to implement priority queue efficiently.
Basic Operations
Enqueue
Dequeue
2.5.1 Enqueue
Algorithm enqueue(pqueue,front,rear,queuesize,num,prno)
{
if rear==queuesize-1;
{
write "Queue is in overflow condition. Enqueue is not possible.";
return;
}
else
{
rear=rear+1;
}
pqueue[rear].data=num;
pqueue[rear].pno=prno;
}
2.5.2 Dequeue
Algorithm dequeue(pqueue,front,rear,max=0,pos=0)
{
if front=-=1
{
write "Queue is in underflow condition. Dequeue operation is not possible.";
return;
}
if front==rear
{
front=rear=-1;
}
else
{
for(i=front to i<=rear)
{
if pqueue[i].pno>max
{
max=pqueue[i].pno;
pos=i;
}
}
for (i=pos to i<=rear)
{
pqueue[i].data=pqueue[i+1].data;
pqueue[i].pno=pqueue[i+1].pno;
}
}
}
Circular Deque
Circular deque is similar to circular queue except the following conditions:
Insertion and deletion are made at front and rear end of the queue.
Irrespective of the position of the front and rear we can insert and delete he data.
When an array of STACK[n] is used to represent two stacks, say Stack A and Stack
B. Then the value of n is such that the combined size of both the Stack[A] and Stack[B] will
never exceed n. Stack[A] will grow from left to right, whereas Stack[B] will grow in opposite
direction ie) right to left.
Here only one single one-dimensional array (Q) is used to store multiple stacks.
B (i) denotes one position less than the position in Q for bottommost element of
stacks i.
T (i) denotes the top most element of stack i.
m denotes the maximum size of the array being used.
SMVEC- Department of Information Technology 12
IT T33- Data Structures
n denotes the number of stacks.
We also assume that equal segments of array will be used for each stack.
Initially let B (i) = T (i) = [m/n]*(i-1) where 1<= i <= n.
Again we can have push or pop operations, which can be performed on each of these
stacks .
T[i] = T[i] – 1;
}
}
Free elements could be anywhere in the Queue such as before the front, after the rear,
and between front and rear in the Queue
Queue 1’s and Queue 2 ‘s size could be change if it is necessary. When the Queue 1
is full and the Queue 2 has free space; the Queue 1 can increase the size to use that
free space from the Queue 2. Same way for the Queue 2
11. List out the difference between Linked stack and Linked Queue.
Stack Queue
UNIT III
Trees : General trees – binary tree – traversal methods – expression trees – game trees.
Binary search trees – AVL trees – Splay trees – B Trees – B+ Trees – Tries – application.
Objective:
To learn the concept and application of different varieties of tree data structure
Outcome:
Able to implement the tree data structure in programming
Properties of a Tree
Any node can be the root of the tree and each node in a tree has the property that
there is exactly one path connecting that node with every other node in the tree.
The tree in which the root is identified is called a rooted tree; a tree in which the
root is not identified is called a free tree.
Each node, except the root, has a unique parent.
From Fig 3.3 Finite collection of elements / nodes that is either empty or partitioned
into three disjoint subsets.
The first subset is termed as the root element.
The remaining elements (if any) are partitioned into two binary trees.
These are called the left and right sub-trees of the binary tree.
Full binary tree: A full binary tree (2-tree or strictly binary tree) is a tree in which every
node other than the leaves has two children.
Complete binary tree: The binary tree in which every level, except possibly the last, is
completely filled, and all nodes are as far left as possible
A height-balanced binary tree is a binary tree such that, the left & right sub trees
for any given node differ in height by no more than one
Note: Each complete binary tree is a height-balanced binary tree
SMVEC- Department of Information Technology 3
IT T33- Data Structures
Fig 3.6 Height Balanced and Not Height Balanced Binary tree
Extended Binary tree:
From Fig 3.7 ,A binary tree T is said to be an extended binary tree if each node in
the tree has either no child or exactly two children.
In extended binary tree, node having two children are called internal node, and node
without children are called as external node.
Degree: The degree of node is equal to number of children that a node has.
In-degree: The number of edges arriving at that node .The root node is the only node
that has in-degree as zero
Out-degree: The number of edges leaving that node
Balancing factor in binary tree:The balance factor of a binary tree is the difference in
height between its left and right sub trees:
B HL HR
Location 0 of the array can be used to store the size of the tree in terms of total number of
nodes.
Example
Disadvantages
Insertion or deletion of a node causes considerable data movement up and down the
array, using an excessive amount of processing time.
Wastage of memory due to partially filled trees.
The advantage of using linked representation of binary tree is that insertion and deletions
involve no data movement of nodes except the rearrangement of pointers.
+ *
A * D E
B C
right.
stack.
3.Next, c, d, and e are read. A one-node 4.Continuing, a '+' is read, and it merges the
tree is created for each and a pointer to the last two
corresponding tree is pushed onto the
trees.
stack
5. Now, a '*' is read. The last two tree 6.Finally, the last symbol is read. The two
pointers are popped and a new tree is trees are merged and a pointer to the final
formed with a '*' as the root tree remain on the stack
scenarios
In game theory, a game tree is a directed graph whose nodes are positions in a
game and whose edges are moves. The complete game tree for a game is the
game tree starting at the initial position and containing all possible moves from each
position.
The diagram shows the first two levels, or ply, in the game tree for tic-tac-toe.
We consider all the rotations and reflections of positions as being equivalent, so the
first player has three choices of move: in the centre, at the edge, or in the corner. The
second player has two choices for the reply if the first player played in the centre,
otherwise five choices and so on.
The number of leaf nodes in the complete game tree is called the game tree
complexity. It is the number of possible different ways the game can be played. The
game tree complexity for tic-tac-toe is 26,830.
Game trees are important in artificial intelligence because one way to pick the best
move in a game is to search the game tree using the min-max algorithm or its
variants.
Struct BSTnode
{
int data;
struct BSTnode *left, *right;
}BSTnode;
3.6.1 Insertion:
Check whether the root node of the binary search tree is NULL.
If the condition is true, it mean, that the binary search tree is empty hence
consider the new node as the root node.
Compare the new node data with root node data, for the following three condition
If the content of the new node is equal to the root node insertion operation
is terminated.
If the content of the new node is lesser than the root node data. Check
whether the left child of the root node is null. If it is true insert the new
node.
If the content of the newnode data is greater than the root node data, check
whether the right child of the root node is NULL. If the condition is true,
insert the new data.
Since the elements in the tree are ordered ,searching is faster and easier
Insertion and deletion are also fast.
Example for Insertion in Binary search Tree:
Insert 10 , Since the tree is 12>10 , insert to the right 5<10,insert to the left since
empty make it as root node since the right is empty the left is empty
3.6.2 Deletion:
Memory is to be released for the node to be deleted. A node can be deleted from the tree
from three different places namely,
Search the parent of the leaf node, and make the link to the leaf node or NULL.
Release the memory for the deleted node(Fig 3.17)
Check whether the root node of the binary search tree is NULL. If yes, binary search
tree is empty.
If the binary search tree is not empty, compare the new node data with root node data
for the following three condition.
o If the content of the new node data is equal to the root node data,
search element is found.
o If the content of the new node data is lesser than the root node
data, check the left child.
o If the content of the new node data is greater than the root node
data, check the right child.
This function returns to address of the node with smallest value in the tree.
AVL tree: G.M. Adelson-Velskii and E.M. Landis, who discovered them in 1962.
A binary search tree, in which the heights of the left and right sub-trees of the
root differ by at most 1.The left and right sub-trees of the root are again AVL trees
An AVL tree may or (in certain cases) may not be balanced.
Calculating balance factor for AVL tree:
In binary search tree every node has a balancing factor of -1 ,0,1 is said to be
height balanced.
A node with any other balance factor is said to be unbalanced tree and thus
required to be rebalancing
Structure of AVL Node:
struct avlnode
{
int data;
struct avl_node *left;
struct avl_node *right;
}*root;
The basic insertion algorithm is similar to that of a binary search tree; during
insertion, the new node is inserted as the leaf node, as nodes are randomly
added or removed the balance factor can get disturbed.
Balance factor will get disturbed when, the new node is added to a sub-tree of the
root, that is higher than the other sub-tree and the new node increases the height of
the sub-tree.
Critical Node: Initially the node was heavy (either left or right) and the new node has been
inserted in the heavy sub-tree thereby creating an unbalanced sub-tree. Such a node is
said to be a critical node.
LR rotations: Inserted node is in the right sub-tree of the left sub-tree of root node A.
RL rotations: Inserted node is in the left sub-tree of the right sub-tree of root node A.
3.7.4 RR Rotation:
An LL imbalance occurs at a node A such that A has a balance factor -2 and a left child
B with a balance factor -1 or 0,this type of imbalance can be fixed by performing a
single right rotation at A.
avlnode*avlTree::RR_rotation(avlnode*parent)
{
avl_node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
3.7.5 LL Rotation:
An RR imbalance occurs at a node A such that A has a balance factor +2 and a right
child B with a balance factor +1 or 0, this type of imbalance can be fixed by performing
a single left rotation at A.
avl_node*avlTree::LLrotation(avlnode*parent)
{
avl_node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
3.7.6 LR Rotation:
An LR imbalance occurs at a node A such that A has a balance factor -2 and a left
child B with a balance factor +1,
Assume B‘s right child is C. This type of imbalance can be fixed by performing a
double rotation at A (first a single left rotation at B and then a single right rotation
at A)
avlnode*avlTree::LR_rotation(avlnode
*parent)
{
avl_node *temp;
temp = parent->left;
parent->left = rr_rotation (temp);
return ll_rotation (parent);
}
3.7.7 RL Rotation:
Splay Tree is a self - adjusted Binary Search Tree in which every operation on an
element re-arrange the tree so that the element is placed at the root position of the tree.
In a splay tree, every operation is performed at root of the tree. All the operations
on a splay tree are involved with a common operation called "Splaying".
Splaying an element is the process of bringing it to the root position by
performing suitable rotation operations.
In a splay tree, splaying an element rearrange all the elements in the tree so that splayed
element is placed at root of the tree. With the help of splaying an element we can bring
most frequently used element closer to the root of the tree so that any operation on
those element performed quickly. That means the splaying operation automatically brings
more frequently used elements closer to the root of the tree.
Advantages
A splay tree gives good performance for search, insert and delete operations
Disadvantages
While sequentially accessing all the nodes of the tree tree becomes completely
unbalanced
For uniform access, the performance of a splay tree will be considerably worse.
Zig Rotation
Zag Rotation
Zig - Zig Rotation
Zag - Zag Rotation
Zig - Zag Rotation
Zag - Zig Rotation
The Zig Rotation in a splay tree is similar to thesingle right rotation in AVL Tree
rotations. In zig rotation every node moves one position to the right from its current
position.
The Zag Rotation in a splay tree is similar to the single left rotation in AVL Tree
rotations. In zag rotation every node moves one position to the left from its current
position. Consider the following example...
3.9 B-Trees
B-Tree:
A B-tree is a specialized m- way tree that is widely used for disk access.
A B tree of order m can have maximum m-1 keys and m pointers to its sub-trees.
A B-tree may contain a large number of key values and pointers to sub-trees.
A B-tree is designed to store sorted data and allows search, insert, and delete
operations.
All insertions start at a leaf node. To insert a new element, search the tree to find the
leaf node where the new element should be added. Insert the new element into that node
with the following steps:
1. Insert the new element in the node, keeping the node's elements ordered.
2. Otherwise the node is full, evenly split it into two nodes so:
o A single median is chosen from among the leaf's elements and the new
element.
o Values less than the median are put in the new left node and values greater
than the median are put in the new right node, with the median acting as a
separation value.
o The separation value is inserted in the node's parent, which may cause it to be
split, and so on. If the node has no parent (i.e., the node was the root), create a
new root above this node (increasing the height of the tree).
3.9.4 Deletion in B-Tree:
Like insertion, deletion must be on a leaf node. If the key to be deleted is not in a
leaf, swap it with either its successor or predecessor (each will be in a leaf).
The successor of a key k is the smallest key greater than k.
The predecessor of a key k is the largest key smaller than k.
In a B-tree the successor and predecessor, if any, of any key is in a leaf node
Deletion Algorithm:
1. If the key k is in node x and x is a leaf, delete the key k from x.
2. If the key k is in node x and x is an internal node, do the following:
Since M=3
Key = (M-1) = 3-1 = 2
Pointers = M = 3
Case 1: Delete a key at a leaf – no underflow. Within this first case, no properties are
violated if we just remove the key:
Case 2: Delete non-leaf key – no underflow. Within this second case, we face the
problem of re-assigning empty key "slots" because there are pointers to leaves that need
to be kept (this is different than in case 1 where we just deleted the key "slot").
When filling the empty key, we have two different options for choosing which key we want
to use as a replacement:
Note: If the node from which we took a key now also has an empty "slot" that needs to be
filled (i.e., it wasn't a leaf), just repeat the same steps above recursively until we hit a leaf
node!
Note that, in the case above, the sibling was "rich" enough to provide a key to the parent in
order for the parent to be able to give a key to the empty node.
3.10 B+Trees
A B+ tree is a balanced tree in which every path from the root of the tree to a leaf is
of the same length, and each non-leaf node of the tree has between [n/2] and [n] children,
where n is fixed for a particular tree. It contains index pages and data pages. The capacity
of a leaf has to be 50% or more.
Operations in B+ Tree
• Search:
o logd(n) – Where d is the order, and n is the number of entries
• Insertion:
o Find the leaf to insert into
o If full, split the node, and adjust index accordingly
o Similar cost as searching
• Deletion
o Find the leaf node
o Delete
o May not remain half-full; must adjust the index accordingly
3.10.2 Insertion
Since insert a value into a B+ tree may cause the tree unbalance, so rearrange the
tree if needed.
3.11 Tries: Tries are an extremely special and useful data-structure that are based on
the prefix of a string. They are used to represent the ―Retrieval” of data and thus the
name Trie.
Prefix : The prefix of a string is nothing but any n letters n≤|S| that can be considered
beginning strictly from the starting of a string. For example , the word ―abacaba‖ has the
following prefixes:
a
ab
aba
abac
A Trie is a special data structure used to store strings that can be visualized like a
graph. It consists of nodes and edges. Each node consists of at max 26 children and
edges connect each parent node to its children. These 26 pointers are nothing but pointers
for each of the 26 letters of the English alphabet A separate edge is maintained for every
edge.
Strings are stored in a top to bottom manner on the basis of their prefix in a trie. All
prefixes of length 1 are stored at until level 1, all prefixes of length 2 are sorted at until
level 2 and so on.
Two Marks
1. Define a tree
A tree is a collection of nodes. The collection can be empty; otherwise, a treeconsists
of a distinguished node r, called the root, and zero or more nonempty (sub) treesT1,
T2,…,Tk, each of whose roots are connected by a directed edge from r.
2. Define root
This is the unique node in the tree to which further sub-trees are attached.
4. Define leaves
These are the terminal nodes of the tree. The nodes with degree 0 are always the leaves.
18. What are the tasks performed during inorder traversal? (November 2013)
• Traverse the left sub-tree
• Process the root node
• Traverse the right sub-tree
19. What are the tasks performed during postorder traversal? (November 2013)
• Traverse the left sub-tree
• Traverse the right sub-tree
• Process the root node
26.Traverse the given tree using Inorder, Preorder and Postorder traversals.
Inorder : D H B E A F C I G J
Preorder: A B D H E C F G I J
Postorder: H D E B F I J G C A
27. In the given binary tree, using array you can store the node 4 at which
location?(May 2014)
At location 6
1 2 3 - - 4 - - 5
where LCn means Left Child of node n and RCn means Right Child of node n
Assignment Questions
Unit IV
Sorting: O notation – efficiency of sorting – bubble sort – quick sort – selection sort –
heap sort – insertion sort – shell sort – merge sort – radix sort.
Objective:
Outcome:
Able to differentiate the different sorting algorithms
Know the suitable sorting method for specific application
Efficiency of an algorithm
Time Complexity - It is the amount of CPU time required to complete the execution of the
algorithm.
Computing Space Complexity: The space required for an algorithm is calculated based
on fixed and variable part of the program.
Fixed part:
Eg 1:
Algorithm ComputeABC(a,b,c)
{
Sum = a+b+c;
}
S(ComputeABC) = C + Sp(instance characteristics)
= C + Sp(0)
=0
Eg 2:
Algorithm Sum(a,n)
{
Sum = 0;
for i=0 to n
{
Sum = Sum + a[i];
}
return Sum;
}
Instance Code a n Sum i Total
1(n=5) C 10 2 2 2 10+6
2(n=10) C 20 2 2 2 20+6
3(n=100) C 200 2 2 2 200+6
4(n=1000) C 2000 2 2 2 2000+6
5(n=10000) C 20000 2 2 2 20000+6
S(Sum) = C + Sp(n)
=C+n+3
=n+3
= O (n)
Computing Time Complexity: Time complexity of any program is the sum of compile
time and run time.
The compile time remains constant for all instance of an algorithm (assume that a
compiled program can be run many times without recompilation).So compile time can be
ignored.
Count method
Step table method
Program Step
It is defined as syntactically and semantically meaningful segment of a program that
has execution time which is independent of the instance characteristics of the problem.
Number of program steps assigned for any program statement depends on kind of
statement.
Count method
Count is a global variable with initial value „0‟. Whenever the statement in the
algorithm is executed, the count is incremented by the program step assigned for that
statement.
Eg:
Algorithm Sum(a,n)
{
Sum = 0; -1
for i=0 to n - n+1
{
Sum = Sum + a[i]; -n
}
return Sum; -1
}
t(Sum) = 1+ n + 1 + n + 1
= 2n + 3
= O (n)
f(n) = O(g(n)), iff there exist a positive constant c,n 0 such the f(n) <= c*g(n) for all
n>= n0.
Eg: f(n) = 2n + 3
f(n) = O(n).
Ω - Omega Notation
f(n) = Ω(g(n)), iff there exist a positive constant c,n0 such the f(n) >= c*g(n) for all
n>= n0.
Ө - Omega Notation
f(n) = Ө(g(n)), iff there exist a positive constant c 1,c2,n0 such the
c1*g(n)<= f(n) <= c2*g(n).
Relation between time complexity in Big „oh‟ notation :
O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) < O(2n).
4.2 Sorting
Sorting a sequence of elements means rearranging them in either ascending or
descending order depending upon the relationship among the data items present in the
group.
Sorting algorithm
A sorting algorithm is defined as an algorithm that puts elements of a list in a certain order
that can either be numerical order, lexicographical order or any user-defined order.
Types of Sorting:
Internal sorting - which deals with sorting the data stored in computer‟s memory
External sorting - This deals with sorting the data stored in files.
Application of Sorting
Speeding up the search process is the most important application of sorting.
Algorithm BubbleSort(a,n)
{
for(i=1 to n)
{
for(j=1 to n-i)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}
Analysis (Efficiency)
Best case: O (n2)
Average case: O (n2)
Worst case: O (n2)
Algorithm QuickSort(low,high)
{
if(low<high)then
{
j=partition(a,low,high+1);
QuickSort(low,j-1);
QuickSort(j+1,high);
}
}
Algorithm partition(a,m,p)
{
v=a[m];
i=m;
j=p;
repeat
Algorithm Interchange(a,i,j)
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Step 3,
If left element belongs to LEFT group, then
increment left index.
If right index element belongs to RIGHT,
then decrement right.
Exchange when you find elements that
belong to the other group. Step 4:
Element 33 belongs to RIGHT group.
Element 22 belongs to LEFT group.
Exchange the two elements.
Step 5:
After the exchange, increment left marker,
decrement right marker
Step 6:
Element 35 belongs to RIGHT group.
Element 12 belongs to LEFT group.
Exchange, increment left, and decrement
right.
Step 7: Step 8:
Element 29 belongs to RIGHT. When the left and right markers pass each
Element 19 belongs to LEFT. other, we are done with the partition task.
Exchange ,increment left, decrement right. Swap the right with pivot.
Step 9:
Apply Quick sort to the LEFT and RIGHT groups, recursively.
Analysis (Efficiency)
Complete Binary tree: A Binary tree with n nodes of depth k is complete if its nodes
correspond to the nodes which are numbered from 1 to n in a full binary tree of depth k
(nodes are numbered starting from nodes at level 1 then go to nodes at level 2 and so
on… from left to right).
Eg:
7
8 12
3 1 12
55
Heap:
There are two types of Heap
(i) Max-Heap
(ii) Min-Heap
Heap Property:
Max-Heap
A Max-heap is a complete binary tree with a property that each node is at least
as large as the values at its children (if they exist)
Min-Heap
A Min-heap is a complete binary tree with a property that each node is at least
as small as the values at its children (if they exist).
4.6.1 Heap Sort Algorithm
Algorithm HeapSort(a,n)
{
Heapify(a,n);
for i = n to 0,step-1 do
{
t=a[i];
a[i]=a[1];
a[1]=t;
adjust(a,1,i-1);
}
}
SMVEC- Department of Information Technology 12
IT T33- Data Structures
Algorithm Heapify(a,n)
{
for i =n/2 to 1,step-1 do
{
adjust(a,i,n);
}
}
Algorithm adjust(a,i,n)
{
j=2i;
item=a[i];
while(j<=n)do
{
if((j<n)and(a[j]<a[j+1]))
{
break;
}
a[j/2]=a[j];
j=2j;
}
a[j/2]=item;
}
Example: Sort the list a[1:6] = {5, 7, 6, 20, 10, 25} using heap sort.
Step 1: Construct a complete binary tree for the given set of elements
7 6
20 10 25
55
Step 2: Convert the complete binary tree into max-heap using adjust.
Adjust Adjust
Adjust
Adjust
Adjust
Adjust
Since all the non-leaf node(root) in the complete binary tree satisfy the max-heap
property, it becomes max-heap and the first largest element in the list is moved into
the root.
Interchange the element in the first root with the element in the n th position node
Interchange
Since, the nth element is in sorted order, it is not considered for the remaining
sorting process. Therefore the node is deleted from the tree.
Step 3: Construct a complete binary tree for the element 1 to n-1 and convert it to max-
heap
Adjust
Since all root in the complete binary tree satisfy the heap property, it becomes max-
heap and second largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-1)th position node
Interchange
Since, the (n-1)th element is in sorted order, it is not considered for the remaining sorting
process. Therefore the node is deleted from the tree.
Step 4: Construct a complete binary tree for the element 1 to n-2 and convert it to max-
heap
Adjust
Since all root in the complete binary tree satisfy the heap property, it becomes max-
heap and third largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-2)th position node
Interchange
Since, the (n-2)th element is in sorted order, it is not considered for the remaining sorting
process. Therefore the node is deleted from the tree.
Step 5: Construct a complete binary tree for the element 1 to n-3 and convert it to max-
heap
Adjust
Since all root in the complete binary tree satisfy the heap property, it becomes max-
heap and fourth largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-3)th position node
Interchange
Since, the (n-3)th element is in sorted order, it is not considered for the remaining sorting
process. Therefore the node is deleted from the tree.
Step 6: Construct a complete binary tree for the element 1 to n-4 and convert it to max-
heap
Since all root in the complete binary tree satisfy the heap property, it becomes max-heap
and fourth largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-4)th position node
Interchange
Since, the (n-4)th element is in sorted order, it is not considered for the remaining sorting
process. Therefore the node is deleted from the tree.
Finally the heap consists of single element i.e. the n th largest element which is placed in 1st
position i.e. the (n-5)th position of array
Step 1: Assume that first element in the list is in sorted portion of the list and remaining all
elements are in unsorted portion.
Step 2: Consider first element from the unsorted list and insert that element into the sorted
list in order specified.
Step 3: Repeat the above process until all the elements from the unsorted list are moved
into the sorted list.
Algorithm InsertionSort(a,n)
{
for i=2 to n
{
key = a[i];
j=i-1;
while (j>0) and (key < a[j])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
}
Example: Sort the elements a[1:5] = {56, 91, 35, 72, 48}
i j a[1] a[2] a[3] a[4] a[5] key< a[i] Operation
{a[j+1]=a[j]…(t)
a[j+1]=key…(f) &
(no more previous
element)
2 1 56 91 35 72 48 91<56 (f) a[2] = key (=91)
56 91 35 72 48 End of pass -
3 2 56 91 35 72 48 35<91 (t) a[3] = a[2] (=91)
1 56 91 91 72 48 35<56 (t) a[2] = a[1] (=56)
0 56 56 91 72 48 No more a[1] = key (=35)
previous
element
35 56 91 72 48 End of pass -
4 3 35 56 91 72 48 72<91 (t) a[4] = a[3] (=91)
2 35 56 91 91 48 72<56 (f) a[3] = key (=72)
35 56 72 91 48 End of pass -
Analysis (Efficiency)
Analysis (Efficiency)
Best case: O (n log n)
Average case: O (n2)
Worst case: O (n2)
Example: Sort the list of elements a[1:6] = {70, 50, 40, 30, 35, 25}.
dist= n/2 = 6/2 = 3;
dist I j a[1] a[2] a[3] a[4] a[5] a[6] j>0 key< a[i] No operation
3 1 1 70 50 40 30 35 25 1>0(t) 30<70 a[j+dist]=a[j]
(t) a[4]=70
j=j-dist (j= -2)
- 70 50 40 70 35 25 -2>0(f) - a[j+dist]=key
2 a[1]=30
- 30 50 40 70 35 25 - - i=2;j=2;key=35;
2 2 30 50 40 70 35 25 2>0(t) 35<50 a[j+dist]=a[j]
(t) a[5]=50
j=j-dist (j= -1)
- 30 50 40 70 50 25 -1>0(f) - a[j+dist]=key
1 a[2]=35
- 30 35 40 70 50 25 - i=3;j=3;key=25;
3 3 30 35 40 70 50 25 3>0(t) 25<40 a[j+dist]=a[j]
(t) a[6]=40
j=j-dist (j= 0)
0 30 35 40 70 50 40 0>0 (f) - a[j+dist]=key
a[3]=25
- 30 35 25 70 50 40 - - i=4;
dist=dist/2 (=1)
1 1 1 30 35 25 70 50 40 1>0 (t) 35<30 a[j+dist]=key
(f) a[2]=35
i=2;j=2;key=25
2 2 30 35 25 70 50 40 2>0 (t) 25<35 a[j+dist]=a[j]
(t) a[3]=35
j=j-dist (j=1)
1 30 35 35 70 50 40 1>0 (t) 25<30 a[j+dist]=a[j]
(t) a[2]=30
j=j-dist (j=0)
Divide: Divide the n-element sequence to be sorted into two subsequences of n/2
elements each
Conquer: Sort the two subsequences recursively using merge sort.
Combine: Merge the two sorted subsequences to produce the sorted answer.
If n=1 terminate (every one-element list is already sorted)
Algorithm MergeSort(low,high) Algorithm Merge(low,mid,high)
{ {
if(low<high) h=low;
{ i=low;
mid=(low+high)/2; j=mid+1;
MergeSort(low,mid);
while((h<=mid)&&(j<=high))
MergeSort(mid+1,high); {
Merge(low,mid,high); if(a[h]<a[j])then
} {
} b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)then
{
for(k=j to high)
{
b[i]=a[k];
i=i+1;
} }
else
{ for(k=h to mid)
{
b[i]=a[k];
i=i+1;
} }
for(k=low to high)
{
a[k]=b[k];
}
}
3 27 38 43 9 10 82
i = 0; h = 0; j = 4;
Step 1:
h<=mid && j <=high
0<=3(t) && 4<=6(t)
a[h]<a[j]
3<9 (t)
b[i]=a[h]
b[0]=3
h=h+1(=1)
i=i+1(=1)
3
Step 2:
h<=mid && j <=high
1<=3(t) && 4<=6(t)
a[h]<a[j]
27<9 (f)
b[i]=a[j]
b[1]=9
j=j+1(=5)
i=i+1(=2)
SMVEC- Department of Information Technology 23
IT T33- Data Structures
3 9
Step 3:
h<=mid && j <=high
1<=3(t) && 5<=6(t)
a[h]<a[j]
27<10 (f)
b[i]=a[j]
b[2]=10
j=j+1(=6)
i=i+1(=3)
3 9 10
Step 4:
h<=mid && j <=high
1<=3(t) && 6<=6(t)
a[h]<a[j]
27< 82(t)
b[i]=a[h]
b[3]=27
h=h+1(=2)
i=i+1(=4)
3 9 10 27
Step 5:
h<=mid && j <=high
2<=3(t) && 6<=6(t)
a[h]<a[j]
38< 82(t)
b[i]=a[h]
b[4]=38
h=h+1(=3)
i=i+1(=5)
3 9 10 27 38 Analysis (Efficiency)
Step 6:
h<=mid && j <=high Best case: O (n log n)
3<=3(t) && 6<=6(t) Average case: O (n log n)
a[h]<a[j] Worst case: O (n log n)
43< 82(t)
b[i]=a[h]
b[5]=43
h=h+1(=4)
i=i+1(=6)
3 9 10 27 38 43
Step 7:
h<=mid && j <=high
4<=3(f) && 6<=6(t)
h>mid
4>3 (t)
SMVEC- Department of Information Technology 24
IT T33- Data Structures
k=6
b[i]=a[k]
b[6]=82
i=i+1 (=7)
3 9 10 27 38 43 82
Algorithm RadixSort(a,n)
{
// To find th maximum element
max=a[1];
for i=2 to n
{
if(a[i]>max)
{
max=a[i];
}
}
// To find the maximum no.of digits in the given element
digit = 0;
while(max>0)
{
digit=digit+1;
max=max/10;
}
// To sort the element using modulo division
divisor = 1;
for(p=1 to digit)
{
for(i=0 to 9)
{
bsize[i]=0;
}
for(i=1 to n)
{
r=((a[i]/divisor)%10);
bucket[r][bsize[r]]=a[i];
bsize[r]++;
}
a[i]=bucket[k][j];
i=i+1;
} } } }
Example: Sort the list a[1:8]={70, 80, 5, 268, 322, 32, 100, 45} using radix sort.
i max a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[i]>max
1 70 70 80 5 268 322 32 100 45 80>70 (t)
i++;
max=80;
2 80 70 80 5 268 322 32 100 45 5>80 (f)
i++;
3 80 70 80 5 268 322 32 100 45 268>80 (t)
i++;
max=268
4 268 70 80 5 268 322 32 100 45 322>268 (t)
i++;
max=322
5 322 70 80 5 268 322 32 100 45 32>322 (f)
i++
6 322 70 80 5 268 322 32 100 45 100>322 (f)
i++
7 322 70 80 5 268 322 32 100 45 45>322 (f)
Here max=322.
Since, digit=3
Step 3:
Sort the list of elements based on each least significant digit.
Change all the elements in the list into 3-digit number by adding required no of 0‟s
to its left.
Based on the first least significant digit in the list, the elements in the list are placed
in the corresponding buckets
100
080 032 045
070 322 005 268
0 1 2 3 4 5 6 7 8 9
After the Pass 1, move the elements from bucket into array using FIFO
a[1:8] = {070,080,100,322,032,005,045,268}
Pass 2
005
100 322 032 045 268 070 080
0 1 2 3 4 5 6 7 8 9
After the Pass 2, move the elements from bucket into array using FIFO
a[1:8] = {100,005,322,032,045,268,070,080}
Pass 3
080
070
045
032
005 100 268 332
0 1 2 3 4 5 6 7 8 9
After the Pass 3, move the elements from bucket into array using FIFO to get sorted order
of the array
a[1:8] = {005,032,045,070,080,100,268,332}.
Analysis (Efficiency)
Time Complexity
Algorithm Data Structure
Best Average Worst
18. What are the two main classifications of sorting based on the source of data?
Internal sorting- Internal sorting is a process of sorting the data in the main memory.
19. What are the various factors to be considered in deciding a sorting algorithm?
Programming time
Execution time of the program
Memory needed for program environment
20. What is the main idea behind insertion sort? When can we use insertion sort?
The main idea of insertion sort is to insert in the i th pass the i th element in A (1) A
(2)...A (i) in its rightful place. Insertion sort is useful only for small files or very nearly sorted
files.
Instead of sorting the entire array at once, it is first divide the array into smaller
segments, which are then separately sorted using the insertion sort.
23. What is the purpose of quick sort and list its advantages? (April 2015)
The purpose of the quick sort is to move a data item in the correct direction, just
enough for to reach its final place in the array.
Advantage :Quick sort reduces unnecessary swaps and moves an item to a greater
distance, in one move.
The average efficiency of heap sort is 0 (n(log2 n)) where, n is the number of elements
sorted.
1. structure property
2. heap order property
A heap in which the parent has a larger key than the child's is called a max heap.
A heap in which the parent has a smaller key than the child's is called a min heap.
Assignment Questions
UNIT V
Hashing: Introduction – Hash function – methods - Hash table implementation - rehashing.
Graph: Directed and un directed graph – representation of graphs – graph traversals: Depth
first search – Breadth first search – transitive closure – spanning trees – application -
topological sorting.
Objective:
This course is aimed to cover a variety of different problems in Graph Theory.
To learn the concept and design graph, traversals and its different applications
Outcomes:
The student will have a strong background of graph theory which has diverse
applications in the areas of computer science, biology, chemistry, physics, sociology,
and engineering.
5.1 Hashing:
Hashing is used for storing relatively large amounts of data in a table called a
hash table. Hashing is a technique used to perform insertions, deletions, and finds the
element in constant average time.
Hash table:
Hash Table is a data structure in which keys are mapped to array positions by a
hash function.
Hash table is usually fixed as M-size, which is larger than the amount of data that we
want to store.
Hash function:
Hash function is a mathematical formula, produces an integer which can be used as an
index for the key in the hash table.
Perfect Hash Function- Each key is transformed into a unique storage location
Imperfect hash Function- Maps more than one key to the same storage location
2=52%10
8=68%10
9=99%10
4=84%10
Square is 256. Then the address as 56(two digits starting form mid of 256).
In this the key is actually positioned into number of parts, each post having the same
length as their of the required address. Add the value of each parts, ignoring the final carry
to get the required address.
Partitioned:12,34,56,78
Collision : It is a situation in which the hash function returns the same hash key for
more than one record, it is called as collision. Sometimes when we are going to resolve
the collision it may lead to an overflow condition and this overflow and collision condition
makes the poor hash function.
If there is a problem of collision occurs then it can be handled by apply some technique.
These techniques are called as collision resolution techniques.
For example: Consider the keys to be placed in their home buckets are
131, 3, 4, 21, 61, 24, 7, 97, 8, 9
Then we will apply a hash function as
H(key) = key mod D
where D is the size of table. The hash table will be: Here D = 10
SMVEC- Department of Information Technology 3
IT T33- Data Structures
Linear probing
Quadratic probing
Double hashing
Example: Consider a hash table with size = 10. Using linear probing insert the keys 72, 27,
36, 24, 63, 81 and 92 into the table.
Step1: Key = 72
h(72, 0) = (72 mod 10 + 0) mod 10
= (2) mod 10
=2
Step2: Key = 27
h(27, 0) = (27 mod 10 + 0) mod 10
= (7) mod 10
=7
Since, T[7] is vacant, insert key 27 at this location
Step3: Key = 36
h(36, 0) = (36 mod 10 + 0) mod 10
= (6) mod 10
=6
Since, T[6] is vacant, insert key 36 at this location
Step4: Key = 24
h(24, 0) = (24 mod 10 + 0) mod 10
= (4) mod 10
=4
Since, T[4] is vacant, insert key 24 at this location
Step5: Key = 63
h(63, 0) = (63 mod 10 + 0) mod 10
= (3) mod 10
=3
Since, T[3] is vacant, insert key 63 at this location
Step6: Key = 81
h(81, 0) = (81 mod 10 + 0) mod 10= (1) mod 10 = 1
Since, T[1] is vacant, insert key 81 at this location
Step7: Key = 92
h(92, 0) = (92 mod 10 + 0) mod 10= (2) mod 10 = 2
Now, T[2] is occupied, so we cannot store the key 92 in T[2]. Therefore, try again for next
location. Thus probe, i = 1, this time.
SMVEC- Department of Information Technology 5
IT T33- Data Structures
Key = 92
h(92, 1) = (92 mod 10 + 1) mod 10= (2 + 1) mod 10 = 3
Now, T[3] is occupied, so we cannot store the key 92 in T[3]. Therefore, try again for next
location. Thus probe, i = 2, this time.
Key = 92
h(92, 2) = (92 mod 10 + 2) mod 10 = (2 + 2) mod 10 = 4
Now, T[4] is occupied, so we cannot store the key 92 in T[4]. Therefore, try again for next
location. Thus probe, i = 3, this time.
Key = 92
Clustering: The main problem with linear probing is clustering, many consecutive elements
form groups and it starts taking time to find a free slot or to search an element.
• If quadratic probing is used and the table size is prime, then a new element can
always be inserted if the table is at least half empty.
• If the table is even one more than half full, the insertion could fail [prime].
Fig 5.2 Quadratic Probing
For each addition of a new entry to the map, check the load factor.
If it‟s greater than its pre-defined value (or default value of 0.75 if not given), then
Rehash.
For Rehash, make a new array of double the previous size and make it the new
bucket array.
Then traverse to each element in the old bucketArray and call the insert() for each so
as to insert it into the new larger bucket array.
Consider we have to insert the elements 37, 90, 55, 22, 17, 49 and 87. The table size is 10
and will use hash function,
The old table size is 10 then we should double this size for new table, that becomes
20. But 20 is not a prime number, we will prefer to make the table size as 23. And new
hash function will be
Keys are stored inside the hash table as well All the keys are stored only inside the hash
as outside the hash table. table. No key is present outside the hash
table.
The number of keys to be stored in the hash The number of keys to be stored in the
table can even exceed the size of the hash hash table can never exceed the size of
table. the hash table.
Some buckets of the hash table are never Buckets may be used even if no key maps
used which leads to wastage of space. to those particular buckets.
5.3 Graph
• A data structure that consists of a set of nodes (vertices) and a set of edges that
relate the nodes to each other
• The set of edges describes relationships among the vertices
• Trees are special cases of graphs.
Definition of graph.
Graph G
SMVEC- Department of Information Technology 10
IT T33- Data Structures
Sub Graph : A sub-graph of G is a graph G„ such that V(G‟) V(G ) and E(G „) E(G).
Complete graph: a graph in which every vertex is directly connected to every other
vertex .The Complete graph can be directed or undirected.
Weighted graph: a graph in which each edge carries a cost for traveling between the
nodes.
In a directed or undirected graph if there is path from every vertex to other vertex
then it is called as strongly connected.
If a directed graph is not strongly connected , but the underlying graph(without
direction to arcs) is connected then the graph is said to weakly connected
Forest in graph: A forest is an acyclic undirected graph (not necessarily connected), i.e.,
each connected component is a tree.
5.4 Representation of graphs
There are two representations of graphs:
Adjacency matrix representation
Adjacency lists representation
Representing the graph as adjacency matrix
In this representation, each graph of n nodes is represented by an n x n matrix A, that is,
a two-dimensional array A. The nodes are labeled 1,2,…,n.
• A[i][j] = 1 if (i,j) is an edge
• A[i][j] = 0 if (i,j) is not an edge
Data Structures for Graphs as Adjacency Matrix
• A two-dimensional matrix or array that has one row and one column for each node in
the graph
• For each edge of the graph (Vi, Vj), the location of the matrix at row i and column j is
1
• All other locations are 0
• For an undirected graph, the matrix will be symmetric along the diagonal
• For a weighted graph, the adjacency matrix would have the weight for edges in the
graph, zeros along the diagonal, and infinity (∞) every place else
Advantage:
– Simple to implement
– Easy and fast to tell if a pair (i,j) is an edge: simply check if A[i][j] is 1 or 0
Disadvantage:
Even if there are few edges, the matrix takes O(n2) in memory
Backtracking is a general algorithm for finding all (or some) solutions to some
computational problem,. Here backtrack means that when you are moving forward and there
are no more nodes along the current path, you move backwards on the same path to find
nodes to traverse. All the nodes will be visited on the current path till all the unvisited nodes
have been traversed after which the next path will be selected.
This recursive nature of DFS can be implemented using stacks.
Steps involved in Depth-First Traversal:
Step1: Select an unvisited node x, visit it, and treat as the current node
Step2: Find an unvisited neighbor of the current node, visit it, and make it the new current
node;
Step 3: If the current node has no unvisited neighbors, backtrack to the its parent, and make
that parent the new current node;
Step 4: Repeat steps 3 and 4 until no more nodes can be visited.
Step 5: If there are still unvisited nodes, repeat from step 1.
Algorithm
// Given an undirected graph G = (V.E) with n vertices and an array visited (n) initially
set to zero
//This algorithm visits all vertices reachable from v .
//G and VISITED are global >
//VISITED (v) 1
procedure DFS(G,v):
label v as discovered
for all edges from v to w in G.adjacentEdges(v) do
if vertex w is not labeled as discovered then
recursively call DFS(G,w)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
}
Output of a depth-first search: The depth first search of a graph outputs a spanning tree
of the vertices reached during the search.
Note: The BFS algorithm uses a queue data structure to store intermediate results as it
traverses the graph
Wars hall's algorithm is an efficient method for computing the transitive closure of a
relation. Wars hall's algorithm takes as input the matrix MR representing the relation and
outputs the matrix MR of the relation R*the transitive closure of R.
Warshall's algorithm determines whether there is a path between any two nodes in
the graph. It does not give the number of the paths between two nodes
Algorithm Warshall(a[1..n,1..n])
{
R(0) = A
for i = 1 to n
{
for j = 1 to n
{
for k = 1 to n
{
R(k) = R(k-1)[i,j] or R(k-1)[i,k] and R(k-1)[j,k]
}
}
}
}
From the Fig 5.18 Application of warshall‟s algorithm to the digraph is shaown. New ones
are in bold.
A Minimum Spanning Tree (MST) is a sub-graph of an undirected graph such that the
sub-graph spans (includes) all nodes, is connected, is acyclic, and has minimum total
edge weight.
Note: The minimum spanning tree may not be unique. However, if the weights of all the
edges are pair wise distinct, it is indeed unique
There are two popular techniques for constructing a minimum cost spanning tree.
Prim’s algorithm
Kruskal’s algorithm
Prim’s Algorithm.
Prim's algorithm for finding an MST is a greedy algorithm.
Start by picking any vertex r to be the root of the tree. •
While the tree does not contain all vertices in the graph find shortest edge leaving
the tree and add it to the tree.
Algorithm Complexity:The running time is O(|V|2) without heaps, which is optimal for
dense graphs, and O(|E| log |V|) using binary heaps, which is good for sparse graphs.
A directed graph with cycle cannot be sorted. The below Figure show as an example where
topological sorting cannot be performed in cyclic graph
Two Marks
1. Define Hashing.
Hashing is the transformation of string of characters into a usually shorterfixed length
value or key that represents the original string. Hashing is used toindex and retrieve items in
a database because it is faster to find the item using theshort hashed key than to find it
using the original value.
11. What are the types of collision resolution strategies in open addressing?
• Linear probing
• Quadratic probing
• Double hashing
19.Classify the Hashing Functions based on the various methods by which the key
value is found.
Direct method,
Subtraction method,
Modulo-Division method,
Digit-Extraction method,
Mid-Square method,
Folding method,
Pseudo-random method.
Traversing a graph is an efficient way to visit each vertex and edge exactly once.
The two graph traversal techniques are
a.Breadthfirstsearch
b. Depth first search
29. What is a minimum spanning tree and list two algorithms to find minimum
spanning tree?
A minimum spanning tree of an undirected graph G is a tree formed from
graph edges that connects all the vertices of G at the lowest total cost.
Two algorithms to find minimum spanning tree
Kruskal‟salgorithm
Prim‟s algorithm
30. List the two important key points of depth first search.
i) If path exists from one node to another node, walk across the edge – exploring
the edge.
ii) If path does not exist from one specific node to any other node, return to the
previous node where we have been before – backtracking.
31.DifferentiateBFSandDFS.(November2015)
Assignment Questions
1. Given input {4371, 1323, 6173, 4199, 4344, 9679, 1989} and a hash function h(x) = x
(mod ( ) 10), show the resulting
a. separate chaining hash table
b. hash table using linear probing
c. hash table using quadratic probing
d. hash table with second hash function h2(x) = 7 − (x mod 7)
What are the advantages and disadvantages of the various collision resolution
strategies?
2. Find a minimum spanning tree for the graph in Figure using both Prim‟s and Kruskal‟s
algorithms. b. Is this minimum spanning tree unique? Why?
4. Perform the BFS and DFS graph traversal on the following graph
Part-B
November 2018
Part-A
11. Explain the operation of inserting an element at front, middle and at the rear in a
double linked list with example
12. Illustrate the algorithm to display the content of a stack with example
13. Briefly explain the operation of queue with example
14. Explain the following concept with illustration.
a) Linked List
b) Priority queue
15. Explain the process of finding the minimum and maximum element of binary search
tree
16. a) Paraphrase Splay tree with example
b) Discuss the B+ tree and compare with B tree
17. Explain the insert and delete operation of heap with example
18. With a neat diagram explain about radix sort with example.
19. Describe about the application of hashing and explain about the rehashing.
20. Write short notes on the following:
a) Breadth first search with example
b) Spanning tre
May 2018
1. What do you understand by abstract data type?Give examples
2. What is recursion?
3. What do you understand by “front=rear” in a queue system?
4. What is a circular queue?
5. What do you mean by a complete binary tree?Give an example.
6. What is a splay tree?
7. What is meant by sorting? What are its types?
8. Write down the total number of comparisons performed by bubble sorting?
9. What is rehashing?
10. Define siblings and path length.
PART B (5 X 11=55)
11. (a)How to add two polynomials using singly linked list? Explain.
(b)Write a procedure to insert a node in to a doubly linked list.
12. (a)Write a procedure to convert an infix to postfix form. Give an example.
(b)Write a note on reverse polish notation.
13. (a)Write the procedure to manipulate the queue.
(b)List the applications of queue ADT.
14. (a)How to implement a queue using a linked list?
(b)What is a circular queue? How to manipulate the same?
15. What is meant by traversals? Explain the binary tree traversals with examples.
16. (a)Draw a binary tree for the following infix expression A/B**C*D+E
(b)Explain the concept of rotation in AVL trees with examples
November 2017
Part A(10 X 2 = 20)
Answer All Questions
1. What is a Stack?
2. List the applications of stack
3. Mention the use of priority queue.
4. Write the applications of queue.
5. What is a binary tree?
6. List the properties of AVL tree.
7. What is a bubble sort?
8. Mention the uses of radix sort?
9. Name the various hashing methods
10. What are the graph traversal methods?
PART B (5 X 11=55)
11. Explain in detail about the conversion of an expression from infix to postfix form.
12. Discuss on the linked implementation of stack
13. Write in detail about linked list implementation of queue
14. Discuss on double ended queue.
15. Explain the expression tree with examples.
16. Write about binary search trees.
17. Explain quick sort with example
18. Discuss about merge sort.
19. Explain hash table implementation and rehashing.
20. Write about representation of graphs and spanning trees.
December 2016
Part A(10 X 2 = 20)
Answer All Questions
PART B (5 X 11=55)
11. Explain the storage representations and operations on stack with necessary
algorithms.
12. (a)Explain the algorithm for inserting an element in the linked list with an
example.(7)
(b)Explain the structure of doubly linked list.(4)
13. Explain the operations on circular queue with necessary algorithms
14. (a)Explain the structure and operations on priority queue
(b)Briefly explain any one application on queue
15. Explain the three tree traversal methods with necessary algorithms
16. (a)Explain the structure and operations of binary search tree
(b)Explain the structure of AVL tree.
17. Explain heap sort algorithm with an example.
18. (a)Write and explain bubble sort algorithm.(7)
(b)Sort the following data using quick sort 95,25,86,11,45
19. Explain the hashing methods with an example
20. Explain the graph traversals methods with an example
April 2016
PART B (5 X 11=55)
11. Give a detail description on postfix operations illustrate with an expression.Also
write the program to evaluate it.
12. Summarize the concept of recursion with tower of Hanoi problem.Also write the
program for the problem
13. Give a detailed note on linked list.Also narrate the procedure of inserting and
deleting nodes from a list with suitable illustrations
17. With program coding,explain the procedure of doing bubble sort for a given set of n
numbers
18. Explain the concept of radix sort with suitable illustrations and program.
19. Describe the concept of hash function .Also explain any two hash methods in detail
20. Give a detailed note on depth first search traversal with suitable examples and
program.
November 2015
Part A (10 X 2 =20)
Answer All Questions
1. Define ADT.
2. List some application of stack.
3. What is circular queue?
4. What do you mean by priority queue?
5. Define binary search tree
6. Differentiate B tree and B+ tree
7. How will you measure the efficiency of sorting
8. What is the basic idea of shell sort
9. What is hash table?
10. Define the terms : BFS and DFS.
PART B (5 X 11 = 55)
11. Write an algorithm for converting infix expression to postfix expression with an
Example
12. Write an algorithm to perform the following operations on a singly linked list
i. Insert new node at beginning
ii. Insert new node at middle
iii. Delete a node from last
iv. Count the number of nodes.
13. Explain the operations of priority queue.
14. Write an algorithms for insertion and deletion operations in a circular queue.
15. Construct an expression tree for the expression A + ( B – C )* D + ( E * F )
16. Explain in detail about insertion in AVL trees
17. State and explain the algorithm to perform heap sort. Also analyze the time
complexity of the algorithm
18. State and explain the algorithm to perform Quick sort. Also analyze the time
complexity of the algorithm
19. Explain the minimum cost spanning Tree algorithms with an example.
20. Explain the various applications of Depth First Search
April 2015
SMVEC- Department of Information Technology 5
IT T33- Data Structures
Part A(10 X 2 = 20)
Answer All Questions
1. Define ADT.
2. What is circular linked list?
3. What do you mean by priority queues?
4. Define double ended queues.
5. Perform RR rotation in AVL tree with an example.
6. Give some of the applications of B-tree
7. List the advantages of quick sort
8. Write the worst case and best case analysis of merge sort.
9. Give the adjacency matrix for the following directed graph
V1 V2
V3 V4
20 20
20 20 20 20
16. (a) Insert the numbers into binary search tree 8,5,10,15,20,18,3
(b) What is tries? Give some of the application of tries
17. Define quick sort. Explain its algorithm with an example
18. Write the procedure for heap sort and trace the given example.
18,13,10,8,5,15.
19. (a) Define hash function and explain its method in detail.
(b) Write the routine for performing breadth first search.
December 2014
Part A (10 X 2 =20)
Answer All Questions
1. Is a relation a data structure or an abstract data type? What is a hash table?
2. Consider the following binary search tree
B F
A E G
PART B (5 X 11 = 55)
Answer all questions
11. Explain the merge sort algorithm using the following set of data. Compare the result
by constructing heap sort for the same set of data:
45,32,71,27,53,33,12,67
12. What are primitive data structures and Linear data structures? Illustrate with
examples. Also explain the significance of Abstract data structures and its uses
13. State and explain the algorithm used to perform addition of two polynomials using
Linked list
14. Explain representation of polynomials and sparse matrices with an application of its
use
15. State the operations performed in a stack. How many times you will pop a stack of
10 elements to reach the sixth element from the top ? Discuss any two
applications of stack and queue.
16. Write an algorithm to perform the operation s for a queue data structure. Explain
priority queue with an example
(b) Show the change in the B-Tree constructed by Q.19 (a) when the following keys
are deleted in the given order 28,50,75
20. (a)Construct an AVL Tree for the following set of number
1,2,3,4,5,6,7,8,9,10,11,12,13
(b) Write the for algorithm for AVL tree construction and discuss a supplication of
AVL trees
May 2014
Part A (10 X 2 =20)
Answer All Questions
1. Define abstract data type.
2. What is radix sort?
3. How do you represent sparse matrices?
4. Mention the advantages of circular linked list over singly linked list
5. What are double ended queues?
6. Differentiate between stack and queue
7. Define binary tree
8. How do you implement linked representation of binary trees?
9. What is the uses of tree indexing?
10. What is hash function? Give an example.
PART B (5 X 11 = 55)
Answer all questions
PART B (5 X 11 = 55)
Answer all questions
UNIT-I
Stack
Gate 2016 - CSE
1. Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns
the element at the head of the queue Q without removing it from Q. Similarly Top(S) returns
the element at the top of S without removing it from S. Consider the algorithm given below.
The maximum possible number of iterations of the while loop in the algorithm is______
[This Question was originally a Fill-in-the-Blanks question]
(A) 16
(B) 32
(C) 256
(D) 64
Answer: (C)
Explanation: The worst case happens when the queue is sorted in decreasing order. In worst
case, loop runs n*n times.
Queue: 4 3 2 1
Stack: Empty
321
4
3214
Empty
214
3
2143
Empty
143
2
1432
Empty
432
1
32
14
324
1
24
13
243
1
43
12
3
124
34
12
4
123
Empty
1234
Answer: (C)
Answer: (B)
Explanation: As it is stated in the question, that m and n are valid Lists but not explicitly
specified if the lists are empty or not. We can have two cases:
1. Case 1: If lists are not NULL : Invocation of join will append list m to the end of list n
if the lists are not NULL. For Example:Before join operation :
m =1->2->3->4->5->null
n =6->7->8->9->nullAfter join operation :
6->7->8->9->1->2->3->4->5->null
2. Case 2: If lists are NULL : If the list n is empty and itself NULL, then joining and
referencing would obviously create NULL pointer issue.
Gate 2017-CSE
4. Let A be an array of 31 numbers consisting of a sequence of 0’s followed by a sequence of
1’s. The problem is to find the smallest index i such that A[i] is 1 by probing the minimum
number of locations in A. The worst case number of probes performed by an optimal
algorithm is________.
Note: This questions appeared as Numerical Answer Type.
(A) 2
(B) 3
(C) 4
(D) 5
Answer: (D)
Explanation: The best way to solve such a problem is by using Binary Search. Search the sorted
array by repeatedly dividing the search interval in half. Begin with an interval covering the
whole array. If the value of the search key is less than the item in the middle of the interval,
narrow the interval to the lower half. Otherwise narrow it to the upper half.
Find mid element
Is mid = 1 ?
Is mid >1?(not possible here)
Is mid <1 ?
Proceed accordingly, Worst case of this problem will be 1 at the end of the array i.e 00000…..1
OR 1…….0000. It will take log n time worst case.
n=31, Hence log 231 = 5.
Therefore, option D is correct.
Gate 2016-CSE
5. N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided
to the record to be deleted. For a decrease-key operation, a pointer is provided to the record
on which the operation is to be performed. An algorithm performs the following operations
on the list in this order:
Θ(N) delete, O(log N) insert, O(log N) find, and Θ(N) decrease-key
What is the time complexity of all these operations put together
(A) O(Log2N)
(B) O(N)
(C) O(N2)
(D) Θ(N2 Log N)
Answer: (C)
Explanation: The time complexity of decrease-key operation is Θ(1) since we have the pointer
to the record where we have to perform the operation. However, we must keep the doubly linked
list sorted and after the decrease-key operation we need to find the new location of the key. This
step will take Θ(N) time and since there are Θ(N) decrease-key operations, the time complexity
becomes O(N²).
Note that the other three operations have a lower bound than this one.
Gate 2015-CSE
Answer: (A)
Explanation: The time complexity of insert in unsorted array is O(1), O(Logn) in Min-Heap,
O(n) in sorted array and sorted DLL.
1. For unsorted array, we can always insert an element at end and do insert in O(1) time
2. For Min Heap, insertion takes O(Log n) time. Refer Binary Heap operations for details.
3. For sorted array, insert takes O(n) time as we may have to move all elements worst case.
4. For sorted doubly linked list, insert takes O(n) time to find position of element to be
inserted.
Since number of insertion operations is asymptotically higher, unsorted array is preferred.
Gate 2015-CSE
Answer: (D)
Explanation: We only need to consider any 3 elements and compare them. So the number of
comparisons is constants, that makes time complexity as Θ(1)
The catch here is, we need to return any element that is neither maximum not minimum.
Let us take an array {10, 20, 15, 7, 90}. Output can be 10 or 15 or 20
Pick any three elements from given liar. Let the three elements be 10, 20 and 7.
Using 3 comparisons, we can find that the middle element is 10.
UNIT-II
Queue
1. A queue is implemented using a non-circular singly linked list. The queue has a head pointer
and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let
'enqueue' be implemented by inserting a new node at the head, and 'dequeue' be implemented
by deletion of a node from the tail.
Which one of the following is the time complexity of the most time-efficient implementation of
'enqueue' and 'dequeue, respectively, for this data structure?
A.Θ(1), Θ(1)
B.Θ(1), Θ(n)
C.Θ(n), Θ(1)
D.Θ(n), Θ(n)
Solution:
For Enqueue operation, performs in constant amount of time (i.e., Θ(1)), because it modifies
only two pointers, i.e.,Create a Node P.
P-->Data = Data
P-->Next = Head
Head = P
For Dequeue operation, we need address of second last node of single linked list to make NULL
of its next pointer. Since we can not access its previous node in singly linked list, so need to
traverse entire linked list to get second last node of linked list, i.e.,temp = head;
while( temp-Next-->Next != NULL){
temp = temp-Next;
}
temp-->next = NULL;
Tail = temp;
Since, we are traversing entire linked for each Dequeue, so time complexity will be Θ(n). Option
(B) is correct.
2. A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are
performed efficiently. Which one of the following statements is CORRECT (n refers to the
number of items in the queue)?
(A) Both operations can be performed in O(1) time
(B) At most one operation can be performed in O(1) time but the worst case time for the other
operation will be Ω(n)
(C) The worst case time complexity for both operations will be Ω(n)
(D) Worst case time complexity for both operations will be Ω(log n)
Answer: (A)
Explanation: We can use circular array to implement both in O(1) time. See below article for
details.
Answer: (C)
UNIT-III
Sorting
1. An array of 25 distinct elements is to be sorted using quicksort. Assume that the pivot
element is chosen uniformly at random. The probability that the pivot element gets placed in
the worst possible location in the first round of partitioning (rounded off to 2 decimal places)
is _________.
(A) 0.08
(B) 0.0016
(C) 0.04
(D) 0.0008
Answer: (A)
Explanation: Given an array of 25 distinct elements, and pivot element is chosen uniformly
randomly. So, there are only 2 worst case position in the pivot element is either first (or) last.
Therefore, required probability is,
= 2/25
= 0.08
So, option (A) is correct.
Gate 2019-CSE
2. There are n unsorted arrays: A1, A2, ….,An. Assume that n is odd. Each of A1, A2,
….,Ancontains n distinct elements. There are no common elements between any two arrays.
The worst-case time complexity of computing the median of the medians of A1, A2, ….,Anis
________ .
(A) Ο(n log n)
(B) Ο(n2)
(C) Ο(n)
(D) Ω(n2log n)
Answer: (B)
Explanation: Since given arrays are not sorted, so the time complexity to find median is O(n) in
an unsorted array. You need to apply this apgorithm n time to find all medians and again once to
find median of all these medians, so total time complexity is,
= O(n)*O(n) + O(n)
= O(n2) + O(n)
≈ O(n2)
So, option (B) is correct.
Gate 2017-CSE
3. Match the algorithms with their time complexities:
Answer: (C)
Explanation:
Tower of Hanoi – Ɵ(2n)
Heap sort worst case – Ɵ(n log n)
Binary Search – Ɵ(log n)
Addition of two nxn matrices – Ɵ (n2)
Therefore Option C is correct
Gate 2016-CSE
4. Assume that the algorithms considered here sort the input sequences in ascending order. If
the input is already in ascending order, which of the following are TRUE ?
I. Quicksort runs in Θ(n2) time
II. Bubblesort runs in Θ(n2) time
III. Mergesort runs in Θ(n) time
IV. Insertion sort runs in Θ(n) time
(A) I and II only
(B) I and III only
(C) II and IV only
(D) I and IV only
Answer: (D)
Explanation: I. Given an array in ascending order, Recurrence relation for total number of
comparisons for quicksort will be
T(n) = T(n-1)+O(n) //partition algo will take O(n) comparisons in any case.
= O(n^2)
T(n) = 2T(n/2) + Θ(n) // In-Place Merge algorithm will take Θ(n) due to copying an entire array.
= Θ(nlogn)
IV. Insertion sort runs in Θ(n) time
Whenever a new element which will be greater than all the elements of the intermediate sorted
sub-array ( because given array is sorted) is added, there won’t be any swap but a single
comparison. In n-1 passes we will be having 0 swaps and n-1 comparisons.
Total time complexity = O(n) // N-1 Comparisons
Gate 2015-CSE
5. Which one of the following is the recurrence equation for the worst case time complexity of
the Quicksort algorithm for sorting n(≥ 2) numbers? In the recurrence equations given in the
options below, c is a constant.
(A) T(n) = 2T (n/2) + cn
(B) T(n) = T(n – 1) + T(0) + cn
(C) T(n) = 2T (n – 2) + cn
(D) T(n) = T(n/2) + cn
Answer: (B)
Explanation: In worst case, the chosen pivot is always placed at a corner position and recursive
call is made for following.
a) forsubarray on left of pivot which is of size n-1 in worst case.
b) forsubarray on right of pivot which is of size 0 in worst case.
Gate 2015-CSE
6. Which one of the following is the tightest upper bound that represents the number of swaps
required to sort n numbers using selection sort?
(A) O(log n)
(B) O(n)
(C) O(nLogn)
(D) O(n^2)
Answer: (B)
Explanation: To sort elements in increasing order, selection sort always picks the maximum
element from remaining unsorted array and swaps it with the last element in the remaining
array. So the number of swaps, it makes in n-1 which is O(n)
Gate 2014-CSE
7. Assume that a mergesort algorithm in the worst case takes 30 seconds for an input of size 64.
Which of the following most closely approximates the maximum input size of a problem that
can be solved in 6 minutes?
(A) 256
(B) 512
(C) 1024
(D) 2048
Answer: (B)
c*64Log64 is 30
c*64*6 is 30
c is 5/64
For time 6 minutes
5/64*nLogn = 6*60
nLogn = 72*64 = 512 * 9
n = 512.
UNIT-IV
Binary Search Tree
Gate 2019 CSE
1. Let T be a full binary tree with 8 leaves. (A full binary tree has every level full.) Suppose two
leaves a and b of T are chosen uniformly and independently at random. The expected value
of the distance between a and b in T (i.e., the number of edges in the unique path between a
and b) is (rounded off to 2 decimal places) ___________ .
Note: This was Numerical Type question.
(A) 5.71 to 5.73
(B) 4.85 to 4.86
(C) 2.71 to 2.73
(D) 4.24 to 4.26
Answer: (D)
Explanation: Full binary tree with 8 leaf nodes,
Answer: (B)
Explanation: B+ tree is height balance search tree, where key values in each node are kept in
sorted order.
All leaf nodes are at same level and connected to next leaf node.
Each non-leaf (i.e., internal) node is of the form:
<P1, K1, P2, K2, ….., Pc-1, Kc-1, Pc>
where c <= a and each Pi is a tree pointer (i.e points to another node of the tree) and, each Ki is a
key value. That means each non-leaf (i.e., internal) nodes has only block (i.e., node or tree
pointers) and keys. These internal (i.e., non-leaf) node do not contain data record pointers. So,
option (B) is not correct.
3. The post order traversal of a binary tree is 8, 9, 6, 7, 4, 5, 2, 3, 1. The inorder traversal of the
same tree is 8, 6, 9, 4, 7, 2, 5, 1, 3. The height of a tree is the length of the longest path from
the root to any leaf. The height of the binary tree above is ________
A.2
B.3
C.4
D.5
Answer: (C)
Solution:
Explanation: Given, post-order – 8, 9, 6, 7, 4, 5, 2, 3, 1
and in-order – 8, 6, 9, 4, 7, 2, 5, 1, 3
Construct a binary tree from postorder and inordertraversal :
Gate 2018-CSE
4. The number of possible min-heaps containing each value from {1, 2, 3, 4, 5, 6, 7} exactly
once is _______.
(A) 80
(B) 8
(C) 20
(D) 210
Answer: (A)
Solution:
Explanation: Set minimum element as root (i.e 1), now 6 are remaining and left subtree will have
3 elements, each left subtree combination can be permuted in 2! ways.
Total ways to design min-heap with 7-elements = 6C_3 *2! * 2! = 20*2*2 = 80
Alternative approach – Total number of min or max heap tree with 1 to N elements are using
recurrence relation:
T(N) =(N-1)Ck * T(k) * T(N-k-1), where k = number of nodes on left subtree
T(1) = 1
T(2) = 1
T(3) = 2
T(4) = 3C2 * T(2) * T(1) = 3
T(5) = 4C3 * T(3) * T(1) = 8
T(6) = 5C3 * T(3) * T(2) = 20
T(7) = 5C3 * T(3) * T(3) = 80
So, answer is 80.
Gate 2017-CSE
5. Let T be a binary search tree with 15 nodes. The minimum and maximum possible heights of
T are:
Note: The height of a tree with a single node is 0.
(A) 4 and 15 respectively
(B) 3 and 14 respectively
(C) 4 and 14 respectively
(D) 3 and 15 respectively
Answer: (B)
Explanation:
The minimum height of a binary search tree will be when tree is full complete tree:
Max height of the binary search tree = n-1 = 15 – 1 = 14, where the tree is Skewed tree
Gate 2017-CSE
6. Let T be a tree with 10 vertices. The sum of the degrees of all the vertices in T is _____.
(A) 18
(B) 19
(C) 20
(D) 21
Answer: (A)
Gate 2016-CSE
7. B+ Trees are considered BALANCED because
(A) the lengths of the paths from the root to all leaf nodes are all equal.
(B) the lengths of the paths from the root to all leaf nodes differ from each other by at most 1.
(C) the number of children of any two non-leaf sibling nodes differ by at most 1.
(D) the number of records in any two leaf nodes differ by at most 1.
Answer: (A)
Explanation: In both B Tree and B+ trees, depth (length of root to leaf paths) of all leaf nodes is
same. This is made sure by the insertion and deletion operations.
In these trees, we do insertions in a way that if we have increase height of tree after insertion, we
increase height from root. This is different from BST where height increases from leaf nodes.
Similarly, if we have to decrease height after deletion, we move the root one level down. This is
also different from BST which shrinks from bottom.
The above ways of insertion and deletion make sure that depth of every leaf node is same.
Gate 2016-CSE
8. A complete binary min-heap is made by including each integer in [1, 1023] exactly once. The
depth of a node in the heap is the length of the path from the root of the heap to that node.
Thus, the root is at depth 0. The maximum depth at which integer 9 can appear is
_____________
(A) 6
(B) 7
(C) 8
(D) 9
Answer: (C)
Explanation: here node with integer 1 has to be at root only. Now for maximum depth of the
tree the following arrangement can be taken. Take root as level 1.
make node 2 at level 2 as a child node of node 1.
make node 3 at level 3 as the child node of node 2.
..
.. and so on for nodes 4,5,6,7
..
make node 8 at level 8 as the child node of node 7.
make node 9 at level 9 as the child node of node 8.
Putting other nodes properly, this arrangement of the the complete binary tree will follow the
property of min heap.
So total levels are 9.node 9 is at level 9 and depth of node 9 is 8 from the root.
Gate 2016-CSE
9. Consider the following New-order strategy for traversing a binary tree:
Visit the root;
Visit the right subtree using New-order
Visit the left subtree using New-order
The New-order traversal of the expression tree corresponding to the reverse polish expression 3 4
* 5 – 2 ˆ 6 7 * 1 + – is given by:
(A) + – 1 6 7 * 2 ˆ 5 – 3 4 *
(B) – + 1 * 6 7 ˆ 2 – 5 * 3 4
(C) – + 1 * 7 6 ˆ 2 – 5 * 4 3
(D) 1 7 6 * + 2 5 4 3 * – ˆ –
Answer: (C)
Explanation:
Reverse Polish expression is derived through Post-Order i.e.
1) Visit Left Node
2) Visit Right Node (L R N)
3) Visit Root Node
— Acc. to Ques. New Order algorithm is :
1) Visit Root Node
2) Visit Right Node (N R L)
3) Visit Left Node
ie. New Order Expression will be a total reverse of the Post-Order algorithm
Post-Order Expression : 34*5–2ˆ67*1+–
Hence , New Order Expression : – + 1 * 7 6 ^ 2 – 5 * 4 3
Gate 2016-CSE
10. The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty
binary search tree, such that the resulting tree has height 6, is _____________
Note: The height of a tree with a single node is 0.
(A) 2
(B) 4
(C) 64
(D) 32
Answer: (C)
7
/
[1..6]
1
\
[2..7]
Therefore count is 26 = 64
Another Explanation:
Consider these cases,
1234567
1234576
1765432
1765423
7654321
7654312
7123456
7123465
For height 6, we have 2 choices. Either we select the root as 1 or 7.
Suppose we select 7.
Now, we have 6 nodes and remaining height = 5.
So, now we have 2 ways to select root for this sub-tree also.
Now, we keep on repeating the same procedure till remaining height = 1
For this last case also, we have 2 ways.
Therefore, total number of ways = 26= 64
Gate 2015-CSE
Which of the following is/are correct inorder traversal sequence(s) of binary search tree(s)?
1. 3, 5, 7, 8, 15, 19, 25
2. 5, 8, 9, 12, 10, 15, 25
3. 2, 7, 10, 8, 14, 16, 20
4. 4, 6, 7, 9, 18, 20, 25
(A) 1 and 4 only
(B) 2 and 3 only
(C) 2 and 4 only
(D) 2 only
Answer: (A)
Gate 2015-CSE
11. What are the worst-case complexities of insertion and deletion of a key in a binary search
tree?
(A) Θ(logn) for both insertion and deletion
(B) Θ(n) for both insertion and deletion
(C) Θ(n) for insertion and Θ(logn) for deletion
(D) Θ(logn) for insertion and Θ(n) for deletion
Answer: (B)
Explanation: The time taken by search, insert and delete on a BST is always proportional to
height of BST. Height may become O(n) in worst case.
Gate 2015-CSE
12. The height of a tree is the length of the longest root-to-leaf path in it. The maximum and
minimum number of nodes in a binary tree of height 5 are
(A) 63 and 6, respectively
(B) 64 and 5, respectively
(C) 32 and 6, respectively
(D) 31 and 5, respectively
Answer: (A)
Explanation:
Number of nodes is maximum for a perfect binary tree.
A perfect binary tree of height h has 2h+1 - 1 nodes
Gate 2015-CSE
13. Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4. Now consider
that a value 35 is inserted into this heap. After insertion, the new heap is
(A) 40, 30, 20, 10, 15, 16, 17, 8, 4, 35
(B) 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
(C) 40, 30, 20, 10, 35, 16, 17, 8, 4, 15
(D) 40, 35, 20, 10, 15, 16, 17, 8, 4, 30
Answer: (B)
Explanation: The array 40, 30, 20, 10, 15, 16, 17, 8, 4 represents following heap
40
/ \
30 20
/\ /\
10 15 16 17
/\
8 4
After insertion of 35, we get
40
/ \
30 20
/\ /\
10 15 16 17
/\ /
8 4 35
After swapping 35 with 15 and swapping 35 again
with 30, we get
40
/ \
35 20
/\ /\
10 30 16 17
/\ /
8 4 15
Gate 2015-CSE
14. A binary tree T has 20 leaves. The number of nodes in T having two children is
(A) 18
(B) 19
(C) 17
(D) Any number between 10 and 20
Answer: (B)
Explanation:
Sum of all degrees = 2 * |E|.
Here considering tree as a k-arytree :
Sum of degrees of leaves + Sum of degrees for Internal Node except root + Root's degree = 2 *
(No. of nodes - 1).
Putting values of above terms,
L + (I-1)*(k+1) + k = 2 * (L + I - 1)
L + k*I - k + I -1 + k = 2*L + 2I - 2
L + K*I + I - 1 = 2*L + 2*I - 2
K*I + 1 - I = L
(K-1)*I + 1 = L
Given k = 2, L=20
==> (2-1)*I + 1 = 20
==> I = 19
==> T has 19 internal nodes which are having two children.
Gate 2015-CSE
15. With reference to the B+ tree index of order 1 shown below, the minimum number of nodes
(including the root node) that must be fetched in order to satisfy the following query: ―Get all
records with a search key greater than or equal to 7 and less than 15‖ is ________
(A) 4
(B) 5
(C) 6
(D) 7
Answer: (B)
Explanation:
We can get all values in range from 7 to 59 by accessing 5 nodes.
1) First search 7 in a leaf node.
2) Once 7 is found, linearly traverse till 15 is found.
Answer: (B)
Answer: (D)
89
/ \
19 50
/ \ / \
17 12 15 2
/ \ /\ / \
5 7 11 6 9 100
100
/ \
19 89
/ \ / \
17 12 50 5
/ \ /\ / \
7 11 6 9 2 15
Gate 2014-CSE
18. Consider a binary tree T that has 200 leaf nodes. Then, the number of nodes in T that have
exactly two children are _________.
(A) 199
(B) 200
(C) Any number between 0 and 199
(D) Any number between 100 and 200
Answer: (A)
Gate 2014-CSE
19. Consider B+ tree in which the search key is 12 bytes long, block size is 1024 bytes, record
pointer is 10 bytes long and block pointer is 8 bytes long. The maximum number of keys that
can be accommodated in each non-leaf node of the tree is
(A) 49
(B) 50
(C) 51
(D) 52
Answer: (B)
Explanation:
Let m be the order of B+ tree
m <= 51
Gate 2014-CSE
20. Consider a rooted Binary tree represented using pointers. The best upper bound on the time
required to determine the number of subtrees having having exactly 4 nodes O(naLogn b).
Then the value of a + 10b is ________
(A) 1
(B) 11
(C) 12
(D) 21
Answer: (A)
Explanation: We can find the subtree with 4 nodes in O(n) time. Following can be a simple
approach.
1) Traverse the tree in bottom up manner and find size of subtree rooted with current node
2) If size becomes 4, then print the current node.
Gate 2013-CSE
21. Which one of the following is the tightest upper bound that represents the time complexity of
inserting an object into a binary search tree of n nodes?
(A) O(1)
(B) O(Logn)
(C) O(n)
(D) O(nLogn)
Answer: (C)
Explanation: To insert an element, we need to search for its place first. The search operation
may take O(n) for a skewed tree like following.
To insert 50, we will have to traverse all nodes.
10
\
20
\
30
\
40
UNIT-V
Answer: (B)
Explanation: Since mod 10 is used, the last digit matters.If you do cube all numbers from 0
to 9, you get following
Number Cube Last Digit in Cube
0 0 0
1 1 1
2 8 8
3 27 7
4 64 4
5 125 5
6 216 6
7 343 3
8 512 2
9 729 9
Therefore all numbers from 0 to 2020 are equally divided in 10 buckets. If we make a table for
square, we don‟t get equal distribution. In the following table. 1, 4, 6 and 9 are repeated, so
these buckets would have more entries and buckets 2, 3, 7 and 8 would be empty.
Number Square Last Digit in Cube
0 0 0
1 1 1
2 4 4
3 9 9
4 16 6
5 25 5
6 36 6
7 49 9
8 64 4
9 81 1
Alternative approach –
Using concept of power of cycle:
(a) (0,1,4,9,6,5,6,9,4,1,0) repeated
(b) (0,1,8,7,4,5,6,3,2,9) repeated
(c) (0,1,4,9,6,5,6,9,4,1,0) repeated
(d) (0,2,4,6,8) repeated
So, only h(i) =i3 mod 10 covers all the digits from 0 to 9.
Option (B) is correct.
Answer: (A)
Explanation: load factor = (no. of elements) / (no. of table slots) = 2000/25 = 80
GRAPH
Gate 2019-CSE
1. Consider the following undirected graph G:
Choose a value for x that will maximize the number of minimum weight spanning trees
(MWSTs) of G. The number of MWSTs of G for this value of x is _________ .
(A) 4
(B) 5
(C) 2
(D) 3
Option (A) is correct.
Explanation: To maximize the number of minimum weight spanning trees of G, the value of x
will be 5 because it will have two more choices for corner vertex which will maximize maximum
number of MSTs.
Now, according to kruskal algorithm for MST:
1. Edges with weights 1 and 3 will be selected first,
2. Now bottom edge with weight 4 will not be selected as will cause cycle on MST,
3. both corner vertices have two-two choices to select the vertices, so these corner edges
with weights 4 and 5 will resultant 2*2 = 4 MSTs.
Therefore, total number of MSTs are 2*2 = 4, which is answer.
Gate 2019-CSE
2. Consider the weights and values of items listed below. Note that there is only one unit of
each item.
The task is to pick a subset of these items such that their total weight is no more than 11
Kgs and their total value is maximized. Moreover, no item may be split. The total value of
items picked by an optimal algorithm is denoted by V opt. A greedy algorithm sorts the items
by their value-to-weight ratios in descending order and packs them greedily, starting from
the first item in the ordered list. The total value of items picked by the greedy algorithm is
denoted by Vgreedy.
The value of Vopt − Vgreedy is ______ .
First we will pick item_4 (Value weight ratio is highest). Second highest is item_1, but cannot be
picked because of its weight. Now item_3 shall be picked. item_2 cannot be included because
of its weight.
Therefore, overall profit by Vgreedy = 20+24 = 44
Hence, Vopt – Vgreedy = 60-44 = 16
So, answer is 16.
Gate 2019-CSE
3. Let G be a simple undirected graph. Let T D be a depth first search tree of G. Let TB be a
breadth first search tree of G. Consider the following statements.
(I) No edge of G is a cross edge with respect to T D. (A cross edge in G is between two nodes
neither of which is an ancestor of the other in T D).
(II) For every edge (u, v) of G, if u is at depth i and v is at depth j in T B, then ∣i − j∣ = 1.
Which of the statements above must necessarily be true?
(A) I only
(B) II only
(C) Both I and II
(D) Neither I nor II
Answer: (A)
Explanation: There are four types of edges can yield in DFS. These are tree, forward, back,
and cross edges. In undirected connected graph, forward and back egdes are the same thing. A
cross edge in a graph is an edge that goes from a vertex v to another vertex u such that u is
neither an ancestor nor descendant of v. Therefore, cross edge is not possible in undirected
graph.
So, statement (I) is correct.
For statement (II) take counterexample of complete graph of three vertices, i.e., K3 with XYZ,
where X is source and Y and Z are in same level. Also,there is an edge between vertices Y and
Z, i.e., |i-j| = 0 ≠ 1 in BFS. So, statement became false.
Gate 2017-CSE
4. Consider the following table
Answer: (C)
Explanation:
Kruskal‟s is a greedy technique of Minimum Spanning Tree Algorithm to find an edge of
the least possible weight that connects any two trees in the forest.
QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions
the given array around the picked pivot.
Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem using
Dynamic Programming. The problem is to find shortest distances between every pair of
vertices in a given edge weighted directed Graph.
Gate 2017-CSE
5. Let G = (V, E) be any connected undirected edge-weighted graph. The weights of the edges
in E are positive any distinct. Consider the following statements:
I. Minimum Spanning Tree of G is always unique.
II. Shortest path between any two vertices of G is always unique.
Which of the above statements is/are necessarily true?
(A) I only
(B) II only
(C) both I and II
(D) neither I and II
Answer: (A)
Explanation: I. Minimum Spanning Tree of G is always unique – MST will wlways be distinct if
the edges are unique so Correct
II. Shortest path between any two vertices of G is always unique – Shortest path between any
two vertices can be same so incorrect
Alternate solution:
We know that minimum spanning tree of a graph is always unique if all the weight are distinct,
so statement 1 is correct.
Now statement 2 , this might not be true in all cases. Consider the graph.
There are two shortest paths from a to b (one is direct and other via node c) So statement 2 is
false.
Gate 2017-CSE
6. Breath First Search(BFS) has been implemented using queue data structure.
Which one of the following is a possible order of visiting the nodes in the graph above.
(A) MNOPQR
(B) NQMPOR
(C) QMNROP
(D) POQNMR
Answer: (D)
Explanation: In BFS, we print a starting node, then its adjacent, then adjacent of adjacent, and
so on..
Option A : MNOPQR Wrong
We cannot visit "O" before "R" as we start from "M".
Note that "O" is adjacent of adjacent for "M" and
"R" is adjacent of "M".
Gate 2016-CSE
7. Consider the following directed graph.
Answer: (D)
Gate 2016-CSE
8. The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:
(A) Θ(n log n), Θ(n log n) and Θ(n 2)
(B) Θ(n2), Θ(n2) and Θ(n Log n)
(C) Θ(n2), Θ(n log n) and Θ(n log n)
(D) Θ(n2), Θ(n log n) and Θ(n2)
Answer: (D)
Explanation:
Insertion Sort takes Θ(n2) in worst case as we need to run two loops. The outer loop is
needed to one by one pick an element to be inserted at right position. Inner loop is used
for two things, to find position of the element to be inserted and moving all sorted greater
elements one position ahead. Therefore the worst case recursive formula is T(n) = T(n-1)
+ Θ(n).
Merge Sort takes Θ(n Log n) time in all cases. We always divide array in two halves, sort
the two halves and merge them. The recursive formula is T(n) = 2T(n/2) + Θ(n).
QuickSort takes Θ(n2) in worst case. In QuickSort, we take an element as pivot and
partition the array around it. In worst case, the picked element is always a corner element
and recursive formula becomes T(n) = T(n-1) + Θ(n). An example scenario when worst
case happens is, arrays is sorted and our code always picks a corner element as pivot.
Gate 2016-CSE
9. Let G be a weighted connected undirected graph with distinct positive edge weights. If every
edge weight is increased by the same value, then which of the following statements is/are
TRUE?
Answer: (A)
Explanation: The shortest path may change. The reason is, there may be different number of
edges in different paths from s to t. For example, let shortest path be of weight 15 and has 5
edges. Let there be another path with 2 edges and total weight 25. The weight of the shortest
path is increased by 5*10 and becomes 15 + 50. Weight of the other path is increased by 2*10
and becomes 25 + 20. So the shortest path changes to the other path with weight as 45.
The Minimum Spanning Tree doesn‟t change. Remember the Kruskal‟s algorithm where we sort
the edges first. IF we increase all weights, then order of edges won‟t change.
Gate 2016-CSE
10. An operator delete(i) for a binary heap data structure is to be designed to delete the item in
the i-th node. Assume that the heap is implemented in an array and i refers to the i-th index
of the array. If the heap tree has depth d (number of edges on the path from the root to the
farthest leaf), then what is the time complexity to re-fix the heap efficiently after the removal
of the element?
(A) O(1)
(B) O(d) but not O(1)
(C) O(2d) but not O(d)
(D) O(d2d) but not O(2d)
Answer: (B)
Explanation:
For this question, we have to slightly tweak the delete_min() operation of the heap data
structure to implement the delete(i) operation. The idea is to empty the spot in the array at the
index i (the position at which element is to be deleted) and replace it with the last leaf in the
heap (remember heap is implemented as complete binary tree so you know the location of the
last leaf), decrement the heap size and now starting from the current position i (position that
held the item we deleted), shift it up in case newly replaced item is greater than the parent of old
item (considering max-heap). If it‟s not greater than the parent, then percolate it down by
comparing with the child‟s value. The newly added item can percolate up/down a maximum of d
times which is the depth of the heap data structure.
Thus we can say that complexity of delete(i) would be O(d) but not O(1).
Gate 2016-CSE
11. Consider the weighted undirected graph with 4 vertices, where the weight of edge {i, j} g is
given by the entry
W ij in the matrix W
The largest possible integer value of x, for which at least one shortest path between some
pair of vertices will contain the edge with weight x is ________
Answer: (B)
12. Let G be a complete undirected graph on 4 vertices, having 6 edges with weights being 1, 2,
3, 4, 5, and 6. The maximum possible weight that a minimum weight spanning tree of G can
have is.
[This Question was originally a Fill-in-the-Blanks question]
(A) 6
(B) 7
(C) 8
(D) 9
Answer: (B)
Explanation: One graph that has maximum possible weight of spanning tree
Gate 2016-CSE
13. G = (V, E) is an undirected simple graph in which each edge has a distinct weight, and e is a
particular edge of G. Which of the following statements about the minimum spanning trees
(MSTs) of G is/are TRUE
I. If e is the lightest edge of some cycle in G, then every MST of G includes e
II. If e is the heaviest edge of some cycle in G, then every MST of G excludes e
(A) I only
(B) II only
(C) both I and II
(D) neither I nor II
Answer: (B)
Explanation:
I is NOT true.
Let G=(V, E) be a rectangular graph where V = {a, b, c, d} and E = {ab, bc, cd, da, ac}.
Let the edges have weights: ab = 1, bc = 2, cd = 4, da = 5, ac = 3. Then, clearly, ac is the
lightest edge of the cycle cdac, however, the MST abcd with cost 7 (= ab + bc + cd) does not
include it.
Let the edges have weights: ab = 6, bc – 7, cd = 4, da = 5, ac = 3. Then, again, ac is the lightest
edge of the cycle cdac, and, the MST bacd with cost 13 (= ba + ac + cd) includes it.
So, the MSTs of G may or may not include the lightest edge.
II is true
Let the heavies edge be e. Suppose the minimum spanning tree which contains e. If we add
one more edge to the spanning tree we will create a cycle. Suppose we add edge e‟ to the
spanning tree which generated cycle C. We can reduce the cost of the minimum spanning tree if
we choose an edge other than e from C for removal which implies that e must not be in
minimum spanning tree and we get a contradiction.
Gate: 2016-CSE
14. Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There
is a vertex t at a distance four from the root. If t is the n-th vertex in this BFS traversal, then
the maximum possible value of n is ________
(A) 15
(B) 16
(C) 31
(D) 32
Answer: (C)
Answer: (B)
Explanation: First you need to find twins of each node. You can do this using level order
traversal (i.e., BFS) once. Time complexity of BFS is Θ(m +n).
And you have to use linked list for representation which is extra space (but memory size is
not a constraint here).
Final, time complexity is Θ(m + n) to set twin pointer.
Option (B) is correct.
Gate 2015-CSE
16. Given below are some algorithms, and some algorithm design paradigms.
List-I
A. Dijkstra‟s Shortest Path
B. Floyd-Warshall algorithm to compute all pairs shortest path
C. Binary search on a sorted array
D. Backtracking search on a graph
List-II
1. Divide and Conquer
2. Dynamic Programming
3. Greedy design
4. Depth-first search
5. Breadth-first search
Match the above algorithms on the left to the corresponding design paradigm they follow
Codes:
ABCD
(a) 1 3 1 5
(b) 3 3 1 5
(c) 3 2 1 4
(d) 3 2 1 5
(A) a
(B) b
(C) c
(D) d
Answer: (C)
Explanation:
Dijkstra‟s Shortest Path is a Greedy Algorithm.
Floyd-Warshallalgorithm is Dynamic Programming.
Binary search is a Divide and Conquer.
Backtracking is Depth-first search
Gate 2015-CSE
17. Let G = (V, E) be a simple undirected graph, and s be a particular vertex in it called the
source. For x ∈ V, let d(x) denote the shortest distance in G from s to x. A breadth first
search (BFS) is performed starting at s. Let T be the resultant BFS tree. If (u, v) is an edge
of G that is not in T, then which one of the following CANNOT be the value of d(u) – d(v)?
(A) -1
(B) 0
(C) 1
(D) 2
Answer: (D)
Explanation: Note that the given graph is undirected, so an edge (u, v) also means (v, u) is
also an edge.
Since a shorter path can always be obtained by using edge (u, v) or (v, u), the difference
between d(u) and d(v) can not be more than 1.
Gate 2015-CSE
18. The graph shown below 8 edges with distinct integer edge weights. The minimum spanning
tree (MST) is of weight 36 and contains the edges: {(A, C), (B, C), (B, E), (E, F), (D, F)}. The
edge weights of only those edges which are in the MST are given in the figure shown below.
The minimum possible sum of weights of all 8 edges of this graph is ______________.
(A) 66
(B) 69
(C) 68
(D) 70
Answer: (B)
Explanation: In every cycle, the weight of an edge that is not part of MST must by greater than
or equal to weights of other edges which are part of MST.
Since all edge weights are distinct, the weight must be greater.
So the minimum possible weight of ED is 7, minimum possible weight of CD is 16 and minimum
possible weight of AB is 10.
Therefore minimum possible sum of weights is 69.
Gate 2014-CSE
19. The number of distinct minimum spanning trees for the weighted graph below is ____
(A) 4
(B) 5
(C) 6
(D) 7
Answer: (C)
Explanation: Below diagram shows a minimum spanning tree. Highlighted (in green) are
the edges picked to make the MST.
In the right side of MST, we could either pick edge „a‟ or „b‟. In the left side, we could either pick
„c‟ or „d‟ or „e‟ in MST.
There are 2 options for one edge to be picked and 3 options for another edge to be picked.
Therefore, total 2*3 possible MSTs.
Gate 2014-CSE
20. Consider the tree arcs of a BFS traversal from a source node W in an unweighted,
connected, undirected graph. The tree T formed by the tree arcs is a data structure for
computing.
(A) the shortest path between every pair of vertices.
(B) the shortest path from W to every vertex in the graph.
(C) the shortest paths from W to only those nodes that are leaves of T.
(D) the longest path in the graph
Answer: (B)
Explanation: BFS always produces shortest path from source to all other vertices in an
unweighted graph.
Question : 2014-CSE
21. Let G be connected undirected graph of 100 vertices and 300 edges. The weight of a
minimum spanning tree of G is 500. When the weight of each edge of G is increased by five,
the weight of a minimum spanning tree becomes ________.
(A) 1000
(B) 995
(C) 2000
(D) 1995
Answer: (B)
Explanation: Since there are 100 vertices, there must be 99 edges in Minimum Spanning
Tree (MST).
When weight of every edge is increased by 5, the increment in weight of MST is = 99 * 5 = 495
So new weight of MST is 500 + 495 which is 995
Gate 2014-CSE
22. Let G=(V,E) be a directed graph where V is the set of vertices and E the set of edges. Then
which one of the following graphs has the same strongly connected components as G ?
(A) A
(B) B
(C) C
(D) D
Answer: (B)
Explanation: If we reverse directions of all arcs in a graph, the new graph has same set of
strongly connected components as the original graph.
Gate 2014-CSE
23. Let G be a graph with n vertices and m edges. What is the tightest upper bound on the
running time on Depth First Search of G? Assume that the graph is represented using
adjacency matrix.
(A) O(n)
(B) O(m+n)
(C) O(n2)
(D) O(mn)
Answer: (C)
Explanation: Depth First Search of a graph takes O(m+n) time when the graph is
represented using adjacency list.
In adjacency matrix representation, graph is represented as an “n x n” matrix. To do DFS, for
every vertex, we traverse the row corresponding to that vertex to find all adjacent vertices (In
adjacency list representation we traverse only the adjacent vertices of the vertex). Therefore
time complexity becomes O(n2)
Question 1 : 2014-CSE
24. Consider the directed graph given below. Which one of the following is TRUE?
Answer: (C)
Explanation: The graph doesn‟t contain any cycle, so there exist topological ordering.
P and S must appear before R and Q because there are edges from P to R and Q, and from S
to R and Q.
Content beyond Syllabus
Advanced Data Structure - Dictionary
A dictionary is a general-purpose data structure for storing a group of objects. A dictionary has a
set of keys and each key has a single associated value. When presented with a key, the
dictionary will return the associated value.
For example, the results of a classroom test could be represented as a dictionary with pupil's
names as keys and their scores as the values:
Instead of using the numerical index of the data, we can use the dictionary names to return
values:
>>> results['Nova']
84
>>> results['Elsa']
29
A dictionary is also called a hash, a map, a hashmap in different programming languages (and
an Object in JavaScript). They're all the same thing: a key-value store.
The concept of a key-value store is widely used in various computing systems, such as caches
and high-performance databases.
Typically, the keys in a dictionary must be simple types (such as integers or strings) while the
values can be of any type. Different languages enforce different type restrictions on keys and
values in a dictionary. Dictionaries are often implemented as hash tables.
Keys in a dictionary must be unique; an attempt to create a duplicate key will typically overwrite
the existing value for that key.
Note that there is a difference (which may be important) between a key not existing in a
dictionary, and the key existing but with its corresponding value being null.
Retreive results['Nova']
Insert results['Nova'] = 99
import java.util.*;
class arrayStack
{
protected int arr[];
protected int top, size, len;
/* Constructor for arrayStack */
public arrayStack(int n)
{
size = n;
len = 0;
arr = new int[size];
top = -1;
}
/* Function to check if stack is empty */
public boolean isEmpty()
{
return top == -1;
}
/* Function to check if stack is full */
public boolean isFull()
{
return top == size -1 ;
}
/* Function to get the size of the stack */
public int getSize()
{
return len ;
}
/* Function to check the top element of the stack */
public int peek()
{
if( isEmpty( ) )
throw new NoSuchElementException("Underflow Exception");
return arr[top];
}
/* Function to add an element to the stack */
public void push(int i)
{
if(top + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
if(top + 1 < size )
arr[++top] = i;
len++ ;
}
/* Function to delete an element from the stack */
public int pop()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
len-- ;
return arr[top--];
}
/* Function to display the status of the stack */
public void display()
{
System.out.print("\nStack = ");
if (len == 0)
{
System.out.print("Empty\n");
return ;
}
for (int i = top; i >= 0; i--)
System.out.print(arr[i]+" ");
System.out.println();
}
}
/* Class StackImplement */
public class StackImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Stack Test\n");
System.out.println("Enter Size of Integer Stack ");
int n = scan.nextInt();
/* Creating object of class arrayStack */
arrayStack stk = new arrayStack(n);
/* Perform Stack Operations */
char ch;
do{
System.out.println("\nStack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to push");
try
{
stk.push( scan.nextInt() );
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 2 :
try
{
System.out.println("Popped Element = " + stk.pop());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = " + stk.peek());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = " + stk.isEmpty());
break;
case 5 :
System.out.println("Full status = " + stk.isFull());
break;
case 6 :
System.out.println("Size = " + stk.getSize());
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* display stack */
stk.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);