0% found this document useful (0 votes)
6 views22 pages

22csc22 Cat-3.1 - Answer Key

Uploaded by

kaushik7806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

22csc22 Cat-3.1 - Answer Key

Uploaded by

kaushik7806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

KONGU ENGINEERING COLLEGE, PERUNDURAI 638 060

CONTINUOUS ASSESSMENT TEST III

22CSC22 - Data Structures using C

1. Define Spanning tree.


● A spanning tree is a sub-graph of an undirected and a connected graph,
which includes all the vertices of the graph having a minimum possible
number of edges(Forms no cycle)

Graph Spanning Tree

2. Construct the minimum spanning tree for the below graph.

3.
Outline the use of Kruskal‟s algorithm.

● Kruskal's Algorithm is used to find the minimum spanning tree for a


connected weighted graph.
4.
Show the steps to find a given graph is Biconnected or not?

● A connected graph is Biconnected if it is connected and doesn’t have


any Articulation Point. We mainly need to check two things in a graph.

● The graph is connected.


● There is not articulation point in graph.
5. List the different sorting algorithms.
 Insertion sort.
 Merge sort.
 Quick sort.
 Heap sort.
6. Display the output for the following input sequence by applying Insertion sort
[After the 1st pass]

16 3 46 9 28 14

Ans: 3 16 46 9 28 14

7. Differentiate between merge sort and quick sort. (Any Two Points)

Basis for
comparison Quick Sort Merge Sort

The partition of The splitting of a array of elements In the merge sort, the array is
elements in the is in any ratio, not necessarily parted into just 2 halves (i.e.
array divided into half. n/2).

Worst case
O(n^2) O(nlogn)
complexity

It operates fine on any size of


Works well on It works well on smaller array
array

It work faster than other sorting


Speed of It has a consistent speed on any
algorithms for small data set like
execution size of data
Selection sort etc

Additional
storage space Less(In-place) More(not In-place)
requirement

Quicksort does not need explicit


merging of the sorted sub-arrays; Merge sort performs explicit
Merging
rather the sub-arrays rearranged merging of sorted sub-arrays.
properly during partitioning.

For merging of sorted sub-


Quicksort does not require arrays, it needs a temporary
Space
additional array space. array with the size equal to the
number of input elements.
8. Show the number of passes required for the elements to be sorted using Insertion

2
sort?
Insertion sort requires n – 1 pass to sort an array of n elements.
9. Translate the following complete binary tree into max heap.

10. If the hash table has size m = 12 and the key is k = 100. Find the hash function
using division method.

h(K) = k mod M

=100 mod 12

=4

11. Construct the minimum spanning tree for the following graph using
Dijkstra‟s Algorithm.

3
Dijkstra’s Algorithm

4
5
6
12. Consider the following Weighted Graph to construct the minimum spanning
tree by applying Prim’s Algorithm

7
Prim’s Algorithm

● Prim's algorithm starts with the single node and explore all the
adjacent nodes with all the connecting edges at every step.
● The edges with the minimal weights causing no cycles in the graph
got selected.

8
9
13. Build a C program to sort the given elements in ascending order using Quick
sort by picking the pivot element.
Input: 35, 33, 42, 10, 14, 19, 27, 44, 26, 31
#include <stdio.h>
void quickSort( int[], int, int);
int partition( int[], int, int);
void main()
{
int i,a[] = { 7, 12, 1, -2, 0, 15, 4, 11, 9};
printf("\n\nUnsorted array is: ");
for(i = 0; i < 9; ++i)
printf(" %d ", a[i]);
quickSort( a, 0, 8);
printf("\n\nSorted array is: ");
for(i = 0; i < 9; ++i)
printf(" %d ", a[i]);
}
void quickSort( int a[], int l, int r)
{
int j;
if( l < r ) { // divide and conquer
j = partition( a, l, r);
quickSort( a, l, j-1);
quickSort( a, j+1, r);
}
}
10
int partition( int a[], int l, int r)
{
int pivot, i, j, t;
pivot = a[l];
i = l;
j = r+1;
while( 1) {
do {
++i;
} while(a[i]<=pivot && i<=r);
do {
--j;
} while( a[j] > pivot );
if( i >= j ) break;
t = a[i];
a[i] = a[j];
a[j] = t;
}
t = a[l];
a[l] = a[j];
a[j] = t;
return j;
}

14. Discuss in detail about Separate Chaining and Open addressing with
examples.
There are mainly two methods to handle collision:
1. Separate Chaining:
2. Open Addressing:

11
Collision resolution technique

1) Separate Chaining
The idea is to make each cell of the hash table point to a linked list of
records that have the same hash function value. Chaining is simple but
requires additional memory outside the table.
Example: We have given a hash function and we have to insert some
elements in the hash table using a separate chaining method for
collision resolution technique.
Hash function = key % 5,
Elements = 12, 15, 22, 25 and 37.
Let’s see step by step approach to how to solve the above problem:
 Step 1: First draw the empty hash table which will have a possible
range of hash values from 0 to 4 according to the hash function
provided.

Hash table

 Step 2: Now insert all the keys in the hash table one by one. The first
key to be inserted is 12 which is mapped to bucket number 2 which is
calculated by using the hash function 12%5=2.

12
Insert 12

 Step 3: Now the next key is 22. It will map to bucket number 2
because 22%5=2. But bucket 2 is already occupied by key 12.

Insert 22

 Step 4: The next key is 15. It will map to slot number 0 because
15%5=0.

13
Insert 15

 Step 5: Now the next key is 25. Its bucket number will be 25%5=0.
But bucket 0 is already occupied by key 25. So separate chaining
method will again handle the collision by creating a linked list to
bucket 0.

Insert 25

Hence In this way, the separate chaining method is used as the collision
resolution technique.
2) Open Addressing
In open addressing, all elements are stored in the hash table itself. Each
table entry contains either a record or NIL. When searching for an
element, we examine the table slots one by one until the desired
element is found or it is clear that the element is not in the table.
14
2.a) Linear Probing
In linear probing, the hash table is searched sequentially that starts from
the original location of the hash. If in case the location that we get is
already occupied, then we check for the next location.
Algorithm:
1. Calculate the hash key. i.e. key = data % size
2. Check, if hashTable[key] is empty
 store the value directly by hashTable[key] = data
3. If the hash index already has some value then
 check for next index using key = (key+1) % size
4. Check, if the next index is available hashTable[key] then store the
value. Otherwise try for next index.
5. Do the above process till we find the space.
Example: Let us consider a simple hash function as “key mod 5” and a
sequence of keys that are to be inserted are 50, 70, 76, 85, 93.
 Step 1: First draw the empty hash table which will have a possible
range of hash values from 0 to 4 according to the hash function
provided.

Hash table

 Step 2: Now insert all the keys in the hash table one by one. The first
key is 50. It will map to slot number 0 because 50%5=0. So insert it
into slot number 0.

15
Insert 50 into hash table

 Step 3: The next key is 70. It will map to slot number 0 because
70%5=0 but 50 is already at slot number 0 so, search for the next
empty slot and insert it.

Insert 70 into hash table

 Step 4: The next key is 76. It will map to slot number 1 because
76%5=1 but 70 is already at slot number 1 so, search for the next
empty slot and insert it.

16
Insert 76 into hash table

 Step 5: The next key is 93 It will map to slot number 3 because


93%5=3, So insert it into slot number 3.

Insert 93 into hash table

2.b) Quadratic Probing


Quadratic probing is an open addressing scheme in computer
programming for resolving hash collisions in hash tables. Quadratic
probing operates by taking the original hash index and adding
successive values of an arbitrary quadratic polynomial until an open slot
is found.
An example sequence using quadratic probing is:
H + 12, H + 22, H + 32, H + 42…………………. H + k2
This method is also known as the mid-square method because in this
17
method we look for i 2‘th probe (slot) in i’th iteration and the value of i = 0,
1, . . . n – 1. We always start from the original hash location. If only the
location is occupied then we check the other slots.
Let hash(x) be the slot index computed using the hash function and n be
the size of the hash table.
If the slot hash(x) % n is full, then we try (hash(x) + 1 2) % n.
If (hash(x) + 12) % n is also full, then we try (hash(x) + 2 2) % n.
If (hash(x) + 22) % n is also full, then we try (hash(x) + 3 2) % n.
This process will be repeated for all the values of i until an empty slot is
found
Example: Let us consider table Size = 7, hash function as Hash(x) = x %
7 and collision resolution strategy to be f(i) = i 2 . Insert = 22, 30, and 50
 Step 1: Create a table of size 7.

Hash table

 Step 2 – Insert 22 and 30


 Hash(25) = 22 % 7 = 1, Since the cell at index 1 is empty,
we can easily insert 22 at slot 1.
 Hash(30) = 30 % 7 = 2, Since the cell at index 2 is empty,
we can easily insert 30 at slot 2.

18
Insert key 22 and 30 in the hash table

 Step 3: Inserting 50
 Hash(25) = 50 % 7 = 1
 In our hash table slot 1 is already occupied. So, we will
search for slot 1+12, i.e. 1+1 = 2,
 Again slot 2 is found occupied, so we will search for cell
1+22, i.e.1+4 = 5,
 Now, cell 5 is not occupied so we will place 50 in slot 5.

Insert key 50 in the hash table

2.c) Double Hashing


19
Double hashing is a collision resolving technique in Open
Addressed Hash tables. Double hashing make use of two hash function,
 The first hash function is h1(k) which takes the key and gives out a
location on the hash table. But if the new location is not occupied or
empty then we can easily place our key.
 But in case the location is occupied (collision) we will use secondary
hash-function h2(k) in combination with the first hash-
function h1(k) to find the new location on the hash table.
This combination of hash functions is of the form
h(k, i) = (h1(k) + i * h2(k)) % n
where
 i is a non-negative integer that indicates a collision number,
 k = element/key which is being hashed
 n = hash table size.
Complexity of the Double hashing algorithm:
Time complexity: O(n)
Example: Insert the keys 27, 43, 692, 72 into the Hash Table of size 7.
where first hash-function is h1(k) = k mod 7 and second hash-function
is h2(k) = 1 + (k mod 5)
 Step 1: Insert 27
 27 % 7 = 6, location 6 is empty so insert 27 into 6 slot.

Insert key 27 in the hash table

 Step 2: Insert 43
 43 % 7 = 1, location 1 is empty so insert 43 into 1 slot.

20
Insert key 43 in the hash table

 Step 3: Insert 692


 692 % 7 = 6, but location 6 is already being occupied and
this is a collision
 So we need to resolve this collision using double hashing.
hnew = [h1(692) + i * (h2(692)] % 7
= [6 + 1 * (1 + 692 % 5)] % 7
= 9 % 7
= 2

Now, as 2 is an empty slot,


so we can insert 692 into 2nd slot.

21
Insert key 692 in the hash table

 Step 4: Insert 72
 72 % 7 = 2, but location 2 is already being occupied and this
is a collision.
 So we need to resolve this collision using double hashing.
hnew = [h1(72) + i * (h2(72)] % 7
= [2 + 1 * (1 + 72 % 5)] % 7
= 5 % 7
= 5,

Now, as 5 is an empty slot,


so we can insert 72 into 5th slot.

Insert key 72 in the hash table

22

You might also like