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

  • Home
  • SEARCH
  • 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 6814063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:38:36+00:00 2026-05-26T20:38:36+00:00

May be it is a novice question, I need your help to compare two

  • 0

May be it is a novice question, I need your help to compare two graphs, having same number and name of the vertices.

Outline of my theme is:

Graph origG, computedG;
...
...
int nmbrSameEdges, nmbrExteraEdges, nmbrMissingEdges, nmbrIncorrectEdges;

void compareGraph(origG, computedG, nmbrSameEdges, nmbrExteraEdges, nmbrMissingEdges, nmbrIncorrectEdges);

Thanks in advance.

  • 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-26T20:38:37+00:00Added an answer on May 26, 2026 at 8:38 pm

    I thought I’d enjoy having a finger exercise at this:

    Warning
    As far as I can see, the output is not entirely as I’d expect it. I assume it is because of the comparison as defined for edge descritors not working as I expected.

    Should you run into any trouble with that, you might, instead use the isomorphism algorithm

    Usage demo

    int main()
    {
        typedef adjacency_list<> Graph;
    
        Graph a;
        {
            graph_traits<Graph>::vertex_descriptor v,u;
            u = vertex(1, a);
            v = vertex(2, a);
            add_edge(u, v, a);
        }
        Graph b;
        {
            graph_traits<Graph>::vertex_descriptor v,u;
            u = vertex(2, b);
            v = vertex(3, b);
            add_edge(u, v, b);
        }
    
        diff<Graph> comparison;
        compare(a,b, comparison);
    
        std::cout << comparison;
    
    /*
     *    std::cout << "edge_stats.same:      " << comparison.edge_stats.same.size()      << std::endl;
     *    std::cout << "edge_stats.extra:     " << comparison.edge_stats.extra.size()     << std::endl;
     *    std::cout << "edge_stats.missing:   " << comparison.edge_stats.missing.size()   << std::endl;
     *    std::cout << "edge_stats.reversed:  " << comparison.edge_stats.reversed.size()  << std::endl;
     *    std::cout << "vertex_stats.same:    " << comparison.vertex_stats.same.size()    << std::endl;
     *    std::cout << "vertex_stats.extra:   " << comparison.vertex_stats.extra.size()   << std::endl;
     *    std::cout << "vertex_stats.missing: " << comparison.vertex_stats.missing.size() << std::endl;
     */
    
    }
    

    The output (using Boost Spirit):

    Diff stats: 
    --------------
    Vertices
        Same:    3 (0, 1, 2)
        Extra:   1 (3)
        Missing: 0 ()
    Edges
        Same:    0
        Extra:   1
        Missing: 1
        Reversed:1
    

    Note that sample code is commented that shows how to get print the diff results without using Boost Spirit, too.

    Implementation

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/reverse_graph.hpp>
    #include <boost/graph/iteration_macros.hpp>
    #include <boost/spirit/include/karma.hpp>
    using namespace boost;
    
    namespace setops
    {
        template <typename T> std::set<T> operator-(const std::set<T>& a, const std::set<T>& b)
        {
            std::set<T> r;
            std::set_difference(
                    a.begin(), a.end(),
                    b.begin(), b.end(),
                    std::inserter(r, r.end()));
    
            return r;
        }
    
        template <typename T> std::set<T> operator/(const std::set<T>& a, const std::set<T>& b)
        {
            std::set<T> r;
            std::set_intersection(
                    a.begin(), a.end(),
                    b.begin(), b.end(),
                    std::inserter(r, r.end()));
    
            return r;
        }
    }
    
    template <typename descriptor>
        struct stats 
        { 
            std::set<descriptor> same, extra, missing; 
        };
    template <typename descriptor>
        struct directed_stats : stats<descriptor>
        { 
            std::set<descriptor> reversed; 
        };
    
    template <typename G>
        struct diff
        {
            typedef typename graph_traits<G>::vertex_descriptor vdesc;
            typedef typename graph_traits<G>::edge_descriptor   edesc;
    
            stats<vdesc> vertex_stats;
            directed_stats<edesc> edge_stats;
    
            template <typename G2>
            friend std::ostream& operator<<(std::ostream& os, const diff<G2>& d)
            {
                using namespace boost::spirit::karma;
    
                os << "Diff stats: \n"
                      "--------------\n"
                      "Vertices\n"
                   << format("\tSame:    " << int_ << " (" << -(auto_ % ", ") << ")" << eol, d.vertex_stats.same.size(), d.vertex_stats.same)
                   << format("\tExtra:   " << int_ << " (" << -(auto_ % ", ") << ")" << eol, d.vertex_stats.extra.size(), d.vertex_stats.extra)
                   << format("\tMissing: " << int_ << " (" << -(auto_ % ", ") << ")" << eol, d.vertex_stats.missing.size(), d.vertex_stats.missing);
                os << "Edges\n"
                   << format("\tSame:    " << int_ << eol, d.edge_stats.same.size())
                   << format("\tExtra:   " << int_ << eol, d.edge_stats.extra.size())
                   << format("\tMissing: " << int_ << eol, d.edge_stats.missing.size())
                   << format("\tReversed:" << int_ << eol, d.edge_stats.missing.size());
                return os;
            }
        };
    
    template <typename G>
    void compare(const G& a, const G& b, diff<G>& result)
    {
        std::set<typename diff<G>::vdesc> av, bv;
        std::set<typename diff<G>::edesc> ae, be, re;
    
        BGL_FORALL_EDGES_T(e, a, G) ae.insert(e);
        BGL_FORALL_EDGES_T(e, b, G) be.insert(e);
    
        // reverse adapt b:
        reverse_graph<G> r(b);
        BGL_FORALL_EDGES_T(e, r, G) re.insert(e);
    
        using namespace setops;
        result.edge_stats.same    = (ae / be);
        result.edge_stats.extra   = (be - ae);
        result.edge_stats.missing = (ae - be);
        result.edge_stats.reversed= (ae / re);
    
        BGL_FORALL_VERTICES_T(v, a, G) av.insert(v);
        BGL_FORALL_VERTICES_T(v, b, G) bv.insert(v);
        result.vertex_stats.same    = (av / bv);
        result.vertex_stats.extra   = (bv - av);
        result.vertex_stats.missing = (av - bv);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a novice question, but I can't figure it out by inspecting
This question may seem like a novice, and perhaps 'stupid' question but please bear
I am a novice in Java, so the below question may look trivial. Background:
This may be a lame question but I am a total novice with regular
You may notice from my last question that a problem caused some more problems,
Hello friends it may sound awkward but i am novice to asp dotnet web
As some of you may notice this question is problem 16 from Project Euler
First question on SOF, please be gentle if this may be a stupid question.
This may be a terribly naive question but I was just wondering if the
May be my question is crazy. 1) ASP.net MVC is stateless, so there is

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.