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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:17:08+00:00 2026-06-06T23:17:08+00:00

This question is connected with: Using boost connected components with cartesian points I’ve made

  • 0

This question is connected with: Using boost connected components with cartesian points

I’ve made some changes in example to use cartesian points. Here is my current code:

using namespace boost;
typedef adjacency_list <vecS, vecS, undirectedS, cv::Point> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::vertices_size_type VertexIndex;

int main()
{
    const int VERTEX_COUNT = 10;
    Graph graph(VERTEX_COUNT);

    std::vector<VertexIndex> rank(num_vertices(graph));
    std::vector<Vertex> parent(num_vertices(graph));

    typedef VertexIndex* Rank;
    typedef Vertex* Parent;

    disjoint_sets<Rank,Parent> ds(&rank[0], &parent[0]);

    initialize_incremental_components(graph,ds);
    incremental_components(graph,ds);

    graph_traits<Graph>::edge_descriptor edge;
    bool flag;

    std::vector<Vertex> verticesVector;
    //vector of points which creates graph
    std::vector<cv::Point> points;

    points.push_back(cv::Point(0,0));
    points.push_back(cv::Point(1,1));
    points.push_back(cv::Point(2,2));
    points.push_back(cv::Point(3,1));
    points.push_back(cv::Point(4,2));
    points.push_back(cv::Point(0,2));
    points.push_back(cv::Point(2,3));
    points.push_back(cv::Point(3,3));

    int i = 0;
    for(std::vector<cv::Point>::iterator it = points.begin(); 
            it !=points.end();it++, i++)  
    {
        verticesVector.push_back(add_vertex(graph));
        graph[i].x = (*it).x;
        graph[i].y = (*it).y;
    }

    typedef component_index<VertexIndex> Components;
    Components components(parent.begin(), parent.end());

    BOOST_FOREACH(VertexIndex current_index, components)
    {
        std::cout<<"component "<<current_index<<" contains: ";
        BOOST_FOREACH(VertexIndex child_index, components[current_index])
        {
            std::cout<<child_index<<" "<<" x = "<<graph[current_index].x<<";"<<graph[current_index].y<<"||";
        }
        std::cout<<std::endl;
    }

    boost::tie(edge,flag) = add_edge(verticesVector[0],verticesVector[1],graph);
    ds.union_set(verticesVector[0],verticesVector[1]);
    boost::tie(edge,flag) = add_edge(verticesVector[1],verticesVector[2],graph);
    ds.union_set(verticesVector[1],verticesVector[2]);
    boost::tie(edge,flag) = add_edge(verticesVector[2],verticesVector[3],graph);
    ds.union_set(verticesVector[2],verticesVector[3]);
    boost::tie(edge,flag) = add_edge(verticesVector[3],verticesVector[4],graph);
    ds.union_set(verticesVector[3],verticesVector[4]);
    boost::tie(edge,flag) = add_edge(verticesVector[1],verticesVector[5],graph);
    ds.union_set(verticesVector[1],verticesVector[5]);
    boost::tie(edge,flag) = add_edge(verticesVector[5],verticesVector[6],graph);
    ds.union_set(verticesVector[5],verticesVector[6]);
    boost::tie(edge,flag) = add_edge(verticesVector[6],verticesVector[7],graph);
    ds.union_set(verticesVector[6],verticesVector[7]);

    Components components2(parent.begin(), parent.end());


    /*BOOST_FOREACH(Vertex current_vertex, vertices(graph)) 
    {
        std::cout<< "representative["<<current_vertex<<"] = "
        << ds.find_set(current_vertex)<<std::endl;
    }

    std::cout<<std::endl;*/

    for(std::vector<Vertex>::iterator it = verticesVector.begin(); it !=  verticesVector.end(); it++)
    {
        std::cout<<"representative = "<<ds.find_set((*it))<<std::endl;
    }

    BOOST_FOREACH(VertexIndex current_index, components2)
    {
        std::cout<<"component "<<current_index<<" contains: ";
        BOOST_FOREACH(VertexIndex child_index, components[current_index])
        {
            std::cout<<child_index<<" "<<" x,y = "<<graph[current_index].x<<";"<<graph[current_index].y<<"||";
        }
        std::cout<<std::endl;
    }

    //write_graphviz(std::cout, graph);

    getchar();
    return 0;
}

I’m getting exception when trying to union_set(): Access violation reading location…
I can not figure out the reason. I’ve read all topics on boost lib, tried to search in docs.

  • 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-06T23:17:13+00:00Added an answer on June 6, 2026 at 11:17 pm

    I’ve figured it out thanks to this tutorial: Algorithm for selecting all edges and vertices connected to one vertex

    The problems was:

    • I initialized vector with some vertices first (copy paste from example and modifying wasn’t best idea)
    • As tutorial says “Notice that graph[ a VertexID ] gives a Vertex, but graph[ an EdgeID ] gives an Edge “
    • Disjoint set should be initialized after adding some vertices

    Working code:

    using namespace boost;
    
    typedef adjacency_list <vecS, vecS, undirectedS, cv::Point> Graph;
    typedef graph_traits<Graph>::vertex_descriptor Vertex;
    typedef graph_traits<Graph>::vertices_size_type VertexIndex;
    
    int main()
    {
        Graph graph;
    
        graph_traits<Graph>::edge_descriptor edge;
        bool flag;
    
        std::vector<Vertex> verticesVector;
        Vertex verticesTable[8];
        std::vector<cv::Point> points;
    
        points.push_back(cv::Point(0,0));
        points.push_back(cv::Point(1,1));
        points.push_back(cv::Point(2,2));
        points.push_back(cv::Point(3,1));
        points.push_back(cv::Point(4,2));
        points.push_back(cv::Point(0,2));
        points.push_back(cv::Point(2,3));
        points.push_back(cv::Point(3,3));
    
        int i = 0;
        for(std::vector<cv::Point>::iterator it = points.begin(); it != points.end(); it++, i++)
        {
            verticesTable[i] = add_vertex(graph);
            graph[verticesTable[i]].x = (*it).x;
            graph[verticesTable[i]].y = (*it).y;
        }
    
        std::vector<VertexIndex> rank(num_vertices(graph));
        std::vector<Vertex> parent(num_vertices(graph));
    
        typedef VertexIndex* Rank;
        typedef Vertex* Parent;
    
        disjoint_sets<Rank,Parent> ds(&rank[0], &parent[0]);
    
        initialize_incremental_components(graph,ds);
        incremental_components(graph,ds);
    
        typedef component_index<VertexIndex> Components;
        Components components(parent.begin(), parent.end());
    
        Graph::vertex_iterator vertexIt, vertexEnd;
        boost::tie(vertexIt,vertexEnd) = vertices(graph);
        for(;vertexIt != vertexEnd; ++vertexIt)
        {
            Vertex a = *vertexIt;
            cv::Point &b = graph[a];
            std::cout<<"Point: "<<b.x<<";"<<b.y<<std::endl;
        }
    
        boost::tie(edge,flag) = add_edge(verticesTable[0],verticesTable[1],graph);
        ds.union_set(verticesTable[0],verticesTable[1]);
        boost::tie(edge,flag) = add_edge(verticesTable[1],verticesTable[2],graph);
        ds.union_set(verticesTable[1],verticesTable[2]);
        //boost::tie(edge,flag) = add_edge(verticesTable[2],verticesTable[3],graph);
        //ds.union_set(verticesTable[2],verticesTable[3]);
        boost::tie(edge,flag) = add_edge(verticesTable[3],verticesTable[4],graph);
        ds.union_set(verticesTable[3],verticesTable[4]);
        boost::tie(edge,flag) = add_edge(verticesTable[1],verticesTable[5],graph);
        ds.union_set(verticesTable[1],verticesTable[5]);
        boost::tie(edge,flag) = add_edge(verticesTable[5],verticesTable[6],graph);
        ds.union_set(verticesTable[5],verticesTable[6]);
        boost::tie(edge,flag) = add_edge(verticesTable[6],verticesTable[7],graph);
        ds.union_set(verticesTable[6],verticesTable[7]);
    
        BOOST_FOREACH(Vertex current_vertex, vertices(graph)) 
        {
            std::cout<< "representative["<<current_vertex<<"] = "
            << ds.find_set(current_vertex)<<std::endl;
        }
    
        std::cout<<std::endl;
    
        Components components2(parent.begin(), parent.end());
    
        BOOST_FOREACH(VertexIndex current_index, components2)
        {
            std::cout<<"component "<<current_index<<" contains: ";
            BOOST_FOREACH(VertexIndex child_index, components2[current_index])
            {
                  std::cout<<child_index<<" "<<" x,y = "<<graph[verticesTable[child_index]].x<<";"<<graph[verticesTable[child_index]].y<<"||";
            }
            std::cout<<std::endl;
        }
    
        //write_graphviz(std::cout, graph);
    
        getchar();
    
        return 0;
    }
    

    I’m still learning boost Graph so please correct my mistakes, make more elegant solutions.
    My conclusions also can be not exactly correct.

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

Sidebar

Related Questions

First, here's my original question that spawned all of this . I'm using Appcelerator
I'm gleaning from this question [ Facebook Connect Won't Validate that using Facebook Connect
This question is connected to my other question but I changed the logic a
This question concerns a Tomcat 7 web application, which is connected to a MySQL
This is an interview question. I have K machines each of which is connected
This question is related to another question I wrote: Trouble using DOTNET from PHP.
I have a question similar to How to manage object life time using Boost
Hey, sorry for posting this here, I know that this question better suites into
basically it is a rehash of this question: Cannot connect using WebSockets, not changing
This question comes from here but can be a totally independent question. I have

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.