vbl_graph_partition.cxx
Go to the documentation of this file.
1 //:
2 // \file
3 #include <algorithm>
4 #include <cmath>
5 #include "vbl_graph_partition.h"
6 #ifdef _MSC_VER
7 # include <vcl_msvc_warnings.h>
8 #endif
9 
10 //:
11 // \p t is a constant that determines the threshold on edge weight
12 // to form disconnected sets
13 void vbl_graph_partition(vbl_disjoint_sets& ds, std::vector<vbl_edge>& edges, float t) {
14  // sort edges by weight in increasing order
15  std::sort(edges.begin(), edges.end());
16 
17 
18  int nv = ds.num_elements();
19  // init thresholds to t
20  std::vector<float> thr(nv, t);
21 
22  int ne = (int)edges.size();
23  for (int i = 0; i < ne; i++) {
24  vbl_edge& e = edges[i];
25 
26  // the roots of the partitions conected by this edge
27  int v0 = ds.find_set(e.v0_);
28  int v1= ds.find_set(e.v1_);
29  // if not the same partition
30  if (v0 != v1) {
31  // if the edge weight is lower than the thresholds of each partition
32  if ((e.w_ <= thr[v0]) &&
33  (e.w_ <= thr[v1])) {
34  ds.set_union(v0, v1);// merge the two partitions
35  v0= ds.find_set(v0);// find the root of the merged set
36  thr[v0] = e.w_ + (t/ds.size(v0));//adapt the threshold
37  //eventually the threshold is just the highest edge weight in the set
38  }
39  }
40  }
41 }
int v1_
Definition: vbl_edge.h:22
float w_
Definition: vbl_edge.h:23
void vbl_graph_partition(vbl_disjoint_sets &ds, std::vector< vbl_edge > &edges, float t)
t is a constant that determines the threshold on edge weight to form disconnected sets
void set_union(int set_id1, int set_id2)
Combine two sets into one.
int v0_
Definition: vbl_edge.h:21
Partitions a graph into disjoint connected components.
int find_set(int element) const
Find the set identifier that an element currently belongs to.
int size(int set_id) const
Returns the number of elements in set specified by set_id.