123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #ifndef SEGMENT_GRAPH
- #define SEGMENT_GRAPH
- #include <algorithm>
- #include <cmath>
- #include "disjoint-set.h"
- #define THRESHOLD(size, c) (c/size)
- typedef struct {
- float w;
- int a, b;
- } edge;
- bool operator<(const edge &a, const edge &b) {
- return a.w < b.w;
- }
-
- universe *segment_graph( const int i_numVertices,
- const int i_numEdges,
- edge *edges,
- const float c
- )
- {
-
-
-
-
-
-
- std::stable_sort(edges, edges + i_numEdges);
-
- universe *u = new universe(i_numVertices);
-
- float *threshold = new float[i_numVertices];
- for (int i = 0; i < i_numVertices; i++)
- {
- threshold[i] = THRESHOLD(1,c);
- }
-
-
- for (int i = 0; i < i_numEdges; i++)
- {
- edge *pedge = &edges[i];
-
-
- int a = u->find(pedge->a);
- int b = u->find(pedge->b);
- if (a != b)
- {
- if ( (pedge->w <= threshold[a]) && (pedge->w <= threshold[b]) )
- {
- u->join(a, b);
- a = u->find(a);
- threshold[a] = pedge->w + THRESHOLD(u->size(a), c);
- }
- }
- }
-
-
- delete threshold;
- return u;
- }
- #endif
|