## Data Structures and Algorithms | Set 30

Following questions have been asked in GATE CS 2013 exam.

**1) Which of the following statements is/are TRUE for an undirected graph?
P: Number of odd degree vertices is even
Q: Sum of degrees of all vertices is even**

A) P Only

B) Q Only

C) Both P and Q

D) Neither P nor Q

Answer (C)

Q is true: Since the graph is undirected, every edge increases the sum of degrees by 2.

P is true: If we consider sum of degrees and subtract all even degrees, we get an even number (because Q is true). So total number of odd degree vertices must be even.

**2) Consider an undirected random graph of eight vertices. The probability that there is an edge between a pair of vertices is 1/2. What is the expected number of unordered cycles of length three? **

(A) 1/8

(B) 1

(C) 7

(D) 8

Answer (C)

A cycle of length 3 can be formed with 3 vertices. There can be total 8C3 ways to pick 3 vertices from 8. The probability that there is an edge between two vertices is 1/2. So expected number of unordered cycles of length 3 = (8C3)*(1/2)^3 = 7

**3) What is the time complexity of Bellman-Ford single-source shortest path algorithm on a complete graph of n vertices? **

(A)

(B)

(C)

(D)

Answer (C).

Time complexity of Bellman-Ford algorithm is where V is number of vertices and E is number edges (See this). If the graph is complete, the value of E becomes . So overall time complexity becomes

**4) Which of the following statements are TRUE? **

** (1) The problem of determining whether there exists a cycle in an undirected graph is in P.
(2) The problem of determining whether there exists a cycle in an undirected graph is in NP.
(3) If a problem A is NP-Complete, there exists a non-deterministic polynomial time algorithm to solve A. **

(A) 1,2 and 3

(B) 1 and 2 only

(C) 2 and 3 only

(D) 1 and 3 only

Answer (A)

**1 **is true because cycle detection can be done in polynomial time using DFS (See this).

**2 **is true because P is a subset of NP.

**3 **is true because NP complete is also a subset of NP and NP means **N**on-deterministic **P**olynomial time solution exists. (See this)

**5) Which one of the following is the tightest upper bound that represents the time complexity of inserting an object into a binary search tree of n nodes? **

(A) O(1)

(B) O(log n)

(C) O(n)

(D) O(n log n)

Answer (C)

The worst case occurs for a skewed tree. In a skewed tree, when a new node is inserted as a child of bottommost node, the time for insertion requires traversal of all node. For example, consider the following tree and the case when something smaller than 70 is inserted.

100 / 90 / 80 / 70

**6) Which one of the following is the tightest upper bound that represents the number of swaps required to sort n numbers using selection sort? **

(A) O(log n)

(B) O(n)

(C) O(n log n)

(D) O(n^2)

Answer (B)

Selection sort requires only O(n) swaps. See this for details.

**7) Consider the following operation along with Enqueue and Dequeue operations on
queues, where k is a global parameter **

MultiDequeue(Q){ m = k while (Q is not empty and m > 0) { Dequeue(Q) m = m - 1 } }

**What is the worst case time complexity of a sequence of n MultiDequeue() operations on an initially empty queue? **

(A)

(B)

(C)

(D)

Answer (A)

Since the queue is empty initially, the condition of while loop never becomes true. So the time complexity is

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.

Writing code in comment?Please useideone.comand share the link here.