Solution of Data Structure ST-2 PDF
Solution of Data Structure ST-2 PDF
Solution of Data Structure ST-2 PDF
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.
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.
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 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,
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.
1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1
Section: B
2a) bubble 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.
#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];
// Driver code
void main()
{
// 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:
Algorithm:
· 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.
· 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.
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.
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.
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:
To understand the Dijkstra’s Algorithm lets take a graph and find the shortest path from source
to all nodes.
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.
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
for k = 1 to n
for i = 1 to n
for j = 1 to n
return A