Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history


Conflicts:
	pink/prim.c
  • Loading branch information
Camille authored and Camille committed Oct 27, 2011
2 parents 6878c66 + 8895096 commit 2d66ac4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 81 deletions.
4 changes: 2 additions & 2 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function imgraph.watershed(...)
end

-- compute image
local nelts = gradient.imgraph.watershed(dest, gradient, minHeight, connex)
local nelts = gradient.imgraph.watershed(dest, gradient:clone(), minHeight, connex)

-- return image
return dest, nelts
Expand Down Expand Up @@ -766,7 +766,7 @@ imgraph._example = [[
-- (4) compute the watershed of the graph
local graph = imgraph.graph(inputimgg, 8)
local gradient = imgraph.gradient(graph)
local watershed = imgraph.watershed(gradient, 0.07, 8)
local watershed = imgraph.watershed(gradient, 0.08, 8)
local watershedgraph = imgraph.graph(watershed, 8)
local watershedcc = imgraph.connectcomponents(watershedgraph, 0.5, true)
Expand Down
40 changes: 20 additions & 20 deletions pink/kruskal.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Kruskal algorithm for Maximum Spanning Forest (MSF) computation
Kruskal algorithm for Maximum Spanning Forest (MSF) computation
implemented to compute an MSF cut in a tree (hierarchy)
author: Camille Couprie
21 oct. 2011
Expand All @@ -23,7 +23,7 @@


/*=====================================================================================*/
list * MSF_Kruskal(MergeTree * MT)
list * MSF_Kruskal(MergeTree * MT)
/*=====================================================================================*/
/*Segment a tree into two components.
Returns a list of nodes correspunding to the Max Spanning Forest cut,
Expand Down Expand Up @@ -54,7 +54,7 @@ list * MSF_Kruskal(MergeTree * MT)
nb_markers = nb_leafs+1;
N=M+nb_markers;
M=N-1;
printf("Nb nodes:%d Nb edges: %d Nb leafs :%d \n", N, M, nb_leafs);
//printf("Nb nodes:%d Nb edges: %d Nb leafs :%d \n", N, M, nb_leafs);

// indexes of edges : son's nodes indexes
//Memory allocation of temporary arrays for Krukal's algorithm
Expand All @@ -76,10 +76,10 @@ list * MSF_Kruskal(MergeTree * MT)
{
SeededNodes[j]= i+CT->nbnodes;
Mrk[SeededNodes[j]] = 1;
j++;
j++;
}
Mrk[M] = 1;

uint32_t * Rnk = (uint32_t*)calloc(N, sizeof(uint32_t));
if (Rnk == NULL) { fprintf(stderr, "kruskal : malloc failed\n"); exit(0); }
uint32_t * Fth = (uint32_t*)malloc(N*sizeof(uint32_t));
Expand All @@ -94,12 +94,12 @@ list * MSF_Kruskal(MergeTree * MT)
float * sorted_weights = (float *)malloc(M*sizeof(float));
for(k=0;k<CT->nbnodes;k++)
sorted_weights[k]=W[k];

for(k=0;k<nb_leafs;k++)
sorted_weights[CT->nbnodes+k]=val;

TriRapideStochastique_dec(sorted_weights,Es, 0, M-1);
free(sorted_weights);
free(sorted_weights);


long nb_arete = 0;
Expand All @@ -116,13 +116,13 @@ list * MSF_Kruskal(MergeTree * MT)
else if(e_max!=M) e2= e_max-CT->nbnodes;
else e2=root_node;
if (e2==-1)e2=M; //e2 = Edges[1][e_max];
printf("(%d %d)\n", e1,e2);
//printf("(%d %d)\n", e1,e2);
x = element_find(e1, Fth );
y = element_find(e2, Fth );
if ((x != y) && (!(Mrk[x]>=1 && Mrk[y]>=1)))
{
root = element_link( x,y, Rnk, Fth);
printf("link\n");
//printf("link\n");
nb_arete=nb_arete+1;
if ( Mrk[x]>=1) Mrk[root]= Mrk[x];
else if ( Mrk[y]>=1) Mrk[root]= Mrk[y];
Expand All @@ -133,9 +133,9 @@ list * MSF_Kruskal(MergeTree * MT)
// (find the root vertex of each tree)
int * Map2 = (int *)malloc(N*sizeof(int));
int * Map = (int *)malloc(N*sizeof(int));
for (i=0; i<N; i++)
for (i=0; i<N; i++)
Map2[i] = element_find(i, Fth);

// Compute the binary labeling in Map
for (i = 1; i < nb_markers; i++)
Map[SeededNodes[i]] = 1;
Expand All @@ -152,28 +152,28 @@ list * MSF_Kruskal(MergeTree * MT)
x = LifoPop(LIFO);
Mrk[x]=true;
j= nb_neighbors(x, CT, nb_leafs);
for (k=0;k<j;k++)
for (k=0;k<j;k++)
{
y = neighbor(x, k, CT, nb_leafs, SeededNodes);
if (y==-1)y=M;
if (Map2[y]==Map2[SeededNodes[i]] && Mrk[y]==false)
y = neighbor(x, k, CT, nb_leafs, SeededNodes);
if (y==-1)y=M;
if (Map2[y]==Map2[SeededNodes[i]] && Mrk[y]==false)
{
LifoPush(LIFO, y);
if (i==0) Map[y]= 0;
else Map[y]= 1;
Mrk[y]=true;
}
}
}
}
LifoFlush(LIFO);
}
for (i = 1; i < nb_markers; i++)
Map[SeededNodes[i]] = 1;
Map[M]=0;

for (i=0; i<N; i++) {
fprintf(stderr,"Map[%d]=%d \n",i,Map[i]);
}
for (i=0; i<N; i++) {
//fprintf(stderr,"Map[%d]=%d \n",i,Map[i]);
}

// Process the tree to find the cut
list * cut = NULL;
Expand Down
115 changes: 56 additions & 59 deletions pink/prim.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Prim's algorithm for Maximum Spanning Forest (MSF) computation
Prim's algorithm for Maximum Spanning Forest (MSF) computation
implemented to compute an MSF cut in a tree (hierarchy)
author: Camille Couprie
21 oct. 2011
Expand Down Expand Up @@ -27,13 +27,13 @@


/*=====================================================================================*/
list * MSF_Prim(MergeTree * MT)
list * MSF_Prim(MergeTree * MT)
/*=====================================================================================*/
/*Segment a tree into two components.
Returns a list of nodes correspunding to the Max Spanning Forest cut,
computed using Prim's algorithm */
{
int32_t i, j,u,v, x,y,z, x_1,y_1;
int32_t i, j,u,v, x,y,z, x_1,y_1;
int nb_markers; int nb_leafs;
long N, M;

Expand Down Expand Up @@ -61,7 +61,7 @@ list * MSF_Prim(MergeTree * MT)

//init Prim
//Creates a Red-Black tree to sort edges
Rbt *L;
Rbt *L;
IndicsInit(M);
L = mcrbt_CreeRbtVide(M);
i=0;
Expand All @@ -70,30 +70,30 @@ list * MSF_Prim(MergeTree * MT)
// Set for already checked edges
for(u = 0; u < M; u ++)
Set(u, false);

// marked nodes
uint32_t * SeededNodes = (uint32_t*)malloc(nb_markers*sizeof(uint32_t));
if (SeededNodes == NULL) { fprintf(stderr, "prim : malloc failed\n"); exit(0);}
// Resulting node labeling goes into G2

// Resulting node labeling goes into G2
uint8_t * G2 = (uint8_t*)calloc(N ,sizeof(uint8_t));

// fill the array SeededNodes with marked index nodes
// fill the tree L only with the marked edges
// fill the array SeededNodes with marked index nodes
// fill the tree L only with the marked edges
SeededNodes[0]= M;
j=1;
for (i = 0; i < CT->nbnodes; i++)
if (CT->tabnodes[i].nbsons == 0)
{
SeededNodes[j]= i+CT->nbnodes;
G2[SeededNodes[j]] = 2;
mcrbt_RbtInsert(&L, (TypRbtKey)(val), SeededNodes[j]);
sizeL++;
// fprintf(stderr,"L=%d", sizeL);
Set(SeededNodes[j], true);
j++;
G2[SeededNodes[j]] = 2;
mcrbt_RbtInsert(&L, (TypRbtKey)(val), SeededNodes[j]);
sizeL++;
// fprintf(stderr,"L=%d", sizeL);
Set(SeededNodes[j], true);
j++;
}
G2[root_node]=1;
G2[root_node]=1;
mcrbt_RbtInsert(&L, (TypRbtKey)(1-W[root_node]), root_node);
sizeL++;
Set(root_node, true);
Expand All @@ -102,7 +102,6 @@ list * MSF_Prim(MergeTree * MT)
float * Weights = (float *)malloc(M*sizeof(float));
for(j=0;j<CT->nbnodes;j++)
Weights[j]=W[j];


for(j=0;j<nb_leafs;j++)
Weights[CT->nbnodes+j]=val;
Expand All @@ -119,52 +118,52 @@ list * MSF_Prim(MergeTree * MT)
else if(u!=M) y= u-CT->nbnodes;
else y=root_node;
if (y==-1)y=M; //y = G->Edges[1][u];
// y must correspond to the marked node.

// y must correspond to the marked node.
if(G2[x] > G2[y])
{z=x; x=y; y=z;}
{z=x; x=y; y=z;}

// if one node is labeled
// if one node is labeled
if((minimum(G2[x],G2[y]) == 0) && (maximum(G2[x],G2[y]) > 0))
{
// assign the same label to the other one
G2[x] = G2[y];
//fprintf(stderr,"Map[%d]=Map[%d]\n",x,y);
// select neighbors edges to place them in the tree
j= nb_neighbors(u, CT, nb_leafs);
//fprintf(stderr,"nb_neigbors= %d \n",j);
for (i=0;i<j;i++)
{
// assign the same label to the other one
G2[x] = G2[y];
//fprintf(stderr,"Map[%d]=Map[%d]\n",x,y);

// select neighbors edges to place them in the tree
j= nb_neighbors(u, CT, nb_leafs);
//fprintf(stderr,"nb_neigbors= %d \n",j);
for (i=0;i<j;i++)
{
v = neighbor(u, i, CT, nb_leafs, SeededNodes);
if (v==-1)v=M;
// fprintf(stderr," %d ",v);
// if the edge v is not processed yet
if(!IsSet(v, true))
{
// Find its extreme nodes (x_1,y_1)
x_1 = v;
if (v<CT->nbnodes) y_1= CT->tabnodes[v].father;
else if(v!=M) y_1= v-CT->nbnodes;
else y_1 = root_node;
if (y_1==-1)y_1=M;
//fprintf(stderr," [%d %d] ",x_1, y_1);
if((minimum(G2[x_1],G2[y_1]) == 0) && (maximum(G2[x_1],G2[y_1]) > 0))
{
//fprintf(stderr,"( insert %d) ",v);
mcrbt_RbtInsert(&L, (TypRbtKey)(1-Weights[v]), v);
sizeL++;
Set(v,true);
}
}
}
// fprintf(stderr," \n");
}
v = neighbor(u, i, CT, nb_leafs, SeededNodes);
if (v==-1)v=M;
// fprintf(stderr," %d ",v);

// if the edge v is not processed yet
if(!IsSet(v, true))
{
// Find its extreme nodes (x_1,y_1)
x_1 = v;
if (v<CT->nbnodes) y_1= CT->tabnodes[v].father;
else if(v!=M) y_1= v-CT->nbnodes;
else y_1 = root_node;
if (y_1==-1)y_1=M;
//fprintf(stderr," [%d %d] ",x_1, y_1);
if((minimum(G2[x_1],G2[y_1]) == 0) && (maximum(G2[x_1],G2[y_1]) > 0))
{
//fprintf(stderr,"( insert %d) ",v);
mcrbt_RbtInsert(&L, (TypRbtKey)(1-Weights[v]), v);
sizeL++;
Set(v,true);
}
}
}
// fprintf(stderr," \n");
}
UnSet(u,true);
}
/* for (i=0; i<N; i++)

/* for (i=0; i<N; i++)
printf("Map[%d]=%d \n",i,G2[i]-1);*/

// Process the tree to find the cut
Expand All @@ -190,5 +189,3 @@ list * MSF_Prim(MergeTree * MT)
return cut;

}


0 comments on commit 2d66ac4

Please sign in to comment.