DataMiner
DataMiner

Reputation: 325

To compare two boost graph having same vertices

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.

Upvotes: 1

Views: 1271

Answers (1)

sehe
sehe

Reputation: 393789

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);
}

Upvotes: 4

Related Questions