Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed dijkstra implementation bug: improved time from exponential to O(E * logV) #848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

taaae
Copy link

@taaae taaae commented Dec 22, 2024

Description

The previous implementation incorrectly defined prio as BTreeMap<V, E>, causing this line:
while let Some((vertex, path_weight)) = prio.pop_first() {
to select elements with the smallest vertex instead of the smallest path_weight potentially leading to exponential time complexity.
I replaced BTreeMap<V, E> with BTreeSet<(E, V)> and added code to remove old entries, which was missing in the previous implementation using heap: #162

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

…y prioritized unvisited nodes with the smallest vertex instead of the smallest weight.
@taaae taaae requested review from imp2002 and vil02 as code owners December 22, 2024 23:17
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.47%. Comparing base (7903120) to head (33b2a33).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #848   +/-   ##
=======================================
  Coverage   95.47%   95.47%           
=======================================
  Files         316      316           
  Lines       22932    22934    +2     
=======================================
+ Hits        21895    21897    +2     
  Misses       1037     1037           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants