{"record":{"id":"4ac14f16683e5174","repo":"TheAlgorithms/C-Sharp","slug":"edge-weight-cannot-be-zero","errorCode":null,"errorMessage":"Edge weight cannot be zero.","messagePattern":"Edge weight cannot be zero\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Graph/DirectedWeightedGraph.cs","lineNumber":206,"sourceCode":"            return adjacencyMatrix[startVertex.Index, endVertex.Index];\n        }\n\n        return 0;\n    }\n\n    private static void ThrowIfNegativeCapacity(int capacity)\n    {\n        if (capacity < 0)\n        {\n            throw new InvalidOperationException(\"Graph capacity should always be a non-negative integer.\");\n        }\n    }\n\n    private static void ThrowIfWeightZero(double weight)\n    {\n        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    }","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Graph/DirectedWeightedGraph.cs#L188-L224","documentation":"AddEdge rejects edges whose weight is exactly 0.0 via ThrowIfWeightZero, because 0.0 is used internally as the sentinel meaning 'no edge exists' in the weight matrix. A zero weight is therefore indistinguishable from a missing edge and is explicitly forbidden.","triggerScenarios":"graph.AddEdge(v1, v2, 0.0) or AddEdge with a computed weight that evaluates to 0.0 (e.g. multiplying by zero, subtracting equal values).","commonSituations":"Modeling unweighted graphs with weight 0 instead of 1, computing weights as differences that cancel to zero, or deserializing edge lists where missing weights default to 0.","solutions":["Use a non-zero weight (e.g. 1.0 for unweighted semantics).","Guard the call: only add the edge when the weight is not 0.0.","If a zero-cost edge is genuinely needed, shift weights (e.g. add 1 to all) or use a different graph structure."],"exampleFix":"// before\ngraph.AddEdge(a, b, weightB - weightA); // both equal -> 0.0 -> throws\n// after\nvar w = weightB - weightA;\nif (w != 0) graph.AddEdge(a, b, w);","handlingStrategy":"validation","validationCode":"static bool IsValidEdgeWeight(double w) => !w.Equals(0.0d);\n// call: if (!IsValidEdgeWeight(w)) throw new ArgumentException(\"zero weight\");","typeGuard":"static bool IsNonZeroWeight(double w) => !w.Equals(0.0d);","tryCatchPattern":"try { graph.AddEdge(u, v, w); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Edge weight cannot be zero.\") { /* skip or remap weight */ }","preventionTips":["Remember 0.0 is the 'no edge' sentinel in this library.","Use 1.0 as the default weight for unweighted semantics.","Validate computed weights before insertion."],"tags":["csharp","graph","weighted-graph","argument-validation"],"backgroundTag":"invalid-argument-value","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"}