Revision 162e1bda custompackages/graph-parser/src/sub_component.cpp

View differences:

custompackages/graph-parser/src/sub_component.cpp
4 4

  
5 5
#include "sub_component.h"
6 6

  
7

  
8 7
SubComponent::SubComponent() {
9 8
    // do nothing
10 9
}
......
35 34
    return weight_reversed_map_;
36 35
}
37 36

  
38
vector< vector< int > > const& SubComponent::traffic_matrix() const {
37
std::map< std::pair<string, string>, int > const& SubComponent::traffic_matrix() const {
39 38
    return traffic_matrix_;
40 39
}
41 40

  
......
105 104
    weight_map_[name] = value;
106 105
}
107 106

  
107
void SubComponent::update_weight_reversed_map(string name, int value) {
108
    weight_reversed_map_[name] = value;
109
}
110

  
111
void SubComponent::calculate_weight_reversed(int V) {
112
    // V is the total number of vertices in the parent component
113
    for (string name : all_vertices_id_) {
114
        int value = get_weight_map(name);
115
        update_weight_reversed_map(name, V - 1 - value);
116
    }
117
}
118

  
108 119
/* TRAFFIC MATRIX */
109 120
void SubComponent::CalculateTrafficMatrix() {
121
    traffic_matrix_pmap_ = TrafficMatrixPMap(traffic_matrix_);
122

  
110 123
    initialize_traffic_matrix();
111 124

  
112 125
    // When only one vertex is an articulation point
......
138 151
}
139 152

  
140 153
void SubComponent::initialize_traffic_matrix() {
141
    // generate_empty_traffic_matrix, with 1 every where, and 0 in the main diagonal
142
    int size = num_of_vertices();
143
    traffic_matrix_ = vector< vector<int> >(size);
144
    for (int i = 0; i < size; ++i) {
145
        traffic_matrix_[i] = vector< int >(size, 1);
146
    }
147

  
148
    // Reset the main diagonal to 0
149
    for (int i = 0; i < size; ++i) {
150
        traffic_matrix_[i][i] = 0;
154
    // generate_empty_traffic_matrix, with 1 for different vertices, and 0 for the same vertices
155
    for (string id1 : all_vertices_id_) {
156
        for (string id2 : all_vertices_id_) {
157
            if (id1.compare(id2) == 0) {
158
                update_traffic_matrix(id1, id2, 0);
159
            }
160
            else {
161
                update_traffic_matrix(id1, id2, 1);
162
            }
163
        }
151 164
    }
152 165
}
153 166

  
154 167
int SubComponent::get_traffic_matrix(string name_1, string name_2) {
155
    int i1 = index_of_vertex_id(name_1);
156
    int i2 = index_of_vertex_id(name_2);
157
    // TODO: exception
158
    return traffic_matrix_[i1][i2];
168
    std::pair<string, string> p;
169
    if (name_1.compare(name_2) <= 0) {
170
        p = std::pair<string, string>(name_1, name_2);
171
    }
172
    else {
173
        p = std::pair<string, string>(name_2, name_1);
174
    }
175
    return traffic_matrix_[p];
159 176
}
160 177

  
161 178
void SubComponent::update_traffic_matrix(string name_1, string name_2, int value) {
162
    int i1 = index_of_vertex_id(name_1);
163
    int i2 = index_of_vertex_id(name_2);
164
    // cout << i1 << " " << i2 << " = " << value << endl;
165
    traffic_matrix_[i1][i2] = value;
166
    traffic_matrix_[i2][i1] = value; // because Traffic Matrix is symmetric
179
    std::pair<string, string> p;
180
    if (name_1.compare(name_2) <= 0) {
181
        p = std::pair<string, string>(name_1, name_2);
182
    }
183
    else {
184
        p = std::pair<string, string>(name_2, name_1);
185
    }
186
    traffic_matrix_[p] = value;
167 187
}
168 188

  
169 189
/* BETWEENNESS CENTRALITY */
170
void SubComponent::CalculateBetweennessCentrality() {
190
void SubComponent::CalculateBetweennessCentralityHeuristic() {
171 191
    initialize_betweenness_centrality();
172 192

  
173
    cout << "Mark 1" << endl;
174

  
175
    boost::brandes_betweenness_centrality(gm_.g_,
176
        boost::centrality_map(v_centrality_pmap_).vertex_index_map(
193
    boost::brandes_betweenness_centrality_heuristic(gm_.g_,
194
        traffic_matrix_pmap_,
195
        boost::centrality_map(
196
            v_centrality_pmap_).vertex_index_map(
177 197
            gm_.v_index_pmap())
178 198
    );
179

  
180
    cout << "Mark 2" << endl;
181

  
182
    cout << "Vertex betweenness\n" << endl;
183
    for (int i = 0; i < num_of_vertices(); ++i) {
184
        cout << v_centrality_vec_.at(i) << endl;
185
    }
186 199
}
187 200

  
188 201
void SubComponent::initialize_betweenness_centrality() {
......
194 207
    // There are 2 ways to retrieve the BC score
195 208
    // 1st way - through v_centrality_vec_
196 209
    int index = gm_.get_index_from_id(name);
197
    cout << "    index = " << index << endl;
198 210
    return v_centrality_vec_.at(index);
199 211
}
200 212

  
......
244 256
    // printhelper::for_map<string, int>(sc.weight_map());
245 257

  
246 258
    cout << "\nTraffic Matrix:\n";
247
    outops::operator<<(cout, traffic_matrix());
259
    print_traffic_matrix();
248 260

  
249 261
    cout << "\nBetweenness Centrality:\n";
250 262
    outops::operator<< <double>(cout, v_centrality_vec());
251 263
}
252 264

  
265
void SubComponent::print_traffic_matrix() {
266
    typedef std::map<std::pair<string, string>, int>::const_iterator Iter;
267

  
268
    for (auto elem : traffic_matrix_) {
269
        cout << elem.first.first << " - " << elem.first.second << ": " << elem.second << endl;
270
    }
271
    // for (Iter iter = traffic_matrix_.begin(); iter != traffic_matrix_.end(); ++iter) {
272
    //     cout << *(iter->first) << " - " << endl;
273
    // }
274
}
275

  
253 276
std::ostream& operator<<(std::ostream& os, const SubComponent& sc) {
254 277
    cout << "Sub-component:" << endl;
255 278
    cout << sc.gm_;
......
265 288
    // printhelper::for_map<string, int>(sc.weight_map());
266 289

  
267 290
    cout << "\nTraffic Matrix:\n";
268
    outops::operator<<(cout, sc.traffic_matrix());
291
    // I didn't write the << for traffic_matrix
292
    // outops::operator<<(cout, sc.traffic_matrix());
269 293

  
270 294
    cout << "\nBetweenness Centrality:\n";
271 295
    outops::operator<< <double>(cout, sc.v_centrality_vec());

Also available in: Unified diff