May Jun 2022 Full Solutions
May Jun 2022 Full Solutions
Edges: 1 3, 1 4, 2 3, 2 5, 3 2, 3 6, 4 6, 5 6.
Adjacency Matrix:
1 2 3 4 5 6
1: 0 0 1 1 0 0
2: 0 0 1 0 1 0
3: 0 1 0 0 0 1
4: 0 0 0 0 0 1
5: 0 0 0 0 0 1
6: 0 0 0 0 0 0
Adjacency List:
1: [3,4]
2: [3,5]
3: [2,6]
4: [6]
5: [6]
6: []
Adjacency Multi-list:
Each edge stored once with two pointers in node list; see diagram in answer sheet.
Q1 b) Consider the graph represented by the following adjacency matrix: [matrix].
Find minimum spanning tree using Prim s Algorithm.
Topological sorting produces a linear ordering of vertices in a DAG such that for
every directed edge u v, u appears before v. It can be done via Kahn s algorithm
(remove nodes with indegree 0 iteratively) or DFS-based method (push nodes to stack
on return). Time complexity O(V+E).
Q2 a) Write non-recursive pseudo for Depth First Search (DFS).
Pseudo-Code:
DFS(G, s):
let Stack S
mark all vertices unvisited
push s onto S; mark s visited
while S not empty:
u = top(S)
if exists unvisited v Adj[u]:
mark v visited; push v onto S
else:
pop S
Q2 b) Consider the given graph and find the shortest path by using Dijkstra s
algorithm from source to all other nodes.
i) Red-Black Tree: a self-balancing BST where each node is red or black, root black,
red nodes have only black children, and every path from root to leaf has same number
of black nodes. Example: insert sequence 10,20,30 yields rotations/recoloring.
ii) Splay Tree: a BST that moves accessed node to root via rotations (zig/zag) to
speed up future accesses; no explicit balance factor.
Q3 b) Construct AVL tree for following sequence of keys: 1,2,3,4,8,7,6,5,11,10
Optimal Binary Search Tree (OBST) is a BST that minimizes expected search cost given
search probabilities for keys and gaps. Advantages: minimizes average lookup time
when search frequency non-uniform.
Q4 a) Explain i) Static and dynamic tree tables with suitable example. ii) Dynamic
programming with principle of optimality.
i) AA Tree: simplified Red-Black tree with level attribute; uses skew and split
operations for balancing.
ii) K-D Tree: a binary tree for k-dimensional points, alternating split dimension at
each level; supports multi-dimensional range search.
Q4 c) Explain AVL tree rotations with example.
Insert in order, split when node has 5 keys. Final B-tree with root and children; see
detailed tree in answer sheet.
Q5 b) Explain B+ tree deletion with example.
Deletion removes key from leaf; if underflow, borrow from sibling or merge nodes;
propagate changes upward. Example: delete 45, borrow or merge accordingly.
Q5 c) What is B+ tree? Give structure of its internal node. Difference between B and
B+ tree.
B+ tree stores keys in leaves only; internal nodes store only separators and
pointers. Leaves are linked. Differences: B-tree stores keys in all nodes; B+
supports range queries better.
Q6 a) Build B+ tree of order 3 for data: F,S,Q,K,C,L,H,T,V,W,M,R
Insert sequence by converting letters to keys; split when >3 children. Final B+ tree
with linked leaves; see diagram.
Q6 b) Write an algorithm of B tree deletion.
Trie: multi-way tree storing strings per character per level. Advantages: fast prefix
search O(m). Applications: autocomplete, spell-check.
Q7 a) Define sequential file organization. Give its advantages and disadvantages.
File: sequence of bytes on storage. Modes: ios::in, out, app, binary, trunc. Inverted
file: index mapping terms to record lists.
Q7 c) Write short note on external sort.
External sort: sorts data larger than memory using multi-way merge sort: create
sorted runs, then merge runs using buffer pages.
Q8 a) Write C++ program to create file. Insert records, search specific record.
#include<fstream>
...
ofstream out("data.txt", ios::app);
inserting records...
ifstream in("data.txt");
search by reading each record and comparing key...
Q8 b) Sort elements using two way merge sort with m=3:
20,47,15,8,9,4,40,30,12,17,11,56,28,35
Divide into runs of size 3: [20,47,15],[8,9,4],... Sort runs, then merge two at a
time: [15,20,47] [4,8,9] ... final sorted sequence.
Q8 c) Explain indexed sequential file organization. Compare it with direct access
file.
Indexed sequential: sequential file with index for direct jumps. Direct access: fixed
record size and direct addressing. Indexed sequential allows range and random access;
direct access allows O(1) jumps but no range.