Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

segment-graph.h

Go to the documentation of this file.
00001 #ifndef SEGMENT_GRAPH
00002 #define SEGMENT_GRAPH
00003 
00004 #include <algorithm>
00005 #include <cmath>
00006 #include "disjoint-set.h"
00007 
00008 // threshold function
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  * Segment a graph
00022  *
00023  * Returns a disjoint-set forest representing the segmentation.
00024  *
00025  * num_vertices: number of vertices in graph.
00026  * num_edges: number of edges in graph
00027  * edges: array of edges.
00028  * c: constant for treshold function.
00029  */
00030 universe *segment_graph(int num_vertices, int num_edges, edge *edges, 
00031                         float c) { 
00032   // sort edges by weight
00033   std::sort(edges, edges + num_edges);
00034 
00035   // make a disjoint-set forest
00036   universe *u = new universe(num_vertices);
00037 
00038   // init thresholds
00039   float *threshold = new float[num_vertices];
00040   for (int i = 0; i < num_vertices; i++)
00041     threshold[i] = THRESHOLD(1,c);
00042 
00043   // for each edge, in non-decreasing weight order...
00044   for (int i = 0; i < num_edges; i++) {
00045     edge *pedge = &edges[i];
00046     
00047     // components conected by this edge
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   // free up
00061   delete threshold;
00062   return u;
00063 }
00064 
00065 #endif

Generated on Wed Oct 5 14:36:12 2005 for RobotFlow by  doxygen 1.4.4