Graph Theory
Single Node Graph
Graph Diameter
Mathematics
Network Analysis

What is the diameter of a graph with just one node?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

For a graph with exactly one node, the diameter is normally 0. The reason is simple: the diameter of a graph is the maximum shortest-path distance between pairs of vertices, and in a one-vertex graph the only relevant distance is the vertex to itself. That distance is zero because no edges need to be traversed.

Start From the Definition

For a graph G, the diameter is usually defined as:

  • take every pair of vertices
  • compute the shortest-path distance between them
  • take the maximum of those distances

In a graph with one vertex v, there is only one meaningful case to consider: v to v.

The shortest path from a node to itself uses zero edges, so:

text
d(v, v) = 0

Therefore the diameter is 0.

Why the Answer Is Not 1

Beginners sometimes wonder whether the diameter could be 1 because there is a node "present" in the graph. But diameter is not counting nodes. It is measuring shortest-path length, typically in edges.

Since you do not traverse any edge to stay at the same node, the distance is zero, not one.

That is the same reason the diagonal entries of a graph distance matrix are zero.

Eccentricity View

Diameter can also be described using eccentricity. The eccentricity of a vertex is the maximum distance from that vertex to any other vertex in the graph.

In the one-node case:

  • the only vertex is v
  • the farthest reachable vertex from v is still v
  • the distance is 0

So the eccentricity of v is 0, and the diameter, which is the maximum eccentricity, is also 0.

Small Example

Consider the graph:

text
V = {A}
E = {}

There is one vertex and no edges.

The only shortest-path value is:

text
d(A, A) = 0

So:

text
diam(G) = 0

This is the standard answer in graph theory.

What About a Self-Loop

Even if the one-node graph has a self-loop, the distance from the node to itself is still considered 0 because the shortest path is the path of length zero. You are already at the destination before traversing any edge.

That matters because shortest-path definitions look for the minimum-length path, not just any existing cycle.

So the presence of a self-loop does not change the usual diameter answer.

Connectedness and Convention

Diameter questions become trickier for disconnected graphs, because some distances are infinite or undefined depending on the convention being used. But a single-node graph is connected in the ordinary graph-theory sense, so that ambiguity does not arise here.

That is one reason this is a clean base case in proofs and algorithm discussions.

Why This Case Matters

The one-node graph may feel trivial, but it is an important edge case in graph algorithms. If an implementation computes diameter, radius, or eccentricity, it should handle the single-vertex case cleanly.

For example, a BFS-based diameter routine should not return an error or -1 for a graph that clearly has a well-defined diameter of zero.

Edge cases like this matter in:

  • graph libraries
  • unit tests
  • algorithm proofs by induction
  • reasoning about base cases in recursive graph procedures

Common Pitfalls

The most common mistake is confusing path length with node count. Diameter is based on shortest-path length, usually counted in edges.

Another issue is overthinking self-loops. A self-loop does not make the shortest distance from a node to itself become 1, because the zero-length path is shorter.

Developers also sometimes import disconnected-graph conventions into the single-node case unnecessarily. For one node, the standard answer is straightforward and finite.

Finally, if you are implementing graph metrics in code, do not forget this base case. It is small, but it is still a real graph.

Summary

  • The diameter of a graph with one node is normally 0.
  • The only shortest-path distance is the node to itself, which is zero.
  • Diameter measures edge distance, not the number of vertices.
  • A self-loop does not change the answer because the zero-length path is still shortest.
  • This is an important base case for graph-theory reasoning and graph algorithms.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.