{"record":{"id":"ad2f14e4f7283999","repo":"krahets/hello-algo","slug":"graph-vertex-count-has-reached-maximum","errorCode":null,"errorMessage":"Graph vertex count has reached maximum\n","messagePattern":"Graph vertex count has reached maximum\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"en/codes/c/chapter_graph/graph_adjacency_matrix.c","lineNumber":39,"sourceCode":"    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        for (int j = 0; j < MAX_SIZE; j++) {\n            graph->adjMat[i][j] = 0;\n        }\n    }\n    return graph;\n}\n\n/* Destructor */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n    free(graph);\n}\n\n/* Add vertex */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"Graph vertex count has reached maximum\\n\");\n        return;\n    }\n    // Add nth vertex and zero nth row and column\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* Remove vertex */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"Vertex index out of bounds\\n\");\n        return;\n    }\n    // Remove the vertex at index from the vertex list","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/c/chapter_graph/graph_adjacency_matrix.c#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Keep the graph at or below 100 vertices: prune, filter, or partition your data before inserting.","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]).","For graphs that exceed ~100 nodes, switch to the adjacency-list implementation (graph_adjacency_list.c) which is not bounded by a square matrix."],"exampleFix":"// before\nfor (int i = 0; i < 1000; i++) addVertex(graph, i);  // fails silently at i == 100\n\n// after\nfor (int i = 0; i < 1000 && graph->size < MAX_SIZE; i++) addVertex(graph, i);","handlingStrategy":"validation","validationCode":"static inline int graphCanAddVertex(const GraphAdjMat *g) {\n    return g != NULL && g->size < MAX_SIZE;\n}\n\n/* usage */\nif (graphCanAddVertex(graph)) {\n    addVertex(graph, val);\n} else {\n    /* handle cap: prune input, grow MAX_SIZE, or switch to adjacency list */\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat MAX_SIZE as a hard contract; assert(graph->size < MAX_SIZE) in debug builds before each addVertex.","If you expect more than 100 nodes, choose the adjacency-list variant at design time.","Wrap addVertex in a helper that returns a status code (int) instead of void, so capacity failures surface programmatically rather than only on stderr."],"tags":["graph","capacity","adjacency-matrix","fixed-size","c"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}