graph_latent_multigraph.hh 3.51 KB
Newer Older
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2019 Tiago de Paula Peixoto <tiago@skewed.de>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_LATENT_MULTIGRAPH_HH
#define GRAPH_LATENT_MULTIGRAPH_HH

#include <tuple>
#include <iostream>
#include <fstream>
#include <iomanip>

#include "graph_tool.hh"
#include "hash_map_wrap.hh"

namespace graph_tool
{

using namespace std;
using namespace boost;

template <class Graph, class WMap, class TMap>
void  get_latent_multigraph(Graph& g, WMap w, TMap theta_out, TMap theta_in,
37
                            double epsilon, size_t max_niter, bool verbose)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
    auto wc = w.get_checked();
    for (auto v : vertices_range(g))
    {
        auto tout = theta_out[v];
        auto tin = theta_in[v];
        auto e = add_edge(v, v, g).first;
        if (graph_tool::is_directed(g))
            wc[e] = tout * tin;
        else
            wc[e] = (tout * tin) / 2;
    }

    double delta = 1 + epsilon;
    size_t niter = 0;
    while (delta > epsilon && (niter < max_niter || max_niter == 0))
    {
        double M = 0;
        delta = 0;
        #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH)   \
            reduction(+:M) reduction(max:delta)
        parallel_edge_loop_no_spawn
            (g,
             [&](auto e)
             {
                 auto u = source(e, g);
                 auto v = target(e, g);
                 auto l = theta_out[u] * theta_in[v];
                 auto nw = ((u != v) ? l / (1-exp(-l)) :
                            (graph_tool::is_directed(g) ? l : l / 2));
                 auto& ew = w[e];
                 delta = std::max(delta, abs(nw - ew));
                 ew = nw;
                 M += ew;
             });

        #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH)   \
            reduction(max:delta)
        parallel_vertex_loop_no_spawn
            (g,
             [&](auto v)
             {
                 if (graph_tool::is_directed(g))
                 {
                     auto& tout = theta_out[v];
                     auto d = out_degreeS()(v, g, w);
                     auto nt = d / sqrt(M);
                     delta = std::max(delta, abs(tout - nt));
                     tout = nt;

                     auto& tin = theta_in[v];
                     d = in_degreeS()(v, g, w);
                     nt = d / sqrt(M);
                     delta = std::max(delta, abs(tin - nt));
                     tin = nt;
                 }
                 else
                 {
                     auto& t = theta_out[v];
                     auto d = out_degreeS()(v, g, w);
                     auto nt = d / sqrt(2 * M);
                     delta = std::max(delta, abs(t - nt));
                     t = nt;
                 }
             });
        niter++;
104 105
        if (verbose)
            cout << delta << endl;
106 107 108 109 110 111
    }
};

} // graph_tool namespace

#endif //GRAPH_LATENT_MULTIGRAPH_HH