Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7492227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:23:55+00:00 2026-05-29T16:23:55+00:00

I have implemented a Bellman-Ford algorithm to solve a problem (with a graph), but

  • 0

I have implemented a Bellman-Ford algorithm to solve a problem (with a graph), but this solution was too slow, so I have replaced the Bellman-Ford’s queue with a heap (std::set) , so the solution for the shortest path will be found faster. (somehow close do Dijkstra algorithm)

Now, I insert the node number in the heap, so the default std::set will sort the nodes using their number, not their cost. Everything is fine, and the algorithm gives the corect answers.

If I impelement a custom compare function for the std::set , so that the nodes will be sorted by their distance and not by their number the algorithm does no longer provide the shortest distance to the rest of the nodes.

This is my compare function:

 struct cmp{

    bool operator() (const int &a,const int &b) const{
        return (d[Q][a] < d[Q][b] );
    }
  };
 set <int,cmp> q;

So, being a BF algorithm, the algorithm runs until no improvement can be made. Can the compare function somehow “mess” the std::set, because this is the only reason I can see why adding this comparison function will give wrong answers…

I mean, why would it work if the nodes are in a completely random order but won’t work if they are ordered by their cost…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:23:56+00:00Added an answer on May 29, 2026 at 4:23 pm

    As far as I recall the Bellman-Ford algorithm it updates the distance of the node. Thus, using the weight as the key for std::set<T> doesn’t easily work: the search in the std::set<T> wouldn’t find the respective value if you just changed the distance. What you would need to do pursue this direction is to remove the node to be updated, change the node’s distance, and reinsert it afterwards. Please also note that objects in std::set<T> need to have a unique key: inserting a key which already exists will fail.

    You said that you are using the std::set<int> as some sort of a priority queue. Did you have a look at std::priority_queue<T>? This is doing exactly this but it tends to be more efficient than using a std::set<T>. The problem with std::priority_queue<T> is that you can’t change an objects priority: you have only access to the current top. A long time ago I had create a collection of priority queues (heaps) which included versions of the priority queues allowing changes to an object’s priority (they were part of the initial libraries for Boost but they never made it through the review process). I don’t know how you use your priority queue and thus I don’t know whether this is reasonable direction to look at.

    That said, I don’t see why you would want to a std::set<T> for the Bellman-Ford algorithm anyway. My understanding is that the algorithm repeatedly goes through the graph and updates nodes with their new shortest distance. Did you have a look at Boost’s implementation of the Bellman-Ford algorithm?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm new to c++ and have been trying to implement the bellman ford algorithm,
I have implemented authentication systems for webapps several times over the years, but before
I have implemented a JSF validator based on this example . How can I
I have implemented a graph editor with Eclipse EMF and GMF frameworks. After completing
I have implemented my own VirtualPathProvider for loading 'embedded' views. This works very well
I have implemented a shared library in Linux and try to test it, but
I have implemented a sorting algorithm for a custom string that represents either time
I have implemented a function to validate .edu domains. This is how I am
I have implemented a Webservice using Apache CXF . The flow is this way
I have implemented tray icon menu for QT application. But the tray icon menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.