Floyd-Warshall Algorithm
This is not a very efficient algorithm because it runs in . However, it is very easy to understand and I actually had to implement one during my first contest (YAC 3).
- Unique Characteristic: Finds all shortest paths in a single run.
Since the implementation is simple, this algorithm might be a good choice. However, the algorithm can only be used when the graph is so small that a time complexity is fast enough.
Code
Implemented using adjacency matrix adj
and distance matrix dist
.
Afterwards, use k
as the intermediary to calculate the shortest distances.