Skip to content

Commit

Permalink
Some formatting, got rid of prints.
Browse files Browse the repository at this point in the history
  • Loading branch information
clementfarabet committed Oct 27, 2011
1 parent b0633b7 commit 8895096
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 109 deletions.
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 @@ -51,7 +51,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 @@ -73,10 +73,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 @@ -91,12 +91,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 @@ -113,13 +113,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 @@ -130,9 +130,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 @@ -147,28 +147,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
175 changes: 86 additions & 89 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;
float val=0; //weight parameter for leafs.
Expand All @@ -58,7 +58,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 @@ -67,30 +67,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 @@ -99,18 +99,18 @@ list * MSF_Prim(MergeTree * MT)
float * Weights = (float *)malloc(M*sizeof(float));
for(j=0;j<CT->nbnodes;j++)
Weights[j]=W[j];

/* Weights[1]=0.1;
Weights[2]=0.2;
Weights[3]=0.3;
Weights[4]=0.4;
Weights[5]=0.5;
Weights[6]=0.6;
Weights[7]=0.7;
Weights[8]=0.8;
Weights[9]=0.9;
Weights[10]=1;
Weights[0]=0;
Weights[2]=0.2;
Weights[3]=0.3;
Weights[4]=0.4;
Weights[5]=0.5;
Weights[6]=0.6;
Weights[7]=0.7;
Weights[8]=0.8;
Weights[9]=0.9;
Weights[10]=1;
Weights[0]=0;
*/


Expand All @@ -131,74 +131,71 @@ Weights[0]=0;
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
list * cut = NULL;
for (i = 0; i < CT->nbnodes; i++)
{
// nodes having a different value than their father are in the cut
if ((CT->tabnodes[i].father != -1) && (G2[CT->tabnodes[i].father] != G2[i]))
Insert(&cut, i);
// leafs having the same label as the root are in the cut
if ((CT->tabnodes[i].nbsons == 0) && (G2[i]-1==0))
Insert(&cut, i);
}

PrintList(cut);
IndicsTermine();
free(G2);
mcrbt_RbtTermine(L);
free(SeededNodes);
free(Weights);
return cut;

}

// Process the tree to find the cut
list * cut = NULL;
for (i = 0; i < CT->nbnodes; i++)
{
// nodes having a different value than their father are in the cut
if ((CT->tabnodes[i].father != -1) && (G2[CT->tabnodes[i].father] != G2[i]))
Insert(&cut, i);
// leafs having the same label as the root are in the cut
if ((CT->tabnodes[i].nbsons == 0) && (G2[i]-1==0))
Insert(&cut, i);
}

PrintList(cut);
IndicsTermine();
free(G2);
mcrbt_RbtTermine(L);
free(SeededNodes);
free(Weights);
return cut;
}

0 comments on commit 8895096

Please sign in to comment.