mission-peace/interview · error · IllegalArgumentException

No vertex in the graph

Error message

No vertex in the graph

What it means

clone() refuses to clone a graph with zero vertices, throwing IllegalArgumentException("No vertex in the graph"). An empty graph is treated as an invalid input rather than producing an empty clone, likely because the algorithm assumes a non-empty vertex set.

Solutions

  1. Populate the graph with vertices before cloning
  2. Skip cloning when the graph is empty and return an empty Graph directly
  3. Guard the call site: if (graph.getAllVertex().isEmpty()) handle separately

Example fix

// before
Graph<T> copy = cloner.clone(maybeEmptyGraph);
// after
Graph<T> copy = maybeEmptyGraph.getAllVertex().isEmpty()
    ? new Graph<>(true)
    : cloner.clone(maybeEmptyGraph);
Defensive patterns

Strategy: validation

Validate before calling

if (graph == null || graph.getAllVertex().isEmpty()) {
    return new Graph<T>(true);
}

Try / catch

try {
    Graph<T> copy = cloner.clone(graph);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("No vertex")) copy = new Graph<>(true);
}

Prevention

When it happens

Trigger: Calling clone(graph) where graph.getAllVertex().size() == 0, e.g. a freshly constructed graph with no addVertex/addEdge calls, or a graph whose vertices were all removed.

Common situations: Cloning before graph construction completed, a deserialization step that produced an empty graph, or filtering that removed every vertex upstream.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of mission-peace/interview@94be5deb0c (2026-09-08). Data as JSON: /api/errors/6695a3dcd5ce6e7b. Report an issue: GitHub.

Appendix: source

Thrown at src/com/interview/graph/CloneDirectedGraph.java:26

 * Test cases
 * Graph is directed/non directed
 * Graph has 0 edges
 * Graph has cycle
 * Graph is linear
 * Graph is dense
 * Graph is sparse
 */
public class CloneDirectedGraph<T> {

    public Graph<T> clone(Graph<T> graph){
        if(graph == null){
            return null;
        }
        if(!graph.isDirected){
            throw new IllegalArgumentException("Cloning non directed graph");
        }
        if(graph.getAllVertex().size() == 0){
            throw new IllegalArgumentException("No vertex in the graph");
        }
        Map<Vertex<T>,Vertex<T>> cloneMap = new HashMap<Vertex<T>,Vertex<T>>();
        for(Vertex<T> vertex : graph.getAllVertex()){
            clone(vertex,cloneMap);
        }
        Graph<T> clonedGraph = new Graph<>(true);
        for(Vertex<T> vertex : cloneMap.values()){
            clonedGraph.addVertex(vertex);
        }
        return clonedGraph;
    }
    
    private void clone(Vertex<T> origVertex,Map<Vertex<T>,Vertex<T>> cloneMap){
        Vertex<T> cloneVertex = null;
        if(cloneMap.containsKey(origVertex)){
            cloneVertex = cloneMap.get(origVertex);
        }else{
            cloneVertex = new Vertex<T>(origVertex.getId()*10);

View on GitHub (pinned to 94be5deb0c)