The Gateway to Computer Science Excellence
+31 votes

Which of the following statement(s) is/are correct regarding Bellman-Ford shortest path algorithm?

P: Always finds a negative weighted cycle, if one exists.

Q: Finds whether any negative weighted cycle is reachable from the source.

  1. $P$ only
  2. $Q$ only
  3. Both $P$ and $Q$
  4. Neither $P$ nor $Q$
in Algorithms by Veteran (52.2k points)
edited by | 5.1k views

4 Answers

+33 votes
Best answer

$\underline{\text{Bellman-ford Algorithm}}$

Single source shortest Path $O(VE)$

Relax every edge once in each iteration

$E \times \underbrace{(V-1)} = E.V-E = O(V.E)$

            at max $(V-1)$ edges can be there


$$\begin{array}{|l|l|l|l|l|}\hline  & \textbf{A}  &  \textbf{B}  &  \textbf{C} & \textbf{D}  \\\hline & \text{0} & \text{$\infty$} &\text{$\infty$} & \text{$\infty$}\\ &\text{null} & \text{null} & \text{null} & \text{null}\\\hline \text{i=0}&\text{0} & \text{50} & \text{70} & \text{$\infty$}\\ & \text{X} & \text{A} & \text{A} & \text{X}\\\hline \text{i=1} & \text{0} & \text{2} & \text{70} & \text{53} \\  & \text{X} & \text{C} & \text{A} & \text{B}\\\hline \text{i=2} & \text{0} & \text{2} & \text{70} & \text{5} \\  & \text{X} & \text{C} & \text{A} & \text{B}\\\hline \text{i=3} & \text{0} & \text{2} & \text{70} & \text{5} \\  & \text{X} & \text{C} & \text{A} & \text{B}\\\hline \end{array}$$ 

As we can see that the last step is the verification step. In that step, values remained unchanged. If there was a negative edge weight cycle reachable from source, then at verification step also, those values will be different from the values above.

In case the cycle is not reachable from source then we can see that they will be at $\infty$ distance(or cost) from the source from the beginning till the last step. As take anything away from the $\infty$ it will still be infinite.

But it can also be the case that there are some points which are not forming a cycle and are still unreachable from source, those also will be at $\infty$ distance from the source from the beginning till end.

Hence, we won't be able to make a distinction among the cycle and such vertices. Thus, we say that this algorithm can detect negative edge weight cycles only if they are reachable from the source.

Answer is option B

by Boss (30.8k points)
edited by
Though answer is correct, but iterations in this given example are not correct. in each iteration it covers all edges not only the adjacent ones!
it is directed graph . then where wrong step done ? point that. which step.

@Anirudh look at the line# 3 of the algorithm, in this algorithm first we do n-1 iterations in which we consider all edges, they don't consider adjacent edges only. and at the end we do one more itertion to know if costs still change.

May be he used any other algo than what ur follow.
this algo is direclty copied from coreman! no chances of mistake!!!
Vijay where i said that ur algo is wrong . i said he use somthing other algo.

But sir given graph contains negative value not negative weight cycle.

How option q is true? How it will find any negative weighted cycle. What if a negative weighted cycle is present but unreachable from source vertex.
In this example there is a negative edge weight cycle right(CBC)? Also it is reachable from source.So, here Bellman ford alg should detect it right(i.e, at verification step atleast one value should differ from step at i=2), that didn't happen here. It means this alg failed to detect negative edge weight cycle which is reachable from source. Please correct me if I am wrong
I can't understand one might be silly..

Statement Q says that Bellman ford can find whether any negative weighted cycle is reachable from source or not.

But all the answers here are explaining that Bellman ford can find negative weighted cycle if it is reachable from source. I mean our task is to find if the algo can determine whether the negative cycle(if at all it exists) is connected or disconnected from source.

As this answer have nicely explained that if after the last iteration we find some vertices still at distance infinity we cannot infer whether those vertices were a part of negative cycle or not, this is what Q asks for right?

Am i misinterpreting the question/answer? Please help
where is negative weight cycle in your graph
There is no negative wt cycle in the graph..CBC is positive weight cycle here with weight  80-68 = 12 .. But answer "B" is correct. Correct me if I am wrong
In this graph,there exist -ve weight cycle reachable from source, but still not able to detect -ve weight cycle as all distances not changing at verification step.
so how Q true??




you have stated wrong examples. Negative edge cycle means everytime when you complete one loop, distendi decreases gradually. 

In ur example, ( -1+2 ) will always increase distance by one.



P : If there exist any negative edge cycle but not reachable from source then it cant be detected. 

Q : If there exist a negative cycle which is reachable then it will surely get detected after verification step.


 negative weighted cycle is reachable from the source.

what is meant by this in simple language?

this qsn is testing, how well we can understand a given question.

P says BF always detect -ve weight cycle(regardless of  reachability from source vertex), which is wrong.

Q says BF detects -ve weight cycle if it's reachable from source vertex, which is absolutely true.
+13 votes
Bellman Ford algorithm Find negative weight cycle only when its is reachable from the source vertex. If the Negative weight cycle is disconnected from the source vertices then its constituent vertices are with remain infinite weight till the end iteration of the algotithm. So that negative weitht weight cycle is not detected by the Bellman Ford algorithm. So the correct option is B(Q only)
by Active (2.4k points)
+6 votes
The answer is B.
by Boss (19.9k points)
Can you explain why?
those vertices won't change from $\infty$ to any other value. To detect a cycle we see at verification step which values are still changing
+2 votes

Dijkstra and Bellmon ford algo. have designed for weighted, connected, directed graphs.

Negative weighted cycle means effective weight of the cycle should be negative not even 0, just like the above example, it is a negative weight cycle of (-2)

So option B is correct.

by Active (4.3k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,388 answers
105,415 users