From 8440bf9e123d587a37cbb214041ec44adf224f3b Mon Sep 17 00:00:00 2001 From: Ujwal200707 <2400030531@kluniversity.in> Date: Wed, 26 Nov 2025 16:48:21 +0530 Subject: [PATCH] Fix loop range in negative weight cycle detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This line ensures Bellman–Ford performs exactly the required number of relaxations (V–1) for correct shortest-path calculation and efficient negative-cycle detection. --- ...egative Weight Cycle in Graphs - Bellman Ford Algorithm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/07_Graph/Bellman Ford Algorithm/Detect Negative Weight Cycle in Graphs - Bellman Ford Algorithm.py b/07_Graph/Bellman Ford Algorithm/Detect Negative Weight Cycle in Graphs - Bellman Ford Algorithm.py index 26db1cb1..019f9961 100644 --- a/07_Graph/Bellman Ford Algorithm/Detect Negative Weight Cycle in Graphs - Bellman Ford Algorithm.py +++ b/07_Graph/Bellman Ford Algorithm/Detect Negative Weight Cycle in Graphs - Bellman Ford Algorithm.py @@ -7,7 +7,7 @@ def isNegativeWeightCycle(self, n, edges): dist = [2**31] * n dist[0] = 0 - for i in range(n): + for i in range(n - 1): for j in range(len(edges)): frm = edges[j][0] to = edges[j][1] @@ -57,4 +57,4 @@ def isNegativeWeightCycle(self, n, edges): # Time: O(N * E * log(E)) # Space: O(N * E) -''' \ No newline at end of file +'''