0% found this document useful (0 votes)
21 views24 pages

May Jun 2022 Full Solutions

The document covers various graph and tree data structures, including directed graphs, minimum spanning trees using Prim's algorithm, topological sorting, and depth-first search (DFS). It also discusses different types of trees such as Red-Black Trees, Splay Trees, AVL Trees, B Trees, and B+ Trees, along with their properties and operations. Additionally, it touches on file organization methods, sorting algorithms, and the concept of tries.

Uploaded by

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

May Jun 2022 Full Solutions

The document covers various graph and tree data structures, including directed graphs, minimum spanning trees using Prim's algorithm, topological sorting, and depth-first search (DFS). It also discusses different types of trees such as Red-Black Trees, Splay Trees, AVL Trees, B Trees, and B+ Trees, along with their properties and operations. Additionally, it touches on file organization methods, sorting algorithms, and the concept of tries.

Uploaded by

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

Q1 a) Draw any directed graph with minimum 6 nodes and represent graph using

adjacency matrix, adjacency list and adjacency multi list.

Directed graph G with nodes {1,2,3,4,5,6}:

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.

Step 1: Start at vertex 1.


Available edges: (1 3,1), (1 4,5), (1 2,6). Choose (1 3,1).
Step 2: Nodes {1,3}. Add edges from 3: (3 6,4),(3 4,5),(3 2,5),(3 5,6). Choose
smallest overall: (3 6,4).
Step 3: Nodes {1,3,6}. Add (6 4,2).
Step 4: Nodes {1,3,6,4}. Next smallest edge to new node: (3 2,5).
Step 5: Nodes {1,2,3,4,6}. Next: (2 5,3).
MST edges: (1 3),(3 6),(6 4),(3 2),(2 5). Total weight = 1+4+2+5+3 = 15.
Q1 c) Write a short note on topological sorting.

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.

Initialize dist[s]=0, others = ; mark all unvisited.


Repeat:
pick unvisited u with smallest dist; mark visited.
for each neighbor v of u:
if dist[u]+w(u,v) < dist[v], update dist[v].
After all visited, dist[] holds shortest paths.
Q2 c) Show BFS and DFS for the following graph with starting vertex as 1. Explain
steps.

BFS order: 1,2,3,4,5 (queue-based level by level).


DFS order: 1,2,5,4,3 (stack/recursive try first neighbor, backtrack).
Q3 a) Explain with example i) Red-Black Tree ii) Splay Tree

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

Insert 1,2,3 imbalance at root, right-right left rotate at 1.


Insert 4,8 tree remains balanced.
Insert 7 imbalance at subtree right-left rotation.
Continue similarly. Final AVL tree is balanced with height log n.
Q3 c) What is OBST in data structure? and what are advantages of OBST?

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) Static tree tables: pointer-based tree where structure fixed at compile-time.


Dynamic: nodes allocated/deallocated at runtime (e.g., using malloc/new).
ii) Dynamic programming: solve complex problems by breaking into overlapping
subproblems, storing results; principle of optimality: optimal solution contains
optimal subsolutions.
Q4 b) Write short note on: i) AA tree ii) K-dimensional tree

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.

LL Rotation: right rotate unbalanced node.


RR Rotation: left rotate.
LR Rotation: left rotate heavy left child, then right rotate.
RL Rotation: right rotate heavy right child, then left rotate.
Examples shown with node keys 30,20,10 etc.
Q5 a) Construct B tree of order 5 for data: 78,21,14,11,97,85,74,63,45,42,57

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.

1. Find key in node. 2. If leaf, remove; if internal, replace with


predecessor/successor. 3. If underflow (<ceil(m/2)-1 keys), borrow or merge. 4.
Recursively fix.
Q6 c) Explain with example trie tree. Give advantages and applications of trie tree.

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.

Sequential: records stored in order by key. Advantages: simple, efficient bulk


processing. Disadvantages: slow random access, costly insert/delete.
Q7 b) What is file? List different file opening modes in C++. Explain concept of
inverted files.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy