22csc22 Cat-3.1 - Answer Key
22csc22 Cat-3.1 - Answer Key
3.
Outline the use of Kruskal‟s algorithm.
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
Additional
storage space Less(In-place) More(not In-place)
requirement
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.
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
Hash table
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.
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
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,
22