graph_rewiring.hh 39.9 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
                e_ep_t -= in_degreeS()(t, _g);
                e_t -= in_degreeS()(ep_t, _g);
            }

            a /= (get_sprob(s_deg, ep_t_deg) / e_ep_t +
                  get_sprob(ep_s_deg, t_deg) / e_t);        // forwards
            a *= (get_sprob(s_deg, t_deg) / e_t +
                  get_sprob(ep_s_deg, ep_t_deg) / e_ep_t);  // backwards
        }
        else
        {
            double e_ep_t = _in_edges[ep_t_deg].size() + _out_edges[ep_t_deg].size();
            double e_t = _in_edges[t_deg].size()  + _out_edges[t_deg].size();
            if (t == ep_t)
            {
                e_ep_t -= out_degree(t, _g);
                e_t -= out_degree(ep_t, _g);
            }

            double e_ep_s = _in_edges[ep_s_deg].size() + _out_edges[ep_s_deg].size();
            double e_s = _in_edges[s_deg].size() + _out_edges[s_deg].size();
            if (s == ep_s)
            {
                e_ep_s -= out_degree(s, _g);
                e_s -= out_degree(ep_s, _g);
            }

            a /= (get_sprob(s_deg, ep_t_deg) / e_ep_t +
                  get_sprob(ep_t_deg, s_deg) / e_s +
                  get_sprob(ep_s_deg, t_deg) / e_t +
                  get_sprob(t_deg, ep_s_deg) / e_ep_s);      // forwards
            a *= (get_sprob(s_deg, t_deg) / e_t +
                  get_sprob(t_deg, s_deg) / e_s +
                  get_sprob(ep_s_deg, ep_t_deg) / e_ep_t +
                  get_sprob(ep_t_deg, ep_s_deg) / e_ep_s);   // backwards
        }

        if (a > 1)
            return ep;
1040

Tiago Peixoto's avatar
Tiago Peixoto committed
1041
1042
        std::uniform_real_distribution<> rsample(0.0, 1.0);
        double r = rsample(base_t::_rng);
1043
        if (r > a)
1044
1045
            return e; // reject
        return ep;
1046
1047
1048
1049
1050
1051
1052
1053
    }

    void update_edge(size_t ei, bool insert)
    {
        if (insert)
        {
            vertex_t v = target(base_t::_edges[ei], _g);
            deg_t d = get_deg(v, _g);
1054
1055
1056
            auto& in_vec = _in_edges[d];
            in_vec.push_back(ei);
            _in_pos[ei] = in_vec.size() - 1;
1057
1058
1059
1060
1061

            if (!is_directed::apply<Graph>::type::value)
            {
                v = source(base_t::_edges[ei], _g);
                deg_t d = get_deg(v, _g);
1062
1063
1064
                auto& out_vec = _out_edges[d];
                out_vec.push_back(ei);
                _out_pos[ei] = out_vec.size() - 1;
1065
1066
1067
1068
            }
        }
        else
        {
1069
1070
1071
1072
1073
1074
1075
1076
            vertex_t v = target(base_t::_edges[ei], _g);
            deg_t d = get_deg(v, _g);
            size_t j = _in_pos[ei];
            auto& in_vec = _in_edges[d];
            _in_pos[in_vec.back()] = j;
            in_vec[j] = in_vec.back();
            in_vec.pop_back();

1077
1078
            if (!is_directed::apply<Graph>::type::value)
            {
1079
                v = source(base_t::_edges[ei], _g);
1080
                deg_t d = get_deg(v, _g);
1081
1082
1083
1084
1085
                size_t j = _out_pos[ei];
                auto& out_vec = _out_edges[d];
                _out_pos[out_vec.back()] = j;
                out_vec[j] = out_vec.back();
                out_vec.pop_back();
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
            }
        }
    }

private:
    Graph& _g;
    EdgeIndexMap _edge_index;
    CorrProb _corr_prob;
    BlockDeg _blockdeg;

1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
#ifdef HAVE_SPARSEHASH
    typedef google::dense_hash_map<deg_t, Sampler<deg_t, boost::mpl::false_>*> sampler_map_t;
#else
    typedef std::unordered_map<deg_t, Sampler<deg_t, boost::mpl::false_>*> sampler_map_t;
#endif

    sampler_map_t _sampler;

#ifdef HAVE_SPARSEHASH
    typedef google::dense_hash_map<deg_t, google::dense_hash_map<deg_t, double>> sprob_map_t;
#else
    typedef std::unordered_map<deg_t, std::unordered_map<deg_t, double>> sprob_map_t;
#endif

    sprob_map_t _sprob;

1112

1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
#ifdef HAVE_SPARSEHASH
    typedef google::dense_hash_map<pair<deg_t, deg_t>, double, boost::hash<pair<deg_t, deg_t>>> prob_map_t;
#else
    typedef std::unordered_map<pair<deg_t, deg_t>, double, boost::hash<pair<deg_t, deg_t>>> prob_map_t;
#endif

    prob_map_t _probs;

#ifdef HAVE_SPARSEHASH
    typedef google::dense_hash_map<deg_t, vector<size_t>> edge_map_t;
#else
    typedef std::unordered_map<deg_t, vector<size_t>> edge_map_t;
#endif

    edge_map_t _in_edges;
    edge_map_t _out_edges;
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
    vector<size_t> _in_pos;
    vector<size_t> _out_pos;
};


// general "traditional" stochastic blockmodel
// this version is based on the alias method, and does not keep the degrees fixed
template <class Graph, class EdgeIndexMap, class CorrProb, class BlockDeg>
class TradBlockRewireStrategy
{
public:
    typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
    typedef typename graph_traits<Graph>::edge_descriptor edge_t;
    typedef typename EdgeIndexMap::value_type index_t;
    typedef typename BlockDeg::block_t deg_t;

    TradBlockRewireStrategy(Graph& g, EdgeIndexMap edge_index,
                            vector<edge_t>& edges, CorrProb corr_prob,
                            BlockDeg blockdeg, bool, rng_t& rng)

        : _g(g), _edge_index(edge_index), _edges(edges), _corr_prob(corr_prob),
1150
          _blockdeg(blockdeg), _rng(rng), _sampler(nullptr)
1151
    {
1152
1153
1154
1155
1156

#ifdef HAVE_SPARSEHASH
        _vertices.set_empty_key(get_null_key<deg_t>()());
#endif

1157
1158
1159
1160
1161
1162
1163
        typename graph_traits<Graph>::vertex_iterator v, v_end;
        for (tie(v, v_end) = vertices(_g); v != v_end; ++v)
        {
            deg_t d = _blockdeg.get_block(*v, g);
            _vertices[d].push_back(*v);
        }

1164
1165
1166
1167
1168
1169
        std::unordered_map<pair<deg_t, deg_t>, double, boost::hash<pair<deg_t, deg_t> > >
            probs;
        _corr_prob.get_probs(probs);

        vector<double> dprobs;
        if (probs.empty())
1170
        {
1171
            for (auto s_iter = _vertices.begin(); s_iter != _vertices.end(); ++s_iter)
1172
            {
1173
1174
1175
1176
1177
                for (auto t_iter = _vertices.begin(); t_iter != _vertices.end(); ++t_iter)
                {
                    double p = _corr_prob(s_iter->first, t_iter->first);
                    if (std::isnan(p) || std::isinf(p) || p <= 0)
                        continue;
1178

1179
1180
1181
                    _items.push_back(make_pair(s_iter->first, t_iter->first));
                    dprobs.push_back(p);
                }
1182
1183
            }
        }
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
        else
        {
            for (auto iter = probs.begin(); iter != probs.end(); ++iter)
            {
                deg_t s = iter->first.first;
                deg_t t = iter->first.second;
                double p = iter->second;
                // avoid zero probability to not get stuck in rejection step
                if (std::isnan(p) || std::isinf(p) || p <= 0)
                    continue;
                _items.push_back(make_pair(s, t));
                dprobs.push_back(p);
            }
        }

        if (_items.empty())
            throw GraphException("No connection probabilities larger than zero!");

        _sampler = new Sampler<pair<deg_t, deg_t> >(_items, dprobs);
1203
1204
1205
1206
    }

    ~TradBlockRewireStrategy()
    {
1207
1208
        if (_sampler != nullptr)
            delete _sampler;
1209
1210
1211
1212
1213
1214
1215
1216
    }

    bool operator()(size_t ei, bool self_loops, bool parallel_edges)
    {
        const pair<deg_t, deg_t>& deg = _sampler->sample(_rng);

        vector<vertex_t>& svs = _vertices[deg.first];
        vector<vertex_t>& tvs = _vertices[deg.second];
Tiago Peixoto's avatar
Tiago Peixoto committed
1217
1218
        std::uniform_int_distribution<size_t> s_sample(0, svs.size() - 1);
        std::uniform_int_distribution<size_t> t_sample(0, tvs.size() - 1);
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246

        typename graph_traits<Graph>::vertex_descriptor s, t;
        s = svs[s_sample(_rng)];
        t = tvs[t_sample(_rng)];

        // reject self-loops if not allowed
        if(!self_loops && s == t)
            return false;

        // reject parallel edges if not allowed
        if (!parallel_edges && is_adjacent(s, t, _g))
            return false;

        remove_edge(_edges[ei], _g);
        edge_t ne = add_edge(s, t, _g).first;
        _edges[ei] = ne;

        return true;
    }

private:
    Graph& _g;
    EdgeIndexMap _edge_index;
    vector<edge_t>& _edges;
    CorrProb _corr_prob;
    BlockDeg _blockdeg;
    rng_t& _rng;

1247
1248
1249
1250
1251
#ifdef HAVE_SPARSEHASH
    google::dense_hash_map<deg_t, vector<vertex_t>> _vertices;
#else
    std::unordered_map<deg_t, vector<vertex_t>> _vertices;
#endif
1252
1253
1254
1255
1256
1257

    vector<pair<deg_t, deg_t> > _items;
    Sampler<pair<deg_t, deg_t> >* _sampler;

};

1258
1259
1260
} // graph_tool namespace

#endif // GRAPH_REWIRING_HH