Determining whether or not a directed or undirected graph is a tree
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Understanding Graphs and Trees
In graph theory, distinguishing between different types of graphs like trees and acyclic graphs is crucial. Let's delve into how to determine whether a directed or undirected graph is a tree. For ease of understanding, we’ll break this down step by step.
What Is a Graph?
A graph is a collection of nodes, also known as vertices, and edges connecting these nodes. Graphs are classified based on the direction of their edges:
- Directed Graph (Digraph): Every edge has a direction, from one vertex to another.
- Undirected Graph: Edges have no direction. They are bi-directional by nature.
What Is a Tree?
A tree is a special type of graph with the following properties:
- Connected: There's a path between any two vertices.
- Acyclic: It does not contain any cycles.
- n vertices, n-1 edges: In an undirected graph, for 'n' vertices, a tree will have 'n-1' edges.
Trees in Directed Graphs
A rooted tree in a directed graph, often called a directed tree, has these additional properties:
- Single Root Node: One node serves as the root from which all other nodes are reachable.
- Unique Paths: There’s exactly one path from the root to any other vertex in the graph.
Determining if a Graph is a Tree
For Undirected Graphs
- Acyclic Nature:
- If a graph contains a cycle, it cannot be a tree.
- Connectivity Check:
- Perform a Depth-First Search (DFS) or Breadth-First Search (BFS) to ensure all nodes are reachable from any starting node.
- Edge Count:
- If the graph has `n` vertices and exactly `n-1` edges, it might be a tree.
For Directed Graphs
Verifying tree properties in a directed graph involves:
- Single Root:
- Find a vertex with no incoming edges (In-degree of 0). This would be the potential root.
- Reachability:
- Ensure all nodes are reachable from the root. This can be done using DFS or BFS starting from the root node.
- Uniqueness of Paths:
- Verify that there is a unique path from the root to every other vertex.
Example and Explanation
Consider an undirected graph with the following edges:
- A-B
- A-C
- B-D
- C-E
- Vertices: 5 (A, B, C, D, E)
- Edges: 4
Steps to determine if it's a tree:
- Check Edge Count: 4 edges for 5 vertices (n-1 condition holds).
- Acyclic: No cycles present.
- Connected: From any vertex, all others are reachable.
This graph is a tree.
For a directed graph with edges:
- A ➔ B
- A ➔ C
- B ➔ D
- C ➔ E
From A, you can reach all other vertices with a unique path, making it a directed tree.
Summary Table
| Graph Type | Key Properties | Tree Verification Steps |
| Undirected | Connected, Acyclic, n-1 edges for n vertices | 1. Check for no cycles. 2. Ensure connectivity. 3. Validate n-1 edge count. |
| Directed | Rooted, Reachable, Unique paths | 1. Identify a single root with in-degree 0. 2. Verify all nodes can be reached from root. 3. Confirm unique paths from root. |
Additional Details
- Weighted Graphs: The definition does not change with weights. Trees can include weighted edges, but weights aren’t used to determine tree validity.
- Directed Acyclic Graphs (DAGs): While acyclic, DAGs without a single root or proper reachability do not qualify as trees.
Understanding these properties and verification techniques can significantly aid in the analysis and application of graphs in areas like network design, algorithm development, and more.
Related reading
- Determining which integer is closest to the kth root of n without using floating point arithmetic?
- deterministic and reversible permutation function to store a message
- DFS Algorithm Traversal
- Diagram connector algorithm
- Dictionaries and default values
- Dictionary - one key, many values
- Diamond-Square implementation produces too high values
- Diamond square algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.