Checking a Tree from Its Data
A graph may be given as a vertex set and an edge list, with no drawing at all. To verify that it is a tree, we need to check the graph itself rather than rely on how a sketch looks. The definition gives two separate requirements: the graph must be connected, and it must be acyclic. An edge-by-edge procedure can check both at once by tracking which vertices are connected using the edges seen so far.
Throughout, graphs are finite and simple. Thus an edge \(uv\) joins two distinct vertices, and there are no multiple edges. The vertex set must be recorded in full: a vertex absent from the edge list is still a vertex of the graph, and is isolated. A path is a sequence of distinct vertices in which consecutive vertices are adjacent. A graph is connected if every pair of its vertices is joined by a path. A cycle is a closed path of length at least three, and a graph is acyclic if it contains no cycle.
The key practical question is how an edge can affect the component structure. Start with each vertex in its own component, as no edges have yet been processed. When an edge is added, either its endpoints belong to different components, in which case those components merge, or they already belong to the same component, in which case the new edge closes a cycle. This gives a direct test for acyclicity and, at the same time, a way to track connectivity.
Why the Edge-by-Edge Check Works
Proof. Suppose first that \(u\) and \(v\) are connected in \(H\). There is a path in \(H\) from \(u\) to \(v\). That path has at least two edges, because \(uv\) is not already an edge and the graph is simple. Adding \(uv\) closes the path into a cycle.
Conversely, suppose adding \(uv\) creates a cycle. The cycle must use the new edge \(uv\), since \(H\) was acyclic. Removing \(uv\) from that cycle leaves a path in \(H\) from \(u\) to \(v\). Thus \(u\) and \(v\) were connected in \(H\). Both implications hold, so the stated test is valid. \(\square\)
Process the edges in any fixed order. At each stage, record the components formed by the processed edges. If the new edge joins different components, merge them. If its endpoints are already in the same component, record that a cycle has been found; the edge does not change the components.
Proof. We first justify the component records. After any initial segment of the edge list has been processed, two vertices belong to the same recorded component exactly when a path between them uses only processed edges. This holds before processing any edges: each vertex is a separate component, and there is no path between distinct vertices. Now process one more edge \(uv\). If \(u\) and \(v\) are in different components, the new edge joins them, and any new path between the components must use this edge. Merging the components therefore records precisely the new reachability. If \(u\) and \(v\) are already in the same component, the edge creates no connection between previously separate components. The records remain correct. This proves the claim at every stage, so the final records are the connected components of the whole graph. Since the vertex set is nonempty, the graph is connected exactly when there is one such component.
For the cycle check, the graph with no processed edges is acyclic. Suppose the edges processed so far form an acyclic graph. By the Lemma, adding the next edge creates a cycle exactly when its endpoints already lie in the same component. If they lie in different components, the enlarged graph remains acyclic; if they lie in the same component, a cycle has been found. In particular, if a cycle exists in the full graph, consider the last edge of that cycle to be processed. The other edges of the cycle already form a path between its endpoints, so the procedure records a cycle at that step. Thus the procedure records a cycle exactly when one exists. Combining the connectivity and acyclicity checks with the definition of a tree proves the result. \(\square\)
Worked Checks from Explicit Edge Lists
Worked Example: A Tree with No Drawing
Let \(G=(V,E)\), where \(V=\{a,b,c,d,e,f\}\) and \(E=\{ab,ac,bd,ce,ef\}\). Process the edges in the displayed order, beginning with the separate components \(\{a\},\{b\},\{c\},\{d\},\{e\},\{f\}\).
| Edge processed | Component update | Cycle recorded? |
|---|---|---|
| \(ab\) | Merge \(\{a\}\) and \(\{b\}\), giving \(\{a,b\}\). | No; the endpoints were in different components. |
| \(ac\) | Merge \(\{a,b\}\) and \(\{c\}\), giving \(\{a,b,c\}\). | No; the endpoints were in different components. |
| \(bd\) | Merge \(\{a,b,c\}\) and \(\{d\}\), giving \(\{a,b,c,d\}\). | No; the endpoints were in different components. |
| \(ce\) | Merge \(\{a,b,c,d\}\) and \(\{e\}\), giving \(\{a,b,c,d,e\}\). | No; the endpoints were in different components. |
| \(ef\) | Merge \(\{a,b,c,d,e\}\) and \(\{f\}\), giving \(\{a,b,c,d,e,f\}\). | No; the endpoints were in different components. |
At the end, all listed vertices are in one component, and no edge joined vertices already in the same component. The graph is therefore connected and acyclic, hence a tree.
Check: Starting at \(a\), the listed edges give paths to every vertex: \(a\) reaches \(b\) and \(c\) directly, \(d\) by \(a,b,d\), \(e\) by \(a,c,e\), and \(f\) by \(a,c,e,f\). This independently confirms connectivity. The edge-by-edge cycle test found no cycle, which verifies the other required condition.
Worked Example: Connected, but Not a Tree
Let \(G=(V,E)\), where \(V=\{1,2,3,4,5\}\) and \(E=\{12,23,31,34,45\}\). Begin with the singleton components \(\{1\},\{2\},\{3\},\{4\},\{5\}\).
| Edge processed | Component update | Cycle recorded? |
|---|---|---|
| \(12\) | Merge \(\{1\}\) and \(\{2\}\), giving \(\{1,2\}\). | No; the endpoints were in different components. |
| \(23\) | Merge \(\{1,2\}\) and \(\{3\}\), giving \(\{1,2,3\}\). | No; the endpoints were in different components. |
| \(31\) | The component remains \(\{1,2,3\}\). | Yes; both endpoints were already in \(\{1,2,3\}\). |
| \(34\) | Merge \(\{1,2,3\}\) and \(\{4\}\), giving \(\{1,2,3,4\}\). | No; the endpoints were in different components. |
| \(45\) | Merge \(\{1,2,3,4\}\) and \(\{5\}\), giving \(\{1,2,3,4,5\}\). | No; the endpoints were in different components. |
The final component contains every vertex, so the graph is connected. But when \(31\) is processed, the earlier edges \(12\) and \(23\) already provide a path from \(3\) to \(1\). The edge \(31\) closes the cycle \(1,2,3,1\). The graph is not acyclic and therefore is not a tree.
Check: The cycle uses exactly the listed edges \(12,23,31\), and its vertices \(1,2,3\) are distinct before returning to \(1\). The remaining listed edges extend from that cycle to \(4\) and \(5\), so every vertex is connected to the same component. The graph fails the tree test only because it has a cycle.
Worked Example: An Isolated Vertex Matters
Let \(G=(V,E)\), where \(V=\{p,q,r,s,t,u\}\) and \(E=\{pq,qr,st\}\). Start with all six singleton components.
| Edge processed | Component update | Cycle recorded? |
|---|---|---|
| \(pq\) | Merge \(\{p\}\) and \(\{q\}\), giving \(\{p,q\}\). | No; the endpoints were in different components. |
| \(qr\) | Merge \(\{p,q\}\) and \(\{r\}\), giving \(\{p,q,r\}\). | No; the endpoints were in different components. |
| \(st\) | Merge \(\{s\}\) and \(\{t\}\), giving \(\{s,t\}\). | No; the endpoints were in different components. |
The final components are \(\{p,q,r\}\), \(\{s,t\}\), and \(\{u\}\). The last component is a singleton because \(u\) appears in \(V\) but in no edge of \(E\). No cycle was recorded, so the graph is acyclic; it is not connected, because its vertices do not all lie in one component. It is a forest, not a tree.
Check: The edge list gives a path between \(p\) and \(r\), and a path between \(s\) and \(t\), but gives no path from \(u\) to any other vertex. The component records include every vertex in \(V\), confirming that the isolated vertex cannot be omitted when checking connectivity.
Common Pitfall: A Cycle Can Be Hidden in the Edge List
A list of distinct edges is not necessarily acyclic. For example, in the second worked example, none of the listed edges is repeated. That fact does not prevent the third edge from closing a cycle. After processing \(12\) and \(23\), the current component is \(\{1,2,3\}\), because those edges give the path \(1,2,3\). The next edge is \(31\), so both endpoints already belong to that component.
Wrong check: “No edge is repeated, so there is no cycle.” This checks whether the edge list has duplicates, not whether the graph has a cycle. Correction: Before adding each edge, check whether its endpoints are already connected using the earlier edges. Here \(3\) and \(1\) are already connected by \(3,2,1\), so adding \(31\) closes a cycle.
There is a second common oversight: checking whether the vertices that appear in edges are connected, while forgetting a vertex explicitly listed in \(V\). The component procedure avoids this error by starting with every vertex, including isolated ones. To verify a tree from an edge list, retain the complete vertex set, track component merges edge by edge, and require both a single final component and no recorded cycle.
Check Your Understanding
For each graph, use its complete vertex set and process the edges in the order shown. State whether it is connected, acyclic, and a tree.
- Let \(V=\{a,b,c,d\}\) and \(E=\{ab,bc,cd\}\). Record the component after each edge.
- Let \(V=\{1,2,3,4\}\) and \(E=\{12,23,31,34\}\). Identify the first edge that records a cycle and explain why.
- Let \(V=\{p,q,r,s\}\) and \(E=\{pq,rs\}\). List all final components, including any singleton components.
- Explain why the endpoints of an edge that closes a cycle must already be connected by earlier edges.
- Give the two conditions that must both hold for a nonempty graph to be a tree.