Floyd-­War­shall

//initialise dist[i][j] to infinity at the start
for (int k=0;k<n;k++)
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
// if there is a shorter path through node k, take it!
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);w

Floyd-­War­shall algorithm uses the idea of triangle inequa­lity, and is very
easy to code (just 4 lines!)

If there are negative cycles, dist[i][i] will be negative. Note the order!!!

Solves the Single Source Shortest Path (SSSP) problem. Means shortest path from one node to all other nodes. Cannot be used with negative edges as it runs too slow
Especially cannot be used with negative cycles

How's Your Readability?

Cheatography is sponsored by Readable.com. Check out Readable.com to make your content and copy more engaging and support Cheatography!

Random Cheat Sheet

About Cheatography

10th JuneOn The Blog

5 Ways Cheatography Benefits Your BusinessCheatography Cheat Sheets are a great timesaver for individuals - coders, gardeners, musicians, everybody! But businesses can benefit from them as well - read on to find out more.