krahets/hello-algo · error

Graph vertex count has reached maximum

Error message

Graph vertex count has reached maximum

What it means

Emitted by addVertex() when the adjacency-matrix graph already holds MAX_SIZE vertices. MAX_SIZE is a compile-time constant (#define MAX_SIZE 100), so both vertices[] and the adjMat[MAX_SIZE][MAX_SIZE] array are fixed-size — this is an illustrative teaching structure, not a dynamically-resizing container. The message is written to stderr and the function returns void, so the caller receives no status code; the vertex is simply not inserted.

Source

Thrown at en/codes/c/chapter_graph/graph_adjacency_matrix.c:39

    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));
    graph->size = 0;
    for (int i = 0; i < MAX_SIZE; i++) {
        for (int j = 0; j < MAX_SIZE; j++) {
            graph->adjMat[i][j] = 0;
        }
    }
    return graph;
}

/* Destructor */
void delGraphAdjMat(GraphAdjMat *graph) {
    free(graph);
}

/* Add vertex */
void addVertex(GraphAdjMat *graph, int val) {
    if (graph->size == MAX_SIZE) {
        fprintf(stderr, "Graph vertex count has reached maximum\n");
        return;
    }
    // Add nth vertex and zero nth row and column
    int n = graph->size;
    graph->vertices[n] = val;
    for (int i = 0; i <= n; i++) {
        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;
    }
    graph->size++;
}

/* Remove vertex */
void removeVertex(GraphAdjMat *graph, int index) {
    if (index < 0 || index >= graph->size) {
        fprintf(stderr, "Vertex index out of bounds\n");
        return;
    }
    // Remove the vertex at index from the vertex list

View on GitHub (pinned to 69932aed18)

Solutions

  1. Keep the graph at or below 100 vertices: prune, filter, or partition your data before inserting.
  2. Raise the compile-time cap by changing #define MAX_SIZE 100 to a larger value and recompiling — note memory cost grows quadratically (vertices[MAX_SIZE] + adjMat[MAX_SIZE][MAX_SIZE]).
  3. For graphs that exceed ~100 nodes, switch to the adjacency-list implementation (graph_adjacency_list.c) which is not bounded by a square matrix.

Example fix

// before
for (int i = 0; i < 1000; i++) addVertex(graph, i);  // fails silently at i == 100

// after
for (int i = 0; i < 1000 && graph->size < MAX_SIZE; i++) addVertex(graph, i);
Defensive patterns

Strategy: validation

Validate before calling

static inline int graphCanAddVertex(const GraphAdjMat *g) {
    return g != NULL && g->size < MAX_SIZE;
}

/* usage */
if (graphCanAddVertex(graph)) {
    addVertex(graph, val);
} else {
    /* handle cap: prune input, grow MAX_SIZE, or switch to adjacency list */
}

Prevention

When it happens

Trigger: Calling addVertex(graph, val) when graph->size == 100 — i.e. after 100 successful insertions, or on a graph that was prefilled to capacity. Any 101st insertion hits this branch.

Common situations: Loading a dataset larger than 100 nodes into the teaching graph; benchmark/stress tests that exceed the illustrative cap; assuming the graph grows like std::vector or a Java ArrayList when it is actually fixed-size.

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/ad2f14e4f7283999. Report an issue: GitHub.