Open In App

Data Structures and Algorithms | Set 15

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Following questions have been asked in GATE CS 2008 exam.

1. The most efficient algorithm for finding the number of connected components in an undirected graph on n vertices and m edges has time complexity.

(A) Θ(n)
(B) Θ(m)
(C) Θ(m + n)
(D) Θ(mn)

Answer: (C)

Explanation: Connected components can be found in O(m + n) using Tarjan’s algorithm. Once we have connected components, we can count them.

2. Consider the Quicksort algorithm. Suppose there is a procedure for finding a pivot element which splits the list into two sub-lists each of which contains at least one-fifth of the elements. Let T(n) be the number of comparisons required to sort n elements. Then:

(A) T(n) <= 2T(n/5) + n
(B) T(n) <= T(n/5) + T(4n/5) + n
(C) T(n) <= 2T(4n/5) + n
(D) T(n) <= 2T(n/2) + n

Answer: (B)

Explanation: Since one sub-list has at least n/5 elements and the other has at most 4n/5 elements, the recurrence becomes T(n) ≤ T(n/5) + T(4n/5) + n, which is answer (B).

3. Dijkstra’s single source shortest path algorithm when run from vertex a in the below graph, computes the correct shortest path distance to

(A) only vertex a
(B) only vertices a, e, f, g, h
(C) only vertices a, b, c, d
(D) all the vertices

Answer: (D)

Explanation: Dijkstra’s algorithm computes the shortest path from a source vertex to all vertices reachable from it, provided there are no negative weight edges.

4. The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?

C
struct node 
{
  int value;
  struct node *next;
};
void rearrange(struct node *list)
{
  struct node *p, * q;
  int temp;
  if ((!list) || !list->next) 
      return;
  p = list;
  q = list->next;
  while(q) 
  {
     temp = p->value;
     p->value = q->value;
     q->value = temp;
     p = q->next;
     q = p?p->next:0;
  }
}

(A) 1,2,3,4,5,6,7
(B) 2,1,4,3,6,5,7
(C) 1,3,2,5,4,7,6
(D) 2,3,4,5,6,7,1

Answer: (B)

Explanation: The function swaps the value of every adjacent pair starting from the first node. For the list 1, 2, 3, 4, 5, 6, 7, the pairs (1,2), (3,4), and (5,6) are swapped, resulting in 2, 1, 4, 3, 6, 5, 7.


Next Article

Similar Reads