DES Pune University - DSA April 2024 Solved Question Paper
Q.1 DFS and BFS Traversal (10 Marks)
1. DFS Algorithm (Adjacency List):
void DFS(int v, boolean[] visited, List<List<Integer>> adj) {
visited[v] = true;
for (int u : adj.get(v)) {
if (!visited[u]) DFS(u, visited, adj);
}
}
2. BFS Algorithm:
void BFS(int start, List<List<Integer>> adj) {
Queue<Integer> q = new LinkedList<>();
boolean[] visited = new boolean[adj.size()];
q.add(start); visited[start] = true;
while (!q.isEmpty()) {
int v = q.poll();
for (int u : adj.get(v)) {
if (!visited[u]) {
visited[u] = true;
q.add(u);
}
}
}
}
Example: A B C (DFS and BFS: A B C)
Q.2 BST Traversals (10 Marks)
1. Postorder Traversal:
void postorder(Node root) {
if (root == null) return;
postorder(root.left);
postorder(root.right);
System.out.print(root.data + " ");
}
2. Height of Tree:
int height(Node root) {
if (root == null) return 0;
return 1 + Math.max(height(root.left), height(root.right));
}
Q.3 Leaf Node Algorithm Explanation (10 Marks)
Algorithm prints leaf nodes only.
Example Output: 7, 5, 14, 12, 33, 10
Q.4 MST using Prim s and Kruskal s (10 Marks)
Prim s (Start at 0): (0-1,6), (1-2,5), (2-4,4), (4-3,5)
Kruskal s: (2-4,4), (1-2,5), (4-3,5), (0-1,6)
Total MST Weight: 20
Q.5 Dijkstra from Node 3 (10 Marks)
Final Distances:
Node 0 6
Node 1 11
Node 2 5
Node 3 0
Node 4 10
Q.6 Hashing and Bipartite Graphs (5 Marks)
A. Collisions: Resolved via Linear Probing, Quadratic Probing, Double Hashing, Chaining
B. Bipartite Graph: 2-colorable, edges only between partitions (U, V)
Q.7 Short Notes (5 Marks)
A. Sorting Complexities:
- Bubble: O(n )
- Merge: O(n log n)
- Quick: O(n log n), worst O(n )
B. Greedy Strategy:
Makes optimal local choices. Used in Prim s, Kruskal s, Huffman Coding.