by R_haterade · 10y ago · 0 comments
I recently had a problem where I needed an adjacency matrix of shortest paths. It was a choice between dijkstra and floyd-warshall. My dijkstra implementation kicked the pants off of floyd warshall for this application by an order of magnitude, which you wouldn't really expect. And the big-O complexity was the same for both algorithms, it's just that for the graph structure, the operation count for dijkstra was much lower. It was the first one to brown up.
1. Using a heap with Dijkstra's algorithm would speed up your program
From your comment I am guessing you are using Dijkstra's algorithm without a heap, which gives O(n * m) for one source, and O(n^2 * m) for all sources (all pairs shortest path), where n is the number of nodes/vertices, and m is the number of edges
You would be able to improve that to O(m * n * lgn) by using a heap, which improves the time to run it by quite a lot in practice
2. The time it takes to run the program also depends on the density of your matrix:
In the case where the graph is sparse: m ~ n, and will result in O(n^2 * lgn) (or O(n^3) without a heap)
In the case where the graph is dense: m ~ n^2, and will result in O(n^3 * lgn) (or O(n^4) without a heap)
Compare the numbers above to O(n^3), which is the time complexity of Floyd-Warshall
So just in terms of time complexity, Floyd-Warshall would be faster in a dense graph
Does he delve into the nuts and bolts of data structure for this, or does it stay more theoretical?
Video Lectures are here: https://www.coursera.org/learn/analysis-of-algorithms
and the book:
http://aofa.cs.princeton.edu/home/
Also a youtube - short history of algorithm analysis by Sedgewick is worth a watch: