I started rattling off the obvious stuff about cycles and then kind of stumbled when I realized there are multiple ways a graph fails to be a tree.
Start by defining the necessary and sufficient conditions for a graph to be a tree: connected and acyclic, with exactly n-1 edges. Then explain how to check each condition using algorithms like BFS/DFS for connectivity and cycle detection, or edge counting. Finally, discuss the trade-offs and edge cases.
Pro tip: Mention that for an undirected graph, if it's connected and has exactly n-1 edges, it's automatically a tree; this avoids explicit cycle detection. Also, highlight that for directed graphs, you need to check that the underlying undirected graph is a tree and that there's a unique root with all nodes reachable.
State that a tree is a connected acyclic graph with exactly n-1 edges. Emphasize that all three conditions are equivalent for undirected graphs.
Use BFS or DFS from any node to see if all nodes are visited. If not, the graph is disconnected and thus not a tree.
During BFS/DFS, detect back edges. Alternatively, if the graph is connected, just count edges: if edges != n-1, it's not a tree.
For directed graphs, first check that the underlying undirected graph is a tree, then ensure exactly one node has indegree 0 (root) and all others have indegree 1, and all nodes are reachable from the root.
Mention special cases like empty graph, single node, or multiple components. Note that the checks can be done in O(V+E) time and O(V) space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.