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 7782121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:26:34+00:00 2026-06-01T19:26:34+00:00

I’m trying to use boost’s prim’s algorithm to find the minimum spanning tree using

  • 0

I’m trying to use boost’s prim’s algorithm to find the minimum spanning tree using edge weight and an id number instead of just edge weight.

For example, if both edge weights were 1 the id would be compared, whichever one was less would break the tie.

I created an EdgeWeight class and overloaded the < and + operators to do this, then changed the edge_weight_t property from int to EdgeWeight in the hopes it would work.

// TestPrim.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <boost/config.hpp>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>

using namespace std;

class EdgeWeight{
    public:
    EdgeWeight(){}
    EdgeWeight(int weightIn, int destinationIdIn){
        weight = weightIn;
        destinationId = destinationIdIn;
    }

        bool operator<(const EdgeWeight& rhs) const {
        if (weight < rhs.weight)
            return true;
        else if(weight == rhs.weight){
            if (destinationId < rhs.destinationId)
                return true;
            else
                return false;
        }
        else
            return false;
        }

    EdgeWeight operator+(const EdgeWeight& rhs) const {
        EdgeWeight temp;
        temp.weight = weight + rhs.weight;
        temp.destinationId = destinationId + rhs.destinationId;
        return temp;
        }

    int weight;
    int destinationId;
};


int _tmain(int argc, _TCHAR* argv[])
{
  using namespace boost;
  typedef adjacency_list < vecS, vecS, undirectedS, property<vertex_distance_t, EdgeWeight>,      property < edge_weight_t, EdgeWeight > > Graph;
  typedef std::pair < int, int >E;
  const int num_nodes = 5;
  E edges[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
    E(3, 4), E(4, 0)
  };
  EdgeWeight weights[] = { EdgeWeight(1, 2), EdgeWeight(1, 3), EdgeWeight(2, 4), 
      EdgeWeight(7, 1), EdgeWeight(3, 3), EdgeWeight(1, 4), EdgeWeight(1, 0) };
  Graph g(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes);
  property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);
  std::vector < graph_traits < Graph >::vertex_descriptor > p(num_vertices(g));
  prim_minimum_spanning_tree(g, &p[0]);

  for (std::size_t i = 0; i != p.size(); ++i)
    if (p[i] != i)
      std::cout << "parent[" << i << "] = " << p[i] << std::endl;
    else
      std::cout << "parent[" << i << "] = no parent" << std::endl;

  return EXIT_SUCCESS;
}

I got an error, “c:\program files (x86)\microsoft visual studio 10.0\vc\include\limits(92): error C2440: ” : cannot convert from ‘int’ to ‘D’
1> No constructor could take the source type, or constructor overload resolution was ambiguous”

Am I doing this the right way? Is there a better way to do this?

http://www.boost.org/doc/libs/1_38_0/libs/graph/doc/prim_minimum_spanning_tree.html
http://www.boost.org/doc/libs/1_38_0/boost/graph/prim_minimum_spanning_tree.hpp

edit: ok so I implemented the weights using cjm’s perturbed method for now, but in the future I believe I will have to use the above method somehow, still wondering how to do it

edit2: based on Jeremiah’s reponse I changed the vertex_distance_t from int to EdgeWeight but got the same error

  • 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-06-01T19:26:35+00:00Added an answer on June 1, 2026 at 7:26 pm

    Boost’s implementation of “Prim’s” algorithm (Jarník discovered the algorithm almost thirty years prior to Prim) uses a generic implementation of Dijkstra’s algorithm as a subroutine. I’m sure someone thought that this was really clever.

    Dijkstra’s algorithm needs nonnegative weights that support addition with identity and comparison, and these operations have to be compatible (for all x, y, z, if x <= y, then x + z <= y + z). In practice, the only useful way to instantiate these operations is the customary one (I take this back; it’s possible to tiebreak Dijkstra’s algorithm the same way), so Boost’s implementation of Dijkstra’s algorithm sensibly assumes the existence of “infinity” (std::numeric_limits<vertex_distance_t>::max()). It also asserts that all weights are nonnegative.

    By contrast, Prim’s algorithm needs weights that support only comparison. You might wonder what the “minimum” in “minimum spanning tree” means without addition, but an alternative characterization of a minimum spanning tree is that, for every path P from a vertex u to a vertex v, the longest edge in P is at least as long as the longest edge in the tree’s path from u to v.

    The result is that Boost’s implementation of Prim’s algorithm makes some unwarranted assumptions. You can code around them as follows, but Boost does not appear to be contractually obligated not to break this hack in the future.

    • Get rid of the addition operator; it’s not used.

    • Since Boost is going to assert that all of your weights are not less than the default-constructed value, let’s make the default “negative infinity”.

      #include <limits>
      ...
      EdgeWeight() : weight(numeric_limits<int>::min()),
                     destinationId(numeric_limits<int>::min()) {}
      
    • Boost needs “positive infinity”, so we need to specialize std::numeric_limits.

      namespace std {
      template<>
      struct numeric_limits<EdgeWeight> {
          static EdgeWeight max() { return EdgeWeight(numeric_limits<int>::max(),
                                                      numeric_limits<int>::max()); }
      };
      }
      
    • You can simplify the comparison.

      bool operator<(const EdgeWeight& rhs) const {
          return (weight < rhs.weight ||
                  (weight == rhs.weight && destinationId < rhs.destinationId));
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.