Given below are two statements: one is labelled as Assertion A and the other is labelled as Reason R Assertion A: Depth first search can be used to perform a topological sort of a directed acyclic graph. Reason R: A topological sort a directed acyclic graph G =(V, E) is a linear ordering of its vertices such that if G contain an edge (u, v) then u appears before v in the ordering. In the light of the above statements, choose the most appropriate answer from the options given below
Assertion A states that Depth First Search (DFS) can be used for topological sorting of a Directed Acyclic Graph (DAG). This is correct. DFS explores a graph by going as deep as possible along each branch before backtracking. In a DAG, the nodes can be topologically sorted by performing a DFS and recording the nodes in the reverse order of their finishing times (i.e., the order in which the recursive calls return).
Reason R correctly defines a topological sort. It is a linear ordering of vertices in a DAG such that for every directed edge from vertex u to vertex v, vertex u comes before vertex v in the ordering. This definition is fundamental to understanding topological sorting.
Reason R provides the definition of a topological sort. Assertion A claims DFS can achieve this. The DFS algorithm naturally produces an ordering consistent with the definition in Reason R. When DFS finishes exploring a node, it means all its descendants have been visited and finished. Placing the node at this point (or rather, its reverse finishing time) ensures it comes before its descendants in the final sort, satisfying the condition stated in Reason R. Therefore, Reason R is the correct explanation for Assertion A.
Both Assertion A and Reason R are correct statements, and Reason R correctly explains why DFS is a suitable algorithm for performing a topological sort on a DAG.
Which traversal algorithm is typically implemented using a stack data structure?
Consider the following algorithm someAlgo that takes an undirected graph G as input.
someAlgo (G)
1. Let $v$ be any vertex in G. Run BFS on G starting at $v$. Let $u$ be a vertex in G at maximum distance from $v$ as given by the BFS.
2. Run BFS on G again with $u$ as the starting vertex. Let $z$ be the vertex at maximum distance from $u$ as given by the BFS.
3. Output the distance between $u$ and $z$ in G.
The output of someAlgo (T) for the tree shown in the given figure is ____________ (Answer in integer)

Which of the following statements about DFS are correct?
A. It can detect cycles in a graph
B. It can be used to find connected components.
C. It works for both directed and undirected graph.
D. Guarantees shortest path in unweighted graphs.
Choose the correct answer from the options given below: