GATE / CS ARCHIVE

SUBJECT COLLECTION

Algorithms · Graph Algorithms

94 questions, newest first

GATE CSE has asked 94 questions on Graph Algorithms (Algorithms) from 1991 to 2026, in 42 of the 46 papers; the most recent is in GATE CSE 2026 Set 2. In the last five years it carried 3.5 marks per 100-mark paper on average, against 3.2 since 2003. Since 2014 the questions have been 19 MCQ, 16 NAT and 7 MSQ. 10 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
Show
2 marks · MCQ
Report issue

Shortest paths with a bounded edge budget

Graph Algorithms

Question 41, GATE 2026: In an undirected integer-weighted graph, d_k(u) is the minimum weight of an s-to-u path with at most k edges, or infinity if none exists. S1 says d_(k+1)(u)≤d_k(u). S2 says that if edge (u,v) lies on a shortest s-to-v path, then d_k(u)≤d_k(v) for every k. Classify S1 and S2.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Givens and goal

In an undirected integer-weighted graph, dk(u) is the minimum weight of an s-to-u path with at most k edges, or infinity if none exists. S1 says dk+1(u)≤dk(u). S2 says that if edge (u,v) lies on a shortest s-to-v path, then dk(u)≤dk(v) for every k. Classify S1 and S2.

Approach

Compare nested feasible path sets for S1 and use an explicit small edge-budget counterexample for S2.

Step-by-step solution

1. Every path using at most k edges also uses at most k+1 edges. The feasible set for dk(u) is therefore contained in the feasible set for dk+1(u).
2. Minimizing over a larger set cannot increase the minimum. Hence dk+1(u)≤dk(u), including the situation where an unavailable path of value infinity becomes available. S1 is true without assuming positive weights.
3. For S2, consider four vertices s,a,u,v with edges s—a of weight 1, a—u of weight 1, u—v of weight 1, and s—v of weight 10. The shortest s-to-v path is s—a—u—v, of total weight 3, and it includes (u,v).
4. Impose budget k=1. There is no one-edge s-to-u path, so d₁(u)=∞. But the direct edge s—v is permitted, giving d₁(v)=10. The asserted inequality ∞≤10 is false.
5. Thus only S1 holds, option A. Being a prefix of an unrestricted shortest path does not imply being reachable under the same smaller edge budget. The counterexample uses strictly positive integer weights, so the failure does not depend on negative cycles or an ambiguity about repeated vertices.

Original paper, page 30 · Answer key

2 marks · MSQ
Report issue

Strict cut and cycle properties of MSTs

Graph Algorithms

Question 49, GATE 2026: A simple undirected edge-weighted graph has distinct edge weights. Assess: A each cycle’s largest edge belongs to no MST; B each cycle’s smallest edge belongs to every MST; C each vertex’s largest incident edge belongs to no MST; D each vertex’s smallest incident edge belongs to every MST.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, D

Givens and goal

A simple undirected edge-weighted graph has distinct edge weights. Assess: A each cycle’s largest edge belongs to no MST; B each cycle’s smallest edge belongs to every MST; C each vertex’s largest incident edge belongs to no MST; D each vertex’s smallest incident edge belongs to every MST.

Approach

Use exchange arguments for the valid cycle/cut statements and connected counterexamples for the invalid ones.

Step-by-step solution

1. For A, suppose an MST T contains the uniquely heaviest edge e of some cycle. Removing e splits T into two components. The rest of the cycle still connects e’s endpoints, so at least one other cycle edge f crosses that cut. Distinctness makes weight(f)<weight(e). Replacing e with f produces a lighter spanning tree, a contradiction. Thus A is true.
2. For D, take the cut {v} versus all other vertices. Let e be v’s unique lightest incident edge. If an MST omitted e, adding e creates a cycle. On that cycle another edge incident to v crosses the same cut and is heavier. Removing it would reduce the tree weight. Thus e must belong to every MST and D is true.
3. B is not a valid cycle rule. Consider edges ad=1, bd=2, ab=3, bc=4, ac=5. In the cycle a—b—c—a the smallest edge is ab=3. But Kruskal’s algorithm selects ad,bd,bc, forming an MST of weight 7 and excluding ab because a and b already have the cheaper connecting path a—d—b.
4. C fails even for a graph that is itself a tree. It has only one spanning tree, so every edge must be present, including a vertex’s heaviest incident edge. A two-vertex graph with one edge is the simplest witness.
5. Select A and D. Distinct weights make these strict exchange comparisons immediate. The ordinary MST question assumes connectivity; for a disconnected graph the corresponding statements apply componentwise to a minimum spanning forest, with no incident-edge claim needed for an isolated vertex.

Original paper, page 37 · Answer key

2 marks · MSQ
Report issue

DFS time intervals on a DAG

Graph Algorithms

Question 50, GATE 2026: DFS assigns each vertex discovery time d and finish time f. The directed input graph is acyclic. For an edge u→v, which time orders are impossible? A d[u]<d[v]<f[v]<f[u]; B d[v]<d[u]<f[u]<f[v]; C d[v]<f[v]<d[u]<f[u]; D d[u]<d[v]<f[u]<f[v].
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, D

Givens and goal

DFS assigns each vertex discovery time d and finish time f. The directed input graph is acyclic. For an edge u→v, which time orders are impossible? A d[u]<d[v]<f[v]<f[u]; B d[v]<d[u]<f[u]<f[v]; C d[v]<f[v]<d[u]<f[u]; D d[u]<d[v]<f[u]<f[v].

Approach

Use the parenthesis theorem for recursive DFS intervals, then use acyclicity to exclude ancestor-directed back edges.

Step-by-step solution

1. A DFS call remains active while all recursive descendants are explored. Therefore two vertices’ discovery-to-finish intervals are either nested, for an ancestor and descendant, or disjoint. Partial overlap is impossible.
2. D has d[u]<d[v]<f[u]<f[v], a partial overlap. Once v is discovered while u’s call is active, v must finish before that call can finish. Thus D is impossible in every DFS, regardless of whether the graph is acyclic.
3. In B, v’s interval strictly contains u’s, so v is a DFS ancestor of u. The DFS-tree edges give a directed path v→…→u. Together with the given edge u→v, this creates a directed cycle, forbidden in a DAG. Hence B is also impossible.
4. A is possible: take the graph with only edge u→v and begin at u. The times can be d[u]=1,d[v]=2,f[v]=3,f[u]=4, a tree-edge interval.
5. C is possible if v is explored first and finishes before u is discovered. With the same single edge u→v, choose v as the first DFS root: times can be d[v]=1,f[v]=2,d[u]=3,f[u]=4. The later edge goes to an already finished vertex and creates no cycle.
6. Select B and D. The DAG restriction is needed for excluding B; the nesting rule alone already excludes D.

Original paper, page 38 · Answer key

2 marks · MCQ
Report issue

Linear-time shortest paths in a DAG

Graph Algorithms

Question 37, GATE 2026: A weighted directed acyclic graph has n vertices and m edges, with a specified source s. Find the fastest worst-case complexity to compute shortest-path distances from s to reachable vertices: A Θ(m+n); B Θ(m+n log n); C Θ(nm); D Θ(n³).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Givens and goal

A weighted directed acyclic graph has n vertices and m edges, with a specified source s. Find the fastest worst-case complexity to compute shortest-path distances from s to reachable vertices: A Θ(m+n); B Θ(m+n log n); C Θ(nm); D Θ(n³).

Approach

Use an order in which every predecessor is settled before its successors.

Step-by-step solution

1. A DAG has a topological ordering: each edge u→v goes forward in that order. Compute one using indegrees or DFS in O(m+n) time with adjacency lists.
2. Initialize d[s]=0 and d[v]=∞ for all other vertices. Process vertices in topological order; for each outgoing edge u→v of weight w, set d[v]=min(d[v],d[u]+w) when d[u] is finite.
3. When u is processed, every possible final edge of a path into u comes from an earlier vertex. By induction, all those predecessor distances are final, so d[u] is the shortest distance. Negative edge weights do not break this argument because there are no cycles or backward dependencies.
4. Each vertex is initialized/processed once and each edge relaxed once, totaling O(m+n). Worst-case instances require reading relevant edge weights and producing vertex distances, giving a matching linear lower bound in the standard input model.
5. Thus A is correct. Dijkstra’s priority queue and Bellman-Ford’s repeated passes are unnecessary when acyclicity supplies the processing order.

Original paper, page 25 · Answer key

1 mark · MCQ
Report issue

Comparing tree and graph shortest paths

Graph Algorithms

Question 18, GATE 2025: G is an undirected graph with positive edge weights and T is a minimum spanning tree. Let d1(u,v) be the shortest-path distance in G and d2(u,v) that in T. Which holds for all vertices and all such graphs? A d1=d2; B d1≤d2; C d1≥d2; D d1≠d2.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Approach: Compare the sets of paths available in the graph and its spanning-tree subgraph.
The unique u-to-v path in T uses only edges of G. Thus it is one candidate among the paths minimized over when defining d1. Minimizing over a set containing that candidate cannot give a larger value than its length: d1(u,v)≤d2(u,v). Therefore B always holds.
Equality is possible, for example if G itself is a tree. Strict inequality is also possible: take a triangle with all edge weights 1 and a spanning tree consisting of two edges. Between the endpoints of the omitted edge, the graph distance is 1 but the tree distance is 2. These examples reject A and D as universal claims and reject C.
An MST minimizes total chosen-edge weight, not every pairwise path distance. Indeed the inequality holds for any spanning tree; minimum total weight is unnecessary for this particular proof.

Original paper, page 18 · Answer key

2 marks · MCQ
Report issue

Bounding the height of every BFS tree

Graph Algorithms

Question 43, GATE 2025: An undirected unweighted graph has 100 vertices and maximum finite shortest-path distance 30 between vertices, in the usual connected-graph interpretation. T is any BFS tree. Which holds universally? A height exactly 15; B height exactly 30; C height at least 15; D height at least 30.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Approach: A BFS tree preserves distance from its root; relate those root distances to two diameter endpoints.
Let r be the BFS root and let h=maxv d(r,v), the tree height. Choose u,v with d(u,v)=30. A route from u to r and then from r to v has length d(u,r)+d(r,v), so the shortest u-to-v path obeys 30≤d(u,r)+d(r,v)≤h+h=2h. Therefore h≥15, giving C.
Also h≤30 because no pair of vertices is farther apart than the diameter. The actual height depends on the chosen root, so neither exact value in A or B is guaranteed. A path of length 30 rooted at its centre has height 15, while rooting at an endpoint gives height 30. To meet the 100-vertex requirement, attach 69 extra leaves at the central vertex; the diameter remains 30 and the same two heights remain possible.
Thus D’s stronger lower bound 30 is false. BFS preserves root distances, but it does not necessarily choose a central root.

Original paper, page 43 · Answer key

2 marks · NAT
Report issue

Making an edge mandatory in every MST

Graph Algorithms

Question 64, GATE 2025: An undirected four-vertex graph has edges AB=7, AD=6, DC=8, BC=x, BD=1, AC=3. The crossing diagonals BD and AC are edges, not an extra intersection vertex. Find the largest integer x for which BC belongs to every minimum spanning tree.
Tap to enlarge
Answer & solution

Official answer: 5

Approach: Find a cut on which BC is uniquely cheapest, then test the equality boundary because “every MST” is stronger than “some MST”.
Consider cut {B,D}|{A,C}. Its crossing edges have weights BC=x, DA=6, BA=7 and DC=8. If x<6, BC is the unique lightest edge of this cut. By the cut property it must occur in every MST. In detail, any spanning tree omitting BC has another crossing edge on the path connecting B to C; adding BC and removing that heavier crossing edge lowers its weight, contradicting minimality.
At x=6, the tree {BD,AC,AD} has weight 1+3+6=10 and omits BC. The tree {BD,AC,BC} also has weight 10, and no spanning tree can have smaller weight than the three globally smallest edge weights, which sum to 10. Thus BC is no longer mandatory.
For x>6, BC is uniquely heaviest on cycle B-D-A-C-B, whose other weights are 1,6,3, so the cycle property excludes it from an MST. Therefore the required condition is x<6, and the largest integer is 5. Equality cannot be included just because a tie allows an MST containing BC.

Original paper, page 64 · Answer key

1 mark · MSQ
Report issue

Graph Algorithms

Question 29, GATE 2025: For BFS and DFS on an undirected simple graph G, choose all true statements.
A. A DFS tree is always a shortest-path tree.
B. Every DFS non-tree edge connects a vertex and an ancestor/descendant, described as a forward/back edge.
C. Endpoints of a BFS non-tree edge have source distances differing by at most one.
D. Either repeated BFS or repeated DFS can find all connected components.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C, D

A is false. In a triangle with edges s–u,u–v,s–v, DFS may choose tree edges s–u and u–v. Its tree distance from s to v is then 2 even though the graph contains a length-1 path s–v. DFS prioritizes depth, not distance.

B is true for undirected DFS. If an edge from a currently explored vertex reaches an unvisited vertex, DFS follows it before finishing that subtree. Consequently an undirected non-tree edge cannot join two completely separate DFS subtrees; its endpoints have an ancestor-descendant relationship. It is usually called a back edge when oriented toward the ancestor. The question's “forward/back” wording refers to that relationship, not to a separate undirected cross-edge type.

C follows from shortest-path distances. For any edge u–v, a shortest route to u followed by that edge shows d(s,v)≤d(s,u)+1. Reversing the roles gives d(s,u)≤d(s,v)+1. BFS tree levels equal these distances, so their difference is at most one.

D is true: traverse from an unvisited vertex, mark its entire component, and repeat while any vertex remains unvisited. This works with either BFS or DFS, including isolated vertices. Select B,C,D.

Original paper, page 23 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 37, GATE 2025: In an undirected graph with positive edge weights, add the same positive constant α to every edge. How are minimum spanning trees (MSTs) and shortest paths (SPs) affected?
A. Every MST and every SP remain optimal.
B. MSTs may change; every SP remains optimal.
C. Every MST remains optimal; SPs may change.
D. Both MSTs and SPs may change.
Consider components where the corresponding spanning tree/path exists.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

For a graph with n vertices, every spanning tree uses exactly n−1 edges. Its new weight is w'(T)=w(T)+(n−1)α. Any two trees receive exactly the same increment, so their weight difference and ordering are unchanged. In particular, every old MST is still an MST, and ties between MSTs are preserved.

Paths do not all contain the same number of edges. Consider a triangle with u–w of weight 1, w–v of weight 1, and u–v of weight 3. Initially the two-edge route from u to v costs 2, beating the direct edge's 3. Add α=2 to every edge. The two-edge route now costs 3+3=6, whereas the direct edge costs 5, so the old shortest path is no longer shortest.

Thus constant addition preserves MSTs but need not preserve shortest paths: C. The distinction is a fixed edge count across all spanning trees versus a potentially variable edge count across paths. Multiplying every edge by a positive constant would preserve both, but that is a different operation.

Original paper, page 28 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 59, GATE 2025: An algorithm runs BFS from any vertex v, picks a farthest vertex u, runs BFS from u, then reports its maximum distance d(u,z). Apply it to the unweighted tree described by these edges (labels added solely to reproduce the source figure): c–l,c–r,c–a,c–b; l–d,l–e,l–g; e–f; g–h,g–i; r–j,r–k; k–m. There are no other edges. Find the reported distance in edges.
Figure orientation: c is the central branching vertex, l its upper-left junction, r its upper-right junction; a,b are the two lower leaves. The left arms end at d,f,h,i and the right arms at j,m.
Tap to enlarge
Answer & solution

Official answer: 6

Step through it (12 steps)

The two-BFS method returns a diameter of an unweighted tree: a farthest vertex from any starting point is a diameter endpoint, and a BFS from that endpoint measures the full diameter. This exact guarantee uses the unique paths of a tree; it should not be assumed for every arbitrary graph.

Compute the diameter directly from the supplied edge list. From central vertex c, the farthest left-side leaves f,h,i are each three edges away: c–l–e–f or c–l–g–h/i. The farthest right-side leaf m is also three edges away: c–r–k–m. Joining a deepest left leaf to m therefore produces a path of length 3+3=6, for example f–e–l–c–r–k–m.

No longer path exists. A path crossing c uses at most two of its branch depths, whose two largest are 3 and 3. A path contained entirely in the left branch has at most 4 edges between f and h/i; a path within the right branch has at most 3 between j and m. The short lower branches of c cannot improve the maximum. Thus the diameter and algorithm's output are 6. The complete 14-vertex, 13-edge tree was reconstructed from the source figure.

Original paper, page 44 · Answer key

2 marks · MSQ
Report issue

Use BFS distances to rule out DFS forward edges

Graph Algorithms

Question 45, GATE 2024: T is a DFS spanning tree of a directed graph G, rooted at v, and is also a BFS tree of G rooted at v. Which claims must hold? A G has no back edges relative to T. B G has no cross edges. C G has no forward edges. D Every edge of G belongs to T.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: C

Approach: A BFS tree preserves shortest-path distances from its root. Compare this constraint with the endpoint depths of each DFS edge type, using the usual simple directed-graph classification.
1. A non-tree forward edge u→w goes from a vertex to a proper descendant that is not its child. Thus depth_T(w)≥depth_T(u)+2.
2. But the edge supplies a root-to-w path of length depth_T(u)+1, shorter than its tree depth. This contradicts T being a BFS tree, so forward edges cannot exist. C is true.
3. Back edges are possible. In the directed cycle v→a→b→v, both DFS and BFS from v have the path v→a→b as their tree, and b→v is a back edge. Thus A and D are false.
4. Cross edges are also possible. Use edges v→a, v→b, b→a, with a explored before b in DFS. Both trees have a and b as root children, while b→a is a cross edge to the already completed branch.
Answer: C only.

Original paper, page 28 · Answer key

2 marks · NAT
Report issue

Recover components from DFS-forest edges

Graph Algorithms

Question 60, GATE 2024: An undirected graph has 100 vertices. A complete DFS traversal produces a spanning forest containing 40 edges. How many connected components does the graph have?
Tap to enlarge
Answer & solution

Official answer: 60

Approach: DFS starts a new tree exactly when it moves to a previously unvisited connected component. Each resulting tree spans that component.
1. Suppose there are c components and their vertex counts are v1,…,vc. The counts sum to 100.
2. A tree with vi vertices has exactly vi−1 edges. This includes an isolated vertex: vi=1 gives zero edges.
3. Summing over the DFS forest gives 40=Σ(vi−1)=Σvi−c=100−c.
4. Solving yields c=100−40=60.
Answer: 60 connected components. The original graph may have additional nontree edges and cycles; the given 40 counts only forest edges. One can also view each of those 40 tree edges as discovering one nonroot vertex, leaving 60 roots, one for each component.

Original paper, page 35 · Answer key

2 marks · NAT
Report issue

Count MSTs after contracting forced light edges

Graph Algorithms

Question 59, GATE 2024: Undirected graph vertices a,b,c,d,e,f,g have weight-1 edges ab,af,cd,de; weight-2 edges ag,bg,fg,cg,dg,eg; weight-3 edges bc,fe; and no others. How many distinct minimum-weight spanning trees exist?
Tap to enlarge
Answer & solution

Official answer: 9

Step through it (11 steps)

Approach: Force uniquely lightest cut edges, contract them, then count independent minimum connections.

1. Derivation. Each weight 1 edge (ab, af, cd, de) is uniquely lightest across the cut isolating its noncentral endpoint b, f, c, e, respectively, so all four occur in every MST. Contracting them gives components L={a, b, f}, R={c, d, e}, and{g}. There are three weight 2 choices linking L to g and three linking R to g. A minimum tree uses one of each, giving 3×3=9 distinct MSTs, each weight 4+2+2=8. Weight3 links L–R cannot improve that value.

2. Explanation and checks. After contraction only three supervertices remain, requiring exactly two additional edges. Connecting g to the left component has three distinct original choices; connecting g to the right has another three. Each pair is acyclic and connects all seven original vertices once the four forced edges are restored. Choosing a weight-3 left–right link instead would require at least one further weight-2 edge and yield total at least nine rather than eight. Therefore it cannot contribute an alternative minimum tree.

Original paper, page 38 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 56, GATE 2023: The vertices of a graph are all eight subsets of {1,2,3}. Distinct vertices are adjacent exactly when one set is a subset of the other. How many different BFS vertex-visit orders can start at ∅, allowing arbitrary neighbor ordering?
Tap to enlarge
Answer & solution

Official answer: 5040

The empty set is a proper subset of every nonempty subset, so it is adjacent to each of the other seven vertices. BFS discovers all seven while scanning the start vertex. Any permutation of those neighbors can be its adjacency order; all seven are already discovered before any is expanded. Thus precisely 7!=5040 visit orders are possible.

Teaching detail. BFS uses a queue and discovers every neighbor of the start before expanding any of those neighbors. Since ∅ is contained in every subset, all other vertices are at distance one. No deeper layer imposes additional order constraints. Choosing any desired permutation of the seven neighbors as the start's adjacency-list order produces exactly that traversal after the fixed initial ∅. Thus the number is 7!, not 8!; the start position is not free. Later edges cannot create new vertices because all seven are already discovered.

Original paper, page 59 · Answer key

2 marks · MSQ
Report issue

Graph Algorithms

Question 49, GATE 2022: A simple undirected weighted graph has distinct edge weights. Select true MST claims: A second-lightest edge is always included; B at least one of third/fourth lightest is included; C unique lightest edge across every nontrivial cut is included; D multiple MSTs can exist. Assume existence of an MST and the referenced edges.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A,B,C

Assume an MST exists and the graph has the referenced four edges. Kruskal always accepts the second-lightest edge: with only one accepted edge a simple graph cannot form a cycle. If the third is rejected, the first three edges form a triangle. The fourth cannot be another simple edge inside that already complete triangle, so it is accepted. Thus at least one of the third and fourth edges belongs to the MST. The unique lightest edge crossing any nontrivial cut belongs to every MST by the cut property. Distinct weights imply the MST is unique, making D false. Hence A, B, C. Connectedness and existence of the named edges are implicit in the question’s wording.

The cut property follows by exchange: if an MST omits the unique cheapest crossing edge e, adding e creates a cycle containing another crossing edge f. Since w(f)>w(e), replacing f by e lowers the total, a contradiction. Distinct weights also prove uniqueness by considering the cheapest edge where two proposed MSTs differ.

Original paper, page 32 · Answer key

1 mark · NAT
Report issue

Graph Algorithms

Question 17, GATE 2021: Find the number of minimum-weight spanning trees in this undirected graph. Essential diagram: nine vertices in a 3×3 grid, named a,b,c (top row), d,e,f (middle), g,h,i (bottom). Edges and weights: ab=.1, bc=.1, de=.1, ef=.9, gh=.9, hi=.1, ad=.9, be=.9, cf=.9, dg=.1, eh=.1, fi=.1. There are no other edges.
Tap to enlarge
Answer & solution

Official answer: 3

Step through it (13 steps)

The seven weight 0.1 edges form a forest with two components: the top row of three vertices and the remaining six vertices. All seven cheap edges can be included without a cycle. Connecting these components requires one weight 0.9 edge; exactly the three vertical links between the top and middle rows do so. Each choice gives total weight 1.6, and any tree using fewer cheap edges is more expensive. Therefore exactly 3 MSTs.

A spanning tree on nine vertices needs eight edges. The seven cheap edges are acyclic and leave precisely two components, so Kruskal's algorithm accepts all seven before considering weight .9. Among the expensive edges only ad,be,cf cross that component cut; ef and gh stay within the lower component and create cycles. Thus each MST contains seven cheap edges and one of those three cross edges. This proves both attainability and exhaustiveness rather than merely counting three plausible trees.

Original paper, page 20 · Answer key

2 marks · MSQ
Report issue

Graph Algorithms

Question 41, GATE 2021: For a DFS tree T of a connected undirected graph G, select all true statements. A its root can never be an articulation point; B its root is an articulation point iff it has at least two DFS children; C a DFS leaf can be an articulation point; D if u is an articulation point, x is its ancestor and y its descendant in T, every graph path x→y must pass through u.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B

The DFS root is an articulation point exactly when it has at least two DFS children. Distinct child subtrees have no connecting edge in an undirected DFS, whereas one child subtree remains connected after deleting the root. This proves B and disproves A. A DFS leaf cannot disconnect the remaining DFS tree when removed, so C is false. D is too strong: use edges x-u, u-y, y-x, u-z and DFS order x, u, y, z. Vertex u is an articulation point because its removal isolates z, but the edge x-y avoids u even though x is its ancestor and y its descendant.

For a nonroot vertex u, the actual test is whether some DFS child v has low[v]≥discovery[u], meaning that child's subtree lacks a back-edge to a strict ancestor of u. It does not assert that every descendant is trapped. The counterexample in the main derivation separates a trapped branch z from a branch y that can reach ancestor x by a back-edge. For the root, two distinct child subtrees cannot connect except through the root, or DFS would have explored them as one subtree.

Original paper, page 34 · Answer key

1 mark · MCQ
Report issue

Graph Algorithms

Question 1, GATE 2021: For connected undirected weighted G: S1 some minimum-weight edge belongs to every MST; S2 distinct edge weights imply a unique MST. A both true; B only S1; C only S2; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

An equal-weight triangle has three MSTs, each omitting a different minimum-weight edge, so no edge belongs to every MST. This refutes S1. Distinct edge weights ensure a unique MST by the cut/exchange property, so S2 is true. Thus C.

For S1 the quantifier “every MST” is crucial: a minimum edge is safe for some MST, but ties can let another edge replace it. For S2 suppose two MSTs differ and choose the lightest edge in their symmetric difference. Adding it to the other tree creates a cycle containing a heavier edge from that difference; exchanging them lowers the weight, a contradiction.

Original paper, page 55 · Answer key

2 marks · MSQ
Report issue

Graph Algorithms

Question 46, GATE 2021: Directed graph is the 4×4 vertex grid in the embedded figure; S is bottom-left. A no topological order; B DFS from S classifies exactly three directed edges as back edges; C no strongly connected component; D every ordered pair is mutually reachable.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A; B

Label grid positions (row, column), top to bottom and left to right. There are three nontrivial SCCs, each a directed four-cycle: {(3, 1),(4, 1),(4, 2),(3, 2)}, {(3, 3),(4, 3),(4, 4),(3, 4)}, and {(1, 2),(2, 2),(2, 3),(1, 3)}. Other vertices are singleton SCCs. Each directed cycle contributes exactly one DFS back edge; edges between SCCs cannot be back edges, since a back edge would make its endpoints mutually reachable. All vertices are reachable from S, so DFS finds exactly three back edges, B. The cycles preclude a topological order, A. C is false, and D is false because the top-right vertex is a sink and cannot reach S.

In each four-cycle, DFS follows the sole internal successor until the closing edge points to a gray ancestor, giving one back edge. Exploring outgoing edges to other components cannot create another such ancestor return, because that would imply a return path and merge the components. A graph always has SCCs, even if all were singletons; C is therefore also terminologically wrong.

Original paper, page 83 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 55, GATE 2021: DAG vertices s,a,b,c,d,e,f,g,t. Edges with weights: s→a 9,s→c 1,a→b 1,a→d 1,c→d 1,c→f 9,d→e 9,d→g 9,b→e 1,f→g 1,g→t 1,e→t 9. Path score is edge-weight product; vertex score is largest path score from s; score(s)=1. Find sum of all vertex scores.
Tap to enlarge
Answer & solution

Official answer: 929

Step through it (10 steps)

Process vertices topologically with Q(v)=max over predecessors u of Q(u)*weight(u, v), starting Q(s)=1. The diagram yields Q(a)=9, Q(c)=1, Q(b)=9, Q(d)=max(1, 9)=9, Q(f)=9, Q(e)=max(9, 81)=81, Q(g)=max(9, 81)=81, Q(t)=max(81, 729)=729. Sum=1+9+1+9+9+9+81+81+729=929.

A topological dynamic program initializes score(s)=1 and all other scores=0, then for each edge u→v sets score(v)=max(score(v),score(u)×weight). A predecessor ordering such as s,a,b,c,d,e,f,g,t suffices. It runs in O(V+E), since a DAG has no profitable cycle to revisit. Store parent pointers when maximizing if an actual maximizing path is also wanted.

Original paper, page 88 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 31, GATE 2020: A weighted undirected graph has MST T maintained in adjacency lists. A new weighted edge (u,v) is added. Without an additional preprocessed path-query index, what is the worst-case time to decide whether T remains an MST? A Θ(|E|+|V|); B Θ(|E||V|); C Θ(|E| log |V|); D Θ(|V|).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Worked explanation

Let e be the new edge and P the unique u-v path in T. If w(e)<max weight on P, replace a maximum-weight path edge by e; the resulting spanning tree is strictly lighter, so T fails. Conversely, if w(e)≥that maximum, consider any competing tree using e. Removing e separates it into two parts; P crosses that cut at some tree edge f with w(f)≤w(e). Replacing e by f gives an old-graph spanning tree no heavier than the competitor. Since T was minimum among old trees, no new competitor is lighter. A DFS of T visits at most |V|−1 edges, carrying the path maximum. No need to rerun an MST algorithm on all of G.

Derivation and answer

Adding edge(u, v) creates one cycle with the unique u-v path in T. T remains minimum exactly when the new weight is at least the maximum weight on that tree path. A DFS of the adjacency-list tree finds the path and its maximum in O(|V|), and an unpreprocessed path-like tree can force inspection ofΘ(|V|) edges. ThusΘ(|V|), D.

Original paper, page 10 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 40, GATE 2020: A directed weighted graph has reweighted edges w′(u,v)=w(u,v)+f(u)−f(v), with f:V→R. Shortest paths are preserved: A for every f; B iff f is everywhere positive; C iff f is everywhere negative; D iff f gives shortest distances from a new source joined to all vertices by zero-weight edges.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Worked explanation

For a path v0=s,v1,…,vk=t, summing the added terms gives f(v0)−f(v1)+f(v1)−f(v2)+…+f(vk−1)−f(vk)=f(s)−f(t). Intermediate contributions cancel pairwise. Thus for two s-t paths P,Q, w′(P)−w′(Q)=w(P)−w(Q), preserving strict inequalities and ties. The signs of individual potentials do not matter. Choosing shortest-distance potentials is useful in Johnson’s algorithm to make edge weights nonnegative, but that is an additional goal; it is not necessary for path-order preservation. This argument compares common endpoints, not unrelated paths with different sources or destinations.

Derivation and answer

For any path from s to t, the added potentials telescope: sum[w(u, v)+f(u)−f(v)]=original path weight+f(s)−f(t). All paths with the same endpoints receive the same constant, so their relative order and shortest paths are unchanged for every real-valued f, A. Cycle weights are unchanged too; this does not create finite shortest paths where reachable negative cycles originally prevented them.

Original paper, page 13 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 49, GATE 2020: The complete undirected graph has vertices v1,…,v100 and edge (vi,vj) for every 1≤i<j≤100, with weight |i−j|. Find its MST weight.
Tap to enlarge
Answer & solution

Official answer: 99

Worked explanation

A spanning tree is connected and acyclic on all 100 vertices, hence has exactly 99 edges. Here i≠j makes every weight |i−j| a positive integer, at least 1. Thus 99 is a universal lower bound. Choose precisely the edges (v1,v2),(v2,v3),…,(v99,v100). They form a path through every vertex, so are a valid spanning tree, and every edge has weight 1. Their sum is 99, attaining the bound. Taking all weight-one edges happens to work because they form this path; merely selecting individually cheap edges would not prove connectivity in a general graph.

Derivation and answer

Every edge has positive integer weight at least 1, and every spanning tree on 100 vertices has 99 edges, so its total weight is at least 99. The path v1-v2-...-v100 uses 99 consecutive-index edges of weight 1 and attains that bound. Hence the MST weight is 99.

Original paper, page 15 · Answer key

2 marks · MCQ
Report issue

Cut properties and MST uniqueness

Graph Algorithms

Question 38, GATE 2019: For a connected weighted undirected graph G, test: I distinct weights on all edges imply a unique MST; II every cut having a unique cheapest crossing edge implies a unique MST. Options A I only, B II only, C both, D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Use the cut property: if an edge is the unique lightest edge crossing some cut, every minimum spanning tree must contain it. Otherwise adding it to an MST forms a cycle containing another crossing edge, and replacing that heavier edge makes a cheaper spanning tree.
2. To prove II, choose any MST T and remove one of its edges e. The remaining components define a cut. Edge e must be a cheapest crossing edge: a strictly cheaper one could reconnect the components and improve T.
3. Under II the cheapest crossing edge is unique, so e is that unique edge. By step 1, every MST must contain e. This reasoning applies to each edge of T.
4. Any other MST must therefore contain all |V|−1 edges of T and hence equal T. This proves II.
5. If every edge weight in the graph is distinct, each nonempty cut automatically has a unique cheapest crossing edge. Thus I implies the condition of II and is also true. Answer C. These are sufficient conditions for uniqueness; the question does not require proving that all edge weights must be distinct in every graph with a unique MST.

Original paper, page 12 · Answer key

2 marks · MCQ
Report issue

DFS and BFS edge restrictions

Graph Algorithms

Question 30, GATE 2018: For a simple undirected graph, I says DFS has no cross edge between unrelated vertices; II says every graph edge joins BFS depths differing by exactly 1. Which must hold? A I only; B II only; C both; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Method

If an undirected DFS edge led to a still-unvisited vertex, DFS would explore it before returning, making that vertex a descendant. If the vertex is already on the recursion stack it is an ancestor; an edge to a completed unrelated subtree would have discovered the current vertex earlier. For BFS, shortest-path distances differ by at most 1 across an edge. A triangle rooted at one vertex places the other two at depth 1 and still has an edge between them, disproving “exactly”.

Solution

In undirected DFS, an edge to an undiscovered vertex is explored before the current DFS call completes, so every non-tree edge joins an ancestor and descendant; no cross edges, making I true. BFS edges can connect equal-depth vertices, for example the edge joining the two non-root vertices of a triangle. The necessary inequality is |i−j|≤1, not equality 1. Thus only I, A.

Original paper, page 12 · Answer key

2 marks · NAT
Report issue

Maximizing the number of minimum spanning trees

Graph Algorithms

Question 47, GATE 2018: Name graph vertices top T, left L, bottom-left B, bottom-right C, right R. Edges: TL=4,LB=4,TB=1,BC=4,TC=3,TR=x,CR=5. Choose x to maximize the number of minimum-weight spanning trees; report that number.
Tap to enlarge
Answer & solution

Official answer: 4

Method

The cut property forces a uniquely lightest edge across a cut into every MST. Across {B}, TB=1 is strictly less than LB=4 and BC=4; across {C}, TC=3 is less than BC=4 and CR=5. Contract these two forced edges to a central component. Connecting L has two tied cost-4 choices. Connecting R has two choices only when x=5. These choices are independent and each yields four edges with no cycle, so their product counts actual spanning trees, not merely edge subsets.

Solution

Name the top vertex T, bottom-left B, bottom-right C, left L and right R. Edge TB of weight 1 is uniquely cheapest across the cut {B}; TC of weight 3 is uniquely cheapest across {C}. Both must be in every MST, independently of x. Contract them. L can connect to that component by either of its two weight 4 edges. R can connect by TR of weight x or CR of weight 5. At x=5 both choices tie, giving 2*2=4 MSTs. Otherwise only one right edge is cheapest and there are 2. The remaining BC edge would form a cycle with the two mandatory edges and cannot be chosen. Thus the maximum count is 4.

Original paper, page 20 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 26, GATE 2017: In any connected undirected graph with positive distinct edge weights, are these necessarily true? I the minimum spanning tree is unique; II the shortest path between any two vertices is unique. A I only; B II only; C both; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

1. For uniqueness of the MST, suppose two different minimum trees exist. Select the lightest edge in their symmetric difference and add it to the tree not containing it.

2. The resulting cycle has an edge belonging to that tree but not the other. This edge is heavier because weights are distinct and the chosen edge was lightest in the difference. Exchanging them strictly reduces weight, contradicting minimality. Thus I is true.

3. Distinct edge weights do not make path sums distinct. In a triangle with weights 1,2,3 the direct weight-3 edge ties the 1+2 route. II fails, so A.

Original paper, page 13 · Answer key

1 mark · MCQ
Report issue

Graph Algorithms

Question 15, GATE 2017: An undirected graph has vertices M,N,O,P,Q,R and edges MN,NO,MR,MQ,NQ,OQ,OP,PQ. Which is a possible BFS visiting order, with the first vertex as the start? A:MNOPQR. B:NQMPOR. C:QMNROP. D:POQNMR.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (4 options)

1. BFS removes vertices from a FIFO queue and discovers all vertices at a smaller distance before any at a larger distance. For D, start at P and enqueue its neighbors O then Q. The visited prefix is P,O,Q.

2. Processing O discovers N; its other neighbors P and Q are already discovered. Processing Q then discovers M, with N already queued. Processing N adds nothing, and processing M finally discovers R. The resulting order is P,O,Q,N,M,R.

3. A fails from M because O at distance 2 precedes its distance-1 neighbors Q and R. B fails from N because P at distance 2 precedes O at distance 1. C fails from Q because R at distance 2 precedes O and P at distance 1. Thus D is possible; BFS neighbor ordering need not be alphabetical.

Original paper, page 6 · Answer key

1 mark · NAT
Report issue

Graph Algorithms

Question 11, GATE 2016: Count the topological orders of the directed graph with vertices {a,b,c,d,e,f} and exactly the edges a→b, b→c, c→f, a→d, d→e, e→f. These two directed branches are the complete content of the source diagram.
Tap to enlarge
Answer & solution

Official answer: 6.0 : 6.0

Our answer: 6

Step through it (8 steps)

The graph consists of a→b→c→f and a→d→e→f. Vertex a must come first and f last. Interleave the two ordered middle chains(b, c) and(d, e), choosing their two positions among four: C(4, 2)=6.

After fixing a and f, the middle orders are bcde, bdce, bdec, dbce, dbec, debc. Each respects b before c and d before e. Choosing the two positions occupied by b,c determines the remaining positions for d,e, giving the binomial coefficient without listing all 4! permutations. A topological order need not follow a single path; it is any ordering in which every directed edge points forward.

Original paper, page 6 · Answer key

1 mark · MCQ
Report issue

Graph Algorithms

Question 14, GATE 2016: In a connected weighted undirected graph with distinct positive edge weights, add the same positive amount to every edge weight. Evaluate P: the minimum spanning tree is unchanged; Q: the shortest path between every pair is unchanged. A: P only. B: Q only. C: neither. D: both.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Every spanning tree has |V|−1 edges, so adding a constant c to each edge adds the same c(|V|−1) to every tree’s weight, preserving the unique MST. Paths may have different edge counts: triangle weights 1, 2, 4 initially favor 1+2 over 4, but adding 2 makes the direct 6 shorter than 3+4. Thus only P, A.

The invariant for trees is equal edge count, not merely preserved ordering of individual edge weights. Compare two trees T1,T2: the added terms cancel in their weight difference. By contrast a k-edge path gains kc, and another path with fewer edges gains less. The triangle counterexample demonstrates an actual reversal while retaining positive, distinct edge weights, so Q fails as a universal statement even though some particular graphs keep their shortest paths.

Original paper, page 7 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 38, GATE 2016: A four-vertex weighted undirected graph has weight matrix W with rows (0,2,8,5), (2,0,5,8), (8,5,0,x), (5,8,x,0). Find the largest integer x for which some shortest path between some pair uses the edge of weight x.
Tap to enlarge
Answer & solution

Official answer: 12.0 : 12.0

Our answer: 12

Without edge 3–4, the shortest route between its endpoints is 3→2→1→4 of length 5+2+5=12; the two-edge alternatives both cost 13. Any edge heavier than an alternative endpoint path cannot occur in a shortest path, since replacing it improves the route. At x=12 the edge itself ties the shortest endpoint route, so the largest integer is 12.

It suffices to compare x against the best route between its own endpoints that avoids that edge. Any shortest path using a heavier edge could be improved by substituting that alternative segment. The relevant routes from 3 to 4 have costs 13 via 1,13 via 2,12 via 2 then 1, and 18 via 1 then 2; thus 12 is the threshold. Equality is allowed because the question requires at least one shortest path, not a uniquely shortest path. At x=12, the direct edge is itself a shortest 3-to-4 path.

Original paper, page 15 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 39, GATE 2016: A complete undirected graph on 4 vertices has its 6 edges assigned the distinct weights 1,2,3,4,5,6. Over all assignments, find the largest possible weight of its minimum spanning tree.
Tap to enlarge
Answer & solution

Official answer: 7.0 : 7.0

Our answer: 7

Kruskal always accepts edges 1 and 2. If edge 3 does not make a triangle, it completes the tree and total is 6. If edges 1, 2, 3 form a triangle, edge 3 is rejected and the next edge 4 must join the fourth vertex, giving total 7. Assigning 1, 2, 3 to one triangle and 4, 5, 6 to the remaining incident edges attains 7.

Kruskal processes weights in increasing order and cannot reject either of the first two edges of a simple graph: a cycle requires at least three. If the first three edges do not form a triangle, they connect all four vertices and total 6. Otherwise they occupy three vertices, and every remaining edge joins that triangle to the fourth vertex, so edge 4 is necessarily accepted. This proves the upper bound 7 and the triangle arrangement witnesses attainability over all 720 weight assignments.

Original paper, page 15 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 40, GATE 2016: In a simple undirected graph with distinct edge weights and an MST, consider edge e. I: If e is the lightest edge of some cycle, every MST includes e. II: If e is the heaviest edge of some cycle, every MST excludes e. Which statements are true? A:I only. B:II only. C:both. D:neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

The unique heaviest edge of a cycle is excluded by the cycle property, so II holds. I fails: take edges ab=3, bc=4, ca=5, ad=1, db=2. Edge ab is the lightest in triangle abc but the heaviest in triangle adb, hence excluded from the MST. Thus II only, B, assuming the connected graph/MST component context.

The valid cycle property concerns the heaviest edge: if a tree used it, deleting it creates a cut, and the rest of that cycle contains a lighter edge crossing the cut. Swapping would lower tree weight, contradicting minimality. The lightest edge of a cycle has no symmetric guarantee, because it might be a heavy choice relative to another route outside that cycle. Do not confuse it with the cut property, which guarantees inclusion of the unique lightest edge crossing a cut.

Original paper, page 16 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 41, GATE 2016: An undirected simple graph with n vertices and m edges uses adjacency lists. Entries [v] in u’s list and [u] in v’s list are twins. With unrestricted auxiliary memory, find the best time to set all twin pointers. A Θ(n²); B Θ(n+m); C Θ(m²); D Θ(n⁴).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Number vertices 0..n−1. Scan the adjacency lists and record each entry’s pointer together with the ordered endpoint pair(min(u, v), max(u, v)). Two stable counting-sort passes on the endpoint labels sort all 2m records in O(n+m) time. Consecutive equal pairs are twins, so link their pointers in O(m). Scanning the n list heads and writing the 2m pointers gives the matching linear input/output bound in the adjacency-list model. Thus Θ(n+m), B; the algorithm uses allowed auxiliary memory without initializing an n² matrix.

Give each adjacency entry a record (min endpoint,max endpoint,address). Integer vertex labels permit stable counting sorts instead of comparison sorting’s extra logarithm. Simplicity ensures exactly two records per undirected edge, so matching neighbors after sorting is unambiguous. A dense n-by-n matrix is unnecessary and initializing it would lose the linear bound for sparse graphs.

Original paper, page 37 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 51, GATE 2015: G is a simple undirected graph, s a source, d(x) shortest distance from s, and T a BFS tree. For an edge (u,v) not in T, which cannot equal d(u)−d(v)? A −1; B 0; C 1; D 2.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

An edge gives a one-step extension of a shortest path: d(u)≤d(v)+1 and d(v)≤d(u)+1. Combining gives −1≤d(u)−d(v)≤1. BFS non-tree edges can connect two vertices on the same level or consecutive levels; being non-tree does not force equal levels. Distances must be finite, so interpret u,v as vertices in the explored source component.

Solution: For any edge joining reachable vertices u, v, a shortest path to v followed by that edge proves d(u)≤d(v)+1, and symmetrically d(v)≤d(u)+1. Hence their distances differ by at most 1, so 2 is impossible, D. The restriction to a non-tree edge does not change the bound; same-level and adjacent-level non-tree edges can occur. Distances are understood within the source component, since subtraction of two infinite unreachable distances is not defined.

Original paper, page 19 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 63, GATE 2015: Graph vertices A,B,C,D,E,F; edges AB,AC,BC,BE,CD,DE,DF,EF. All eight weights are distinct integers. Its MST has weight 36 and edges AC=9,BC=2,BE=15,EF=4,DF=6. Find the minimum total of all eight edges.
Tap to enlarge
Answer & solution

Official answer: 69

Adding a non-tree edge creates one cycle. If its weight were below the largest edge on that tree path, replacing that tree edge would give a lighter spanning tree. Distinctness forces a strict inequality. AB's path maximum is 9, CD's is 15, and DE's is 6, so their least integer choices are 10,16,7. These choices do not clash with any existing weight. The cycle conditions are also sufficient to preserve this MST, establishing attainability, not merely a lower bound. Total is 36+33=69.

Solution: For a tree to remain the MST with distinct weights, every non-tree edge must be heavier than the maximum edge on its tree path. AB must exceed max(AC9, CB2)=9, so at least 10. CD must exceed max(CB2, BE15, EF4, FD6)=15, so at least 16. DE must exceed max(DF6, FE4)=6, so at least 7. Values10, 16, 7 are mutually distinct and different from the given tree weights, and satisfy all cycle conditions. Minimum total is 36+10+16+7=69.

Original paper, page 24 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 55, GATE 2015: A connected undirected graph G has 100 vertices and 300 edges. The weight of a minimum spanning tree is 500. If the weight of every edge in G is increased by 5, what is the new minimum spanning-tree weight?
Tap to enlarge
Answer & solution

Official answer: 995

1. The 300 graph edges do not all belong to the spanning tree. Connectedness makes every spanning tree have exactly |V|−1=99 edges, so the increment is 99·5 rather than 300·5. Because all candidate trees receive the same increment, a previously minimum tree remains minimum.

2. Every spanning tree on 100 vertices has 99 edges. Adding5 to every edge increases every spanning-tree weight by exactly 495, preserving their ordering and the set of minimum spanning trees. The new minimum weight is 500+495=995.

Original paper, page 75 · Answer key

1 mark · MCQ
Report issue

Graph Algorithms

Question 11, GATE 2014: A graph has n vertices and m edges and is stored as an adjacency matrix. What is the tightest asymptotic upper bound for a complete depth-first search? A: Θ(n). B: Θ(n+m). C: Θ(n²). D: Θ(m²).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Method and setup. For adjacency lists, examining neighbors costs the degree of the current vertex. With a matrix, locating all neighbors requires inspecting n possible entries even for a low-degree vertex. A full DFS forest covers disconnected components too, so all n rows are scanned.

2. Work through the result and its assumptions. DFS scans an entire adjacency-matrix row for each visited vertex. A full traversal visits all n vertices, giving n rows of n entries and Θ(n²) time, C.

Original paper, page 10 · Answer key

1 mark · MCQ
Report issue

Graph Algorithms

Question 13, GATE 2014: A directed graph has vertices P,Q,R,S and exactly the edges P→Q, P→R, R→Q, S→R, S→Q. Which statement holds? A: No topological ordering exists. B: Both PQRS and SRQP are topological orderings. C: Both PSRQ and SPRQ are topological orderings. D: PSRQ is the only topological ordering.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (2 orders)

1. Method and setup. Topological order must place the tail of every edge before its head. Initially P and S have indegree zero. Both must precede R, and R must precede Q. There is no edge imposing an order between P and S, so either may appear first.

2. Work through the result and its assumptions. The edges require P and S before R, and R before Q; there is also P→Q and S→Q. P and S may occur in either order, giving exactly PSRQ and SPRQ. Thus C.

Original paper, page 10 · Answer key

1 mark · MCQ
Report issue

Graph Algorithms

Question 14, GATE 2014: Run BFS from W in a connected, undirected, unweighted graph and retain its tree edges in T. This tree supports which task? A shortest paths between every vertex pair; B shortest paths from W to every vertex; C shortest paths from W only to leaves of T; D a longest path in the graph.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. Set up the reasoning. BFS assigns a vertex to the earliest reachable distance layer. When a vertex is first discovered, its parent is one layer closer to W, so following parents yields a path of exactly that shortest distance.

2. Derive the result. BFS explores vertices in increasing edge-distance from W. Parent pointers in the BFS tree therefore reconstruct a shortest path from W to every vertex. They need not preserve shortest paths between arbitrary other pairs. Thus B.

3. Check the interpretation. The guarantee is rooted at W and covers internal vertices as well as leaves. A non-tree edge may shorten the route between two other vertices, so one BFS tree is not an all-pairs shortest-path representation.

Original paper, page 32 · Answer key

2 marks · NAT
Report issue

Graph Algorithms

Question 52, GATE 2014: How many distinct MSTs does the illustrated undirected weighted graph have? Textual equivalent: label left cluster vertices A top-left, B top-right, C middle-left, D center, E middle-right, F bottom-left; right triangle G bottom-left, H top, I bottom-right. Edges: AB=2, AC=2, AD=1, BD=2, BE=1, CD=2, CF=1, DE=2, DF=1, FG=2, GH=2, HI=1, GI=2. No other edges exist.
Tap to enlarge
Answer & solution

Official answer: 6

Step through it (15 steps)

1. Set up the reasoning. Run Kruskal by weight groups. Weight-one edges AD,BE,CF,DF,HI form a forest, so include them and contract each connected component before counting choices among weight-two edges.

2. Derive the result. The weight 1 edges form an acyclic forest and all are included before any weight 2 edge by Kruskal. In the left component, contraction leaves three parallel weight 2 choices joining its two weight 1 components. The long horizontal bridge of weight 2 is mandatory. In the right triangle, the weight 1 edge is mandatory and either of the two weight 2 edges connects the third vertex. Thus independent choices give 3·2=6 MSTs. The remaining left weight 2 edges internal to contracted components cannot be used.

3. Check the interpretation. After contraction, the left cluster has three parallel choices AB,BD,DE between its two components. FG is the only connection to the right cluster and is compulsory. On the right choose GH or GI. The independent choices yield 3×1×2 trees, each with eight edges and total weight 11.

Original paper, page 43 · Answer key

1 mark · NAT
Report issue

Graph Algorithms

Question 13, GATE 2014: The undirected graph consists of two 3×3 square grids, each with horizontal and vertical edges only between adjacent grid vertices. Label their vertices L(r,c) and R(r,c), rows and columns 1…3 from top-left. Add connector v with edges L(3,3)−v−R(3,1), a pendant vertex u attached only to v, and pendant w attached only to R(2,3). Recursive DFS starts at an unspecified vertex and calls visit only after confirming the neighbor is unvisited. Find the maximum possible call-stack depth, including the initial call.
Tap to enlarge
Answer & solution

Official answer: 19

Step 1: Active DFS calls form a simple path because an already visited vertex is never recursively called. Conversely, a desired simple path can be followed first by a suitable starting vertex and neighbor order. Step 2: A path spanning both grids must pass through connector v using its two grid edges; it cannot also visit pendant u without revisiting v. It uses at most nine left-grid vertices. Step 3: On the right, color the 3×3 grid like a checkerboard. A Hamilton path through all nine vertices starting at R(3,1), in the five-vertex color class, must end in that same color class. The pendant w attaches at R(2,3), in the opposite class, so a path entering at R(3,1) cannot include all nine grid vertices and then w. The right side contributes at most nine vertices including w. Step 4: Thus a spanning path has at most 9+1+9=19 vertices. Paths not spanning both grids are shorter, even with pendant leaves. Step 5: Attain 19 by snaking through all left vertices from L(1,1) to L(3,3), crossing v, then snaking through all right vertices from R(3,1) to R(1,3). DFS can follow this path before exploring unused pendants. The maximum depth is 19.

Original paper, page 56 · Answer key

1 mark · MCQ
Report issue

Bellman-Ford on a complete graph

Graph Algorithms

Question 19, GATE 2013: What is standard Bellman–Ford complexity on a complete n-vertex graph? AΘ(n²), BΘ(n²log n), CΘ(n³), DΘ(n³log n).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

The standard Bellman-Ford algorithm performs up to n−1 full edge-relaxation passes. A complete graph has Θ(n²) edges, yielding Θ(n³) time, C, for the usual worst-case/full-pass algorithm.

One relaxation pass examines all edges. The standard algorithm allows n−1 passes because a simple shortest path can use up to n−1 edges. A complete directed graph has n(n−1) edges; an undirected one has n(n−1)/2, both Θ(n²). Multiplying by Θ(n) passes gives Θ(n³). Early termination may improve a particular instance, but does not change the stated standard worst-case/full-pass bound.

Original paper, page 5 · Answer key

2 marks · MCQ
Report issue

Squaring edge weights and minimum spanning trees

Graph Algorithms

Question 29, GATE 2012: Let G be a weighted graph with edge weights greater than one and G′ be the graph constructed by
        squaring the weights of edges in G. Let T and T′ be the minimum spanning trees of G and G′,
        respectively, with total weights t and t′. Which of the following statements is TRUE?
        (A) T′ = T with total weight t′ = t²
        (B) T′ = T with total weight t′ < t²
        (C) T′ ≠ T but total weight t′ = t²
        (D) None of the above
Tap to enlarge
Your answer
Answer & solution

Official answer: Marks to All

Our answer: B under additional uniqueness and size assumptions

Squaring positive weights preserves every pairwise edge order and equality. Consequently it preserves the set of minimum spanning trees: any chosen MST of G remains an MST of G′, although independently chosen T and T′ can differ if the MST is not unique. For a fixed spanning tree with edge weights wi, its new weight is Σwi², while t²=Σwi²+2Σ_{i<j}w_iw_j. With at least two edges this gives t′<t²; with one edge there is equality. Thus B describes a consistent common choice of tree on at least three vertices, but the unqualified statement T′=T need not hold when multiple MSTs exist, and small graphs also matter. The official entry is Marks to All. These are independent qualifications, not a claimed official cancellation rationale.

Check: For an explicit nonuniqueness example, give every edge of a triangle weight 2. Any two edges form an MST, before and after squaring. T and T′ can therefore be chosen differently even though the collection of optimal trees is unchanged.

Original paper, page 6 · Answer key

2 marks · MCQ
Report issue

Dijkstra path under strict improvements

Graph Algorithms

Question 40, GATE 2012: Run Dijkstra from S to T on the directed weighted graph below. Update a vertex’s path only when a strictly shorter path is found, never on an equal-distance tie. Which path is reported? The complete directed edge list (source,target,weight) is: (S,A,4), (S,B,3), (B,S,3), (S,D,7), (A,C,1), (B,D,4), (C,D,3), (C,E,1), (D,E,1), (D,F,5), (D,T,3), (E,G,2), (G,E,2), (E,T,4), (G,T,3), (T,F,5).
(A) SDT (B) SBDT (C) SACDT (D) SACET.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (27 steps)

After settling S, tentative distances are B=3, A=4 and D=7. Settling B offers D=3+4=7, an equal value that does not replace predecessor S. Settling A discovers C=5. Settling C discovers E=6; its edge to D offers 8 and is ignored. E is settled before D and gives T=6+4=10 with predecessor E (and G=8). D then offers T=7+3=10, which is equal and therefore cannot replace the predecessor. The path retained is S→A→C→E→T of length 10, option D. The graph’s G→E edge and T→F edge directions must not be reversed.

Check: The decisive relaxation is E→T, performed at distance 6 before D is settled at distance 7. Both propose distance 10, but only the first establishes T’s predecessor. Thus equal shortest lengths do not imply that a later equal path replaces the already stored path.

Original paper, page 10 · Answer key

2 marks · MCQ
Report issue

Minimum spanning tree of a banded weighted graph

Graph Algorithms

Question 54, GATE 2011: An undirected graph has vertices v1,...,vn for n>2. Vertices vi and vj are adjacent exactly when 0<abs(i-j)≤2, and that edge has weight i+j. (For n=4 the edges are 12:3,13:4,23:5,24:6,34:7.) What is its minimum spanning-tree weight?

(A) (11n²-5n)/12
(B) n²-n+1
(C) 6n-11
(D) 2n+1
Question 54, GATE 2011: An undirected graph has vertices v1,...,vn for n>2. Vertices vi and vj are adjacent exactly when 0<abs(i-j)≤2, and that edge has weight i+j. (For n=4 the edges are 12:3,13:4,23:5,24:6,34:7.) What is its minimum spanning-tree weight?

(A) (11n²-5n)/12
(B) n²-n+1
(C) 6n-11
(D) 2n+1
Tap to enlarge
Your answer
Answer & solution

Official answer: B

For each vertex vi with i≥3, its lightest incident edge is (vi−2, vi), with weight 2i−2. The edge (v1, v2), of weight 3, is likewise the unique lightest edge incident to v1 and to v2. By the cut property all these edges must belong to an MST. They form two parity chains joined by (v1, v2), so they are connected and contain exactly n−1 edges, hence already form a spanning tree. Its weight is 3+sum from i=3 to n of (2i−2)=3+2[(n−1)n/2−1]=n²−n+1, option B.

For vertex vi with i≥3, possible smaller neighbors are v(i-2) and v(i-1), of weights 2i-2 and 2i-1; larger neighbors have still greater weights. Thus its distance-two backward edge uniquely minimizes the cut separating vi from all other vertices. The cut property forces every such edge, and also v1-v2. The odd-index chain and even-index chain are internally connected and joined by that last edge, showing the forced edges are sufficient, not merely a lower bound on the optimum.

Original paper, page 15 · Answer key

2 marks · MCQ
Report issue

Weighted path within the prescribed minimum spanning tree

Graph Algorithms

Question 55, GATE 2011: An undirected graph has vertices v1,...,vn, edges precisely when 0<abs(i-j)≤2, and edge weight i+j. For n=10, what is the weighted length of the path from v5 to v6 in the minimum spanning tree of this graph?

(A) 11
(B) 25
(C) 31
(D) 41
Question 55, GATE 2011: An undirected graph has vertices v1,...,vn, edges precisely when 0<abs(i-j)≤2, and edge weight i+j. For n=10, what is the weighted length of the path from v5 to v6 in the minimum spanning tree of this graph?

(A) 11
(B) 25
(C) 31
(D) 41
Question 55, GATE 2011: An undirected graph has vertices v1,...,vn, edges precisely when 0<abs(i-j)≤2, and edge weight i+j. For n=10, what is the weighted length of the path from v5 to v6 in the minimum spanning tree of this graph?

(A) 11
(B) 25
(C) 31
(D) 41
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (19 steps)

The MST consists of (v1, v2) and (vi−2, vi) for i=3,..., 10. Its unique path from v5 to v6 is v5→v3→v1→v2→v4→v6. The edge weights are 8, 4, 3, 6 and 10, respectively, so its weighted length is 8+4+3+6+10=31, option C. The direct graph edge (v5, v6) has weight 11 but is not in the MST.

Write the two chains explicitly for n=10: odd vertices 1-3-5-7-9 and even vertices 2-4-6-8-10, joined by 1-2. Any route inside this tree from an odd to an even vertex must pass through that bridge. The requested route therefore goes downward in the odd chain to 1, crosses to 2, and climbs the even chain to 6. 'Length' here is total edge weight, not the number of edges; the path uses five edges but has weight 31.

Original paper, page 15 · Answer key

2 marks · MCQ
Report issue

Minimum spanning tree with a prescribed leaf

Graph Algorithms

Question 50, GATE 2010: A complete undirected graph has vertices 0,1,2,3,4 and symmetric weight matrix W, with rows/columns in that order: [[0,1,8,1,4],[1,0,12,4,9],[8,12,0,7,3],[1,4,7,0,2],[4,9,3,2,0]]. What is the minimum weight of a spanning tree in which vertex 0 is a leaf?
Options: (A) 7; (B) 8; (C) 9; (D) 10.
Question 50, GATE 2010: A complete undirected graph has vertices 0,1,2,3,4 and symmetric weight matrix W, with rows/columns in that order: [[0,1,8,1,4],[1,0,12,4,9],[8,12,0,7,3],[1,4,7,0,2],[4,9,3,2,0]]. What is the minimum weight of a spanning tree in which vertex 0 is a leaf?
Options: (A) 7; (B) 8; (C) 9; (D) 10.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (6 steps)

If vertex 0 is a leaf, removing it leaves a spanning tree on {1,2,3,4}; conversely any such tree plus one edge incident on 0 is feasible. Kruskal on the induced four-vertex graph chooses (3,4) of weight 2, (2,4) of weight 3, and (1,3) of weight 4, totaling 9. The cheapest edge from 0 has weight 1, either (0,1) or (0,3), hence the constrained minimum is 9+1=10, D. The unconstrained MST need not satisfy the leaf requirement.

The leaf constraint permits exactly one edge incident to 0. Therefore 0 cannot serve as a cheap connector joining vertices 1 and 3 through two weight-one edges. On vertices 1,2,3,4, the sorted edges begin 3–4:2, 2–4:3, 1–3:4; these three create no cycle and already connect all four vertices, proving the induced-tree minimum of 9.

Original paper, page 12

2 marks · MCQ
Report issue

Shortest path using at most three edges

Graph Algorithms

Question 51, GATE 2010: A complete undirected graph has vertices 0,1,2,3,4 and symmetric weight matrix W, with rows/columns in that order: [[0,1,8,1,4],[1,0,12,4,9],[8,12,0,7,3],[1,4,7,0,2],[4,9,3,2,0]]. What is the minimum weight of a path from vertex 1 to vertex 2 containing at most three edges?
Options: (A) 7; (B) 8; (C) 9; (D) 10.
Question 51, GATE 2010: A complete undirected graph has vertices 0,1,2,3,4 and symmetric weight matrix W, with rows/columns in that order: [[0,1,8,1,4],[1,0,12,4,9],[8,12,0,7,3],[1,4,7,0,2],[4,9,3,2,0]]. What is the minimum weight of a path from vertex 1 to vertex 2 containing at most three edges?
Options: (A) 7; (B) 8; (C) 9; (D) 10.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (14 steps)

The direct 1→2 path costs 12. With one intermediate vertex, costs via 0,3,4 are 9,11,12. With two distinct intermediate vertices, the six paths have costs: 1→0→3→2=9; 1→0→4→2=8; 1→3→0→2=13; 1→3→4→2=9; 1→4→0→2=21; 1→4→3→2=18. Thus minimum 8 is attained by 1→0→4→2, option B. All edge weights are positive, so adding a repeated-vertex cycle cannot improve a path within the edge limit.

An unconstrained shortest-path calculation is insufficient because it may return a path with four edges. Indeed 1→0→3→4→2 has weight 1+1+2+3=7 but violates the three-edge limit. Enumerating zero, one and two intermediate vertices is exhaustive for simple paths with at most three edges; the weight-eight candidate obeys the constraint.

Original paper, page 12

1 mark · MCQ
Report issue

Bellman-Ford negative-cycle scope

Graph Algorithms

Question 13, GATE 2009: For single-source Bellman–Ford, assess P: always detects a negative cycle if any exists; Q: detects whether a negative cycle is reachable from the source. A P only; B Q only; C both; D neither.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Single-source Bellman-Ford detects a negative cycle reachable from that source by checking for a further relaxable edge after n−1 passes. A negative cycle in an unreachable component remains at infinite distance and need not be found. Therefore Q is true but P is not, giving B.

Bellman–Ford initializes only the source distance as finite. Vertices unreachable from it remain at infinity, so a disconnected negative cycle does not cause a finite relaxation. After |V|−1 passes, an additional successful relaxation among reachable vertices detects a reachable negative cycle. Therefore Q is true and P, which omits reachability, is false. Connecting a new super-source to every vertex would change the setup and allow global detection.

Original paper, page 3

2 marks · MCQ
Report issue

Invalid Kruskal edge order

Graph Algorithms

Question 38, GATE 2009: Weighted undirected graph edges ab 5, ac 3, bc 4, be 2, bd 6, cd 5, de 6, df 6, cf 6, ef 3, eg 5, fg 4. Which accepted-edge sequence cannot be Kruskal? A be, ef, ac, bc, fg, cd; B be, ef, ac, fg, bc, cd; C be, ac, ef, bc, fg, cd; D be, ef, bc, ac, fg, cd.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (4 options)

Kruskal considers accepted edges in nondecreasing weight. The relevant weights are be 2, ef 3, ac 3, bc 4, fg 4, cd 5. Equal-weight 3 or 4 edges may be interchanged in the valid choices. D instead accepts bc of weight 4 before ac of weight 3, even though ac can be safely accepted at that point. Thus D is not a possible Kruskal acceptance order.

Kruskal sorts by edge weight and accepts an edge whenever it connects two different components. The unique weight 2 edge be comes first. Both weight 3 edges ef and ac can be accepted, in either order, before any weight 4 edge. The weight 4 pair bc, fg can also be exchanged. D violates this by putting bc before ac. At that point ac is not cyclic, so it cannot have been skipped. The other orders respect both sorted weights and cycle avoidance.

Original paper, page 9

1 mark · MCQ
Report issue

Counting graph connected components

Graph Algorithms

Question 7, GATE 2008: What is the most efficient time complexity to count connected components in an undirected graph with n vertices and m edges?

(A) Θ(n)
(B) Θ(m)
(C) Θ(m+n)
(D) Θ(mn)
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Maintain a visited flag for each vertex. Start a BFS/DFS at every vertex not yet visited and increment a component counter.
2. Each search visits precisely one previously unvisited component. Across all searches each vertex is processed once and each undirected edge twice.
3. With adjacency lists the total is Θ(n+m), C. The n term includes isolated vertices; an adjacency-matrix representation instead requires Θ(n²).

Original paper, page 2

1 mark · MCQ
Report issue

Valid queue-based BFS order

Graph Algorithms

Question 19, GATE 2008: An undirected graph has vertices M,N,O,P,Q,R and edges MN,NO,OP,PQ,QM,QN,MR. Which is a possible queue-based breadth-first traversal?

(A) MNOPQR
(B) NQMPOR
(C) QMNPRO
(D) QMNPOR
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (4 options)

1. BFS uses a FIFO queue, so all newly discovered neighbors enter behind previously queued vertices. Start at Q and discover M,N,P in that order.
2. Processing M next discovers R. Processing N then discovers O. Processing P adds nothing because O is already discovered.
3. The dequeue/visit order is Q,M,N,P,R,O, C. D puts O before R despite the specified first-layer order; the other offered orders violate BFS layers.

Original paper, page 4

2 marks · MCQ
Report issue

Dijkstra on the given negative-edge graph

Graph Algorithms

Question 45, GATE 2008: Run Dijkstra from a in this directed weighted graph: a→b:1, b→c:2, c→d:3, d→a:2, b→e:−3, c→h:−5, d→g:2, h→e:1, g→h:2, g→e:1, e→f:2, f→g:3. To which vertices does it compute correct shortest-path distances?

(A) Only a
(B) Only a,e,f,g,h
(C) Only a,b,c,d
(D) All vertices
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (22 steps)

1. All vertices receive the correct distance despite the negative edges. From a, settle b at 1, which sets c=3 and e=−2. Settle e at −2, then f at 0, setting g=3. Vertices c and g tie at 3.
2. Whichever is handled first, c sets h=3−5=−2 before h is settled (if g was handled first it only proposes h=5). Vertex h proposes e=−1, not an improvement over −2. Vertex d gets 6 from c and offers no improvement to a or g. Final distances (a,b,c,d,e,f,g,h)=(0,1,3,6,−2,0,3,−2).
3. They also satisfy every edge relaxation inequality, with each value attained by a path, proving optimality. Negative edges invalidate Dijkstra’s general guarantee, but do not force failure on every particular input.

Original paper, page 8

1 mark · MCQ
Report issue

Invalid topological order

Graph Algorithms

Question 5, GATE 2007: The directed acyclic graph has V={1,2,3,4,5,6} and edges 1→2, 1→3, 2→4, 3→4, 2→5, 4→5, 3→6, 4→6. Which sequence is NOT a topological ordering?
Options:
(A) 1,2,3,4,5,6
(B) 1,3,2,4,5,6
(C) 1,3,2,4,6,5
(D) 3,2,4,1,6,5
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (4 options)

1. Approach. A topological ordering places the tail of every directed edge before its head. It suffices to find one violated edge to reject an ordering.

2. Derivation and interpretation. The figure has 1→2 and 1→3, then 2,3 preceding 4 and 4 preceding 5,6. A topological ordering must place 1 before 2 and 3. D begins 3,2,4,1 and violates both outgoing constraints from 1; A,B,C respect the displayed edges. Thus D.

Original paper, page 2

2 marks · MCQ
Report issue

Single-source shortest paths in an unweighted graph

Graph Algorithms

Question 41, GATE 2007: In an unweighted undirected connected graph, which method most efficiently finds shortest paths from a source S to every other vertex?
Options:
(A) Dijkstra from S
(B) Warshall
(C) DFS from S
(D) BFS from S
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Approach. All edges have equal cost, so distance is the number of edges. A FIFO queue processes every distance-d vertex before any distance-(d+1) vertex.

2. Derivation and interpretation. BFS starts at S and discovers vertices in increasing number of edges from S. When a vertex is first discovered, its level is its shortest-path distance; parent pointers recover paths. With adjacency lists, every vertex and edge is processed O(1) times, giving O(V+E). DFS does not generally find shortest paths; generic Dijkstra adds priority-queue overhead, and Warshall solves a more expensive all-pairs/reachability problem. Thus D is the appropriate algorithm.

Original paper, page 10

2 marks · MCQ
Report issue

A globally minimum-weight edge and minimum spanning trees

Graph Algorithms

Question 49, GATE 2007: In a connected undirected graph, w is the minimum edge weight and e is a particular edge of weight w. Which statement is false?
Options:
(A) Some minimum spanning tree contains e
(B) If an MST T omits e, every edge of the cycle in T+e has the same weight
(C) Every MST contains an edge of weight w
(D) Every MST contains e
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Approach. Use the exchange argument: inserting a non-tree edge creates one cycle, and removing a cycle edge gives another spanning tree. Equal minimum-weight edges need not be individually mandatory.

2. Derivation and interpretation. Take any MST T. If e is absent, adding it creates a cycle. Every edge of that cycle has weight at least w by global minimality of w, and none can have weight greater than w: exchanging such an edge for e would make a strictly cheaper spanning tree. Thus all cycle edges have weight w, proving B; exchanging any one of them for e produces an MST containing e, proving A. Every MST contains an edge of weight w by the same argument (or contains e itself), proving C. However a triangle with all edges weight w has MSTs omitting any chosen edge, so e need not belong to every MST. D is false, assuming the usual loop-free graph convention.

Original paper, page 12

1 mark · MCQ
Report issue

Minimum spanning tree on indexed vertices

Graph Algorithms

Question 11, GATE 2006: A weighted complete graph has vertices v₁,…,vₙ and edge weight w(vᵢ,vⱼ)=2|i−j|. What is the minimum spanning-tree weight?
Options:
(A) n−1
(B) 2n−2
(C) C(n,2)
(D) n²
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Every edge has weight 2|i−j|≥2, so every spanning tree costs at least 2(n−1). The path v1,v2,...,vn uses n−1 edges of weight 2 and attains the bound.

2. Hence2n−2, B.

3. Teaching check: This is a lower-bound-plus-construction proof. Every spanning tree has exactly n−1 edges and every available edge costs at least two. Connecting consecutive indices achieves that lower bound; no search over all spanning trees is necessary.

Original paper, page 2

1 mark · MCQ
Report issue

Unweighted shortest paths

Graph Algorithms

Question 12, GATE 2006: Which data structure lets the shortest-path method on an unweighted graph run in linear time, as the unweighted specialization of Dijkstra’s algorithm?
Options:
(A) Queue
(B) Stack
(C) Heap
(D) B-tree
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. All edges have equal unit cost, so increasing distance order is breadth-first order. A FIFO queue processes each vertex and scans each adjacency list once, in O(V+E) time.

2. Thus A; a priority heap is unnecessary.

3. Teaching check: In an unweighted graph, every discovered neighbor is exactly one hop farther than its predecessor. A FIFO queue keeps frontier layers in nondecreasing distance order, which is the priority rule Dijkstra needs here. With adjacency lists the total scan cost is the sum of degrees, O(V+E).

Original paper, page 2

2 marks · MCQ
Report issue

Kruskal's edge order

Graph Algorithms

Question 47, GATE 2006: An undirected graph has vertices a,b,c,d,e,f and weighted edges ab:1, ac:6, bc:3, bd:4, bf:2, cd:2, ce:4, cf:7, de:3, df:1, ef:5. Which sequence cannot be the edges added, in order, by Kruskal’s minimum-spanning-tree algorithm? GFG A prints af, an edge absent from the graph; GateForum A instead prints df. Both readings are retained.
Options:
(A) ab, af, bf, dc, de [GFG]; ab, df, bf, dc, de [GateForum]
(B) ab, df, dc, bf, de
(C) df, ab, dc, bf, de
(D) df, ab, bf, de, dc
Question 47, GATE 2006: An undirected graph has vertices a,b,c,d,e,f and weighted edges ab:1, ac:6, bc:3, bd:4, bf:2, cd:2, ce:4, cf:7, de:3, df:1, ef:5. Which sequence cannot be the edges added, in order, by Kruskal’s minimum-spanning-tree algorithm? GFG A prints af, an edge absent from the graph; GateForum A instead prints df. Both readings are retained.
Options:
(A) ab, af, bf, dc, de [GFG]; ab, df, bf, dc, de [GateForum]
(B) ab, df, dc, bf, de
(C) df, ab, dc, bf, de
(D) df, ab, bf, de, dc
Tap to enlarge
Your answer
Answer & solution

Answer: A, D (no official key was published)

1. The graph has ab=df=1, bf=dc=2, and de=bc=3. Kruskal first adds ab and df in either order, then bf and dc in either order, then de.

2. B and C are legal. D adds de of weight 3 before dc of weight 2, although dc still joins distinct components, so D is impossible.

3. However, the paper prints (a-f) as the second edge of A; no such edge exists, making A impossible too. Replacing that entry with (d-f) would make A legal and yield the intended unique answer D.

4. This is a the paper-specific ambiguity, not an established defect in the original exam.

6. Teaching check: Kruskal processes available edges in nondecreasing weight and only skips an edge when its endpoints are already connected. Tie order can vary for equal weights, but it cannot justify selecting weight 3 while a useful weight-2 edge remains. The absent af edge is a separate transcription defect.

Original paper, page 6

2 marks · MCQ
Report issue

Cycles through an undirected DFS leaf

Graph Algorithms

Question 48, GATE 2006: T is a depth-first-search tree of an undirected graph G. Distinct u and v are leaves of T and both have graph degree at least 2. Which statement must hold?
Options:
(A) Some vertex w is adjacent to both u and v
(B) Some vertex w has removal disconnecting u from v
(C) Some cycle contains both u and v
(D) Some cycle contains u and every neighbor of u
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. In an undirected DFS, every non-tree edge joins a vertex to an ancestor. Since u is a DFS leaf, all its neighbors lie on its single root-to-u tree path.

2. Let w be its highest ancestor-neighbor. The tree path w to u plus the edge uw forms a cycle containing every neighbor of u, since all those neighbors lie between w and u.

3. Degree at least two ensures a genuine cycle. Thus D.

4. Two distinct cycles sharing only a root refute the asserted common cycle; other DFS graphs refute the universal common neighbor and separator assertions.

5. Teaching check: DFS in an undirected graph has no cross edge joining separate finished subtrees. A leaf’s non-parent neighbors must therefore be ancestors. The path from its highest such neighbor to the leaf contains every other neighbor along that ancestor chain, and the closing back edge creates the required cycle.

Original paper, page 7

1 mark · MCQ
Report issue

Computing transitive closure

Graph Algorithms

Question 7, GATE 2005: A known bound for computing transitive closure of an n-element relation is: A O(n); B O(n log n); C O(n^(3/2)); D O(n³).
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Warshall’s algorithm considers each possible intermediate vertex k and updates reachability R[i, j]←R[i, j]∨(R[i, k]∧R[k, j]) for every i, j. Its three nested loops take O(n³), D. Faster algebraic algorithms exist; the question asks a known upper bound, not a tight lower bound. The other listed bounds are subquadratic and cannot explicitly output every entry of an arbitrary n×n closure matrix.

Warshall’s algorithm loops over intermediate vertex k and every ordered pair(i, j), updating reachability using i→k→j. There are n choices in each loop, giving O(n³). This is a known upper bound, not a proof that faster algebraic methods are impossible. An explicitly written arbitrary n×n closure has quadratic output size, so the three listed subquadratic alternatives cannot generally output the full matrix.

Original paper, page 1

2 marks · MCQ
Report issue

Dijkstra with a binary heap

Graph Algorithms

Question 38, GATE 2005: Dijkstra on positive-weight undirected G(V, E), using adjacency lists and a binary heap, has standard bound: A O(V²); B O(E+V log V); C O(V log V); D O((E+V)log V).
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Using adjacency lists, up to V extract-min operations and O(E) decrease-key operations each cost O(log V) with a binary heap. Including initialization gives O((E+V)log V), D. The O(E+V log V) bound requires different amortized heap operations, such as a Fibonacci heap, not a standard binary heap.

Dijkstra extracts each vertex once and may relax each edge. A binary heap takes logarithmic time for extract-min and decrease-key, producing V log V plus E log V. The sum is O((V+E)log V). The tempting E+V log V bound uses an amortized heap with cheaper decrease-key, not the specified ordinary binary heap. Positive weights ensure the settled-distance greedy step is valid.

Original paper, page 5

2 marks · MCQ
Report issue

Unique light edge of a cut

Graph Algorithms

Question 82a, GATE 2005: Undirected connected graph has distinct positive edge weights. Partition X, Y separates s∈X, t∈Y; e is the minimum-weight edge crossing that cut. Printed 82(a): e must belong to which object: minimum spanning tree, weighted shortest s–t path, every s–t path, or weighted longest s–t path? GFG has a blank A and shifted labels; preserve the concept rather than a universal option letter.
Question 82a, GATE 2005: Undirected connected graph has distinct positive edge weights. Partition X, Y separates s∈X, t∈Y; e is the minimum-weight edge crossing that cut. Printed 82(a): e must belong to which object: minimum spanning tree, weighted shortest s–t path, every s–t path, or weighted longest s–t path? GFG has a blank A and shifted labels; preserve the concept rather than a universal option letter.
Tap to enlarge
Your answer
Answer & solution

Answer: B: MST (no official key was published)

Let T be a minimum spanning tree. If its edges omit the unique lightest crossing edge e, insert e into T, making a cycle. That cycle contains another edge f crossing the same cut. Distinct weights give w(e)<w(f); replacing f by e makes a lighter spanning tree, a contradiction. Hence e belongs to the MST. Connectivity is implicitly required for a spanning tree (otherwise use a minimum spanning forest in relevant components). The answer concept is unambiguous, but the source option letters are not interchangeable.

Use an exchange argument. If an MST omitted the lightest crossing edge, adding it creates a cycle. Because its endpoints are on opposite sides, the rest of that cycle must cross the same cut elsewhere. Replace that heavier crossing edge by e; distinct weights make the improvement strict, contradicting minimality. This proves MST membership but not membership in every s–t path: a path can cross on a more expensive edge. Minimum total distance is a different objective.

Original paper, page 11

2 marks · MCQ
Report issue

MST path minimizes the bottleneck

Graph Algorithms

Question 82b, GATE 2005: Undirected connected graph has distinct positive edge weights. Partition X, Y separates s∈X, t∈Y; e is the minimum-weight edge crossing that cut. Printed 82(b): path congestion is its largest edge weight. Which always minimizes congestion from s to t? A path in an MST; B weighted shortest path; C Euler walk; D Hamiltonian path.
Question 82b, GATE 2005: Undirected connected graph has distinct positive edge weights. Partition X, Y separates s∈X, t∈Y; e is the minimum-weight edge crossing that cut. Printed 82(b): path congestion is its largest edge weight. Which always minimizes congestion from s to t? A path in an MST; B weighted shortest path; C Euler walk; D Hamiltonian path.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Take the s–t path in an MST and let f be its heaviest edge. Removing f partitions the tree with s and t on opposite sides. Any other s–t path must cross this cut. A crossing edge lighter than f could replace f to improve the MST, so every crossing edge has weight at least w(f). Every alternative path therefore has bottleneck at least w(f), exactly the MST path’s bottleneck. Option A.

Take the unique s–t path in an MST and remove its heaviest edge f. This creates a cut separating s, t. If any crossing edge were lighter than f, substituting it would create a cheaper spanning tree. Thus every alternative s–t path must use a crossing edge at least as heavy as f. Its maximum edge cannot be lower than the MST path’s maximum, proving minimax optimality even though the MST path need not minimize total distance.

Original paper, page 11

2 marks · MCQ
Report issue

Dijkstra finalization sequence

Graph Algorithms

Question 44, GATE 2004: Run Dijkstra from P on the directed graph with vertices P,Q,R,S,T,U and weighted arcs P→Q:1, P→S:6, P→T:7, Q→R:1, Q→S:4, R→S:2, R→U:1, S→T:3, S→U:2, T→U:2. In what order are shortest-path distances finalized? This arc list completely specifies the source figure.
Options:
(A) P,Q,R,S,T,U
(B) P,Q,R,U,S,T
(C) P,Q,R,U,T,S
(D) P,Q,T,R,U,S
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (18 steps)

1. From P, tentative distances are Q=1,S=6,T=7. Finalizing Q sets R=2 and improves S=5.

2. Finalizing R sets U=3 and improves S=4. U has no outgoing improvement, so S=4 is next; S gives T=min(7,4+3)=7.

3. The order is P,Q,R,U,S,T, with final distances 0,1,2,3,4,7 in that order. In particular the diagonal edge is directed R→S, not S→R.

4. Teaching check: Record the finalized distances in extraction order: P:0,Q:1,R:2,U:3,S:4,T:7. Once R is processed, its outgoing R→S edge lowers S to 4. Reversing that arrow would alter the computation, so direction is part of the essential figure data.

Original paper, page 6

1 mark · MCQ
Report issue

Possible depth-first traversal sequences

Graph Algorithms

Question 21, GATE 2003: An undirected graph has vertices {a,b,e,f,g,h} and edges {ae,ab,af,eb,eg,bf,bh,fh,fg,gh}. Which listed sequences can be depth-first traversals starting at a, allowing arbitrary neighbor order? I. a,b,e,g,h,f; II. a,b,f,e,h,g; III. a,b,f,h,g,e; IV. a,f,g,h,b,e. A. I, II and IV only; B. I and IV only; C. II, III and IV only; D. I, III and IV only.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (4 options)

1. DFS must finish every reachable unexplored neighbor before returning from a vertex. The three accepted sequences can be realized without any backtrack until all six vertices are visited because each consecutive pair is an edge. In II the transition from f to e would require backtracking through b, but f still has unvisited neighbors g and h, so DFS cannot yet leave f. Hence e cannot be next.

2. I, III and IV each trace a simple path through all vertices using displayed edges, so a neighbor order realizes each. In II, f has no edge to e and still has unvisited neighbors g and h; DFS must visit one of these before backtracking. Thus II is impossible and the answer is D.

Original paper, page 3

2 marks · MCQ
Report issue

Detecting subgraph connectivity with zero-weight edges

Graph Algorithms

Question 67, GATE 2003: G=(V,E) is an undirected graph with subgraph G1=(V1,E1). Give an edge weight 0 if it is in E1, and 1 otherwise. Run single-source shortest paths from an arbitrary v1∈V1. Which property can always be inferred from the computed path costs?
A. Number of edges in shortest paths from v1 to every vertex of G
B. G1 is connected
C. V1 forms a clique in G
D. G1 is a tree
Interpret B as determining whether G1 is connected, not asserting connectivity on every input.
Question 67, GATE 2003: G=(V,E) is an undirected graph with subgraph G1=(V1,E1). Give an edge weight 0 if it is in E1, and 1 otherwise. Run single-source shortest paths from an arbitrary v1∈V1. Which property can always be inferred from the computed path costs?
A. Number of edges in shortest paths from v1 to every vertex of G
B. G1 is connected
C. V1 forms a clique in G
D. G1 is a tree
Interpret B as determining whether G1 is connected, not asserting connectivity on every input.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. With only weights 0 and 1, a finite path has total cost 0 precisely when all of its edges belong to E1.
2. Hence d(v1,v)=0 precisely for vertices in the G1 component of v1. Test this for every v∈V1: all distances zero means G1 is connected; any positive/infinite distance means it is not.
3. Cost does not count zero-weight edges. A zero-weight chain and a zero-weight clique on V1 can give identical distance-zero results but different edge counts and cycle structure.
4. Thus the distances determine connectivity (B), but neither clique/tree status nor shortest-path edge counts.

Solution: An edge has weight zero exactly when it belongs to E1. Consequently, for a vertex v in V1, distance(v1,v)=0 exactly when v is connected to v1 using only edges of G1. Checking whether every vertex of V1 has zero distance therefore decides whether G1 is connected, B. Zero distances do not reveal edge counts, clique structure, or acyclicity: a tree and a triangle within the reachable zero-weight component can have identical distances. B must be read as determining connectivity, not asserting that every possible input G1 is connected.

Original paper, page 9

2 marks · MCQ
Report issue

Minimum spanning tree of a weighted graph

Graph Algorithms

Question 68, GATE 2003: Find the weight of a minimum spanning tree of the undirected weighted graph with vertices a,b,c,d,e,f,g,h,i,j and edges:
ac:1; ad:2; cd:2; bg:2; ei:2; bd:3; fh:4; hi:4; ij:5; ab:6; ae:8; eh:8; gh:8; fi:9; ef:11; gi:14; dh:15; gj:19.
These edges describe the complete printed diagram; no other edges are present.
A. 29  B. 31  C. 38  D. 41
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (15 steps)

1. Kruskal processes edges by nondecreasing weight, accepting an edge only when its endpoints lie in different components.
2. Accept ac(1), ad(2); reject cd(2), since a,c,d are already connected. Accept bg(2), ei(2), then bd(3), which joins {b,g} with {a,c,d}.
3. Accept fh(4), hi(4), connecting {f,h} to {e,i}; accept ij(5) to add j. Reject ab(6), whose endpoints are already in the first component.
4. The two remaining components are {a,b,c,d,g} and {e,f,h,i,j}. Any weight-8 edge ae or gh can join them; choose gh. Nine accepted edges connect all ten vertices.
5. Total=1+2+2+2+3+4+4+5+8=31, B. eh(8) is internal to the second component and is not needed.

Solution: Kruskal's algorithm can select ac(1), ad(2), bg(2), ei(2), bd(3), fh(4), hi(4), ij(5), and gh(8). These nine edges connect all ten vertices without a cycle. Their total is 1+2+2+2+3+4+4+5+8=31, option B. The other weight-2 edge cd is skipped because ac and ad already connect c and d; the final weight-8 edge joins the two remaining components. Equal-weight alternatives may produce a different MST with the same total.

Original paper, page 10

2 marks · MCQ
Report issue

Max-plus Floyd recurrence and simple-path bounds

Graph Algorithms

Question 70, GATE 2003: G is a directed n-vertex graph. A path may repeat vertices; a simple path has no repeated vertex. Initialize A[j,k]=1 when edge (j,k) exists, otherwise 0. Run in-place:
for i=1,...,n:
  for j=1,...,n:
    for k=1,...,n:
      A[j,k]=max(A[j,k], A[j,i]+A[i,k])
Which is necessarily true at termination?
A. A[j,k]≤n
B. If A[j,j]≥n−1, G has a Hamiltonian cycle
C. Whenever a path j→k exists, A[j,k] is its longest-path length
D. Whenever a path j→k exists, every simple j→k path has at most A[j,k] edges
Question 70, GATE 2003: G is a directed n-vertex graph. A path may repeat vertices; a simple path has no repeated vertex. Initialize A[j,k]=1 when edge (j,k) exists, otherwise 0. Run in-place:
for i=1,...,n:
  for j=1,...,n:
    for k=1,...,n:
      A[j,k]=max(A[j,k], A[j,i]+A[i,k])
Which is necessarily true at termination?
A. A[j,k]≤n
B. If A[j,j]≥n−1, G has a Hamiltonian cycle
C. Whenever a path j→k exists, A[j,k] is its longest-path length
D. Whenever a path j→k exists, every simple j→k path has at most A[j,k] edges
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Initially every one-edge path has its length represented by A[j,k]≥1. Updates only increase entries, so established lower bounds persist.
2. For a simple path with internal vertices, take its highest-numbered internal vertex i. Splitting there gives two subpaths whose internal vertices have smaller indices. Inductively their lengths are bounded above by A[j,i] and A[i,k] before processing i.
3. The update therefore makes A[j,k] at least the sum of those lengths, the length of the original path. This proves D for every simple path.
4. This is not a correct longest-simple-path algorithm: it never checks that the joined paths are disjoint. Cycles can repeatedly inflate values, and zero is an unsafe unreachable marker for maximizing lengths. Therefore exactness, an n bound and Hamiltonian-cycle inference do not follow.

Solution: The recurrence A[j,k]=max(A[j,k],A[j,i]+A[i,k]) gives option D. Induct on the largest-index internal vertex of a simple path: direct edges start at one; splitting a path at its largest-index internal vertex combines previously established lower bounds for the two pieces. Monotone updates cannot destroy them. Thus the final A[j,k] is at least the length of every simple j-to-k path. It need not equal a longest-path length: cycles and zero values used for nonexistent edges can inflate entries, so the other claims fail. For example, a directed two-vertex cycle with an isolated third vertex can produce values larger than n and large diagonal entries although there is no Hamiltonian cycle. Neither the claimed n bound nor the exact-longest-path interpretation is justified.

Original paper, page 10

Descriptive
Report issue

Initialize all-pairs shortest-path distances

Graph Algorithms

Question 12(a), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Copy and complete the initialization line so that the algorithm is correct.
Question 12(a), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Copy and complete the initialization line so that the algorithm is correct.
Tap to enlarge
Shared context

For a directed unweighted graph, binary adjacency A[i,j] equals 1 for an edge i → j and 0 otherwise. Initialize a distance table P and run the printed loops in the order i outermost, then j, then k. Standard shortest-path distances permit an empty path of length 0.

Answer & solution

Answer: For i≠j: ∞ if A[i,j]=0, otherwise 1; for i=j: 0 (no official key was published)

1. Fix the graph interpretation. The adjacency entries are binary: A[i,j]=1 means a directed edge i→j of length 1, and A[i,j]=0 means that edge is absent.

2. Handle distinct vertices before any intermediate vertices are allowed. If i≠j and A[i,j]=1, the one-edge path has length 1, so initialize P[i,j]=1. If i≠j and A[i,j]=0, no direct path is available yet, so initialize P[i,j]=∞. Zero would incorrectly claim a free path between disconnected vertices.

3. Handle the diagonal separately. Standard shortest-path distance permits the empty path, so P[i,i]=0 whether or not A[i,i] contains a self-loop. A length-1 self-loop cannot beat that empty path.

4. Fill the original two blanks with expressions that incorporate these cases, without changing the printed outer condition:

if A[i,j] = 0 then P[i,j] = (0 if i = j else ∞)
              else P[i,j] = (0 if i = j else 1);

Equivalently, initialize off-diagonal entries to ∞/1 and then explicitly set each P[i,i] to 0.

5. Check a small graph. If the only edge is 1→2, initialization gives rows [0,1] and [∞,0]. This correctly represents the direct path, lack of a reverse path, and both empty paths.

6. State the convention caveat. The abbreviated fills “∞,1” alone make a diagonal entry the length of a shortest nonempty closed walk, or ∞ if none exists, rather than the standard distance 0. That abbreviation suffices only if diagonal results are excluded or that other convention is intended. The conditional fill above is correct for every vertex pair under the standard convention and respects the template’s two blanks.

Original paper, page 8

Descriptive
Report issue

Complete the Floyd–Warshall update for the given loop order

Graph Algorithms

Question 12(b), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Copy and complete the line in the algorithm’s triple loop so that the algorithm is correct.
Question 12(b), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Copy and complete the line in the algorithm’s triple loop so that the algorithm is correct.
Tap to enlarge
Shared context

For a directed unweighted graph, binary adjacency A[i,j] equals 1 for an edge i → j and 0 otherwise. Initialize a distance table P and run the printed loops in the order i outermost, then j, then k. Standard shortest-path distances permit an empty path of length 0.

Answer & solution

Answer: P[j,k] = min(P[j,k], P[j,i] + P[i,k]) (no official key was published)

1. Respect the printed loop order: i is outermost, j is next, and k is innermost. In Floyd–Warshall the outer loop must identify the newly allowed intermediate vertex. Therefore i is that intermediate, j is the source, and k the destination.

2. State the invariant. Before outer iteration i, P[j,k] is the shortest distance from j to k using only vertices 1,…,i−1 as internal vertices. Initialization supplies paths with no internal vertices: 0 on the diagonal, 1 for a direct edge, and ∞ otherwise.

3. Split paths into two exhaustive cases. A shortest path allowed after iteration i either avoids i internally, in which case its best length is the old P[j,k], or passes through i, in which case it can be split into a j→i portion and an i→k portion. Their best combined length is P[j,i]+P[i,k]. Nonnegative unit weights let us choose a shortest path without repeated positive-length cycles.

4. Take the better candidate and fill the line as:

P[j,k] = min(P[j,k], P[j,i] + P[i,k]);

For example, if the current j→k value is ∞ while P[j,i]=2 and P[i,k]=3, the update discovers a path of length 5. If P[j,k] were already 4, the minimum would retain 4.

5. Verify in-place and final correctness. During iteration i, the zero diagonal means an update through i does not lower the distances into or out of i; the in-place recurrence therefore preserves the invariant. After i=n, all vertices are allowed internally, so the table gives every shortest-path distance. Interpret ∞+x as ∞, or guard finite arithmetic against overflow.

6. Avoid a loop-order substitution error. The familiar form P[i,j]=min(P[i,j],P[i,k]+P[k,j]) assumes k is outermost. Under the source’s i,j,k loop order, using that form would generally be incorrect. Exhaustive comparison with BFS on every directed three-vertex graph, including self-loops, verifies the completed initialization and the stated j,k update.

Original paper, page 8

Descriptive
Report issue

Determine the all-pairs algorithm running time

Graph Algorithms

Question 12(c), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Complete the running-time expression O(____).
Question 12(c), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Complete the running-time expression O(____).
Tap to enlarge
Shared context

For a directed unweighted graph, binary adjacency A[i,j] equals 1 for an edge i → j and 0 otherwise. Initialize a distance table P and run the printed loops in the order i outermost, then j, then k. Standard shortest-path distances permit an empty path of length 0.

Answer & solution

Answer: O(n³), more precisely Θ(n³) (no official key was published)

1. Count initialization work. There are n choices for the first index and n for the second, so initializing P visits n² entries. Each requires only a constant-time case selection and assignment, giving Θ(n²).

2. Count the algorithm’s updates. The source has three nested loops, over i, j, and k, each running from 1 to n. There is no data-dependent early termination. Thus the innermost statement executes n×n×n = n³ times.

3. Count work per update. Reading two candidate distances, adding, comparing with the old entry, and writing the minimum take constant time under the usual RAM model with constant-time distance arithmetic. Therefore the update phase costs Θ(n³).

4. Combine the phases. Θ(n²)+Θ(n³)=Θ(n³), since the cubic term dominates as n grows. The requested big-O blank is n³, so the answer is O(n³); the tighter bound is Θ(n³).

5. Keep time and space distinct. The distance table contains n² entries and uses Θ(n²) space. Its space requirement does not reduce the cubic number of updates, and an adjacency-matrix input does not make this particular triple-loop implementation linear in the number of edges.

Original paper, page 8

2 marks · MCQ
Report issue

BFS order and shortest-path distances

Graph Algorithms

Question 2.14, GATE 2001: In BFS from r in an undirected unweighted graph, vertex u is visited before v. Must d(r,u)<d(r,v), >, ≤, or none of these?
Question 2.14, GATE 2001: In BFS from r in an undirected unweighted graph, vertex u is visited before v. Must d(r,u)<d(r,v), >, ≤, or none of these?
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: Breadth-first search’s FIFO queue processes all vertices at distance k before any vertex at distance k+1. Thus dequeue/discovery order is nondecreasing in distance from r.

Step 2: Two distinct vertices in the same layer can occur in either order, so visiting u first does not imply a strict inequality. A star centered at r supplies such an equal-distance example.

Therefore d(r,u)≤d(r,v), C, for vertices reached by this BFS. Unreachable vertices are not visited by the traversal from r.

Original paper, page 5

Descriptive
Report issue

Construct a minimum spanning tree

Graph Algorithms

Question 15(a), GATE 2001: Undirected vertices n1,…,n6; edges (n1,n2,2),(n1,n3,8),(n1,n6,3),(n2,n4,4),(n2,n5,12),(n3,n4,7),(n4,n5,9),(n4,n6,4).
Question 15(a), GATE 2001: Undirected vertices n1,…,n6; edges (n1,n2,2),(n1,n3,8),(n1,n6,3),(n2,n4,4),(n2,n5,12),(n3,n4,7),(n4,n5,9),(n4,n6,4).
Question 15(a), GATE 2001: Undirected vertices n1,…,n6; edges (n1,n2,2),(n1,n3,8),(n1,n6,3),(n2,n4,4),(n2,n5,12),(n3,n4,7),(n4,n5,9),(n4,n6,4).
Tap to enlarge
Answer & solution

Answer: {n1–n2,n1–n6,n2–n4,n3–n4,n4–n5}, weight 25. (no official key was published)

Step through it (10 steps)

Apply Kruskal in nondecreasing weight order. Take n1–n2 (2), then n1–n6 (3). The weight-4 edges n2–n4 and n4–n6 both connect n4 to the same component; take either, say n2–n4. Reject the other because it closes a cycle. Take n3–n4 (7), joining n3. Reject n1–n3 (8), which now closes a cycle. Take n4–n5 (9), joining the final vertex, and stop after five edges. Total weight is 2+3+4+7+9=25. The tree is connected and acyclic; Kruskal’s cut property establishes minimality.

Original paper, page 9

Descriptive
Report issue

Count distinct minimum spanning trees

Graph Algorithms

Question 15(b), GATE 2001: Undirected vertices n1,…,n6; edges (n1,n2,2),(n1,n3,8),(n1,n6,3),(n2,n4,4),(n2,n5,12),(n3,n4,7),(n4,n5,9),(n4,n6,4).
Question 15(b), GATE 2001: Undirected vertices n1,…,n6; edges (n1,n2,2),(n1,n3,8),(n1,n6,3),(n2,n4,4),(n2,n5,12),(n3,n4,7),(n4,n5,9),(n4,n6,4).
Question 15(b), GATE 2001: Undirected vertices n1,…,n6; edges (n1,n2,2),(n1,n3,8),(n1,n6,3),(n2,n4,4),(n2,n5,12),(n3,n4,7),(n4,n5,9),(n4,n6,4).
Tap to enlarge
Answer & solution

Answer: 2 (no official key was published)

Step through it (10 steps)

The unique cheapest edge crossing cut {n2} is n1–n2 (2), so every MST contains it. Similarly cut {n6} forces n1–n6 (3), cut {n3} forces n3–n4 (7), and cut {n5} forces n4–n5 (9). These four edges leave two components, {n1,n2,n6} and {n3,n4,n5}. Their cheapest connecting edges are n2–n4 and n6–n4, both weight 4. Either one completes a tree of weight 25; choosing both would create a cycle and require losing a forced edge. Hence there are exactly two MSTs.

Original paper, page 9

Descriptive
Report issue

Is the lightest MST weight invariant?

Graph Algorithms

Question 15(c), GATE 2001: For a connected weighted undirected graph, compare the minimum edge weight present in different MSTs.
Question 15(c), GATE 2001: For a connected weighted undirected graph, compare the minimum edge weight present in different MSTs.
Question 15(c), GATE 2001: For a connected weighted undirected graph, compare the minimum edge weight present in different MSTs.
Tap to enlarge
Answer & solution

Answer: Yes: every MST has minimum edge weight equal to the graph’s globally minimum edge weight. (no official key was published)

Let m be the minimum weight of any edge joining distinct vertices. Suppose an MST T had no edge of weight m. Add an edge e of weight m to T. A tree plus one edge contains a cycle, and every other edge on that cycle would be heavier than m. Removing any such edge yields a spanning tree lighter than T, a contradiction. Therefore every MST includes an edge of weight m. The identity of that edge need not be unique, but the numerical minimum is. For the given graph it is 2.

Original paper, page 9

Descriptive
Report issue

Is the heaviest MST weight invariant?

Graph Algorithms

Question 15(d), GATE 2001: For a connected weighted undirected graph, compare the maximum edge weight present in different MSTs.
Question 15(d), GATE 2001: For a connected weighted undirected graph, compare the maximum edge weight present in different MSTs.
Question 15(d), GATE 2001: For a connected weighted undirected graph, compare the maximum edge weight present in different MSTs.
Tap to enlarge
Answer & solution

Answer: Yes; for the given graph the invariant maximum is 9. (no official key was published)

Suppose MST T has maximum edge weight M, and another MST U has all edge weights less than M. Remove an edge of weight M from T, splitting its vertices into two components. Because U is connected, some U edge crosses this cut. Its weight is less than M; inserting it into T minus the removed edge produces a lighter spanning tree, contradicting minimality. Swapping T and U rules out either having a strictly larger maximum. This proves equality of the maximum value, not uniqueness of the heaviest edge or of the MST itself. In the supplied graph n5’s cheapest incident edge has weight 9, and the constructed MST’s largest weight is 9.

Original paper, page 9

2 marks · MCQ
Report issue

Extremal edge weights in a spanning tree

Graph Algorithms

Question 2.18, GATE 2000: Connected undirected G has distinct edge weights. Which is false: every MST contains the minimum edge; if the maximum edge lies in an MST it is a bridge; no MST contains the maximum edge; G has a unique MST?
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: The globally lightest edge is the unique lightest edge across a cut separating one endpoint from the other, so the cut property forces it into the MST. Distinct weights also give a unique MST.

Step 2: If the maximum edge lies on a cycle, it is the unique heaviest edge there and the cycle property excludes it. Thus if it appears in an MST, it must be a bridge. But a bridge is mandatory, even when globally heaviest.

A graph that is itself a weighted tree supplies a counterexample to C. Therefore C is false, while B correctly handles the bridge case.

Original paper, page 5

2 marks · MCQ
Report issue

Consecutive DFS discoveries

Graph Algorithms

Question 2.19, GATE 2000: In undirected DFS, u is discovered and v is the next newly discovered vertex. Which is always true? The options assert a direct edge with ancestry, or that absent edge implies u is a leaf, or that absent edge implies the same parent.
Question 2.19, GATE 2000: In undirected DFS, u is discovered and v is the next newly discovered vertex. Which is always true? The options assert a direct edge with ancestry, or that absent edge implies u is a leaf, or that absent edge implies the same parent.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: If u has an unvisited neighbor when its exploration begins, DFS immediately follows an edge to such a neighbor. Then the next discovery v is adjacent and becomes a child of u.

Step 2: If the next discovery is not adjacent to u, DFS must have exhausted u without discovering any child and backtracked to an ancestor before reaching v. Therefore u is a leaf in the DFS tree, proving C.

The backtrack may cross several ancestors, so u and v need not share a parent. The two options requiring adjacency in every case are also too strong.

Original paper, page 5

2 marks · Descriptive
Report issue

Minimum lattice spanning-tree weight

Graph Algorithms

Question 9(a), GATE 1997: The vertices are the n² integer lattice points (x,y), 1≤x,y≤n, n≥2. Distinct vertices are adjacent when BOTH |x1−x2|≤1 and |y1−y2|≤1; an edge has Euclidean length sqrt((x1−x2)²+(y1−y2)²). This is the GateForum p11 adjacency, including the second-coordinate bound omitted in GFG. Find the minimum spanning-tree weight.
Question 9(a), GATE 1997: The vertices are the n² integer lattice points (x,y), 1≤x,y≤n, n≥2. Distinct vertices are adjacent when BOTH |x1−x2|≤1 and |y1−y2|≤1; an edge has Euclidean length sqrt((x1−x2)²+(y1−y2)²). This is the GateForum p11 adjacency, including the second-coordinate bound omitted in GFG. Find the minimum spanning-tree weight.
Question 9(a), GATE 1997: The vertices are the n² integer lattice points (x,y), 1≤x,y≤n, n≥2. Distinct vertices are adjacent when BOTH |x1−x2|≤1 and |y1−y2|≤1; an edge has Euclidean length sqrt((x1−x2)²+(y1−y2)²). This is the GateForum p11 adjacency, including the second-coordinate bound omitted in GFG. Find the minimum spanning-tree weight.
Tap to enlarge
Answer & solution

Answer: n²−1. (no official key was published)

There are n² vertices, so every spanning tree has exactly n²−1 edges. Distinct integer lattice points are at distance at least 1; hence every spanning tree has weight at least n²−1.

This bound is achievable without using any diagonals. In each row, connect its n vertices consecutively with n−1 horizontal unit edges. This uses n(n−1) edges and leaves n connected row paths. Connect consecutive rows by vertical unit edges along the first column, using n−1 more edges. The result is connected and has n(n−1)+(n−1)=n²−1 edges; equivalently, joining the disjoint row trees by a path cannot create a cycle. Its weight is exactly n²−1, matching the lower bound.

For n=2 this construction has three unit edges and weight 3. Using a diagonal would replace a unit edge by one of length √2 and cannot improve the minimum.

For this minimum, however, the same answer remains valid under that literal variant. All distinct vertices are still at least unit distance apart, and the unit-edge construction is still allowed. Thus this particular answer is robust even though the printed graph definitions differ.

Original paper, page 6

3 marks · Descriptive
Report issue

Maximum lattice spanning-tree weight

Graph Algorithms

Question 9(b), GATE 1997: The vertices are the n² integer lattice points (x,y), 1≤x,y≤n, n≥2. Distinct vertices are adjacent when BOTH |x1−x2|≤1 and |y1−y2|≤1; an edge has Euclidean length sqrt((x1−x2)²+(y1−y2)²). This is the GateForum p11 adjacency, including the second-coordinate bound omitted in GFG. Find the maximum spanning-tree weight; also distinguish the literal GFG graph that constrains only |x1−x2|≤1.
Question 9(b), GATE 1997: The vertices are the n² integer lattice points (x,y), 1≤x,y≤n, n≥2. Distinct vertices are adjacent when BOTH |x1−x2|≤1 and |y1−y2|≤1; an edge has Euclidean length sqrt((x1−x2)²+(y1−y2)²). This is the GateForum p11 adjacency, including the second-coordinate bound omitted in GFG. Find the maximum spanning-tree weight; also distinguish the literal GFG graph that constrains only |x1−x2|≤1.
Question 9(b), GATE 1997: The vertices are the n² integer lattice points (x,y), 1≤x,y≤n, n≥2. Distinct vertices are adjacent when BOTH |x1−x2|≤1 and |y1−y2|≤1; an edge has Euclidean length sqrt((x1−x2)²+(y1−y2)²). This is the GateForum p11 adjacency, including the second-coordinate bound omitted in GFG. Find the maximum spanning-tree weight; also distinguish the literal GFG graph that constrains only |x1−x2|≤1.
Tap to enlarge
Answer & solution

Answer: (2n−2)√(1+(n−1)²)+(n−1)+nΣ_(j=2)^(n−1)√(1+max(j−1,n−j)²) (no official key was published)

In the stated king-move grid, every edge is either horizontal/vertical with weight 1 or diagonal with weight √2. We therefore want as many diagonal edges as a spanning tree can contain.

Color vertex (x,y) by the parity of x+y. A diagonal changes both coordinates by one, so it preserves this parity; a horizontal or vertical edge changes it. Both parity classes are nonempty for n≥2. A connected spanning tree must consequently include at least one unit edge to join the two classes. Since it has n²−1 edges in total, its weight is at most (n²−2)√2+1.

To attain the bound, observe that the diagonal-only graph has exactly two connected components, one of each parity. Here is a constructive connectivity argument. Repeatedly use an upward diagonal to reduce y until reaching the first row; at a side boundary choose the inward direction, always possible because n≥2. On the first row, vertices of a fixed parity can move two columns at a time through a vertex of the second row. Thus all vertices of each parity are connected by diagonals. Take a spanning tree inside each diagonal component. Together they use n²−2 diagonal edges. Add any unit edge between the components. This produces a spanning tree attaining the upper bound.

For n=2 the two diagonals, joined by one side, have weight 2√2+1. Under the paper's literal omission of the y-bound, longer edges are allowed when n≥3. Its maximum is instead

(2n−2)√(1+(n−1)²) + (n−1) + nΣ_(j=2)n−1√(1+max(j−1,n−j)²).

Here is a separate optimality proof for that literal graph. Call vertices on y=1 or y=n boundary vertices. For each interior vertex at row j, select an edge to a farthest boundary row in an adjacent column. Such a column always exists for n≥2. Its length is √(1+max(j−1,n−j)²), a maximum edge across the cut isolating that vertex. Add these edges while each interior vertex is still isolated; all chosen parents are boundary vertices, so no cycle arises. By the maximum-spanning-tree cut property, each addition is safe.

Next add all 2n−2 boundary diagonals with |dx|=1 and |dy|=n−1. They are globally maximum edges, each of length √(1+(n−1)²), and form two disjoint alternating-row paths on the 2n boundary vertices. Interior attachments do not join those paths. Finally join them by a same-column edge from y=1 to y=n, of length n−1. Every competing cross-component edge with an interior endpoint is at most √(1+(n−2)²)<n−1 for n≥3. Remaining boundary cross-edges have length at most n−1, so this last edge is also cut-safe. At n=2 there are no interior vertices and the bridge length is 1.

The safe selections form a spanning tree and give the displayed sum: n equal-length interior attachments for each row j, 2n−2 boundary diagonals, and one bridge. Thus both materially different printed graphs have an explicit maximum and a proof, rather than silently restoring a bound.

Original paper, page 6

Descriptive
Report issue

Find an absolute-difference MST

Graph Algorithms

Question 16(a), GATE 1996: A complete undirected graph on vertices 0,1,…,n−1 has weight w(u,v)=|u−v| for distinct vertices. Draw a minimum spanning tree.
Question 16(a), GATE 1996: A complete undirected graph on vertices 0,1,…,n−1 has weight w(u,v)=|u−v| for distinct vertices. Draw a minimum spanning tree.
Tap to enlarge
Answer & solution

Answer: Path 0—1—2—…—(n−1), total weight n−1. (no official key was published)

Every edge joins distinct integers, so its weight is at least 1. A spanning tree on n vertices contains exactly n−1 edges. Consequently every spanning tree has total weight at least n−1.

Connect consecutive vertices: (0,1),(1,2),…,(n−2,n−1). This is a connected path with no cycles, so it is a spanning tree. Each of its n−1 edges has weight 1, giving total n−1 and attaining the lower bound. Text diagram: 0 --1-- 1 --1-- 2 --1-- … --1-- (n−1), where the labels between dashes are edge weights.

For n≥2 it is also the unique MST. Achieving the lower bound requires every selected edge to have weight 1, and the only weight-1 edges in this graph are exactly the consecutive pairs. There are n−1 of them, so all must be selected. Alternatively, for the cut {0,…,k}|{k+1,…,n−1}, edge (k,k+1) is the unique minimum crossing edge and is mandatory by the cut property. When n=1 the empty tree has cost 0; the same n−1 formula applies.

Original paper, page 9

Descriptive
Report issue

Find a vertex-sum MST

Graph Algorithms

Question 16(b), GATE 1996: A complete undirected graph on vertices 0,1,…,n−1 has weight w(u,v)=u+v for distinct vertices. Draw a minimum spanning tree.
Question 16(b), GATE 1996: A complete undirected graph on vertices 0,1,…,n−1 has weight w(u,v)=u+v for distinct vertices. Draw a minimum spanning tree.
Tap to enlarge
Answer & solution

Answer: Star centered at 0, edges (0,v) for 1≤v<n; total n(n−1)/2. (no official key was published)

For each vertex v>0, consider the cut separating v from every other vertex. Its incident edges have weights v+u, with u≠v. The smallest possible u is 0, so (v,0) is the unique minimum-weight edge across this cut. By the cut property every MST must contain it.

Doing this for every v=1,…,n−1 gives exactly the star centered at 0. These n−1 edges connect all vertices and have no cycle, so the mandatory set itself is a spanning tree. Its weight is Σ_(v=1)n−1 v=n(n−1)/2. Text diagram: center 0 with a spoke to each v; spoke (0,v) has label v.

A second lower-bound proof roots any spanning tree at 0. Each nonroot vertex v has one parent p(v)≥0, so its parent edge costs v+p(v)≥v. Summing over all nonroot vertices gives total at least Σv. The star attains equality by taking every parent to be 0. This argument also shows uniqueness for n≥2: equality requires all parents 0. For n=1 there are no spokes and the minimum weight is 0. The complete graph assumption guarantees that every required spoke exists.

Original paper, page 9

Descriptive
Report issue

Run Dijkstra's algorithm

Graph Algorithms

Question 17(a), GATE 1996: In the GFG p9 directed graph, edges with weights are A→B:6, A→C:90, A→F:2, A→E:70, B→D:41, C→B:12, C→E:10, D→C:12, F→B:22, F→C:60, F→D:50, E→F:15. GateForum p15 swaps the names E and F while keeping these positions and weights. Start at A. Give the Dijkstra settlement order.
Question 17(a), GATE 1996: In the GFG p9 directed graph, edges with weights are A→B:6, A→C:90, A→F:2, A→E:70, B→D:41, C→B:12, C→E:10, D→C:12, F→B:22, F→C:60, F→D:50, E→F:15. GateForum p15 swaps the names E and F while keeping these positions and weights. Start at A. Give the Dijkstra settlement order.
Tap to enlarge
Answer & solution

Answer: A, F, B, D, C, E (no official key was published)

Use the the paper labels first. Initialize d(A)=0 and all other labels to infinity. Settle A: tentative B=6,C=90,F=2,E=70. Settle F (2): B via F would be 24 and does not improve 6; C improves to 62 and D becomes 52. Settle B (6): D improves to 6+41=47. Settle D (47): C improves to 47+12=59. Settle C (59): E improves to 59+10=69; the edge back to B gives 71 and changes nothing. Finally settle E (69), whose edge to F gives 84 and changes nothing.

Thus the settlement order is A,F,B,D,C,E, with final distances 0,2,6,47,59,69 in that order. Predecessors for the E path are E←C←D←B←A, giving A→B→D→C→E and cost 6+41+12+10=69. This improves on the direct A→E cost 70 and the route A→F→C→E cost 72. All weights are nonnegative, so Dijkstra's settled distances are final.

A material the paper difference changes the answer to the named destination. Applying that relabelling, its settlement order is A,E,B,D,C,F. Its shortest A→E path is the direct edge of weight 2; the 69-cost bottom-vertex path ends at F there. The answers must remain tied to the chosen diagram. Decimal labels 90,60,50,70,10 contain zero glyphs, not an added symbolic variable.

Original paper, page 9

Descriptive
Report issue

Recover the shortest path

Graph Algorithms

Question 17(b), GATE 1996: In the GFG p9 directed graph, edges with weights are A→B:6, A→C:90, A→F:2, A→E:70, B→D:41, C→B:12, C→E:10, D→C:12, F→B:22, F→C:60, F→D:50, E→F:15. GateForum p15 swaps the names E and F while keeping these positions and weights. Start at A. Give the shortest A→E vertex sequence.
Question 17(b), GATE 1996: In the GFG p9 directed graph, edges with weights are A→B:6, A→C:90, A→F:2, A→E:70, B→D:41, C→B:12, C→E:10, D→C:12, F→B:22, F→C:60, F→D:50, E→F:15. GateForum p15 swaps the names E and F while keeping these positions and weights. Start at A. Give the shortest A→E vertex sequence.
Tap to enlarge
Answer & solution

Answer: A, B, D, C, E (no official key was published)

Use the the paper labels first. Initialize d(A)=0 and all other labels to infinity. Settle A: tentative B=6,C=90,F=2,E=70. Settle F (2): B via F would be 24 and does not improve 6; C improves to 62 and D becomes 52. Settle B (6): D improves to 6+41=47. Settle D (47): C improves to 47+12=59. Settle C (59): E improves to 59+10=69; the edge back to B gives 71 and changes nothing. Finally settle E (69), whose edge to F gives 84 and changes nothing.

Thus the settlement order is A,F,B,D,C,E, with final distances 0,2,6,47,59,69 in that order. Predecessors for the E path are E←C←D←B←A, giving A→B→D→C→E and cost 6+41+12+10=69. This improves on the direct A→E cost 70 and the route A→F→C→E cost 72. All weights are nonnegative, so Dijkstra's settled distances are final.

A material the paper difference changes the answer to the named destination. Applying that relabelling, its settlement order is A,E,B,D,C,F. Its shortest A→E path is the direct edge of weight 2; the 69-cost bottom-vertex path ends at F there. The answers must remain tied to the chosen diagram. Decimal labels 90,60,50,70,10 contain zero glyphs, not an added symbolic variable.

Original paper, page 9

Descriptive
Report issue

Compute the shortest-path cost

Graph Algorithms

Question 17(c), GATE 1996: In the GFG p9 directed graph, edges with weights are A→B:6, A→C:90, A→F:2, A→E:70, B→D:41, C→B:12, C→E:10, D→C:12, F→B:22, F→C:60, F→D:50, E→F:15. GateForum p15 swaps the names E and F while keeping these positions and weights. Start at A. Give the shortest A→E cost.
Question 17(c), GATE 1996: In the GFG p9 directed graph, edges with weights are A→B:6, A→C:90, A→F:2, A→E:70, B→D:41, C→B:12, C→E:10, D→C:12, F→B:22, F→C:60, F→D:50, E→F:15. GateForum p15 swaps the names E and F while keeping these positions and weights. Start at A. Give the shortest A→E cost.
Tap to enlarge
Answer & solution

Answer: 69 (no official key was published)

Use the the paper labels first. Initialize d(A)=0 and all other labels to infinity. Settle A: tentative B=6,C=90,F=2,E=70. Settle F (2): B via F would be 24 and does not improve 6; C improves to 62 and D becomes 52. Settle B (6): D improves to 6+41=47. Settle D (47): C improves to 47+12=59. Settle C (59): E improves to 59+10=69; the edge back to B gives 71 and changes nothing. Finally settle E (69), whose edge to F gives 84 and changes nothing.

Thus the settlement order is A,F,B,D,C,E, with final distances 0,2,6,47,59,69 in that order. Predecessors for the E path are E←C←D←B←A, giving A→B→D→C→E and cost 6+41+12+10=69. This improves on the direct A→E cost 70 and the route A→F→C→E cost 72. All weights are nonnegative, so Dijkstra's settled distances are final.

A material the paper difference changes the answer to the named destination. Applying that relabelling, its settlement order is A,E,B,D,C,F. Its shortest A→E path is the direct edge of weight 2; the 69-cost bottom-vertex path ends at F there. The answers must remain tied to the chosen diagram. Decimal labels 90,60,50,70,10 contain zero glyphs, not an added symbolic variable.

Original paper, page 9

5 marks · Descriptive
Report issue

Enumerating all minimum spanning trees

Graph Algorithms

Question 22, GATE 1995: The weighted undirected graph has vertices A,B,C,D,E and edges AB=2, AE=3, BC=3, BD=4, CD=4, DE=5. Draw and count all minimum spanning trees. These are the edges visibly present in GFG p8. GateForum p15 omits the AB line while retaining a detached weight 2; also analyze that literal graph.
Tap to enlarge
Answer & solution

Answer: 2 minimum spanning trees, each of weight 12: {AB, AE, BC, BD} and {AB, AE, BC, CD} (no official key was published)

A spanning tree on five vertices has four edges. Graph, AB is the unique cheapest edge crossing the cut {A}, so it belongs to every MST. AE is the unique cheapest edge crossing {E}, and BC is the unique cheapest edge crossing {C}; these too are forced by the cut property. The three edges AB,AE,BC form a tree on A,B,C,E, with D still isolated. Connect D using either BD or CD, each of weight 4. Using DE of weight 5 instead would cost more. Thus the optimum total is 2+3+3+4=12 and there are exactly two choices.

Draw tree1 as E−(3)−A−(2)−B−(3)−C, with an additional B−(4)−D branch. Draw tree2 as the path E−(3)−A−(2)−B−(3)−C−(4)−D. The edge sets in the answer uniquely specify every diagram and show that both graphs are connected and acyclic. Forced edges plus the only two minimum connectors prove completeness, rather than merely exhibiting two examples.

If interpreted literally, A has only neighbor E, and {A,E} connects to the rest only through ED. Consequently AE=3 and ED=5 are forced. In the remaining BCD triangle, choose BC=3 and either BD=4 or CD=4. This gives exactly two spanning trees of minimum weight 15. The count happens to remain 2, but the trees and weights differ.

Original paper, page 8

Report issue

A false graph-search claim

Graph Algorithms

Question 1.22, GATE 1994: Which is false: optimal BST via dynamic programming; BFS cannot find connected components; preorder/postorder do not generally uniquely determine a binary tree; DFS can find components?
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Start BFS from an unvisited vertex and mark everything reachable. In an undirected graph this is exactly its connected component. Repeating from remaining unvisited vertices finds every component in O(V+E), disproving B. DFS works similarly. Optimal BST construction has a dynamic-programming formulation. Preorder and postorder alone do not generally distinguish a node’s sole left child from its sole right child, so nonuniqueness is possible. That last statement is a general insufficiency, not a claim that every traversal pair is ambiguous.

Original paper, page 3

Descriptive
Report issue

Leaf elimination for a maximum independent set

Graph Algorithms

Question 24(a), GATE 1994: Complete this scheme for a tree: V is its vertex set, I=∅; while V≠∅ choose u∈V satisfying blank 1, set V:=V−{u}, and if u satisfies blank 2 set I:=I∪{u}; output I. Fill both properties to obtain a maximum independent set.
Question 24(a), GATE 1994: Complete this scheme for a tree: V is its vertex set, I=∅; while V≠∅ choose u∈V satisfying blank 1, set V:=V−{u}, and if u satisfies blank 2 set I:=I∪{u}; output I. Fill both properties to obtain a maximum independent set.
Tap to enlarge
Answer & solution

Answer: Choose u of degree at most one in the current induced forest; add u iff no original neighbor of u is already in I. (no official key was published)

Fill the first blank with: u is a leaf or isolated vertex in the forest induced by the current V. Fill the second with: u has no neighbor already in I, where adjacency is measured in the original tree. The removal changes the working forest only; it must not erase the adjacency information needed to test membership conflicts. Because every nonempty forest has a vertex of degree at most one, selection is always possible.

Independence is immediate: a vertex enters I only after checking that it is nonadjacent to every earlier selected vertex. To prove maximum size, maintain the stronger invariant that some maximum independent set of the original tree contains I and uses no previously rejected vertex. Consider a selected leaf u of the working forest. If u already has a neighbor in I, no independent set extending I can use u; rejecting it preserves the invariant. Otherwise u is eligible. In the remaining eligible graph it has at most one neighbor. If it has none, every maximum extension includes it. If it has one neighbor v, take a maximum extension: when it omits u, it must contain v (otherwise u could be added). Replace v by u. This preserves cardinality and independence, since u has no other eligible neighbor and none in I. Thus some maximum extension contains u, so adding u is safe.

At termination no undecided vertices remain; the invariant makes I itself maximum. “Choose any vertex and add it if possible” would guarantee only maximality, not maximum size: choosing the center of a star first is a counterexample. The leaf condition is essential.

Original paper, page 7

Descriptive
Report issue

Implementing leaf elimination in linear time

Graph Algorithms

Question 24(b), GATE 1994: For the tree independent-set algorithm that repeatedly removes a current leaf or isolated vertex and adds it if it has no selected neighbor, determine the time complexity and necessary implementation assumptions.
Question 24(b), GATE 1994: For the tree independent-set algorithm that repeatedly removes a current leaf or isolated vertex and adds it if it has no selected neighbor, determine the time complexity and necessary implementation assumptions.
Question 24(b), GATE 1994: For the tree independent-set algorithm that repeatedly removes a current leaf or isolated vertex and adds it if it has no selected neighbor, determine the time complexity and necessary implementation assumptions.
Tap to enlarge
Answer & solution

Answer: Θ(n) time and O(n) working space with adjacency lists, degree counters and a leaf queue; naïve rescanning may take Θ(n²). (no official key was published)

A tree with n vertices has n−1 edges. Store its original adjacency lists, each vertex's current degree, a removed flag, a selected-neighbor (blocked) flag, and a queue of vertices whose current degree is at most one. Initializing these structures takes O(n+|E|)=O(n) time.

Pop a still-unremoved queue entry u, mark it removed, and add it to I exactly when its blocked flag is false. If added, mark its neighbors blocked. For each still-present neighbor, decrement the neighbor's degree; enqueue it when its degree first falls to one. Isolated initial vertices are queued from the start. Any harmless duplicate or stale entries can be skipped with the removed flag; alternatively use a queued flag.

Every vertex is removed once. Its adjacency list is scanned only a constant number of times, and the sum of degrees is 2(n−1). Hence all degree maintenance, blocking updates and queue operations total O(n). Reading all vertices and producing the result requires Ω(n) in the worst case, yielding Θ(n) time. The queue, flags, degree counts and adjacency representation require O(n) space.

The abstract scheme by itself does not force this bound: repeatedly searching all remaining vertices for a leaf or scanning I to test every candidate can take quadratic time. The linear bound is the efficient adjacency-list implementation, not a representation-independent guarantee. This distinction matters because the printed question specifies a tree and an algorithm outline but not data structures.

Original paper, page 7

Report issue

Kruskal when edges are already sorted

Graph Algorithms

Question 1(ix), GATE 1992: For n vertices and m presorted edges, state Kruskal’s running time and the union-find model.
Question 1(ix), GATE 1992: For n vertices and m presorted edges, state Kruskal’s running time and the union-find model.
Tap to enlarge
Answer & solution

Answer: O(m α(n)+n) with optimized union-find; O(m log n+n) with balanced-tree bounds (no official key was published)

Initialize n singleton components, scan edges in the given order and use two find operations to test whether each edge crosses components. Accepted edges trigger a union, at most n−1 times. With path compression and union by rank, the total is O(m α(n)+n), since sorting has already been removed. A simpler union-find implementation with logarithmic operations gives O(m log n+n). Reporting only the usual O(m log m) sorting bound would ignore the supplied sorted order. The exact implementation must be stated rather than treating one data-structure bound as universal.

Original paper, page 1

Descriptive
Report issue

Print a DFS fundamental cycle

Graph Algorithms

Question 8, GATE 1992: T is a depth-first tree of an undirected graph G. Only a parent array P is given, with the root its own parent. For non-tree edge (u,v), give Pascal-like code printing its unique cycle after addition to T, in time proportional to cycle length.
Tap to enlarge
Answer & solution

Answer: Climb from both endpoints in parallel until one reaches the other original endpoint; print the successful parent path and the added closing edge. (no official key was published)

In an undirected DFS tree, every non-tree edge joins an ancestor and a descendant. To see why, suppose an edge joined distinct DFS subtrees: while its first endpoint’s subtree was explored, the still-undiscovered other endpoint would be reached by that edge and placed in the same subtree, a contradiction. Thus the tree path between u and v is a single upward parent chain from the descendant. Which endpoint is deeper is not given, so climbing just one chosen endpoint can waste time or stick at the root.

Climb both parent chains in lockstep, saving both traversals, and stop as soon as one reaches the other original endpoint:

procedure PrintCycle(u,v);
begin
  a:=u; b:=v; lenA:=1; lenB:=1;
  pathA[1]:=u; pathB[1]:=v;
  while (a<>v) and (b<>u) do begin
    a:=P[a]; lenA:=lenA+1; pathA[lenA]:=a;
    b:=P[b]; lenB:=lenB+1; pathB[lenB]:=b
  end;
  if a=v then begin
    for k:=1 to lenA do write(pathA[k]);
    write(u)
  end else begin
    for k:=1 to lenB do write(pathB[k]);
    write(v)
  end
end.

Assume arrays can hold the tree height plus one and append/write are constant-time operations. Storage can instead be growable linked lists with constant-time append. Arrays need no initial clearing; only positions written above are read.

Let the ancestor–descendant distance be d. The descendant walk reaches the ancestor after exactly d rounds, even if the other walk has already reached the root and is repeating its self-parent. The algorithm performs at most 2d parent moves and prints d+1 distinct cycle vertices plus the repeated start: Θ(d+1), proportional to the cycle’s d+1 edges, with O(d+1) temporary storage. For example a parent chain 7→5→2 and new edge (2,7) prints 7,5,2,7.

No full-tree traversal, depth computation or global visited-array clearing is required. The argument assumes the usual simple undirected graph; a self-loop can be handled immediately by printing u,u. For an arbitrary spanning tree the ancestor property is false, so this algorithm is specifically justified by the DFS premise.

Original paper, page 6

2 marks · MSQ
Report issue

Kruskal bounds in a multiple-answer question

Graph Algorithms

Question 3(vi), GATE 1991: For n vertices and m edges, choices are A O(n²), B O(mn), C O(m+n), D O(m log n), E O(m²). Identify Kruskal’s complexity.
Question 3(vi), GATE 1991: For n vertices and m edges, choices are A O(n²), B O(mn), C O(m+n), D O(m log n), E O(m²). Identify Kruskal’s complexity.
Tap to enlarge
Select all that apply
Answer & solution

Answer: D is the usual informative bound; B, D, E are valid upper bounds for connected simple graphs (no official key was published)

Comparison-sort Kruskal first sorts m edges in O(m log m), then processes them with disjoint-set operations. For a connected simple graph, n−1≤m≤n(n−1)/2, so log m=O(log n), yielding O(m log n). This supports D. Because big-O is an upper bound, the same algorithm is also O(mn) and O(m²), so B and E cannot be called false merely for being looser in a multiple-answer task. O(m+n) is not the generic comparison-sorting bound; O(n²) does not cover the usual sorting cost for dense arbitrary-weight inputs. Different integer-sorting or presorted-edge models can change the analysis.

Original paper, page 3