Professional Documents
Culture Documents
Course Outcome
CO 1 To understand the concepts of CO 2 To Learn linear data
ADTs structures – lists, stacks, and
queues
CO 3 To understand non-linear data C04 To understand sorting, searching and
structures – trees and graphs hashing algorithms
CO5 To apply Tree and Graph structures
Part A 7 x 2 =14
Q.NO Questions Marks CO BT PO Marks
Obtained
1 Define graph and How its differ from Tree? 2 3 R 1
2 What is weakly Connected graph? 2 3 R 1
3 Difference between Linear Search and Binary Search 2 5 U 1
4 Sort the following elements 82,901,100,12,150,77,55,23 using 2 5 R 2
radix sort
5 What is Euler Circuits and Topological Sorting? 2 3 U 2
6 What is hashing and List out the various Techniques ? 2 5 A 2
7 What is rehashing? 2 5 R 1
Part B 2 x 13 = 26
Q.N Questions Mark CO B PO
O s T
8.a Explain in detail about Linear Search and Binary Search 13 5 R 2
with program?
Or
8.b Explain in detail about Topological Sort and Cut Vertex with 13 5 R 1
Programs?
9.a Explain in detail about Breadth First Search with Program? 13 4 R 1
Or
9.b Explain in detail about Bubble and Selection Sort with Example 13 4 U 1
programs?
Part – C 1 x 10 = 10
Q.NO Questions Marks CO BT PO
R 47(2m,2m,2m,2m,13m,13,13m) 61.84
U 27(2m,2m,13m,10m) 35.53
A 2(2m) 2.63
L 0 0
E 0 0
a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed
edge uv from vertex u to vertex v, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks
to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a
topological ordering is just a valid sequence for the tasks.
An Euler circuit is a circuit that uses every edge of a graph exactly once. ▶ An Euler path starts and ends at different vertices.
▶ An Euler circuit starts and ends at the same vertex.
7. What is rehashing?
It can be also defined as rehashing is the process of re-calculating the hash code of already stored entries and moving them to a
bigger size hash map when the number of elements in the map reaches the maximum threshold value.
8.a. Explain in detail about Linear Search and Binary Search with program?
A linear search is also known as a sequential search that simply scans each element at a time. Suppose we want to search an
element in an array or list; we simply calculate its length and do not jump at any item.
The above figure shows an array of character type having 10 values. If we want to search 'E', then the searching
begins from the 0th element and scans each element until the element, i.e., 'E' is not found. We cannot directly jump from the
0th element to the 4th element, i.e., each element is scanned one by one till the element is not found.
As linear search scans each element one by one until the element is not found. If the number of elements increases, the number
of elements to be scanned is also increased. We can say that the time taken to search the elements is proportional to the
number of elements. Therefore, the worst-case complexity is O(n)
A binary search is a search in which the middle element is calculated to check whether it is smaller or larger than the element
which is to be searched. The main advantage of using binary search is that it does not scan each element in the list. Instead of
scanning each element, it performs the searching to the half of the list. So, the binary search takes less time to search an
element as compared to a linear search.
The one pre-requisite of binary search is that an array should be in sorted order, whereas the linear search works on both
sorted and unsorted array. The binary search algorithm is based on the divide and conquer technique, which means that it will
divide the array recursively.
In the above case, 'a' is the name of the array, mid is the index of the element calculated recursively, data is the element that is
to be searched, left denotes the left element of the array and right denotes the element that occur on the right side of the array.
Suppose we have an array of 10 size which is indexed from 0 to 9 as shown in the below figure:
Step 1: First, we calculate the middle element of an array. We consider two variables, i.e., left and right. Initially, left =0 and
right=9 as shown in the below figure:
Step 2: As data>a[mid], so the value of left is incremented by mid+1, i.e., left=mid+1. The value of mid is 4, so the value of
left becomes 5. Now, we have got a subarray as shown in the below figure:
Now again, the mid-value is calculated by using the above formula, and the value of mid becomes 7. Now, the mid can be
represented as:
In the above figure, we can observe that a[mid]>data, so again, the value of mid will be calculated in the next step.
Step 3: As a[mid]>data, the value of right is decremented by mid-1. The value of mid is 7, so the value of right becomes 6.
The array can be represented as:
The value of mid will be calculated again. The values of left and right are 5 and 6, respectively. Therefore, the value of mid is
5. Now the mid can be represented in an array as shown below:
Step 4: As a[mid]<data, the left value is incremented by mid+1. The value of mid is 5, so the value of left becomes 6.
Now the value of mid is calculated again by using the formula which we have already discussed. The values of left and right
are 6 and 6 respectively, so the value of mid becomes 6 as shown in the below figure:
We can observe in the above figure that a[mid]=data. Therefore, the search is completed, and the element is found
successfully.
The following are the differences between linear search and binary search:
Description
Linear search is a search that finds an element in the list by searching the element sequentially until the element is found in the
list. On the other hand, a binary search is a search that finds the middle element in the list recursively until the middle element
is matched with a searched element.
The linear search starts searching from the first element and scans one element at a time without jumping to the next element.
On the other hand, binary search divides the array into half by calculating an array's middle element.
Implementation
The linear search can be implemented on any linear data structure such as vector, singly linked list, double linked list. In
contrast, the binary search can be implemented on those data structures with two-way traversal, i.e., forward and backward
traversal.
Complexity
The linear search is easy to use, or we can say that it is less complex as the elements for a linear search can be arranged in any
order, whereas in a binary search, the elements must be arranged in a particular order.
Sorted elements
The elements for a linear search can be arranged in random order. It is not mandatory in linear search that the elements are
arranged in a sorted order. On the other hand, in a binary search, the elements must be arranged in sorted order. It can be
arranged either in an increasing or in decreasing order, and accordingly, the algorithm will be changed. As binary search uses
a sorted array, it is necessary to insert the element at the proper place. In contrast, the linear search does not need a sorted
array, so that the new element can be easily inserted at the end of the array.
Approach
The linear search uses an iterative approach to find the element, so it is also known as a sequential approach. In contrast, the
binary search calculates the middle element of the array, so it uses the divide and conquer approach.
Data set
Linear search is not suitable for the large data set. If we want to search the element, which is the last element of the array, a
linear search will start searching from the first element and goes on till the last element, so the time taken to search the element
would be large. On the other hand, binary search is suitable for a large data set as it takes less time.
Speed
If the data set is large in linear search, then the computational cost would be high, and speed becomes slow. If the data set is
large in binary search, then the computational cost would be less compared to a linear search, and speed becomes fast.
Dimensions
Linear search can be used on both single and multidimensional array, whereas the binary search can be implemented only on
the one-dimensional array.
Efficiency
Linear search is less efficient when we consider the large data sets. Binary search is more efficient than the linear search in the
case of large data sets.
Definition The linear search starts searching from the It finds the position of the searched
first element and compares each element element by finding the middle element of
with a searched element till the element is the array.
not found.
Sorted data In a linear search, the elements don't need to The pre-condition for the binary search is
be arranged in sorted order. that the elements must be arranged in a
sorted order.
Implementation The linear search can be implemented on The implementation of binary search is
any linear data structure such as an array, limited as it can be implemented only on
linked list, etc. those data structures that have two-way
traversal.
Approach It is based on the sequential approach. It is based on the divide and conquer
approach.
Size It is preferrable for the small-sized data sets. It is preferrable for the large-size data sets.
Efficiency It is less efficient in the case of large-size It is more efficient in the case of large-size
data sets. data sets.
Worst-case In a linear search, the worst- case scenario In a binary search, the worst-case scenario
scenario for finding the element is O(n). for finding the element is O(log2n).
Best-case In a linear search, the best-case scenario for In a binary search, the best-case scenario
scenario finding the first element in the list is O(1). for finding the first element in the list is
O(1).
8.b. Explain in detail about Topological Sort and Cut Vertex with Programs?
Given a Directed Acyclic Graph (DAG), find Topological Sort of the graph.
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv,
vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.
For example, a topological sorting of the following graph is “5 4 2 3 1 0”. There can be more than one topological sorting
for a graph. For example, another topological sorting of the following graph is “4 5 2 3 1 0”.
Please note that the first vertex in topological sorting is always a vertex with in-degree as 0 (a vertex with no incoming
edges). For above graph, vertex 4 and 5 have no incoming edges.
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
We have already discussed a DFS-based algorithm using stack and Kahn’s Algorithm for Topological Sorting. We have
also discussed how to print all topological sorts of the DAG here. In this post, another DFS based approach is discussed for
finding Topological sort of a graph by introducing concept of arrival and departure time of a vertex in DFS.
What is Arrival Time & Departure Time of Vertices in DFS?
In DFS, Arrival Time is the time at which the vertex was explored for the first time and Departure Time is the time at
which we have explored all the neighbors of the vertex and we are ready to backtrack.
How to find Topological Sort of a graph using departure time?
To find Topological Sort of a graph, we run DFS starting from all unvisited vertices one by one. For any vertex, before
exploring any of its neighbors, we note the arrival time of that vertex and after exploring all the neighbors of the vertex, we
note its departure time. Please note only departure time is needed to find Topological Sort of a graph, so we can skip arrival
time of vertex. Finally, after we have visited all the vertices of the graph, we print the vertices in order of their decreasing
departure time which is our desired Topological Order of Vertices.
Below is C++ implementation of above idea –
C++
Python3
C#
Javascript
Java
#include <bits/stdc++.h>
// list representation
class Graph {
list<int>* adj;
public:
Graph(int); // Constructor
~Graph(); // Destructor
void topologicalSort();
};
Graph::Graph(int V)
this->V = V;
// of all vertex
visited[v] = true;
if (!visited[i])
departure[time++] = v;
void Graph::topologicalSort()
int time = 0;
if (visited[i] == 0) {
int main()
Graph g(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
g.topologicalSort();
return 0;
Output
Topological Sort of the given graph is
542310
Time Complexity of above solution is O(V + E).
Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root node and explores all the
neighboring nodes. Then, it selects the nearest node and explores all the unexplored nodes. While using BFS for traversal, any
node in the graph can be considered as the root node.
There are many ways to traverse the graph, but among them, BFS is the most commonly used approach. It is a recursive
algorithm to search all the vertices of a tree or graph data structure. BFS puts every vertex of the graph into two categories -
visited and non-visited. It selects a single node in a graph and, after that, visits all the nodes adjacent to the selected node.
o BFS can be used to find the neighboring locations from a given source location.
o In a peer-to-peer network, BFS algorithm can be used as a traversal method to find all the neighboring nodes. Most
torrent clients, such as BitTorrent, uTorrent, etc. employ this process to find "seeds" and "peers" in the network.
o BFS can be used in web crawlers to create web page indexes. It is one of the main algorithms that can be used to index
web pages. It starts traversing from the source page and follows the links associated with the page. Here, every web
page is considered as a node in the graph.
o BFS is used to determine the shortest path and minimum spanning tree.
o BFS is also used in Cheney's technique to duplicate the garbage collection.
o It can be used in ford-Fulkerson method to compute the maximum flow in a flow network.
Algorithm
The steps involved in the BFS algorithm to explore a graph are given as follows -
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT
Now, let's understand the working of BFS algorithm by using an example. In the example given below, there is a directed
graph having 7 vertices.
In the above graph, minimum path 'P' can be found by using the BFS that will start from Node A and end at Node E. The
algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes that are to be processed, while
QUEUE2 holds all the nodes that are processed and deleted from QUEUE1.
1. QUEUE1 = {A}
2. QUEUE2 = {NULL}
Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
1. QUEUE1 = {B, D}
2. QUEUE2 = {A}
Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all neighbors of node B to queue1.
1. QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}
Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all neighbors of node D to queue1. The only neighbor
of Node D is F since it is already inserted, so it will not be inserted again.
1. QUEUE1 = {C, F}
2. QUEUE2 = {A, B, D}
Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbors of node C to queue1.
1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}
Step 5 - Delete node F from queue1 and add it into queue2. Insert all neighbors of node F to queue1. Since all the neighbors of
node F are already present, we will not insert them again.
1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}
Step 6 - Delete node E from queue1. Since all of its neighbors have already been added, so we will not insert them again.
Now, all the nodes are visited, and the target node E is encountered into queue2.
1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}
Time complexity of BFS depends upon the data structure used to represent the graph. The time complexity of BFS algorithm
is O(V+E), since in the worst case, BFS algorithm explores every node and edge. In a graph, the number of vertices is O(V),
whereas the number of edges is O(E).
The space complexity of BFS can be expressed as O(V), where V is the number of vertices.
In this code, we are using the adjacency list to represent our graph. Implementing the Breadth-First Search algorithm in Java
makes it much easier to deal with the adjacency list since we only have to travel through the list of nodes attached to each
node once the node is dequeued from the head (or start) of the queue.
9.B. Explain in detail about Bubble and Selection Sort with Example programs?
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the
wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.
How does Bubble Sort Work?
Input: arr[] = {5, 1, 4, 2, 8}
First Pass:
Bubble sort starts with very first two elements, comparing them to check which one is greater.
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not
swap them.
Second Pass:
Now, during second iteration it should look like this:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Third Pass:
Now, the array is already sorted, but our algorithm does not know if it is completed.
The algorithm needs one whole pass without any swap to know it is sorted.
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Illustration:
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from
the unsorted part and putting it at the beginning.
The algorithm maintains two subarrays in a given array.
The subarray which already sorted.
The remaining subarray was unsorted.
In every iteration of the selection sort, the minimum element (considering ascending order) from the unsorted subarray is
picked and moved to the sorted subarray.
Flowchart of the Selection Sort:
64 25 12 22 11
Thus, replace 64 with 11. After one iteration 11, which happens to be the least value in the array, tends to appear in the
first position of the sorted list.
11 25 12 22 64
Second Pass:
For the second position, where 25 is present, again traverse the rest of the array in a sequential manner.
11 25 12 22 64
After traversing, we found that 12 is the second lowest value in the array and it should appear at the second place in the
array, thus swap these values.
11 12 25 22 64
Third Pass:
Now, for third place, where 25 is present again traverse the rest of the array and find the third least value present in the
array.
11 12 25 22 64
While traversing, 22 came out to be the third least value and it should appear at the third place in the array, thus
swap 22 with element present at third position.
11 12 22 25 64
Fourth pass:
Similarly, for fourth position traverse the rest of the array and find the fourth least element in the array
As 25 is the 4th lowest value hence, it will place at the fourth position.
11 12 22 25 64
hash = hashfunc(key)
index = hash % array_size
The hash function must satisfy the following requirements:
A good hash function is easy to compute.
A good hash function never gets stuck in clustering and distributes keys evenly across the hash table.
A good hash function avoids collision when two elements or items get assigned to the same hash value.
One of the hashing techniques of using a hash function is used 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:
1. Collision free
2. Property to be hidden
3. Puzzle friendly
Hash Table
Hashing in data structure uses hash tables to store the key-value pairs. The hash table then uses the hash
11 12 22 25 64
function to generate an index. Hashing uses this unique index to perform insert, update, and search operations.
It can be defined as a bucket where the data are stored in an array format. These data have their own index value.
If the index values are known then the process of accessing the data is quicker.
1 3 3%30 = 3 3
2 1 1%30 = 1 1
3 40 40%30 = 10 10
4 5 5%30 = 5 5
5 11 11%30 = 11 11
6 15 15%30 = 15 15
7 18 18%30 = 18 18
8 16 16%30 = 16 16
9 38 38%30 = 8 8
The process of taking any size of data and then converting that into smaller data value which can be named as hash value. This
hash alue can be used in an index accessible in hash table. This process define hashing in data structure.
Also Read: Types of Data Structures in Python
Collision Resolution Techniques
Hashing in data structure falls into a collision if two keys are assigned the same index number in the hash table. The
collision creates a problem because each index in a hash table is supposed to store only one value. Hashing in data
structure uses several collision resolution techniques to manage the performance of a hash table.
It is a process of finding an alternate location. The collision resolution techniques can be named as-
1. Open Hashing (Separate Chaining)
2. Closed Hashing (Open Addressing)
Linear Probing
Quadratic Probing
Double Hashing
Linear Probing
Hashing in data structure results in an array index that is already occupied to store a value. In such a case, hashing performs
a search operation and probes linearly for the next empty cell.
Linear probing in hash techniques is known to be the easiest way to resolve any collisions in hash tables. A sequential search
can be performed to find any collision that occurred.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced
Certificate Programs, or Masters Programs to fast-track your career.
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) (1,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) + 1) % T). If (hash(n) + 1) % T is also full, then we try (hash(n) + 2) % T. If
(hash(n) + 2) % T is also full, then we try (hash(n) + 3) % T.
The hash table will look like the following:
Serial Number Key Hash Array Index Array Index after Linear Probing
1 3 3%30 = 3 3 3
2 1 1%30 = 1 1 1
3 63 63%30 = 3 3 4
4 5 5%30 = 5 5 5
5 11 11%30 = 11 11 11
6 15 15%30 = 15 15 15
7 18 18%30 = 18 18 18
8 16 16%30 = 16 16 16
9 46 46%30 = 8 16 17
Double Hashing
The double hashing technique uses two hash functions. The second hash function comes into use when the first function
causes a collision. It provides an offset index to store the value.
The formula for the double hashing technique is as follows:
(firstHash(key) + i * secondHash(key)) % sizeOfTable
Where i is the offset value. This offset value keeps incremented until it finds an empty slot.
For example, you have two hash functions: h1 and h2. You must perform the following steps to find an empty slot:
1. Verify if hash1(key) is empty. If yes, then store the value on this slot.
2. If hash1(key) is not empty, then find another slot using hash2(key).
3. Verify if hash1(key) + hash2(key) is empty. If yes, then store the value on this slot.
4. Keep incrementing the counter and repeat with hash1(key)+2hash2(key), hash1(key)+3hash2(key), and so on, until it finds an
empty slot.
Also visit upGrad’s Degree Counselling page for all undergraduate and postgraduate programs.
Double Hashing Example
Imagine you need to store some items inside a hash table of size 20. The values given are: (16, 8, 63, 9, 27, 37, 48, 5, 69, 34,
1).