5

For example,

Let's say

1->2 costs 100
2->4 costs 600

So 1->2->4 costs 700

What if there was an edge from 4 to 3 costing -500 ? And a different edge from 3 to 4 costing 200

4->3 costs -500
3->4 costs 200

So 1->2->4->3->4 costs 400

Which is less than 700

So is 1->2->4->3->4 considered a shorter path than 1->2->4 ???

I understand that no cycles are allowed, this is an example of a path with no repeating edges.

What about vertices? If they repeat is it an allowable cycle in Floyd Warhsall?

Because I know there's different types of algorithms, one which allows cycles of one kind and disallows cycles from other kinds.

Can someone explain this to me? Answer the question of, is 1->2->4->3->4 considered a shorter path than 1->2->4 ???

Thank you all in advance.

Edit:

Here's a picture, showing you don't have to visit the same edge twice.

enter image description here

ABCD123
  • 137
  • 1
  • 6
  • 1
    The wikipedia page says it can't have any negative cycles, which your example does. 4->3->4 is a cycle, right? – aardvarkk Dec 02 '14 at 21:32
  • Probably a better site to ask on would be [programmers.stackexchange.com](http://programmers.stackexchange.com) – outlyer Dec 02 '14 at 21:35
  • outlyer, I've asked on that site and was warned for it. – ABCD123 Dec 02 '14 at 22:07
  • @outlyer if you believe a question would be better addressed by another stack exchange site, please flag it for migration and explain why rather than suggesting reposting. Just reposting the same question on multiple sites (especially when there are answers *here*) can make it harder for the next person searching for floyd-warshall and finding the answers scattered around multiple sites. –  Dec 03 '14 at 04:05
  • @MichaelT thank you for the suggestion, I wasn't sure about the policy with regards to migration flags – outlyer Dec 03 '14 at 04:09
  • @outlyer If the original poster of the question flags it for migration and it is a good question (this one is reasonable) and it is on topic on the target site (it would likely be on topic on P.SE or [CS.SE](http://cs.stackexchange.com) (if you wanted to go that way instead)), a mod will honor the flag and move it. You need to think about how you want it answered - do you want it to be answered by more 'architecty' types (P.SE), academic types (CS.SE), or coder types (SO) - though thats a bit of an over simplification. The important thing is to bring along the answers to wherever it goes. –  Dec 03 '14 at 04:15
  • @MichaelT thanks again for the helpful detailed explanation – outlyer Dec 03 '14 at 04:28

2 Answers2

3

The Floyd–Warshall algorithm requires a graph with no negative cycles. In your example, 4->3->4 is a negative cycle because the sum of the weights over the cycle is -500 + 200 = -300.

aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • Yes, but what kind of cycle? Repeating edges or repeating vertices? Or both? And what does Floyd Warshall algorithm say of negative edges? Let's say there's no cycles, but there's one negative edge, is that ok? Does Floyd Warshall also disallow negative edges? – ABCD123 Dec 02 '14 at 21:39
  • 1
    A graph that contains cycles is a property of the underlying graph itself, not of a path you happen to take through the graph. A graph contains a negative cycle if it's *possible* to take a path such that the sum of the weights along the path is less than zero. In your case shown above, the underlying graph contains a cycle 4->3->4. If the underlying graph didn't contain the directed edge 4->3, for instance, then you wouldn't have to worry about that particular cycle. – aardvarkk Dec 02 '14 at 21:41
  • So any negative edge is disallowed? As if there's a negative edge, it's possible to go from one point to another and have the cost be less than zero. – ABCD123 Dec 02 '14 at 22:46
  • Negative edges are fine, you just can't have a *cycle* in your graph with a sum less than 0. In other words, you have to make sure there's no way (*anywhere* in your graph!) to start and end a path at the same vertex with a negative total cost. In the example you gave, we see that 3->4->3 is a negative cycle. 4->3->4 is also a negative cycle. If the edge from 3->4 (cost of 200) was removed in your graph, it should work fine with the algorithm even though there's still a negative edge in the graph. This is because you've eliminated the possibility of a negative cycle. – aardvarkk Dec 03 '14 at 00:06
  • http://i.imgur.com/7H41Faz.png just to make sure, the upper one is wrong, and the lower one is right? It's a shortest path table. Thank you again. – ABCD123 Dec 03 '14 at 12:24
  • You've again made a graph with a negative cycle. You have a path `3->3` with a negative cost. There is no such thing as a shortest path when you have negative cycles, because you can just sit on your negative cycle for an arbitrarily long time and have the cost approach negative infinity. Regardless, the content of that image no longer relates to this question in any meaningful way. I'd recommend marking this question as answered and asking another question if you're confused about how to calculate the shortest path. – aardvarkk Dec 03 '14 at 18:19
3

Floyd Warshall is an algorithm with constraint :graph with no negative cycle, if you want to find the shortest path in a graph with negative cycle you cant use Floyd Warshal, and this has a reason consider your graph with negative cycle 4->3->4 with cost -300. if you go one time through this cycle your cost reduce to 400 from 700, but why just stop there? go another time, and your cost will be 100, and again and again and again, it will cost you -200 , -500 , ... . You could do it forever and algorithm will never stop. This is why there is this constraint with no negative cycle in Floyd Warshall algorithm.

Lrrr
  • 4,755
  • 5
  • 41
  • 63
  • Why stop there? Because you'll be revisiting edges. In my example, no edges were visited twice. – ABCD123 Dec 02 '14 at 21:40
  • @Nebster173 this is definition of cycle "A cycle of a graph G,is a subset of the edge set of G that forms a path such that the first node of the path corresponds to the last." and 4->3->4 is a cycle and it doesnt matter how many time you visit an edge. – Lrrr Dec 02 '14 at 21:43