Programming, Data Structures and Algorithms

GATE DA previous year questions · 22 questions

GATE DA 2024 | Question 51

MSQ · 2 marks

Consider the directed acyclic graph (DAG) below:

Directed graph with seven vertices: P and R each have an edge into Q, Q has edges to S and V, S has an edge to U, and V has an edge to T.

Which of the following is/are valid vertex orderings that can be obtained from a topological sort of the DAG?

Options for GATE DA 2024 | Question 51

Explanation

Answer: (B), (D)

In a topological order every edge points forward, so each vertex comes after all of its predecessors. The edges are \(P \to Q\), \(R \to Q\), \(Q \to S\), \(Q \to V\), \(S \to U\) and \(V \to T\): P and R must precede Q, Q must precede S and V, S must precede U, and V must precede T.

  • A and C fail: both place Q before R, which breaks the edge \(R \to Q\).
  • B holds: after P, R, Q come V, S, U, T, so U follows S and T follows V.
  • D holds: P, R, Q, S, V, T, U also respects every edge.

More than one order is valid because the chains \(S \to U\) and \(V \to T\) do not depend on each other.

Answer: B, D

GATE DA 2024 | Question 52

MSQ · 2 marks

Let \(H, I, L\), and \(N\) represent height, number of internal nodes, number of leaf nodes, and the total number of nodes respectively in a rooted binary tree.

Which of the following statements is/are always TRUE?

Options for GATE DA 2024 | Question 52

Explanation

Answer: (A), (B), (C)

Height here is the number of edges on the longest root-to-leaf path, so a single node has height 0; the bounds in the options use this convention.

  • A holds: if \(n_{2}\) nodes have two children, the tree has exactly \(L = n_{2} + 1\) leaves, and those \(n_{2}\) nodes are internal, so \(L \le I + 1\).
  • B holds: the longest path alone has \(H + 1\) nodes, and levels 0 to \(H\) hold at most \(1 + 2 + \dots + 2^{H} = 2^{H+1} - 1\) nodes.
  • C holds: the first \(H\) nodes on the longest path are internal, and internal nodes can sit only on levels 0 to \(H - 1\), which hold at most \(2^{H} - 1\) nodes.
  • D fails: a path of height 2 has a single leaf, so \(H \le L\) breaks, and a perfect tree has \(2^{H}\) leaves, more than \(2^{H-1}\).

Answer: A, B, C