{"record":{"id":"9bb63dbe8a9cb851","repo":"TheAlgorithms/C-Sharp","slug":"detected-the-same-node-twice-confusion-how-this-could-ever","errorCode":null,"errorMessage":"Detected the same node twice. Confusion how this could ever happen","messagePattern":"Detected the same node twice\\. Confusion how this could ever happen","errorType":"exception","errorClass":"PathfindingException","httpStatus":null,"severity":"error","filePath":"Algorithms/Search/AStar/AStar.cs","lineNumber":135,"sourceCode":"                connected.EstimatedCost = connected.CurrentCost + connected.DistanceTo(to);\n                connected.State = NodeState.Open;\n                queue.Enqueue(connected);\n            }\n            else if (current != connected)\n            {\n                // Updating the cost of the node if the current way is cheaper than the previous\n                var newCCost = current.CurrentCost + current.DistanceTo(connected);\n                var newTCost = newCCost + current.EstimatedCost;\n                if (newTCost < connected.TotalCost)\n                {\n                    connected.Parent = current;\n                    connected.CurrentCost = newCCost;\n                }\n            }\n            else\n            {\n                // Codacy made me do it.\n                throw new PathfindingException(\n                    \"Detected the same node twice. Confusion how this could ever happen\");\n            }\n        }\n    }\n}\n","sourceCodeStart":117,"sourceCodeEnd":141,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Search/AStar/AStar.cs#L117-L141","documentation":"AStar's AddOrUpdateConnected throws PathfindingException when it encounters a neighbor node that is neither new nor already tracked in its open/closed sets — i.e. the same node appears twice where the algorithm's bookkeeping expects exactly one entry. This indicates corrupted or inconsistent node/state data rather than a normal pathfinding condition.","triggerScenarios":"A graph whose node equality/hashing is inconsistent (GetHashCode/Equals disagree), duplicate node objects with equal identity, or a neighbor list containing the same node twice so Compute processes it in conflicting states.","commonSituations":"Custom node classes overriding Equals but not GetHashCode (or vice versa); graph with duplicate nodes loaded from data; mutable node keys changing while A* runs.","solutions":["Fix node Equals/GetHashCode so equal nodes hash identically.","Deduplicate the graph's node/neighbor lists before running A*.","Ensure node identity fields are immutable during the search."],"exampleFix":"// before\npublic override bool Equals(object o) => Id == ((Node)o).Id;\n// hashCode not overridden\n// after\npublic override bool Equals(object o) => o is Node n && Id == n.Id;\npublic override int GetHashCode() => Id.GetHashCode();","handlingStrategy":"try-catch","validationCode":"var distinct = graph.Nodes.Distinct().ToList();\nif (distinct.Count != graph.Nodes.Count) throw new InvalidOperationException(\"Duplicate nodes in graph\");","typeGuard":"static bool HasConsistentIdentity<T>(IEnumerable<T> nodes) where T : class =>\n    nodes.All(n => n.GetHashCode() != 0) && nodes.Distinct().Count() == nodes.Count();","tryCatchPattern":"try { var path = AStar.Compute(start, goal); }\ncatch (PathfindingException ex) { logger.LogError(ex, \"A* node bookkeeping violated; check Equals/GetHashCode\"); }","preventionTips":["Always override GetHashCode when overriding Equals on nodes","Keep node identity fields immutable during search","Deduplicate neighbor lists when building the graph"],"tags":["pathfinding","csharp","invariant","graph"],"backgroundTag":"internal-invariant-violation","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"}