{"record":{"id":"37f286c92018083b","repo":"TheAlgorithms/C-Sharp","slug":"vertex-does-not-belong-to-graph-vertex","errorCode":null,"errorMessage":"Vertex does not belong to graph: {vertex}.","messagePattern":"Vertex does not belong to graph: (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Graph/DirectedWeightedGraph.cs","lineNumber":230,"sourceCode":"        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":212,"sourceCodeEnd":234,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Graph/DirectedWeightedGraph.cs#L212-L234","documentation":"ThrowIfVertexNotInGraph verifies vertex.Graph == this before any graph operation (AddEdge, RemoveVertex, RemoveEdge, GetNeighbors, AreAdjacent) and throws InvalidOperationException if the Vertex instance belongs to a different graph (or none). Vertices are graph-bound objects, not plain values.","triggerScenarios":"Manually constructed new Vertex<T>(...) passed to graph methods, or using a vertex from graphA with graphB: graphB.AddEdge(vertexFromA, other, w).","commonSituations":"Copying vertices between graphs, caching Vertex instances across graph rebuilds, deserializing vertices without re-attaching them to the target graph.","solutions":["Always obtain vertices via the target graph's AddVertex return value.","Verify vertex.Graph == graph before operating, or re-add the vertex to the intended graph.","Catch InvalidOperationException to detect cross-graph misuse in generic code."],"exampleFix":"// before\nvar v = new Vertex<string>(\"a\");\ngraph.AddEdge(v, other, 1.0); // throws: v belongs to no graph\n// after\nvar v = graph.AddVertex(\"a\");\ngraph.AddEdge(v, other, 1.0);","handlingStrategy":"validation","validationCode":"static bool BelongsToGraph<T>(Vertex<T> v, DirectedWeightedGraph<T> g) => ReferenceEquals(v.Graph, g);\n// call: if (!BelongsToGraph(v, graph)) v = graph.AddVertex(v.Value);","typeGuard":"static bool IsInGraph<T>(Vertex<T> v, DirectedWeightedGraph<T> g) => ReferenceEquals(v?.Graph, g);","tryCatchPattern":"try { graph.AddEdge(u, v, w); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Vertex does not belong to graph\")) { /* re-add vertices to this graph */ }","preventionTips":["Only use Vertex instances returned by this graph's AddVertex.","Never construct Vertex<T> manually for graph operations.","Rebuild cached vertices when the graph instance is recreated."],"tags":["csharp","graph","object-identity","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}