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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:31:08+00:00 2026-06-12T04:31:08+00:00

I am trying to create a graph of objects which I need to traverse

  • 0

I am trying to create a graph of objects which I need to traverse using some traversal algorithm. At this very moment I am stuck trying to create the graph using my custom objects. The way I am trying to accomplish it is as follows:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <iostream>

using namespace std;
typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS> CustomGraph;
typedef boost::graph_traits<CustomGraph>::vertex_descriptor CustomVertex;

class CustomVisitor:public boost::default_dfs_visitor
{
        public:
                void discover_vertex(CustomVertex v,const CustomGraph& taskGraph) const
                {
                        cerr<<v<<endl;
                        return;
                }


};

class CustomObject{
        private:
                int currentId;
        public:
                CustomObject(int id){
                        currentId = id;
                }

};


int main()
{
        CustomGraph customGraph;
        CustomObject* obj0 = new CustomObject(0);
        CustomObject* obj1 = new CustomObject(1);
        CustomObject* obj2 = new CustomObject(2);
        CustomObject* obj3 = new CustomObject(3);

        typedef std::pair<CustomObject*,CustomObject*> Edge;
        std::vector<Edge> edgeVec;
        edgeVec.push_back(Edge(obj0,obj1));
        edgeVec.push_back(Edge(obj0,obj2));
        edgeVec.push_back(Edge(obj1,obj2));
        edgeVec.push_back(Edge(obj1,obj3));
        customGraph(edgeVec.begin(),edgeVec.end());
        CustomVisitor vis;
        boost::depth_first_search(customGraph,boost::visitor(vis));

        return 0;
}

But this doesn’t seem to be the right way to create objects within the vertices. Can somebody guide me as to what is the correct way to create nodes such that I can retrieve my objects while traversing the graph.

Thanks

  • 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-12T04:31:10+00:00Added an answer on June 12, 2026 at 4:31 am

    Hi I know this is a rather old question, but there might others that can benefit from an answer.

    It seems as if you have forgotten to define that your Graph will have a custom class as vertices. You have to add a fourth parameter to your typedef and add a typedef for your edge:

    typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS, CustomObject> CustomGraph;
    typedef boost::graph_traits<CustomGraph>::vertex_descriptor CustomVertex;
    typedef boost::graph_traits<CustomGraph>::edge_descriptor CustomEdge;
    

    Then I usually add my nodes before connecting them with edges:

    // Create graph and custom obj's
    CustomGraph customGraph
    CustomObject obj0(0);
    CustomObject obj1(1);
    CustomObject obj2(2);
    CustomObject obj3(3);
    
    // Add custom obj's to the graph 
    // (Creating boost vertices)
    CustomVertex v0 = boost::add_vertex(obj0, customGraph);
    CustomVertex v1 = boost::add_vertex(obj1, customGraph);
    CustomVertex v2 = boost::add_vertex(obj2, customGraph);
    CustomVertex v3 = boost::add_vertex(obj3, customGraph);
    
    // Add edge
    CustomEdge edge; 
    bool edgeExists;
    // check if edge allready exist (only if you don't want parallel edges)
    boost::tie(edge, edgeExists) = boost::edge(v0 , v1, customGraph);
    if(!edgeExists)
        boost::add_edge(v0 , v1, customGraph);
    
    // write graph to console
    cout << "\n-- graphviz output START --" << endl;
    boost::write_graphviz(cout, customGraph);
    cout << "\n-- graphviz output END --" << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a combination of graph edges which are stored inside a
I have a very simple line graph I'm trying to create in gRaphael. My
So I'm trying to create some graphs using Core Plot, but for the following
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying to create a scene graph for my 3D game in which
I'm trying to create an aggregation for my open graph action using the number
I am trying to create linear correlation graph between to variables using ggplot2 :
I'm trying to make a graph implementation for an assignment, which has Graph(GraphImp) objects
I'm trying to create an event through the Facebook Graph API and I keep
I am trying to use matplotlib to create a graph to reflect real time

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.