diff options
Diffstat (limited to 'SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c')
| -rw-r--r-- | SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c b/SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c new file mode 100644 index 0000000..13b219b --- /dev/null +++ b/SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | /******************************** | ||
| 2 | Author: Sravanthi Kota Venkata | ||
| 3 | ********************************/ | ||
| 4 | |||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include "segment.h" | ||
| 8 | |||
| 9 | // threshold function | ||
| 10 | #define THRESHOLD(size, c) (c/size) | ||
| 11 | |||
| 12 | int find(universe* u, int x) | ||
| 13 | { | ||
| 14 | int y=x; | ||
| 15 | while (y != u->elts[y].p) | ||
| 16 | y = u->elts[y].p; | ||
| 17 | u->elts[x].p = y; | ||
| 18 | return y; | ||
| 19 | } | ||
| 20 | |||
| 21 | void join(universe* u, int x, int y) | ||
| 22 | { | ||
| 23 | if (u->elts[x].rank > u->elts[y].rank) | ||
| 24 | { | ||
| 25 | u->elts[y].p = x; | ||
| 26 | u->elts[x].size += u->elts[y].size; | ||
| 27 | } | ||
| 28 | else | ||
| 29 | { | ||
| 30 | u->elts[x].p = y; | ||
| 31 | u->elts[y].size += u->elts[x].size; | ||
| 32 | if (u->elts[x].rank == u->elts[y].rank) | ||
| 33 | u->elts[y].rank++; | ||
| 34 | } | ||
| 35 | u->num--; | ||
| 36 | return ; | ||
| 37 | } | ||
| 38 | |||
| 39 | universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c, F2D* edgeWeights, F2D* in, I2D* ind, | ||
| 40 | universe* u) | ||
| 41 | { | ||
| 42 | float threshold[num_vertices]; | ||
| 43 | int i, a, b, j, k; | ||
| 44 | //universe *u; | ||
| 45 | //F2D *edgeWeights; | ||
| 46 | I2D *indices; | ||
| 47 | |||
| 48 | edgeWeights = fResetHandle(edgeWeights,1,num_edges); | ||
| 49 | |||
| 50 | for(i=0; i<num_edges; i++) | ||
| 51 | asubsref(edgeWeights,i) = edges[i].w; | ||
| 52 | |||
| 53 | // sort edges by weight | ||
| 54 | indices = fResortIndices(edgeWeights,1, in, ind); | ||
| 55 | |||
| 56 | // make a disjoint-set forest | ||
| 57 | //u = (universe*)malloc(sizeof(universe)); | ||
| 58 | //u->elts = (uni_elt*)malloc(sizeof(uni_elt)*num_vertices); | ||
| 59 | u->num = num_vertices; | ||
| 60 | for(i=0; i<num_vertices; i++) | ||
| 61 | { | ||
| 62 | u->elts[i].rank = 0; | ||
| 63 | u->elts[i].size = 1; | ||
| 64 | u->elts[i].p = i; | ||
| 65 | } | ||
| 66 | |||
| 67 | // init thresholds | ||
| 68 | for (i = 0; i < num_vertices; i++) | ||
| 69 | arrayref(threshold,i) = THRESHOLD(1,c); | ||
| 70 | |||
| 71 | // for each edge, in non-decreasing weight order... | ||
| 72 | for (i = 0; i < num_edges; i++) | ||
| 73 | { | ||
| 74 | edge *pedge = &edges[ asubsref(indices,i) ]; | ||
| 75 | |||
| 76 | // components conected by this edge | ||
| 77 | a = find(u, pedge->a); | ||
| 78 | b = find(u, pedge->b); | ||
| 79 | if (a != b) | ||
| 80 | { | ||
| 81 | if ((pedge->w <= arrayref(threshold,a)) && (pedge->w <= arrayref(threshold,b))) | ||
| 82 | { | ||
| 83 | join(u, a, b); | ||
| 84 | a = find(u, a); | ||
| 85 | arrayref(threshold,a) = pedge->w + THRESHOLD(u->elts[a].size, c); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | //fFreeHandle(edgeWeights); | ||
| 91 | //iFreeHandle(indices); | ||
| 92 | |||
| 93 | return u; | ||
| 94 | } | ||
| 95 | |||
