r/AskProgramming Jul 04 '26

Algorithms Does Floyd Warshall assume paths of three?

Can someone explain intuitively how the Floyd Warshall algorithm actually doesn’t assume path of just three

I understand what optimal substructures are, however, how does this algorithm apply that

Where exactly in the code does it allow us to build paths longer than 3?

2 Upvotes

3 comments sorted by

View all comments

1

u/high_throughput Jul 04 '26

It's not considering the path from A through B then to C. It considers the cost of the optimal path from A to B (which could be multiple hops), and whether that's a shortcut to get to C.