# 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
Graph Traversals
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
Graph Traversals
Discuss it

Question 2 Explanation:
 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```
Graph Traversals
Discuss it

Question 3 Explanation:
 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 A MNOPQR B NQMPOR C QMNPRO D QMNPOR
Graph Traversals
Discuss it

Question 4 Explanation:
Option (A) is MNOPQR. It cannot be a BFS as the traversal starts with M, but O is visited before N and Q. In BFS all adjacent must be visited before adjacent of adjacent. Option (B) is NQMPOR. It also cannot be BFS, because here, P is visited before O. (C) and (D) match up to QMNP. We see that M was added to the queue before N and P (because M comes before NP in QMNP). Because R is M's neighbor, it gets added to the queue before the neighbor of N and P (which is O). Thus, R is visited before O.
 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
Graph Traversals
Discuss it

Question 5 Explanation:
```In DFS, if 'v' is visited
after 'u', then one of the following is true.
1) (u, v) is an edge.
u
/
v     w
/     /
x     y   z

2) 'u' is a leaf node.
w
/
x     v
/     /
u     y   z ```
In DFS, after visiting a node, we first recur for all unvisited children. If there are no unvisited children (u is leaf), then control goes back to parent and parent then visits next unvisited children.
 Question 6
Consider the following graph, 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?
 A I, II and IV only B I and IV only C II, III and IV only D I, III and IV only
Graph Traversals
Discuss it

Question 6 Explanation:
We can check all DFSs for following properties.
```In DFS, if a vertex 'v' is visited
after 'u', then one of the following is true.
1) (u, v) is an edge.
u
/
v     w
/     /
x     y   z

2) 'u' is a leaf node.
w
/
x     v
/     /
u     y   z```
In DFS, after visiting a node, we first recur for all unvisited children. If there are no unvisited children (u is leaf), then control goes back to parent and parent then visits next unvisited children.
 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
Graph Traversals
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
Graph Traversals
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
Graph Traversals
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
Graph Traversals
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.
There are 21 questions to complete.