Blog
Data Structures and Algorithms MIT 3rd Final Past Paper 2014 – UOS Main Campus
University of Sargodha main campus (Department of CS & IT) Data Structures and Algorithms final term paper for MIT 3rd (SS1-SS2), 16 June 2014. Retyped from the original paper, with solved answers below.
Solved answers
Q1
a) Circular queue, size 5, one slot sacrificed (capacity 4). front = index of first element, rear = next free index; empty when front = rear, full when (rear + 1) % 5 = front.
| Operation | [0] | [1] | [2] | [3] | [4] | front | rear |
|---|---|---|---|---|---|---|---|
| start | 0 | 0 | |||||
| add(A) | A | 0 | 1 | ||||
| add(B) | A | B | 0 | 2 | |||
| add(C) | A | B | C | 0 | 3 | ||
| remove → A | B | C | 1 | 3 | |||
| add(D) | B | C | D | 1 | 4 | ||
| add(E) | B | C | D | E | 1 | 0 (full) | |
| remove → B | C | D | E | 2 | 0 | ||
| add(F) | F | C | D | E | 2 | 1 (full) | |
| add(G) | F | C | D | E | 2 | 1 — overflow, G not added |
b) Output: obj = que.first() = “a” (first is not removed). The queue becomes a, b, c, ad, axa, so the program prints a_b_c_ad_axa_
Q2 – Doubly linked list with nodes 1 to 4
struct DNode { int data; DNode *prev, *next; };
DNode *head = NULL, *tail = NULL;
for (int i = 1; i <= 4; i++) {
DNode *n = new DNode;
n->data = i; n->next = NULL; n->prev = tail;
if (tail == NULL) head = n; else tail->next = n;
tail = n;
}
// NULL <- 1 <-> 2 <-> 3 <-> 4 -> NULLQ3 – Traversals
- Preorder: A B C D E F G H I J K L M N
- Post-order: D C G F H E B K J M N L I A
Q4 – Kruskal’s algorithm
Edges in increasing order: c–f 1, c–d 2, f–g 2, a–b 4, e–f 5, e–g 6, d–e 7, a–c 8, b–d 8, b–c 9, d–f 9, b–e 10.
| Edge | Weight | Action |
|---|---|---|
| c–f | 1 | accept |
| c–d | 2 | accept |
| f–g | 2 | accept |
| a–b | 4 | accept |
| e–f | 5 | accept |
| e–g | 6 | reject (cycle e–f–g) |
| d–e | 7 | reject (cycle) |
| a–c | 8 | accept — joins {a, b} with the rest |
MST T = {c–f, c–d, f–g, a–b, e–f, a–c}, total weight = 1 + 2 + 2 + 4 + 5 + 8 = 22 (6 edges for 7 vertices). Choosing b–d (also 8) instead of a–c gives an equally minimal tree.
Q5 – Dijkstra from s
Edges: s→c 3, s→d 9, s→e 2, c→s 2, c→b 4, c→d 1, b→c 7, b→s 7, d→e 5, e→d 2.
| Iteration | Vertex added | S | d(s,b,c,d,e) | π(b,c,d,e) |
|---|---|---|---|---|
| init | — | {} | 0, ∞, ∞, ∞, ∞ | –, –, –, – |
| 1 | s | {s} | 0, ∞, 3, 9, 2 | –, s, s, s |
| 2 | e | {s, e} | 0, ∞, 3, 4, 2 | –, s, e, s |
| 3 | c | {s, e, c} | 0, 7, 3, 4, 2 | c, s, e, s |
| 4 | d | {s, e, c, d} | 0, 7, 3, 4, 2 | c, s, e, s |
| 5 | b | {s, e, c, d, b} | 0, 7, 3, 4, 2 | c, s, e, s |
Shortest distances: s 0, e 2, c 3, d 4 (s→e→d), b 7 (s→c→b).
