Solved_DS_A_2024_Sem4 (1)
Solved_DS_A_2024_Sem4 (1)
i) Graph: A graph is a non-linear data structure consisting of vertices (nodes) and edges
(connections). It can be directed or undirected, weighted or unweighted.
ii) Adjacency List: Represents a graph as a list where each vertex has a list of adjacent
vertices. Efficient for sparse graphs.
iii) Adjacency Matrix: A 2D array where cell (i, j) holds value 1 (or weight) if there's an edge
from vertex i to j.
Tree:
- Hierarchical structure
- No cycles
- Always connected
- One parent per node (except root)
- Edge count = n-1
Graph:
- Network structure
- May have cycles
- May not be connected
- No parent-child restriction
- Any number of edges
FloydWarshall(dist[][]):
for k = 0 to V-1:
for i = 0 to V-1:
for j = 0 to V-1:
if dist[i][j] > dist[i][k] + dist[k][j]:
dist[i][j] = dist[i][k] + dist[k][j]
4. Q2 a) Find the shortest path using Dijkstra’s algorithm.
Steps:
1. Mark all nodes unvisited. Set distance of source = 0, others = ∞.
2. Set current node = source.
3. Update distances to neighbors.
4. Mark current node as visited.
5. Choose unvisited node with smallest distance.
6. Repeat until all nodes visited.
Prim(G):
- Initialize all keys = ∞, parent[] = NIL
- Start from any vertex, set its key = 0
- For V vertices:
- Pick u with min key and not in MST
- Include u in MST
- Update key and parent for adjacent vertices
10. Q4 a) What is the need of AA tree? List the five invariants that AA tree must satisfy.
AA tree is a balanced binary search tree that simplifies the balancing logic of Red-Black
trees.
Invariants:
1. Left child level is one less than parent.
2. Right child's level equals parent or one less.
3. No two consecutive right links on the same level.
4. Leaf nodes have level 1.
5. Every node has level ≥ 1.
After deleting nodes (30, 55, 60), rebalance tree using rotations as needed to maintain AVL
properties.
Insert: F, S, Q, K, C, L, H, T, V, W, M, R.
Split nodes when overflow occurs. All data in leaves.