Professional Documents
Culture Documents
DSA by Nikhar_Bhatnagar
DSA by Nikhar_Bhatnagar
2
3
4
5
Program Outcomes/Program Specific Outcomes – Indicators - Competencies
PO 10: Communication: Communicate effectively on 10.1.2 Produce clear, well-constructed, and well- supported written engineering documents
complex engineering activities with the engineering 10.1.3 Create flow in a document or presentation
community and with the society at large, such as,
being able to comprehend and write effective reports 10.2.1 Listen to and comprehend information, instructions, and viewpoints of others
and design documentation, make effective 10.2.2 Deliver effective oral presentations to technical and non- technical audiences
presentations, and give and receive clear
instructions. 10.3.1 Create engineering-standard figures, reports and drawings to complement writing and
presentations
10.3.2 Use a variety of media effectively to convey a message in a document or a presentation
11.1.1 Describe various economic and financial costs/benefits of an engineering activity
PO 11: Project management and finance:
Demonstrate knowledge and understanding of the 11.1.2 Analyze different forms of financial statements to evaluate the financial status of an
engineering and management principles and apply engineering project
these to one’s own work, as a member and leader in a 11.2.1 Analyze and select the most appropriate proposal based on economic and financial
team, to manage projects and in multidisciplinary considerations.
environments. Identify the tasks required to complete an engineering activity, and the resources
11.3.1
required to complete the tasks.
11.3.2 Use project management tools to schedule an engineering project so it is completed on
time and on budget.
12.1.1 Describe the rationale for requirement for continuing professional development
PSO1: Core Engineering Skills: Acquire basic PSO1.1.1 Possess the concepts of Data Structure and Database Management System
concepts of Data Structures, Databases, Operating Possess the concepts of core engineering subjects including Operating System,
Systems, Computer Network, Theory of Computation, PSO1.1.2
Computer Networks and Software Engineering.
Advanced Programming and Software Engineering.
PSO1.1.3 Apply basic programming skills to solve real world problems
PSO2: Standard Software Engineering practices: PSO2.1.1
Demonstrate an ability to design, develop, test, Apply fundamental software engineering concepts to solve real world problem
debug, deploy, analyse, troubleshoot, maintain, and PSO2.1.2 Possess conceptual knowledge for designing, analysing and testing a software
secure mobile applications and software solutions for
automation applications. PSO2.1.3 Estimate and evaluate the cost related to a Software
PSO3.1.1 Recognise the need and feasibility of project and apply standard practices for software
project development
PSO3: Project Endeavours: Provide a platform for
students to develop new and innovative projects as PSO3.1.2
per current industry needs. Identify the functional and nonfunctional requirement of current industry trends.
PSO3.1.3 Recognise the challenges of changing trends and career opportunities as per current
industry needs.
Bloom’s Taxonomy (Revised)
Leve
l Descriptor Level of Attainment Keywords
List, define, tell, describe, recite, recall, identify, show, label, tabulate, quote, name,
1 Remembering Recalling from memory
who, when, where, etc.
describe, explain, paraphrase, restate, associate, contrast, summarize, differentiate
2 Understanding Explaining ideas or concepts interpret, discuss
Using information in another familiar Calculate, predict, apply, solve, illustrate, use, demonstrate, determine, model,
3 Applying situation experiment, show, examine, modify
Breaking information into parts to
explore understandings and
4 analysing relationships classify, outline, break down, categorize, analyze, diagram, illustrate, infer, select
assess, decide, choose, rank, grade, test, measure, defend, recommend, convince,
5 Evaluating Justifying a decision or course of action select, judge, support, conclude, argue, justify, compare, summarize, evaluate
Generating new ideas, products or views Design, formulate, build, invent, create, compose, generate, derive, modify,
6 Creating to do things develop, integrate
** It may be noted that some of the verbs in the above table are associated with multiple Bloom’s Taxonomy level. These verbs are actions that could apply to
different activities. We need to keep in mind that it’s the skill, action or activity we need out students to demonstrate that will determine the contextual
meaning of the verb used in the assessment question.
DSA Tutorial Sheets with EMD Analysis
UNIT I
9 Define queue.
E
It is a linear data structure that maintains a list of
elements such that insertion happens at
rear end and deletion happens at front end.
FIFO – First In First Out principle
10 What are the operations of a queue?
E
The operations of a queue are
● isEmpty()
● isFull()
● insert()
● delete()
● display()
11 Write the routine to insert a element onto a queue. E
void insert(int element)
{
if(front==-1 )
{
front = rear = front +1;
queue[front] = element; return;
}
if(rear==99)
{
printf(“Queue is full”); getch();
return;
}
rear = rear +1;
queue[rear]=element;
}
12 What are the types of queue?
E
The following are the types of queue:
● Double ended queue
● Circular queue
● Priority queue
13 Define double ended queue
E
● It is a special type of queue that allows insertion and
deletion of elements at both
Ends.
● It is also termed as DEQUE.
STACK QUEUE
Insertion and deletion are made Insertion at one end rear and
at one end. deletion at other end front.
UNIT II
EMD Analysis
S.
Question
No.
1 What is a data structure?
● A data structure is a method for organizing and storingdata which
would allow efficient
data retrieval and usage.
● A data structure is a way of organizing data that considers not E
only the items stored, but
also their relationships to each other.
struct polynomial
{
int coefficient;int exponent;struct polynomial *next;
};
21 What are the operations performed in list? M
The following operations can be performed on a list
i. Insertion
a. Insert at beginning
b. Insert at end
c. Insert after specific node
d. Insert before specific node
ii. Deletion
a. Delete at beginning
b. Delete at end
c. Delete after specific node
d. Delete before specific node
iii. Merging
iv. Traversal
Header of the linked list is the first element in the list and it
stores the number of elements in the list. It points to the first data
element of the list.
UNIT III
SEARCHING, SORTING AND HASHING TECHNIQUES
19 What is sorting? M
Sorting is the process of arranging the given items in a logical order.
Sorting is an example where the analysis can be precisely performed.
20 What is mergesort?
M
The mergesort algorithm is a classic divide conquer strategy. The
problem is divided into two arrays and merged into single array
21 Compare the various hashing techniques.
M
Technique Load Factor
Separate chaining - close to 1
Open Addressing - should not exceed 0.5
Rehashing - reasonable load factor
22 Define collision in hashing. M
When two different keys or identifiers compute into the same
location or address in the hash table through any of the hashing
functions, then it is termed Collision.
23 Define Double Hashing.
M
Double Hashing is a collision-resolution technique used in open
addressing category. In double hashing, we apply a second hash function
to x and probe at a distance of hash2 (x),
2hash2 (x)… ..........., and so on.
24 What are applications of hashing? M
The applications of hashing are,
● Compliers use hash table to keep track of declared variables on
source code.
● Hash table is useful for any graph theory problem, where the
nodes have real names instead of numbers.
● Hash tables are used in programs that play games.
● Online spelling checkers use hashing.
25 What does internal sorting mean?
M
Internal sorting is a process of sorting the data in the main
memory
26 What are the various factors to be considered in deciding a
M
sorting algorithm?
Factors to be considered in deciding a sorting algorithm are,
1. Programming time
2. Executing time for program
3. Memory or auxiliary space needed for the programs
environment.
27 How does the bubble sort get its name?
M
The bubble sort derives its name from the fact that the
smallest data item bubbles up to the top of the sorted array.
28 What is the main idea behind the selection sort? M
The main idea behind the selection sort is to find the smallest entry among
in a(j),a(j+1),… ................. a(n) and then interchange it with a(j).
This process is then repeated for each value of j.
29 Is the heap sort always better than the quick sort?
M
No, the heap sort does not perform better than the quick sort.
Only when array is nearly sorted to begin with the heap sort algorithm
gains an advantage. In such a case, the quick deteriorates to its worst
performance of O (n2).
30 Name some of the external sorting methods.
M
Some of the external sorting methods are,
1. Polyphase sorting
2. Oscillation sorting
3. Merge sorting
31 Define radix sort
M
Radix Sort is aclever and intuitive little sorting algorithm.
Radixsortisaoncomparativeintegersortingalgorithmthatsorts
data with integer keys by grouping keys by the individual digits
which share the same significant position
32 Define searching
Searching refers to determining whether an element is present in a M
given list of elements
or not. If the element is present, the search is considered as successful,
otherwise it is considered as an unsuccessful search. The choice of a
searching technique is based on the following factors
a. Order of elements in the list i.e., random or sorted
b. Size of the list
3 Explain hashing D
5 Write a C program to sort the elements using bubble sort, insertion sort and D
radix sort.
UNIT IV
S. EMD Analysis
Question
No.
1 Define non-linear data structure
Data structure which is capable of expressing more complex
E
relationship than that of physical adjacency is called non-linear data
structure.
2 Define tree?
E
A tree is a data structure, which represents hierarchical
relationship between individual data items.
3 Define leaf?
E
In a directed tree any node which has out degree o is called a
terminal node or a leaf.
4 Explain the representations of priority queue. E
Using Heap structure, Using Linked List
5 List out the steps involved in deleting a node from a binary search E
tree.
1. t has no right hand child node t->r == z
2. t has a right hand child but its right hand child node has no left sub
tree
t->r->l == z
3.t has a right hand child node and the right hand child node has a
left hand child node t->r->l != z
6 Convert the infix expression (A-B/C)*(D/E-F) into a postfix. E
Postfix: ABC/-DE/F-*
7 What are the steps to convert a general tree into binary tree? E
* use the root of the general tree as the root of the binary tree
* determine the first child of the root. This is the leftmost node in the
general tree at the next
level
* insert this node. The child reference of the parent node refers to this
node
* continue finding the first child of each parent node and insert it below
the parent node with the
child reference of the parent to this node.
* when no more first children exist in the path just used, moveback to the
parent of the last node
entered and repeat the above process. In other words,
determine the first sibling of the last
node entered.
* complete the tree for all nodes. In order to locate where the node fits you
must search for the
first child at that level and then follow the sibling references to a nil
where the next sibling can
be inserted. The children of any sibling node can be inserted by
locating the parent and then
inserting the first child. Then the above process is repeated.
What is meant by directed tree?
E
8 ed tree is an acyclic diagraph which has one node called its root with in
degree o while all other nodes have in degree I.
9 What is a ordered tree?
E
In a directed tree if the ordering of the nodes at each level is
prescribed then such a tree is called ordered tree.
10 What are the applications of binary tree? E
1. Binary tree is used in data processing.
2. File index schemes
3. Hierarchical database management system
11 What is meant by traversing?
E
Traversing a tree means processing it in such a way, that each node is
visited only once.
12 What are the different types of traversing?
E
The different types of traversing are
a. Pre-order traversal-yields prefix form of expression.
b. In-order traversal-yields infix form of expression.
c. Post-order traversal-yields postfix form of expression.
13 What are the two methods of binary tree implementation? E
UNIT V-GRAPHS
S.
EMD
N Question
Analysis
o.
1 Define Graph?
A graph G consist of a nonempty set V which is a set of nodes of
the graph, a set E which is the set of edges of the graph, and a mapping E
from the set for edge E to a set of pairs of elements of V. It can also be
represented as G= (V, E).
2 Explain the topological sort. E
It is an Ordering of vertices in a directed acyclic graph such that if
there is a path from vi to vj, then vj appears after vi in the ordering.
3 Define NP E
NP is the class of decision problems for which a given proposed
solution for a given input can be checked quickly to see if it is really a
solution.
4 Define biconnected graph.
E
A connected undirected graph is biconnected if there are no
vertices whose removal disconnects the rest of the graph.
5 Define shortest path problem?
E
For a given graph G=(V, E), with weights assigned to the edges
of G, we have to find the shortest path (path length is
defined as sum of the weights of the edges) from any given source
vertex to all the remaining vertices of G.
6 Mention any two decision problems which are NP-Complete. E
NP is the class of decision problems for which a given proposed
solution for a given input can be checked quickly to see if it is really a
solution
7 Define adjacent nodes?
E
Any two nodes which are connected by an edge in a graph are called
adjacent nodes. For E is associated with a pair of nodes∈example, if and
edge x (u,v) where u, v V, then we say that the edge x connects the nodes u
and v.∈
8 What is a directed graph? E
A graph in which every edge is directed is called a directed graph.
9 What is a undirected graph?
E
A graph in which every edge is undirected is called a directed graph.
10 What is a loop?
E
An edge of a graph which connects to itself is called a loop or sling.
6 Write and explain the prim’s algorithm and depth first search
D
algorithm.
Primitive DS Non-Primitive DS
Integer
Linear Non Linear
Float
Char Array
Tree
Pointers Linked list
Graph
Stack
Queue
• Queue is a linear data structure.
• It is used for temporary storage of data values.
• A new element is added at one end called rear
end.
• The existing elements deleted from the other
end called front end.
• First-in-First-out property. rear
34 12 53 61 9 23 42
front
Operations on Queue
1.Insertion :
Placing an item in a queue is called “insertion or
enqueue”, which is done at the end of the queue
called “rear”.
Front
Rear
2.Deletion :
Removing an item from a queue is called
“deletion or dequeue” , which is done at the
other end of the queue called “front”.
Front
Rear
3
12 9 7 18 14
45 56
front rear
Queue after deletion of anelement
Algorithm to Enqueue an element in queue
STEP-1 IF REAR= MAX-1 INITIALLY
write OVERFLOW REAR = -1
go to step 4 FRONT = -1
[end of if]
STEP-2 if REAR = -1
set FRONT=REAR= 0
else
set REAR = REAR+1
STEP-3 set QUEUE [ REAR ] = NUM
STEP-4 EXIT
Algorithm to delete an element from queue
STEP-1 If FRONT = -1 or FRONT > REAR
write UNDERFLOW
Else
set VAL = QUEUE [ FRONT ]
set FRONT = FRONT + 1
[end of if]
STEP-2 EXIT
Types of Queues
1. Circular Queue
2. Deque
3. Priority Queue
Drawback of Linear Queue
When a new item is inserted at the rear, the pointer
to rear moves upwards.
Similarly, when an item is deleted from the queue the
front arrow moves downwards.
After a few insert and delete operations the rear
might reach the end of the queue and no more items
can be inserted although the items from the front of
the queue have been deleted and there is space in
the queue.
Circular Queue
To solve this problem, queues implement wrapping
around. Such queues are called Circular Queues.
Both the front and the rear pointers wrap around to
the beginning of the array.
It is also called as “Ring buffer”.
Items can inserted and deleted from a queue in O(1)
time.
CircularQueue
0 1 2 3 4
10 Initial queue
f r
0 1 2 3 4
10 20 30 After inserting 20 & 30
f r
14
CircularQueue
0 1 2 3 4
f r
0 1 2 3 4
f r
0 1 2 3 4
60 56 30 40 50
After inserting 60
r f
15
Algorithm to insert an element in
Circular queue
STEP-1 If FRONT = 0 and REAR= MAX-1, F=R+1
write OVERFLOW go to
step 4 INITIALLY
[end of if] FRONT= -1
REAR= -1
STEP-2 If FRONT = -1 and REAR= -1
SET REAR = FRONT = 0
Else If REAR = MAX-1 and FRONT!=0
Set REAR=0
Else
Set REAR=REAR+1
[ End of If ]
STEP-3 CQ[REAR] = NUM
STEP-4 EXIT
CONDITIONS FOR DEQUE IN CQ
1. 1. FRONT(35)
2. 2.
1 REAR(2) FRONT(4)
CONDITIONS FOR DEQUE IN CQ
Algorithm to delete an element from queue
STEP-1 If FRONT = -1
write UNDERFLOW
Go to step 4
[ end of if ]
STEP-2 Set VAL=QUEUE[FRONT]
STEP-3 If FRONT = REAR
FRONT = REAR= -1
Else
If FRONT = MAX-1
Set FRONT=0
Else
Set FRONT=FRONT+1
[End of If]
[End of If]
STEP-4 EXIT
DEQUES
1.Deque stands for double ended queue.
2.Elements can be inserted or deleted at
either end.
3. Also known as head-tail linked list.
insertion insertion
34 12 53 61 9
front rear
deletion deletion
DEQUES
No element can be added and deleted from the
middle.
Implemented using either a circular array or a
circular doubly linked list.
In a deque, two pointers are maintained, LEFT and
RIGHT, which point to either end of the deque.
The elements in a deque extend from the LEFT end to
the RIGHT end and since it is circular, Dequeue[N–1]
is followed by Dequeue[0].
Types Of Deque
insertion
1.Input restricted deque:
34 12 53 61 9
front rear
deletion deletion
34 12 53 61 9
front rear
deletion
PRIORITY QUEUE
3
Why we need Linked List?
Arrays can be used to store linear data of similar types, but arrays have the following
limitations.
1) The size of the arrays is fixed: once it is created, therefore cannot be extended or
reduced the size of an array. So once created it remains same.
2) Inserting a new element in an array of elements is expensive because the room has
to be created for the new elements and to create room existing elements have to be
shifted.
3) One potential problem of using arrays for storing data is that arrays require a
contiguous block of memory which may not be available, if the requested contiguous
block is too large.
Advantages of linked list over arrays
1) Dynamic size
2) Ease of insertion/deletion
Occupies less memory than a linked list for It occupies more memory(extra memory for
the same no. of element. pointers )
Insertion and Deletions are inefficient: Insertion and Deletions are efficient: no
Elements are usually shifted. shifting
Representation
A linked list is represented by a pointer to the first node of the linked list. This
pointer is called the head or start. If the linked list is empty, then the value of the
head is NULL
NODE STRUCTURE:
In c we define a node as:
To access data:
Ptr->data=12;
Ptr->next =Null;
Example:
Here the address of first node is 4800 whose data part having 10 and address
4900(which is the address of next node ). Here head (or start )is pointer which
points to the first node. Linked list is created inside the heap.
Types of Linked List
A doubly
linked list
contains next
and previous
members
A single
linked list
contains only
a next
member.
11
11
Basic Operations
Traversing Searching
Inserting Deletion
12
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
}
Allocate the space for the new node and add the item to the data part
of it. This will be done by using the following statements.
ptr = (struct node *) malloc (sizeof(struct node));
ptr->data = item;
Now, we just need to make a few more link adjustments and our node
at will be inserted at the specified position. Since, at the end of the
loop, the loop pointer temp would be pointing to the node after
which the new node will be inserted. Therefore, the next part of the
new node ptr must contain the address of the next part of the temp
(since, ptr will be in between temp and the next of the temp). This
will be done by using the following statements.
ptr→ next = temp → next
now, we just need to make the next part of the temp, point to the new
node ptr. This will insert the new node ptr, at the specified position.
temp ->next = ptr;
Algorithm
STEP 1: IF AVAIL = NULL
WRITE OVERFLOW
GOTO STEP 12
END OF IF
STEP 2: SET NEW_NODE = AVAIL
STEP 3: NEW_NODE → DATA = VAL
STEP 4: SET TEMP = HEAD
STEP 6: REPEAT STEP 5 AND 6 UNTIL TEMP<loc
STEP 7: TEMP = TEMP → NEXT
STEP 8: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
END OF LOOP
STEP 9: AVAIL → NEXT = TEMP → NEXT
STEP 10: TEMP → NEXT = AVAIL
STEP 11: SET AVAIL = NEW_NODE
STEP 12: EXIT
Deletion in singly linked list at beginning
Deleting a node from the beginning of the list is the simplest operation
of all. It just need a few adjustments in the node pointers. Since the first
node of the list is to be deleted, therefore, we just need to make the
head, point to the next of the head. This will be done by using the
following statements.
ptr = head;
head = ptr->next;
Now, free the pointer ptr which was pointing to the head node of the
list. This will be done by using the following statement.
free(ptr)
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: SET HEAD = PTR -> NEXT
Step 4: FREE PTR
Step 5: EXIT
Deletion in singly linked list at the end
There are two scenarios in which, a node is deleted from the end of the linked list.
1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be deleted.
if(ptr == NULL)
{
printf("\nThere are less than %d elements in the list..",loc);
return;
}
}
Now, our task is almost done, we just need to make a few
pointer adjustments. Make the next of ptr1 (points to the
specified node) point to the next of ptr (the node which is to
be deleted).
This will be done by using the following statements.
ptr1 ->next = ptr ->next;
free(ptr);
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
STEP 2: SET TEMP = HEAD
STEP 3: SET I = 0
STEP 4: REPEAT STEP 5 TO 8 UNTIL I<LOC
STEP 5:TEMP1 = TEMP
STEP 6:TEMP = TEMP → NEXT
STEP 7: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
STEP 8: I =I+1
END OF LOOP
STEP 9:TEMP1 → NEXT = TEMP → NEXT
STEP 10: FREE TEMP
STEP 11: EXIT