The Wayback Machine - https://web.archive.org/web/20140920212505/http://geeksquiz.com:80/algorithms/graph-traversals/

GeeksQuiz

Computer science mock tests for geeks

Graph Traversals

Question 1
Which of the following algorithms can be used to most efficiently determine the presence of a cycle in a given graph ?
A
Depth First Search
B
Breadth First Search
C
Prim's Minimum Spanning Tree Algorithm
D
Kruskal' Minimum Spanning Tree Algorithm

Discuss it


Question 2
Traversal of a graph is different from tree because
A
There can be a loop in graph so we must maintain a visited flag for every vertex
B
DFS of a graph uses stack, but inorrder traversal of a tree is recursive
C
BFS of a graph uses queue, but a time efficient BFS of a tree is recursive.
D
All of the above

Discuss it


Question 3
What are the appropriate data structures for following algorithms?
 1) Breadth First Search                           2) Depth First Search                            3) Prim's Minimum Spanning Tree 4) Kruskal' Minimum Spanning Tree 
A
 1) Stack 2) Queue 3) Priority Queue 4) Union Find
B
 1) Queue 2) Stack 3) Priority Queue 4) Union Find 
C
 1) Stack 2) Queue 3) Union Find 4) Priority Queue 
D
 1) Priority Queue 2) Queue 3) Stack 4) Union Find

Discuss it


Question 4
The Breadth First Search algorithm has been implemented using the queue data structure. One possible order of visiting the nodes of the following graph is gate_2008
A
MNOPQR
B
NQMPOR
C
QMNPRO
D
QMNPOR

Discuss it


Question 5
Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting depth-first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited after visiting u in the traversal. Which of the following statements is always true? (GATE CS 2000)
A
{u,v} must be an edge in G, and u is a descendant of v in T
B
{u,v} must be an edge in G, and v is a descendant of u in T
C
If {u,v} is not an edge in G then u is a leaf in T
D
If {u,v} is not an edge in G then u and v must have the same parent in T

Discuss it


Question 6
Consider the following graph grap2003
Among the following sequences
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 Which are depth first traversals of the above graph? (GATE CS 2003)
A
I, II and IV only
B
I and IV only
C
II, III and IV only
D
I, III and IV only

Discuss it


Question 7
Make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program. Which of the following standard graph algorithms is used by Make.
A
Strongly Connected Components
B
Topological Sorting
C
Breadth First Search
D
Dijkstra's Shortest Path

Discuss it


Question 7 Explanation: 
Make can decide the order of building a software using topological sorting. Topological sorting produces the order considering all dependencies provide by makefile. See following for details. Topological Sorting
Question 8
Given two vertices in a graph s and t, which of the two traversals (BFS and DFS) can be used to find if there is path from s to t?
A
Only BFS
B
Only DFS
C
Both BFS and DFS
D
Neither BFS nor DFS

Discuss it


Question 8 Explanation: 
We can use both traversals to find if there is a path from s to t.
Question 9
Which of the following condition is sufficient to detect cycle in a directed graph?
A
There is an edge from currently being visited node to an already visited node.
B
There is an edge from currently being visited node to an ancestor of currently visited node in DFS forest.
C
Every node is seen twice in DFS.
D
None of the bove

Discuss it


Question 9 Explanation: 
Question 10
Is following statement true/false If a DFS of a directed graph contains a back edge, any other DFS of the same graph will also contain at least one back edge. Source: http://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-s2009-sol.pdf
A
True
B
False

Discuss it


Question 10 Explanation: 
A back edge means a cycle in graph. So if there is a cycle, all DFS traversals would contain at least one back edge.
Question 11
Is following statement true/false? A DFS of a directed graph always produces the same number of tree edges, i.e., independent of the order in which vertices are considered for DFS. (Source http://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-f2008-sol.pdf)
A
True
B
False

Discuss it


Question 11 Explanation: 
Consider the following graph. If we start from 'a', then there is one tree edge. If we start from 'b', then there is no tree edge. a---->b
Question 12
If the DFS finishing time f[u] > f[v] for two vertices u and v in a directed graph G, and u and v are in the same DFS tree in the DFS forest, then u is an ancestor of v in the depth first tree. (Source http://courses.csail.mit.edu/6.006/oldquizzes/solutions/q2-f2007-sol.pdf)
A
True
B
False

Discuss it


Question 12 Explanation: 
In a graph with three nodes, r u and v, with edges (r; u) and (r; v), and r is the starting point for the DFS, u and v are siblings in the DFS tree, neither as the ancestor of the other.
Question 13
Consider the DAG with Consider V = {1, 2, 3, 4, 5, 6}, shown below. Which of the following is NOT a topological ordering? Q5
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

Discuss it


Question 13 Explanation: 
1 appears after 2 and 3 which is not possible in Topological Sorting.
Question 14
Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time on Depth First Search of G? Assume that the graph is represented using adjacency matrix.
A
O(n)
B
O(m+n)
C
O(n2)
D
O(mn)

Discuss it


Question 14 Explanation: 
Depth First Search of a graph takes O(m+n) time when the graph is represented using adjacency list. In adjacency matrix representation, graph is represented as an "n x n" matrix. To do DFS, for every vertex, we traverse the row corresponding to that vertex to find all adjacent vertices (In adjacency list representation we traverse only the adjacent vertices of the vertex). Therefore time complexity becomes O(n2)
Question 15
Consider the tree arcs of a BFS traversal from a source node W in an unweighted, connected, undirected graph. The tree T formed by the tree arcs is a data structure for computing.
A
the shortest path between every pair of vertices.
B
the shortest path from W to every vertex in the graph.
C
the shortest paths from W to only those nodes that are leaves of T.
D
the longest path in the graph

Discuss it


Question 15 Explanation: 
BFS always produces shortest path from source to all other vertices in an unweighted graph.
Question 16
Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume that a recursive call to visit a vertex is made only after first checking that the vertex has not been visited earlier. Then the maximum possible recursion depth (including the initial call) is _________. GATECS2014Q20
A
17
B
18
C
19
D
20

Discuss it


Question 16 Explanation: 
The following diagram shows the worst case situation where the recursion tree has maximum depth. GATECS2014Q20Sol So the recursion depth is 19 (including the first node).
Question 17
Let T be a depth first search tree in an undirected graph G. Vertices u and n are leaves of this tree T. The degrees of both u and n in G are at least 2. which one of the following statements is true?
A
There must exist a vertex w adjacent to both u and n in G
B
There must exist a vertex w whose removal disconnects u and n in G
C
There must exist a cycle in G containing u and n
D
There must exist a cycle in G containing u and all its neighbours in G.

Discuss it


Question 17 Explanation: 
Since they are leaves in DFS, they must be separable by a vertex. See Articulation Points in a graph.
There are 17 questions to complete.


    

close