Data Structure 5 Mark's
Data Structure 5 Mark's
5 Marks
Applications:
Function call management (call stack in programming).
Expression evaluation (postfix. prefix, and infix notations).
Undo mechanisms in applications.
Backtracking algorithms.
Implementation:
Can be implemented using arrays or linked lists.
Queues:
Aqueue is a first-in, first-out (FIFO) data structure, where the first element added is the
first one to be removed. It follows the principle of adding elements to the "rear" and
removing them from the "front."
Operations:
Engueue: Add an element to the rear of the queue.
Dequeue: Remove the element from the front of the queue.
Front: View the element at the front without removing it.
isEmpty: Check if the queue is empty.
Applications:
Task scheduling in operating systems.
Print job management.
Breadth-tirst search in graph algorithms,
Handling requests in netvworking.
Implementation:
Can be implemented using arrays. linked lists. or specialized data structures like a
circular queue.
Differences:
Order of Removal
Stack: Last In, First Out (LIFO).
Queue: First In, First Out (FIFO).
Operations:
Stack: Push, Pop. Peek.
Queue: Enqueue, Dequeue, Front.
Implementation:
Stacks can be casily implemented using arrays or linked lists.
Queues can be implemented using arravs, linked lists, or circular buffers.
Applications:
Stacks are used in sccenarios where the last operation nceds to be undone or revisited.
Qucues arc used when tasks are processed in the order they arive.
Both stacks and queues are fundamental data structures, and their simplicity makes thenm
Iseful in various applications. Choosing between them depends on the specific
requirements of a problem.
Intemediute node
Level l
3
Intemedinte node
Leved 2
Leaf Node
Level 3
10 11 12) ---+
*A binary tree has the characteristic of all nodes having at most two branclhes, that is,
all nodes have a degree of at most 2. Therefore, a binary tree can be enmpty or consist
of a root node and two disjointed binary trees termed left subtree and right subtree.
Figure 5,4 shows an example binarv tree.
level 0
root node
Components of a Graph:
Vertices
Edges
Applications of Graph:
Following are the real-life applications
*Graph data structures can be used to represent the interactions between players on a
team, such as passes, shots, and tackles. Analyzing these interactions can provide insights
into team dynamics and areas for
improvement
"Commonly used to represent social networks, such as networks of friends on social media
Graphs can be used to represent the topology of computer networks, such as the
connections between routers and switches.
*Graphs are used to represent the connections between different places in a transportation
network, such as roads and airports
*Graphs are used in Neural Networks where vertices represent neurons and edges represent
the synapses between them, Neural networks are used to understand how our brain works
and how cOnnections change when we learn. The human brain has about 10^11 neurons
and close to 10^15 synapses.
A directed graph is defined as a type of graph where the edges have a direction associated
with them.
Information Retrieval: Search engines like Google, Bing, and Yahoo use
sophisticated searching algorithms to retrieve relevant information from vast amounts
of data on the web.
Database Svstems: Searching is fundamental in database sYstems for retrieving
specific data records based on user queries, improving efficiency in data retrieval.
E-commere: Searching is crucial in ecommerce platforms for users to find products
quickly based on their preterences, specitications, or keywords.
Networking: In networking., searching algorith1s are used for routing packets
efticiently through networks. finding optimal paths, and managing network resources.
Artificial Intelligenee: Searching algorithms play a vital role in Al applications, such
as problem-solving. game playing (e.g. chess), and decision-making processes
Pattern Recognition: Searching algorithms are used in pattern matching tasks, such
as image recognition. speech recognition, and handwriting recognition.
5) Define hashing
Hashing in the data structure is a technique of mapping a large chunk of data into
small tables using a hashing function. It is also known as thec message digest function,
It is a technique that uniquely identifies aspecific item from a collection of similar
items.
vli].clear():
dislo) =2e9;
for(int i 0; < m, i++)
disl0] =0;
for(int i 0, i<n-1; i++)
int j =0,
while(vu].size() != 0)
++:
A very important application of Bellman Ford is to check if there is a negative cycie in the
graph, Time Complexity of Bellman Ford algorithm is relatively high O(VE), in case
Hashing in data structure results in an array index that is already occupied to store a
value. In such a case. hashing pertorms a scarch operation and probes linearly for the
next empty cell.
Lincar probing in hash techniques is known to be the easiest way to resolve any
collisions in hash tables. Asequential search can be performed to find any collision
that occurred.
Linear Probing Example
Imagine you have been asked to store some items inside a hash table of size 30, The
items are already sorted in a key-value pair format. The values given are: (3,21)0.72)
(63,36) (5,30) (11,44) (15,33) (18.12) (16.80) (46.99).
The hash(n) is the index computed using a hash function and T is the table size. If slot
index = (hash(n) %T) is full. then we look for the next slot index by adding 1
((hash(n) + ) %T). If (hashin) + |) %T is also full. then we try (hash(n) -2) %T. If
(hashín) +2) %T is also full. then we try (hashin) +3) %T.
The hash table will look like the following:
The idea of lincar probing is sinmple, we take a fixed sized hash table and every time
we face a hash collision, we lincarly traverse the table in a cyclic manner to find the
next empty slot.
Assume a scenario where we intend to store the following set of numbers
0.1,2.4.5,7) into a hash table of size 5 with the heip of the following hash function
H. such that H(x) = N%5.
So, if we were to map the given data vith the given hash function we'll get the
corresponding values
H(01-> 0%5 =0
H(l)-> 1%5 = 1
H(2)-> 2°%5 =2
H(4)-> 4%5 = 4
H(S)-> 59%5 = 0
in this case we see a collision of two terms (0 & Z). In this situation we move
linearly down the table to find the first empty slot. Note that this linear traversal is
cyelic in nature, i.e. in the event we exhaust the last element during the search we start
again from the beginning until the initial kev is reached.
In this case our hash function can be considered as this: H(x. i) =(H() +iloN
where Nis the size of the table and irepresents the linearly increasing variable which
starts from l(until empty bucket is found).
Despite being easy to compute, implement and deliver best cache pertormance, this
suflers from the problem of clustering (many consecutive clements get grouped
together. which eventually reduces the efficiency of finding clements or empty
buckets).
8) Explain types of heaps in data structure
we have some general understanding of heap data structure, therc are two types of
heap which can be classified as:.
1. Min Heap :- The smallest element is present at the root of the trece in the nin heap
such that it is casier to extract the smallest clement when heap pop is performed.
2. Max Heap :- The greatest element is present at the root of the tree in the max heap
such that it is casier to extract the largest element when heap pop is
pertormed.
The illustration stating the both is represented below:
Then,
Abinary tree is said to be a complete binary tree if all its levels, except possitbly the last level,
have the maximum number of possible nodes, and all the nodes in the last level appear as
far left as possible
There is no particular sequence tor All nodes should be on the left. This
filling in nodes. means that there is no node that
has a right child but not a left child.
One of the hashing techniques of using a hash function is uscd for data integrity. If
using a hash function one change in a message will create a different hash.
The three characteristics of the hash function in the data structure are:
LCollision free
2. Property to be hidden
3. Puzzle friendly
Algorithm Steps:
Set all vertices distances infinity except for the source vertex, set the source
distance0.
Push the source vertex in a min-priority queue in the form (distance, vertex), as the
comparison in the min-priority queue will be according to vertices distances
Pop the vertex with the minimum distance from the priority queue (at first the
popped vertex =source).
Update the distances of the connected vertices to the popped vertex in case of
"current vertex distance +edge weight < next vertex distance", then push the vertex
with the new distance to the priority queue.
If thepopped vertex is visited before, just continue without using it.
Apply the same algorithm again until the priority queue is empty.
Implementation:
Assume the source vertex =I
Vector <pair < int , int>>v(SIZE): /each vertex has all the connected vertices
with the edges weights
void dijkstra)1
/set the vertices distances as infinity
memset(vis, false, sizeof vis): set all vertes as unvisited
dist[ 1] =0:
multiset< pair < int. int > >s: multiset do the job as a min-priority queue
while(!s.empty0)
pair <int , int> p="s.begin): //pop the vertex with the minimum distance
S.erase(s.begin).
dist[el =dist[x] + w:
s.insert((dist(e]. e} ): insert the next vertex with the updated distance
Time Complexity of Dijkstra's Algorithm is but with min-priority queue it drops down to
D(V+ Elog V)
this example:
Each rectangular box represents a node.
The arrow symbol(->) represents the link from one node to the next.
The last node points to NULL, indicating the end of the list.
Key characteristics of singly linked lists include
Dynamic Size
Sequential Access
No Random Access
Representing sequences of data that can be easily modified without the need for
shifting elements (unlike arrays).
* Performing operations like insertion and deletion efficiently, particularly when
dealing with large datasets
12)What is a header?
Aheader node is a special node that is found at the beginning of the list. Alist that contains
this type of node, is called the header-linked list. This type of list is useful when information
other than that found in each node is needed.
For example, suppose there is an application in which the number of items in a list is often
calculated. Usually, a list is always traversed to find the length of the list. However, if the
Current length is maintained in an additional header node that information can be easily
obtained.
reference because last node of fa circular linked list does not contain the NULL pointer. The
possible operations on this type of linked ist are Insertion, Deletion and Traversing.