# Solutions for "May_Jun_2022 (2) (1).
pdf"
## Q1
### a) Draw any directed graph with minimum 6 nodes and represent graph using
adjacency matrix, adjacency list, and adjacency multi-list. [6 Marks]
Consider a directed graph with 6 nodes labeled A, B, C, D, E, F and the following
directed edges: A→B, A→C, B→D, C→D, C→E, D→F, E→F, F→A. This forms a graph with a
cycle and sufficient connectivity.
- **Diagram Description:** Visualize a graph where node A has arrows pointing to B
and C. B has an arrow to D. C has arrows to D and E. D points to F. E points to F,
and F loops back to A, forming a cycle. Nodes are arranged roughly in a circular
pattern with directed edges indicated by arrows.
- **Adjacency Matrix:**
A 6x6 matrix where rows and columns correspond to nodes A through F (A=0,
B=1, ..., F=5). A '1' indicates a directed edge, '0' indicates none.
```
A B C D E F
A [0 1 1 0 0 0]
B [0 0 0 1 0 0]
C [0 0 0 1 1 0]
D [0 0 0 0 0 1]
E [0 0 0 0 0 1]
F [1 0 0 0 0 0]
```
- **Adjacency List:**
For each node, list its outgoing neighbors.
- A: [B, C]
- B: [D]
- C: [D, E]
- D: [F]
- E: [F]
- F: [A]
- **Adjacency Multi-List:**
Each edge is a node in a list with pointers to the next edges from both vertices.
For example:
- Edge A→B: (A, B, next_from_A: A→C, next_from_B: B→D)
- Edge A→C: (A, C, next_from_A: null, next_from_C: C→D)
- Edge B→D: (B, D, next_from_B: null, next_from_D: D→F)
- And so on, forming a linked structure.
### b) Find minimum spanning tree of this graph using Prim's Algorithm. [6 Marks]
Given adjacency matrix (assuming undirected for MST):
```
1 2 3 4 5 6
1 [0 6 1 5 0 0]
2 [6 0 5 0 3 0]
3 [1 5 0 5 6 4]
4 [5 0 5 0 0 2]
5 [0 3 6 0 0 6]
6 [0 0 4 2 6 0]
```
- **Prim's Algorithm Steps:**
1. Start at node 1: MST = {1}, edges: 1-2(6), 1-3(1), 1-4(5). Add 1-3 (1).
2. Nodes: 1, 3: Edges: 1-2(6), 1-4(5), 3-2(5), 3-4(5), 3-5(6), 3-6(4). Add 3-6
(4).
3. Nodes: 1, 3, 6: Edges: 1-2(6), 1-4(5), 3-2(5), 3-4(5), 3-5(6), 6-4(2), 6-5(6).
Add 6-4 (2).
4. Nodes: 1, 3, 4, 6: Edges: 1-2(6), 3-2(5), 3-5(6), 6-5(6). Add 3-2 (5).
5. Nodes: 1, 2, 3, 4, 6: Edges: 2-5(3), 3-5(6), 6-5(6). Add 2-5 (3).
- **MST Edges:** 1-3(1), 3-6(4), 6-4(2), 3-2(5), 2-5(3). Total weight = 15.
- **Diagram Description:** The MST is a tree with node 3 central, connected to 1
(weight 1), 2 (weight 5), and 6 (weight 4). Node 6 connects to 4 (weight 2), and 2
connects to 5 (weight 3). Edges form a connected structure without cycles.
### c) Write a short note on topological sorting. [6 Marks]
Topological sorting orders vertices in a directed acyclic graph (DAG) such that for
every edge U→V, U precedes V. Not possible with cycles.
- **Key Points:**
- **Purpose:** Schedules tasks with dependencies (e.g., course prerequisites).
- **Methods:** DFS (add nodes post-exploration), Kahn’s (remove nodes with no
incoming edges).
- **Complexity:** O(V + E).
- **Example:** DAG with A→B, A→C, B→D, C→D. Order: A, B, C, D.
- **Diagram Description:** A directed graph with A at the top, arrows to B and C,
both pointing to D. No cycles, showing a clear dependency flow.
## Q2
### a) Write non-recursive pseudo-code for Depth First Search (DFS). [6 Marks]
DFS explores deeply along each branch before backtracking, using a stack.
DFS(graph, start):
stack = empty stack
visited = empty set
stack.push(start)
while stack is not empty:
node = stack.pop()
if node not in visited:
visited.add(node)
process(node) // e.g., print node
for neighbor in graph[node]:
if neighbor not in visited:
stack.push(neighbor)
- **Diagram Description:** For a graph 1-2-4, 1-3, stack starts with 1, pops to
explore 2, then 4, backtracks to 3, showing depth-first traversal.
### b) Consider the given graph and find the shortest path by using Dijkstra's
algorithm from source to all other nodes. [6 Marks]
Assume nodes A, B, C, D, E with edges: A-B(2), A-C(5), B-D(3), C-D(1), C-E(4), D-
E(2). Source A.
- **Steps:**
1. Dist[A]=0, others ∞. Queue = {A(0)}.
2. Extract A: B(2), C(5). Queue = {B(2), C(5)}.
3. Extract B: D(5). Queue = {C(5), D(5)}.
4. Extract C: D(5), E(9). Queue = {D(5), E(9)}.
5. Extract D: E(7). Queue = {E(7)}.
6. Extract E: Done.
- **Shortest Paths:** A:0, B:2, C:5, D:5 (via C), E:7 (via D).
- **Diagram Description:** A tree from A: A to B (2), A to C (5), C to D (1), D to
E (2), showing shortest paths.
### c) Show BFS and DFS for the following graph with starting vertex as 1. Explain
with proper steps. [6 Marks]
Graph: 1-2, 1-3, 2-4, 3-4.
- **BFS:**
- Queue: [1], Visited: [1]
- Dequeue 1, enqueue 2, 3: [2, 3], [1, 2, 3]
- Dequeue 2, enqueue 4: [3, 4], [1, 2, 3, 4]
- Dequeue 3, 4: [], Done
- **Order:** 1, 2, 3, 4
- **DFS:**
- Stack: [1], Visited: [1]
- Pop 1, push 3, 2: [3, 2], [1, 3, 2]
- Pop 2, push 4: [3, 4], [1, 2, 3, 4]
- Pop 4, 3: [], Done
- **Order:** 1, 2, 4, 3
- **Diagram Description:** BFS: 1 at center, level 1 with 2 and 3, level 2 with 4.
DFS: 1 to 2 to 4, back to 3, showing depth path.
## Q3
### a) Explain with example: i) Red-Black Tree, ii) Splay Tree [6 Marks]
- **Red-Black Tree:** Self-balancing BST with red/black nodes.
- **Example:** Insert 10, 20, 30 → 20 (black), 10 (red), 30 (red).
- **Diagram:** 20 at root, 10 left (red), 30 right (red), balanced.
- **Splay Tree:** Moves accessed nodes to root.
- **Example:** Insert 10, 20, 30, access 30 → 30-20-10.
- **Diagram:** 30 at root, 20 left, 10 below 20, right-skewed.
### b) Construct AVL tree for the sequence: 1, 2, 3, 4, 8, 7, 6, 5, 1, 10 [6 Marks]
Sequence (ignoring duplicate 1): 1, 2, 3, 4, 8, 7, 6, 5, 10.
- **Steps:**
1. 1: [1]
2. 2: [1-2]
3. 3: [2-1-3]
4. 4: [3-2-4-1]
5. 8: [3-2-4-8]
6. 7: [3-2-4-(8-7)]
7. 6: [3-2-4-(6-7-8)]
8. 5: [3-2-4-(6-5-(7-8))]
9. 10: [3-2-4-(6-5-(7-8-10))]
- **Diagram Description:** Root 3, left 2-1, right 4 with subtree 6-5-(7-8-10),
balanced at each level.
### c) What is OBST in data structure? And what are advantages of OBST? [5 Marks]
- **OBST:** Minimizes expected search cost in BST using access probabilities.
- **Advantages:** Efficient for frequent keys, adaptable to patterns, useful in
databases.
## Q4
### a) Explain: i) Static and dynamic tree tables with suitable example, ii)
Dynamic programming with principle of popularity [6 Marks]
- **Static Tree Tables:** Fixed array representation.
- **Example:** [1, 2, 3], 1 root, 2 left, 3 right.
- **Dynamic Tree Tables:** Pointer-based.
- **Example:** 1→2, 1→3.
- **Dynamic Programming:** Optimal subproblem solutions (assuming "optimality").
- **Example:** Fibonacci bottom-up.
### b) Write short note on: i) AA tree, ii) K-dimensional tree [6 Marks]
- **AA Tree:** Simplified red-black tree.
- **KD-Tree:** Organizes k-dimensional points.
- **Example:** (2,3), (5,4) split on x, y.
### c) Explain AVL tree rotations with example. [5 Marks]
- **Rotations:** Left, Right, Left-Right, Right-Left.
- **Example:** 1-2-3 → 2-1-3 (left rotation).
- **Diagram:** Before: 1-2-3 skew right; after: 2 root, 1 left, 3 right.
## Q5
### a) Construct B-tree of order 5 for the following data: 78, 21, 14, 11, 97, 85,
74, 63, 45, 42, 57 [6 Marks]
- **Steps:**
1. [78]
2. [21, 78]
3. [14, 21, 78]
4. [11, 14, 21, 78]
5. [11, 14, 21, 78, 97] → [21], [11, 14], [78, 97]
6. [21], [11, 14], [78, 85, 97]
7. [21], [11, 14], [74, 78, 85, 97]
8. [21, 78], [11, 14], [63, 74], [85, 97]
9. [21, 78], [11, 14], [45, 63, 74], [85, 97]
10. [21, 78], [11, 14], [42, 45, 63, 74], [85, 97]
11. [21, 57, 78], [11, 14], [42, 45], [63, 74], [85, 97]
- **Diagram Description:** Root [21, 57, 78], four children [11, 14], [42, 45],
[63, 74], [85, 97], all balanced.
### b) Explain B+ tree deletion with example. [6 Marks]
- **Deletion:** Remove from leaves, adjust if underflow.
- **Example:** [10], [5], [15, 20] → Delete 15: [10], [5], [20].
- **Diagram:** Before: Leaves linked 5, 15, 20; after: 5, 20 linked.
### c) What is B+ tree? Give structure of its internal node. What is the difference
between B and B+ tree? [6 Marks]
- **B+ Tree:** Data in leaves, linked.
- **Internal Node:** [P1, K1, P2, K2, ...]
- **Differences:** B-tree data in all nodes, B+ only in leaves, linked leaves.
## Q6
### a) Build B-tree of order 3 for the following [6 Marks]
Assume: 1, 2, 3, 4, 5, 6.
- **Steps:**
1. [1]
2. [1, 2]
3. [2], [1], [3]
4. [2], [1], [3, 4]
5. [2, 4], [1], [3], [5]
6. [2, 4], [1], [3], [5, 6]
- **Diagram Description:** Root [2, 4], children [1], [3], [5, 6], showing splits.
### b) Write an algorithm of B-tree deletion. [6 Marks]
BTreeDelete(tree, key):
node = findNode(tree.root, key)
if node is leaf:
remove key from node
if underflow(node):
balance(node)
else:
pred = getPredecessor(node, key)
replace key with pred in node
remove pred from its node
if underflow(pred’s node):
balance(pred’s node)
balance(node):
if node.keys < minKeys:
sibling = getSibling(node)
if sibling.keys > minKeys:
borrowFromSibling(node, sibling)
else:
mergeWithSibling(node, sibling)
updateParent(node.parent)
### c) Explain with example trie tree. Give advantage and applications of trie tree
[6 Marks]
- **Trie:** Stores strings character-by-character.
- **Example:** "cat", "car" → Root-c-a-(t, r).
- **Diagram:** Root to c, to a, splitting to t and r.
- **Advantages:** Fast prefix search, space-efficient.
- **Applications:** Autocomplete, IP routing.
## Q7
### a) Define sequential file organization. Give its advantages and disadvantages.
[6 Marks]
- **Definition:** Linear, ordered records.
- **Advantages:** Simple, efficient sequentially.
- **Disadvantages:** Slow random access, updates hard.
### b) What is file? List different file opening modes in C++. Explain concept of
inverted files. [6 Marks]
- **File:** Data collection on storage.
- **Modes:** `ios::in`, `ios::out`, `ios::app`, etc.
- **Inverted Files:** Index terms to locations.
### c) Write short note on external sort. [5 Marks]
- **External Sort:** Disk-based sorting for large data.
- **Process:** Sort chunks, merge.
## Q8
### a) Write a C++ program to create a file, insert records in append mode, and
search for a specific record. [6 Marks]
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void insertRecord(string filename) {
ofstream file(filename, ios::app);
string record;
cout << "Enter record: ";
getline(cin, record);
file << record << endl;
file.close();
}
void searchRecord(string filename, string key) {
ifstream file(filename);
string line;
bool found = false;
while (getline(file, line)) {
if (line.find(key) != string::npos) {
cout << "Found: " << line << endl;
found = true;
}
}
if (!found) cout << "Record not found.\n";
file.close();
}
int main() {
string filename = "records.txt";
insertRecord(filename);
string key;
cout << "Enter search key: ";
getline(cin, key);
searchRecord(filename, key);
return 0;
}
### b) Sort the following elements using two-way merge sort with n=3: 20, 47, 15,
8, 9, 4, 40, 30, 12, 17, 11, 56, 28, 35 [6 Marks]
- **Steps:**
- Split: [20, 47, 15, 8, 9, 4, 40], [30, 12, 17, 11, 56, 28, 35]
- Merge: [4, 8, 9, 15, 20, 40, 47], [11, 12, 17, 28, 30, 35, 56]
- Final: [4, 8, 9, 11, 12, 15, 17, 20, 28, 30, 35, 40, 47, 56]
- **Diagram:** Split into two sublists, merge showing sorted order.
### c) Explain indexed sequential file organization. Compare it with direct access
file. [5 Marks]
- **Indexed Sequential:** Sequential with index.
- **Comparison:** Direct access faster for random, no sequential support.