Skip to content

Conversation

@starovoid
Copy link
Collaborator

SPFA (Shortest Path Faster Algorithm) is the improved variation of Bellman-Ford.

Time Complexity:

  • Average Time Complexity: O(|E|) , but this bound has not been approved yet.
  • Worstcase Time Complexity: O(|V|*|E|)

Space complexity

  • Space complexity: O(V)

Wikipedia says:

A variation of the Bellman–Ford algorithm described by Moore (1959), reduces the number of relaxation steps that need to be performed within each iteration of the algorithm. If a vertex v has a distance value that has not changed since the last time the edges out of v were relaxed, then there is no need to relax the edges out of v a second time. In this way, as the number of vertices with correct distance values grows, the number whose outgoing edges that need to be relaxed in each iteration shrinks, leading to a constant-factor savings in time for dense graphs. This variation can be implemented by keeping a collection of vertices whose outgoing edges need to be relaxed, removing a vertex from this collection when its edges are relaxed, and adding to the collection any vertex whose distance value is changed by a relaxation step. In China, this algorithm was popularized by Fanding Duan, who rediscovered it in 1994, as the "shortest path faster algorithm".[6]

About implementation

I used a stack instead of a queue to improve performance. In this regard, there is a slight discrepancy with the classic description of the "SPFA" algorithm, but this option was also tested on random tests against Bellman-Ford here.

@starovoid
Copy link
Collaborator Author

Made the rebase to start a new branch based on spfa soon. I plan to add an implementation of Johnson's algorithm based on spfa in order to get maximum performance on sparse graphs.

Now the SPF benchmark shows `1,599.55 ns/iter (+/- 22.69)` on my
computer. The previous version had about `80,500.00 ns/iter`.
For comparison, `bellman_ford` has `51,704.94 ns/iter (+/- 405.06)`.
Of course, `spfa` performance is more sensitive than `bellman_ford` to the choice of the initial vertex,
but even in the "bad case" performance turns out to be 10 times better than the version with `VecDeque`.
@starovoid starovoid changed the title Add Shortest Path Faster Algorithm Implementation feat: Add Shortest Path Faster Algorithm Implementation Mar 24, 2025
@starovoid starovoid added this pull request to the merge queue Mar 24, 2025
Merged via the queue into petgraph:master with commit 74339ab Mar 24, 2025
11 of 12 checks passed
@starovoid starovoid deleted the spfa branch March 24, 2025 19:23
github-merge-queue bot pushed a commit that referenced this pull request Apr 5, 2025
A sufficient number of proposed changes have accumulated to combine them
and publish a new major release numbered `0.8.0`.


BREAKING CHANGE:

This will require the user to provide extra type parameter in some APIs
(Read more in #747).

## List of changes
- [x] #747
The main innovation of the current release, the long-awaited feature
that has become very relevant due to the transition of dependent
projects to support `no_std`.
- [x] #662 
- [x] #611
- [x] #728
- [x] #686
- [x] #737 
- [x] #720 
- [x] #718 

## Note
There are still a large number of PRs that we want to adopt in the near
future, so we should expect at least a release of `0.8.1` soon after the
completion of `0.8.0`.

Thank you all for participating!

---------

Co-authored-by: Agustin Borgna <agustinborgna@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-new-algorithm Category: A feature request for a new graph algorithm C-performance Category: A change motivated by improving speed, memory usage or compile times

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants