Question 1
Consider the directed graph shown in the figure below. There are multiple shortest paths between vertices S and T. Which one will be reported by Dijstra?s shortest path algorithm? Assume that, in any iteration, the shortest path to a vertex v is updated only when a strictly shorter path to v is discovered.

Graph

A
SDT
B
SBDT
C
SACDT
D
SACET
Graph Shortest Paths    
Discuss it


Question 1 Explanation: 
See Dijkstra’s shortest path algorithm When the algorithm reaches vertex 'C', the distance values of 'D' and 'E' would be 7 and 6 respectively. So the next picked vertex would be 'E'
Question 2
To implement Dijkstra’s shortest path algorithm on unweighted graphs so that it runs in linear time, the data structure to be used is:
A
Queue
B
Stack
C
Heap
D
B-Tree
Graph Shortest Paths    
Discuss it


Question 2 Explanation: 
The shortest path in an un-weighted graph means the smallest number of edges that must be traversed in order to reach the destination in the graph. This is the same problem as solving the weighted version where all the weights happen to be 1. If we use Queue (FIFO) instead of Priority Queue (Min Heap), we get the shortest path in linear time O(|V| + |E|). Basically we do BFS traversal of the graph to get the shortest paths.
Question 3
Dijkstra’s single source shortest path algorithm when run from vertex a in the below graph, computes the correct shortest path distance to gate_2008_21
A
only vertex a
B
only vertices a, e, f, g, h
C
only vertices a, b, c, d
D
all the vertices
Graph Shortest Paths    
Discuss it


Question 3 Explanation: 
Dijkstra’s single source shortest path is not guaranteed to work for graphs with negative weight edges, but it works for the given graph. Let us see... Let us run the 1st pass b 1 b is minimum, so shortest distance to b is 1. After 1st pass, distances are c 3, e -2. e is minimum, so shortest distance to e is -2 After 2nd pass, distances are c 3, f 0. f is minimum, so shortest distance to f is 0 After 3rd pass, distances are c 3, g 3. Both are same, let us take g. so shortest distance to g is 3. After 4th pass, distances are c 3, h 5 c is minimum, so shortest distance to c is 3 After 5th pass, distances are h -2 h is minimum, so shortest distance to h is -2
Question 4
In an unweighted, undirected connected graph, the shortest path from a node S to every other node is computed most efficiently, in terms of time complexity by
A
Dijkstra’s algorithm starting from S.
B
Warshall’s algorithm
C
Performing a DFS starting from S.
D
Performing a BFS starting from S.
Graph Shortest Paths    
Discuss it


Question 4 Explanation: 
 * Time Comlexity of the Dijkstra’s algorithm is O(|V|^2 + E)  
 * Time Comlexity of the Warshall’s algorithm is O(|V|^3)
 * DFS cannot be used for finding shortest paths
 * BFS can be used for unweighted graphs. Time Complexity for BFS is O(|E| + |V|)
Question 5
Suppose we run Dijkstra’s single source shortest-path algorithm on the following edge weighted directed graph with vertex P as the source. In what order do the nodes get included into the set of vertices for which the shortest path distances are finalized? (GATE CS 2004) gate2004
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
Graph Shortest Paths    
Discuss it


Question 6
What is the time complexity of Bellman-Ford single-source shortest path algorithm on a complete graph of n vertices? (A) \Theta(n^2) (B) \Theta(n^2 Logn) (C) \Theta(n^3) (D) \Theta(n^3 Logn)
A
A
B
B
C
C
D
D
Graph Shortest Paths    
Discuss it


Question 6 Explanation: 
Time complexity of Bellman-Ford algorithm is [Tex]\Theta(VE)[/Tex] where V is number of vertices and E is number edges (See this). If the graph is complete, the value of E becomes [Tex]\Theta(V^2)[/Tex]. So overall time complexity becomes [Tex]\Theta(V^3)[/Tex]
Question 7
In a weighted graph, assume that the shortest path from a source 's' to a destination 't' is correctly calculated using a shortest path algorithm. Is the following statement true? If we increase weight of every edge by 1, the shortest path always remains same.
A
Yes
B
No
Graph Shortest Paths    
Discuss it


Question 7 Explanation: 
See the following counterexample. There are 4 edges s-a, a-b, b-t and s-t of wights 1, 1, 1 and 4 respectively. The shortest path from s to t is s-a, a-b, b-t. IF we increase weight of every edge by 1, the shortest path changes to s-t.

   1      1     1
s-----a-----b-----t
 \              /
   \          /
     \______/
        4
Question 8
Following statement is true or false?
If we make following changes to  Dijkstra, then it can be used to find 
the longest simple path, assume that the graph is acyclic.

1) Initialize all distances as minus infinite instead of plus infinite.

2) Modify the relax condition in  Dijkstra's algorithm to update distance
  of an adjacent v of the currently considered vertex u only
  if "dist[u]+graph[u][v] > dist[v]". In shortest path algo, 
  the sign is opposite. 
A
True
B
False
Graph Shortest Paths    
Discuss it


Question 8 Explanation: 
In shortest path algo, we pick the minimum distance vertex from the set of vertices for which distance is not finalized yet. And we finalize the distance of the minimum distance vertex. For maximum distance problem, we cannot finalize the distance because there can be a longer path through not yet finalized vertices.
Question 9
Which of the following algorithm can be used to efficiently calculate single source shortest paths in a Directed Acyclic Graph?
A
Dijkstra
B
Bellman-Ford
C
Topological Sort
D
Strongly Connected Component
Graph Shortest Paths    
Discuss it


Question 9 Explanation: 
Using Topological Sort, we can find single source shortest paths in O(V+E) time which is the most efficient algorithm. See following for details. Shortest Path in Directed Acyclic Graph
Question 10
Given a directed graph where weight of every edge is same, we can efficiently find shortest path from a given source to destination using?
A
Breadth First Traversal
B
Dijkstra's Shortest Path Algorithm
C
Neither Breadth First Traversal nor Dijkstra's algorithm can be used
D
Depth First Search
Graph Shortest Paths    
Discuss it


Question 10 Explanation: 
With BFS, we first find explore vertices at one edge distance, then all vertices at 2 edge distance, and so on.
Question 11
Is the following statement valid about shortest paths? Given a graph, suppose we have calculated shortest path from a source to all other vertices. If we modify the graph such that weights of all edges is becomes double of the original weight, then the shortest path remains same only the total weight of path changes.
A
True
B
False
Graph Shortest Paths    
Discuss it


Question 11 Explanation: 
The shortest path remains same. It is like if we change unit of distance from meter to kilo meter, the shortest paths don't change.
Question 12
Match the following
              
Group A                                                Group B
a) Dijkstra's single shortest path algo               p) Dynamic Programming
b) Bellmen Ford's single shortest path algo           q) Backtracking  
c) Floyd Warshell's all pair shortest path algo.      r) Greedy Algorithm
A
a-r, b-q, c-p
B
a-p, b-p, c-p
C
a-r, b-p, c-p
D
a-p, b-r, c-q
Graph Shortest Paths    
Discuss it


Question 12 Explanation: 
Dijkstra is a greedy algorithm where we pick the minimum distant vertex from not yet finalized vertices. Bellman Ford and Floyd Warshell both are Dynamic Programming algorithms where we build the shortest paths in bottom up manner.
Question 13
Is the following statement valid?.

Given a weighted graph where weights of all edges are unique (no two edge have same weights), there is always a unique shortest path from a source to destination in such a graph.
A
True
B
False
Graph Shortest Paths    
Discuss it


Question 13 Explanation: 
There can be more than one paths with same weight. Consider a path with one edge of weight 5 and another path with two edges of weights 2 and 3. Both paths have same weights.
Question 14
Is the following statement valid?.

Given a graph where all edges have positive weights, the shortest paths produced by Dijsktra and Bellman Ford algorithm may be different but path weight would always be same.
A
True
B
False
Graph Shortest Paths    
Discuss it


Question 14 Explanation: 
Dijkstra and Bellman-Ford both work fine for a graph with all positive weights, but they are different algorithms and may pick different edges for shortest paths.
Question 15
Which of the following statement(s)is / are correct regarding Bellman-Ford shortest path algorithm?
P: Always finds a negative weighted cycle, if one exist s.
Q: Finds whether any negative weighted cycle is reachable 
   from the source. 
A
P Only
B
Q Only
C
Both P and Q
D
Neither P nor Q
Graph Shortest Paths    GATE-CS-2009    
Discuss it


Question 15 Explanation: 
Bellman-Ford shortest path algorithm is a single source shortest path algorithm. So it can only find cycles which are reachable from a given source, not any negative weight cycle. Consider a disconnected where a negative weight cycle is not reachable from the source at all. If there is a negative weight cycle, then it will appear in shortest path as the negative weight cycle will always form a shorter path when iterated through the cycle again and again.
Question 16
Let G(V, E) an undirected graph with positive edge weights. Dijkstra's single-source shortest path algorithm can be implemented using the binary heap data structure with time complexity:
A
O(| V |2)
B
O (| E | + | V | log | V |)
C
O (| V | log | V |)
D
O ((| E | + | V |) log | V |)
Graph Shortest Paths    GATE-CS-2005    
Discuss it


Question 17
Let G = (V, E) be an undirected graph with a subgraph G1 = (V1, El). Weights are assigned to edges of G as follows :

GATECS2003Q67 
A single-source shortest path algorithm is executed on the weighted graph (V, E, w) with an arbitrary vertex ν1 of V1 as the source. Which of the following can always be inferred from the path costs computed?
A
The number of edges in the shortest paths from ν1 to all vertices of G
B
G1 is connected
C
V1 forms a clique in G
D
G1 is a tree
Graph Shortest Paths    GATE-CS-2003    
Discuss it


Question 17 Explanation: 
When shortest path shortest path from v1 (one of the vertices in V1) is computed. G1 is connected if the distance from v1 to any other vertex in V1 is greater than 0, otherwise G1 is disconnected.
Question 18
Let G (V, E) be a directed graph with n vertices. A path from vi to vj in G is sequence of vertices (vi, vi+1, ......., vj) such that (vk, vk+1) ∈ E for all k in i through j - 1. A simple path is a path in which no vertex appears more than once. Let A be an n x n array initialized as follow
GATECS2003Q70 
Consider the following algorithm.
for i = 1 to n
   for j = 1 to n
      for k = 1 to n
         A [j , k] = max (A[j, k] (A[j, i] + A [i, k]); 
Which of the following statements is necessarily true for all j and k after terminal of the above algorithm ?
A
A[j, k] ≤ n
B
If A[j, k] ≥ n - 1, then G has a Hamiltonian cycle
C
If there exists a path from j to k, A[j, k] contains the longest path lens from j to k
D
If there exists a path from j to k, every simple path from j to k contain most A[j, k] edges
Graph Shortest Paths    GATE-CS-2003    
Discuss it


Question 18 Explanation: 
In the original input matrix,  A[j , k] is 1 if there
is an edge from j to k, else 0.

Below expression is important to note:

A[j , k] = max(A[j, k] (A[j, i] + A [i, k]);

This expression puts the count of maximum edges on a path from
j to k.  In this expression, we consider every vertex k that can 
become an intermediate vertex and can give longer path.
Question 19
Let G = (V, E) be a simple undirected graph, and s be a particular vertex in it called the source. For x ∈ V, let d(x) denote the shortest distance in G from s to x. A breadth first search (BFS) is performed starting at s. Let T be the resultant BFS tree. If (u, v) is an edge of G that is not in T, then which one of the following CANNOT be the value of d(u) – d(v)?
A
-1
B
0
C
1
D
2
Graph Shortest Paths    GATE-CS-2015 (Set 1)    
Discuss it


Question 19 Explanation: 
Note that the given graph is undirected, so an edge (u, v) also means (v, u) is also an edge. Since a shorter path can always be obtained by using edge (u, v) or (v, u), the difference between d(u) and d(v) can not be more than 1.
Question 20
Let G be a directed graph whose vertex set is the set of numbers from 1 to 100. There is an edge from a vertex i to a vertex j iff either j = i + 1 or j = 3i. The minimum number of edges in a path in G from vertex 1 to vertex 100 is    
A
4
B
7
C
23
D
99
Graph Shortest Paths    Gate IT 2005    
Discuss it


Question 20 Explanation: 
The task is to find minimum number of edges in a path in G from vertex 1 to vertex 100 such that we can move to either i+1 or 3i from a vertex i.
Since the task is to minimize number of edges, 
we would prefer to follow 3*i.  

Let us follow multiple of 3.

1 => 3 => 9 => 27 => 81, now we can't follow multiple
of 3. So we will have to follow i+1. This solution gives
a long path.

What if we begin from end, and we reduce by 1 if 
the value is not multiple of 3, else we divide by 3.
100 => 99 => 33 => 11 => 10 => 9 => 3 => 1

So we need total 7 edges.
Question 21
Consider the weighted undirected graph with 4 vertices, where the weight of edge {i, j} g is given by the entry Wij in the matrix W gt164 The largest possible integer value of x, for which at least one shortest path between some pair of vertices will contain the edge with weight x is ________   Note : This question was asked as Numerical Answer Type.
A
8
B
12
C
10
D
11
Graph Shortest Paths    GATE-CS-2016 (Set 1)    
Discuss it


Question 21 Explanation: 
Let vertices be 0, 1, 2 and 3. x directly connects 2 to 3. The shortest path (excluding x) from 2 to 3 is of weight 12 (2-1-0-3).
Question 22
The Floyd-Warshall algorithm for all-pair shortest paths computation is based on:
A
Greedy paradigm.
B
Divide-and-Conquer paradigm.
C
Dynamic Programming paradigm.
D
neither Greedy nor Divide-and-Conquer nor Dynamic Programming paradigm.
Graph Shortest Paths    GATE-CS-2016 (Set 2)    
Discuss it


Question 22 Explanation: 
Floyd Warshall Algorithm is a Dynamic Programming based algorithm. It finds all pairs shortest paths using following recursive nature of problem. For every pair (i, j) of source and destination vertices respectively, there are two possible cases. 1) k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is. 2) k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i][k] + dist[k][j]. The following figure is taken from the Cormen book. It shows the above optimal substructure property in the all-pairs shortest path problem. floyd-warshell1 Since there are overlapping subproblems in recursion, it uses dynamic programming.
Question 23
Consider a weighted undirected graph with positive edge weights and let uv be an edge in the graph. It is known that the shortest path from the source vertex s to u has weight 53 and the shortest path from s to v has weight 65. Which one of the following statements is always true?
A
weight (u, v) < 12
B
weight (u, v) ≤ 12
C
weight (u, v) > 12
D
weight (u, v) ≥ 12
Graph Shortest Paths    Graph Theory    Gate IT 2007    
Discuss it


Question 23 Explanation: 
2007_3 The minimum weight happens when (S,U) + (U,V) = (S,V) Else (S,U) + (U,V) >= (S,V) Given (S,U) = 53, (S,V) = 65 53 + (U,V) >= 63 (U,V) >= 12. This solution is contributed by Anil Saikrishna Devarasetty
There are 23 questions to complete.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.