00001 #ifndef SEGMENT_GRAPH
00002 #define SEGMENT_GRAPH
00003
00004 #include <algorithm>
00005 #include <cmath>
00006 #include "disjoint-set.h"
00007
00008
00009 #define THRESHOLD(size, c) (c/size)
00010
00011 typedef struct {
00012 float w;
00013 int a, b;
00014 } edge;
00015
00016 bool operator<(const edge &a, const edge &b) {
00017 return a.w < b.w;
00018 }
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 universe *segment_graph(int num_vertices, int num_edges, edge *edges,
00031 float c) {
00032
00033 std::sort(edges, edges + num_edges);
00034
00035
00036 universe *u = new universe(num_vertices);
00037
00038
00039 float *threshold = new float[num_vertices];
00040 for (int i = 0; i < num_vertices; i++)
00041 threshold[i] = THRESHOLD(1,c);
00042
00043
00044 for (int i = 0; i < num_edges; i++) {
00045 edge *pedge = &edges[i];
00046
00047
00048 int a = u->find(pedge->a);
00049 int b = u->find(pedge->b);
00050 if (a != b) {
00051 if ((pedge->w <= threshold[a]) &&
00052 (pedge->w <= threshold[b])) {
00053 u->join(a, b);
00054 a = u->find(a);
00055 threshold[a] = pedge->w + THRESHOLD(u->size(a), c);
00056 }
00057 }
00058 }
00059
00060
00061 delete threshold;
00062 return u;
00063 }
00064
00065 #endif