GATE 2008
185 questions
MCQ 1851-mark 52 · 2-mark 133
Questions 111–115 of 185
The most efficient algorithm for finding the number of connected components in an undirected graph on n vertices and m edges has time complexity
Answer: (C)
Start DFS (or BFS) from any vertex. It visits every vertex reachable from it, which is exactly one connected component. Then pick any vertex not yet visited, start a new DFS there, and add 1 to the count. Repeat until every vertex is visited. The number of DFS starts is the number of components.
Cost: each vertex is visited once, which takes , including the check for unvisited vertices. Each edge is scanned twice, once from each end, which takes . Total . Every vertex and every edge has to be looked at, so no algorithm can do better in general. and alone leave out part of the work, and is slower.
Answer: (C)
Cost: each vertex is visited once, which takes , including the check for unvisited vertices. Each edge is scanned twice, once from each end, which takes . Total . Every vertex and every edge has to be looked at, so no algorithm can do better in general. and alone leave out part of the work, and is slower.
Answer: (C)
For the undirected, weighted graph given below, which of the following sequences of edges represents a correct execution of Prim's algorithm to construct a Minimum Spanning Tree?
Answer: (C)
Prim's algorithm grows one tree. It starts at any vertex and at each step adds the lightest edge that joins a vertex in the tree to a vertex outside it.
(C) is valid. Start at d.
- Tree {d}: lightest edge out is d-f (2).
- Tree {d, f}: lightest is f-c (3).
- Tree {d, f, c}: lightest is d-a (5).
- Tree {d, f, c, a}: lightest is a-b (1).
- Tree {d, f, c, a, b}: lightest is c-e (7).
- Add e. Lightest is f-h (8).
- Add h. Lightest is h-g (7).
- Add g. Lightest is g-i (3).
These are the edges (d,f), (f,c), (d,a), (a,b), (c,e), (f,h), (g,h), (g,i) in the same order.
(A) picks (a,b) and then (d,f), but neither d nor f is in the tree {a, b}.
(B) picks (g,h) when neither g nor h is in the tree built so far.
(D) picks (f,c) after (h,f), but at that point (f,d) is the lighter edge out of the tree.
Answer: (C)
(C) is valid. Start at d.
- Tree {d}: lightest edge out is d-f (2).
- Tree {d, f}: lightest is f-c (3).
- Tree {d, f, c}: lightest is d-a (5).
- Tree {d, f, c, a}: lightest is a-b (1).
- Tree {d, f, c, a, b}: lightest is c-e (7).
- Add e. Lightest is f-h (8).
- Add h. Lightest is h-g (7).
- Add g. Lightest is g-i (3).
These are the edges (d,f), (f,c), (d,a), (a,b), (c,e), (f,h), (g,h), (g,i) in the same order.
(A) picks (a,b) and then (d,f), but neither d nor f is in the tree {a, b}.
(B) picks (g,h) when neither g nor h is in the tree built so far.
(D) picks (f,c) after (h,f), but at that point (f,d) is the lighter edge out of the tree.
Answer: (C)
G is a graph on n vertices and 2n-2 edges. The edges of G can be partitioned into two edge-disjoint spanning trees. Which of the following is NOT true for G?
Answer: (D)
G has edges, and each of the two edge-disjoint spanning trees has edges, so every edge of G is in one of the trees.
(A) True. Take any vertices. Each tree, restricted to them, has no cycle, so it has at most edges. Two trees give at most edges.
(B) True. Each spanning tree is connected, so it has at least one edge across any cut. The trees share no edge, so every cut has at least 2 edges.
(C) True. The path between two vertices inside tree 1 and the path inside tree 2 use different edges.
(D) Not always true. Take two copies of sharing one vertex : and edges. Each splits into two edge-disjoint paths (1-2-3-4 and 3-1-4-2). Joining one such path from each copy gives a spanning tree, so G has two edge-disjoint spanning trees. But is a cut vertex: every path between a vertex in one copy and a vertex in the other passes through , so there are not two vertex-disjoint paths.
Answer: (D)
(A) True. Take any vertices. Each tree, restricted to them, has no cycle, so it has at most edges. Two trees give at most edges.
(B) True. Each spanning tree is connected, so it has at least one edge across any cut. The trees share no edge, so every cut has at least 2 edges.
(C) True. The path between two vertices inside tree 1 and the path inside tree 2 use different edges.
(D) Not always true. Take two copies of sharing one vertex : and edges. Each splits into two edge-disjoint paths (1-2-3-4 and 3-1-4-2). Joining one such path from each copy gives a spanning tree, so G has two edge-disjoint spanning trees. But is a cut vertex: every path between a vertex in one copy and a vertex in the other passes through , so there are not two vertex-disjoint paths.
Answer: (D)
Dijkstra's single source shortest path algorithm when run from vertex a in the above graph, computes the correct shortest path distance to
Answer: (D)
Run Dijkstra from and keep a tentative distance for each vertex.
1. Take (0): .
2. Take (1): , .
3. Take (-2): .
4. Take (0): .
5. Take (3): , .
6. Take (-2): the edge to gives , which is not better than .
7. Take (3): the edges to and give 4 and 5, no improvement.
8. Take (6): the edges to and give 8, no improvement.
Final distances: . No vertex is improved after it is finalised, so these are the true shortest distances (they can be checked by hand, and the graph has no negative cycle).
Dijkstra is not safe for negative edges in general. Here it works because no later vertex offers a shorter route to an already finalised one. So it is correct for all vertices.
Answer: (D)
1. Take (0): .
2. Take (1): , .
3. Take (-2): .
4. Take (0): .
5. Take (3): , .
6. Take (-2): the edge to gives , which is not better than .
7. Take (3): the edges to and give 4 and 5, no improvement.
8. Take (6): the edges to and give 8, no improvement.
Final distances: . No vertex is improved after it is finalised, so these are the true shortest distances (they can be checked by hand, and the graph has no negative cycle).
Dijkstra is not safe for negative edges in general. Here it works because no later vertex offers a shorter route to an already finalised one. So it is correct for all vertices.
Answer: (D)
Which of the following regular expressions describes the language over consisting of strings that contain exactly two 1's?
Answer: (C)
Say what each expression describes:
- (A) Strings that have at least consecutive 's, with any number of 's and 's around them. matches, so it is not "exactly two".
- (B) Strings that have exactly two 's, and they must be consecutive. It misses , which has exactly two 1's.
- (C) Strings that have exactly two 's, which need not be consecutive, with any number of 0's before, between and after them.
- (D) Strings with any number of 's and 's, but at least two 's. matches.
Answer: (C)
- (A) Strings that have at least consecutive 's, with any number of 's and 's around them. matches, so it is not "exactly two".
- (B) Strings that have exactly two 's, and they must be consecutive. It misses , which has exactly two 1's.
- (C) Strings that have exactly two 's, which need not be consecutive, with any number of 0's before, between and after them.
- (D) Strings with any number of 's and 's, but at least two 's. matches.
Answer: (C)