{"record":{"id":"6ba65c615a377340","repo":"TheAlgorithms/C-Sharp","slug":"graph-overflow","errorCode":null,"errorMessage":"Graph overflow.","messagePattern":"Graph overflow\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Graph/DirectedWeightedGraph.cs","lineNumber":222,"sourceCode":"        if (weight.Equals(0.0d))\n        {\n            throw new InvalidOperationException(\"Edge weight cannot be zero.\");\n        }\n    }\n\n    private static void ThrowIfEdgeExists(double currentEdgeWeight)\n    {\n        if (!currentEdgeWeight.Equals(0.0d))\n        {\n            throw new InvalidOperationException($\"Vertex already exists: {currentEdgeWeight}\");\n        }\n    }\n\n    private void ThrowIfOverflow()\n    {\n        if (Count == capacity)\n        {\n            throw new InvalidOperationException(\"Graph overflow.\");\n        }\n    }\n\n    private void ThrowIfVertexNotInGraph(Vertex<T> vertex)\n    {\n        if (vertex.Graph != this)\n        {\n            throw new InvalidOperationException($\"Vertex does not belong to graph: {vertex}.\");\n        }\n    }\n}\n","sourceCodeStart":204,"sourceCodeEnd":234,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Graph/DirectedWeightedGraph.cs#L204-L234","documentation":"ThrowIfOverflow guards the fixed-capacity graph: AddVertex throws InvalidOperationException('Graph overflow.') when Count == capacity, i.e. the graph is full and cannot accept another vertex. This is a hard capacity limit, not an auto-growing collection.","triggerScenarios":"Calling AddVertex on a graph whose vertex count already equals the capacity given at construction (e.g. capacity 10 and adding an 11th vertex).","commonSituations":"Underestimating the number of vertices when sizing the graph, loading a dataset larger than the configured capacity, or reusing a full graph instance across batches.","solutions":["Construct the graph with a larger capacity.","Check Count against capacity before calling AddVertex.","Catch InvalidOperationException and rebuild/migrate to a bigger graph."],"exampleFix":"// before\nvar graph = new DirectedWeightedGraph<string>(10);\nforeach (var v in allVertices) graph.AddVertex(v); // throws if allVertices.Count > 10\n// after\nvar graph = new DirectedWeightedGraph<string>(Math.Max(10, allVertices.Count));\nforeach (var v in allVertices) graph.AddVertex(v);","handlingStrategy":"validation","validationCode":"if (graph.Count < capacity) { graph.AddVertex(v); } else { /* resize or reject */ }","typeGuard":"static bool CanAcceptVertex(DirectedWeightedGraph<T> g) => g.Count < g.Capacity; // if capacity exposed","tryCatchPattern":"try { graph.AddVertex(v); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Graph overflow.\") { /* rebuild with larger capacity */ }","preventionTips":["Size capacity from the dataset count up front.","Add headroom (e.g. count * 1.2) for growth.","Track Count before bulk inserts."],"tags":["csharp","graph","capacity","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}