Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Solution of Data Structure ST-2 PDF

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

ROLL NO.

G.L. BAJAJ INSTITUTE OF TECHNOLOGY & MANAGEMENT


GREATER NOIDA
B. TECH (III) - (AI,CSE-AIML,CSE,CSE-DS,AIML,AIDS,CSE-H)
SESSIONAL TEST 2 (ODD SEM 2022-23)
Data Structure (KCS 301)-Solution

Section: A
Q-1 (a) Define radix sort?
The idea of Radix Sort is to do digit-by-digit sort starting from least significant digit to the most significant
digit. Radix sort uses counting sort as a subroutine to sort.·
➢ Radix sort is a non-comparative sorting algorithm.
➢ Radix sort can be applied to data that can be sorted lexicographically, be they integers, words, punch
cards, playing cards, or the mail.
➢ Time complexity of Radix Sort is O(nd), where n is the size of the array and d is the number
of digits in the largest number.
➢ It is not an in-place sorting algorithm because it requires extra space.

Q1(b) Differentiate the Stable and Unstable sorting.

Ans.A stable sorting algorithm maintains the relative order of the items with equal sort keys. An unstable
sorting algorithm does not. In other words, when a collection is sorted with a stable sorting algorithm, items
with the same sort keys preserve their order after the collection is sorted.

Q1(c) Discuss graphs and their representations.

Ans. Graph: A graph is intuitively defined as a pair consisting of a set of vertices and a set of edges.
A graph G with a set of V vertices together with a set of E edges is represented as G= (V, E).
Representation of Graphs:

A graph can be represented using 3 data structures:

1. Adjacency matrix 2. Adjacency list 3.Adjacency set.

Q1(d): Explain minimum cost spanning tree. Give its applications.

Ans. What is minimum cost spanning tree explain with example?

A Minimum Spanning Tree (MST) is a subset of edges of a connected weighted undirected graph that
connects all the vertices together with the minimum possible total edge weight.

Applications: Traveling Salesman Problem, direct applications in the design of networks, including
computer networks, telecommunications networks,

Q1(e): Explain transitive closure of a Graph.

Given a directed graph, find out if a vertex j is reachable from another vertex i for all vertex
pairs (i, j) in the given graph. Here reachable means that there is a path from vertex i to j.
The reach-ability matrix is called the transitive closure of a graph.

Transitive closure of above graphs is

1 1 1 1

1 1 1 1

1 1 1 1

0 0 0 1
Section: B
2a) bubble sort.

Q2(b)Explain the following as a short note:


i)Internal sorting vs external sorting with example
ii)in-place sorting vs out-place sorting with example
i)If the data sorting process takes place entirely within a computer's Random-Access Memory
(RAM), it’s called internal sorting. This is possible whenever the size of the dataset to be sorted
is small enough to be held in RAM.Example:Insertion Sort
For sorting larger datasets, it may be necessary to hold only a smaller chunk of data in memory at
a time, since it won’t all fit in the RAM. The rest of the data is normally held on some larger, but
slower medium, like a hard disk. The sorting of these large datasets will require different sets of
algorithms which are called external sorting.Example:Merge Sort

ii)Sorting algorithms may require some extra space for comparison and temporary storage of few
data elements. These algorithms do not require any extra space and sorting is said to happen in-
place, or for example, within the array itself. This is called in-place sorting. Bubble sort is an
example of in-place sorting.

However, in some sorting algorithms, the program requires space which is more than or equal to
the elements being sorted. Sorting which uses equal or more space is called out-place sorting.
Merge-sort is an example of out-place sorting.

Q2(c) Write c program for indexed Sequential Search

#include <stdio.h>
#include <stdlib.h>
void indexedSequentialSearch(int arr[], int n, int k)
{
int GN = 3; // GN is group number that is number of
// elements in a group
int elements[GN], indices[GN], i, set = 0;
int j = 0, ind = 0, start, end;
for (i = 0; i < n; i += 3) {

// Storing element
elements[ind] = arr[i];

// Storing the index


indices[ind] = i;
ind++;
}
if (k < elements[0]) {
printf("Not found");
exit(0);
}
else {
for (i = 1; i <= ind; i++)
if (k <= elements[i]) {
start = indices[i - 1];
end = indices[i];
set = 1;
break;
}
}
if (set == 0) {
start = indices[GN - 1];
end = GN;
}
for (i = start; i <= end; i++) {
if (k == arr[i]) {
j = 1;
break;
}
}
if (j == 1)
printf("Found at index %d", i);
else
printf("Not found");
}

// Driver code
void main()
{

int arr[] = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };


int n = sizeof(arr) / sizeof(arr[0]);

// Element to search
int k = 8;
indexedSequentialSearch(arr, n, k);
}

Q2 (d): Apply Prim’s algorithm to find a minimum spanning tree in the following weighted graph as shown
below. Differentiate between Prim’s and Kruskal algorithm.
Q2 ( e)Discuss Connected Components in a graph. Write down algorithm to print and count connected
components.
2 e) Discuss Connected Components.

If an undirected graph is not connected, it is the union of two or more disjoint connected subgraphs; such
subgraphs are called connected components of the graph.

A connected component or simply component of an undirected graph is a subgraph in which each pair of
nodes is connected with each other via a path.

In connected components, all the nodes are always reachable from each other.
Algorithm to print and count connected components.

Steps:

. First, mark all vertices as unvisited.


· Iterate over all vertices.
· If a vertex is not visited, perform DFS on that vertex and increment the count by 1.
· After iterating over all vertices, the value of count will be the number of connected components in the
graph.

Algorithm:

Algorithm to test whether a graph is connected or not


Approach: (Directed Graph)

· Take two bool arrays vis1 and vis2 of size N (number of nodes of a graph) and keep false in
all indexes.
· Start at a random vertex v of the graph G, and run a DFS(G, v).
· Make all visited vertices v as vis1[v] = true.
· Now reverse the direction of all the edges.
· Start DFS at the vertex which was chosen at step 2.
· Make all visited vertices v as vis2[v] = true.
· If any vertex v has vis1[v] = false and vis2[v] = false then the graph is not connected.

Approach: (Undirected Graph)

· Take one bool array vis1 of size N (number of nodes of a graph) and keep false in all indexes.
· Start at a random vertex v of the graph G, and run a DFS(G, v).
· Make all visited vertices v as vis1[v] = true.
· If any vertex v has vis1[v] = false then the graph is not connected.

Q2(f) Explain the working of heap sort algorithm of given array= 81,89,9,11,14,76,54,22 .

:
Section: C

Q3 (a) (i) The keys 12, 17, 13, 2, 5, 43, 5 and 15 are inserted into an initially empty hash table of length 15
using open addressing with hash function h(k) = k mod 10 and linear probing. Show the resultant hash table?
ii) Differentiate between linear and quadratic probing techniques

Q3 a (ii)
3. (b)

(i) Use the merge sort algorithm to sort the following elements in ascending order. 13, 16, 10, 11, 4, 12, 6,
7. What is the time and space complexity of merge sort?
(ii) Use quick sort algorithm to sort 15,22,30,10,15,64,1,3,9,2. Is it a stable sorting algorithm? Justify
(i)
ii)

4-a) Differentiate between DFS and BFS with examples. Compute all-possible Breadth First Tree for
the given graph.
Following are the important differences between BFS and DFS.

Sr. No. Key BFS DFS

1 Definition BFS, stands for Breadth DFS, stands for Depth First
First Search. Search.

2 Data BFS uses Queue to find DFS uses Stack to find the
structure the shortest path. shortest path.

3 Source BFS is better when DFS is better when target is far


target is closer to from source.
Source.

4 Suitablity for As BFS considers all DFS is more suitable for decision
decision tree neighbour so it is not tree. As with one decision, we
suitable for decision need to traverse further to
tree used in puzzle augment the decision. If we reach
games. the conclusion, we won.

5 Speed BFS is slower than DFS. DFS is faster than BFS.

6 Time Time Complexity of BFS Time Complexity of DFS is also


Complexity = O(V+E) where V is O(V+E) where V is vertices and E
vertices and E is edges. is edges.

4(b) Discuss Dijkstra and Floyd Warshall algorithms. Apply Dijkstra’s algorithm to find the shortest paths
from source to all other vertices in the following graph.
OR
4b
Discuss Dijkstra and Floyd Warshall algorithms. Apply Dijkstra’s algorithm to find the shortest paths from source
to all other vertices in the following graph.
Dijkstra Algorithm: Dijkstra algorithm is a single-source shortest path algorithm.
Here, single-source means that only one source is given, and we have to find the
shortest path from the source to all the nodes.
Therefore, we conclude that the formula for calculating the distance between the vertices:

{if( d(u) + c(u, v) < d(v))


d(v) = d(u) +c(u, v) }

To understand the Dijkstra’s Algorithm lets take a graph and find the shortest path from source
to all nodes.

Consider below graph and src = 0

Step 1:
The set sptSet is initially empty and distances assigned to vertices are {0, INF, INF, INF,
INF, INF, INF, INF} where INF indicates infinite.
Now pick the vertex with a minimum distance value. The vertex 0 is picked, include it in
sptSet. So sptSet becomes {0}. After including 0 to sptSet, update distance values of its
adjacent vertices.
Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and 8.

The following subgraph shows vertices and their distance values, only the vertices with
finite distance values are shown. The vertices included in SPT are shown in green colour.

Step 2:
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). The vertex 1 is picked and added to sptSet.

So sptSet now becomes {0, 1}. Update the distance values of adjacent vertices of 1.

The distance value of vertex 2 becomes 12.

Step 3:

Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}.
Update the distance values of adjacent vertices of 7. The distance value of vertex 6 and 8
becomes finite (15 and 9 respectively).
Step 4:
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}.

Update the distance values of adjacent vertices of 6. The distance value of vertex 5 and 8
are updated.

We repeat the above steps until sptSet includes all vertices of the given graph. Finally, we
get the following Shortest Path Tree (SPT).

Floyd-Warshall

Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the
pairs of vertices in a weighted graph. This algorithm works for both the directed and
undirected weighted graphs. But, it does not work for the graphs with negative cycles
(where the sum of the edges in a cycle is negative).
Floyd-Warshall Algorithm

n = no of vertices

A = matrix of dimension n*n

for k = 1 to n

for i = 1 to n

for j = 1 to n

Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])

return A

You might also like