+ added CMakeLists.txt

+ added headers
+ added library 'add_main.c'
+ some optimization
This commit is contained in:
arcan1s
2013-07-22 06:06:01 +04:00
parent 2057208ef6
commit 51d31d0a2f
22 changed files with 347 additions and 210 deletions

View File

@ -1,24 +1,22 @@
/* Library for graph structure analyze
* Usage:
* graph_analyze (N, connect, max_depth)
* graph_analyze (N, connect, max_depth, isomorphism_class)
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int check_cycle (const int N, const int *matrix)
int check_cycle (const int N, const int *pn)
// function to return number of cycles
{
int cycle, i, j;
int cycle, i;
/* cycle - number of cycle
*/
cycle = 0;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
cycle += matrix[i*N+j];
for (i=1; i<N; i++)
cycle += i*pn[i];
// for linear (0.5*cycle == N-1)
cycle = 0.5 * cycle - (N - 1);
@ -27,21 +25,19 @@ int check_cycle (const int N, const int *matrix)
}
int check_cycle_size (const int N, const int *matrix, const int size)
int check_cycle_size (const int N, const int *matrix, const int depth, int *n_cycle)
// function to return number of cycles of certain size
{
int cur_N, cycle, i, j, k, n, num, *submatrix, *vertex;
int cur_N, cycle, i, j, k, n, p, *vertex;
/* cur_N - current number of elements in submatrix
* cycle - if ((0.5 * cycle) == size) that cycle exist
* cycle - if (cycle == 1) that cycle exist
* n - number of samples
* num - number of cycles of certain size
* submatrix - connectivity matrix for subgraph
* vertex - vertexes of subgraph
*/
num = 0;
submatrix = (int *) malloc (size * size * sizeof (int));
vertex = (int *) malloc (N * sizeof (int));
for (i=0; i<depth-2; i++)
n_cycle[i] = 0;
// matrix generation from
// http://wincode.org/acm-icpc/subsets-generation
@ -56,89 +52,82 @@ int check_cycle_size (const int N, const int *matrix, const int size)
cur_N++;
}
if (cur_N == size)
if ((cur_N > 2) && (cur_N <= depth))
{
// copy connectivity matrix
cycle = 1;
for (j=0; j<cur_N; j++)
{
p = 0;
for (k=0; k<cur_N; k++)
submatrix[j*cur_N+k] = matrix[vertex[j]*N+vertex[k]];
p += matrix[vertex[j]*N+vertex[k]];
if (p != 2)
cycle = 0;
}
cycle = 0;
// analyze subgraph
for (j=0; j<cur_N; j++)
for (k=0; k<cur_N; k++)
cycle += submatrix[j*cur_N+j];
if ((0.5 * cycle) == size)
num++;
if (cycle == 1)
n_cycle[cur_N-3]++;
}
}
free (vertex);
free (submatrix);
return num;
return 0;
}
int check_tail (const int N, const int *matrix)
int check_tail (const int *pn)
// function to return number of tails
{
int i, j, pn, tail;
/* pn - weight one vertex
* tail - number of tails
*/
tail = 0;
for (i=0; i<N; i++)
{
pn = 0;
for (j=0; j<N; j++)
pn += matrix[i*N+j];
if (pn == 1)
tail++;
}
return tail;
return pn[1];
}
int graph_analyze (const int N, const int *matrix, const int max_depth, char *iso)
int graph_analyze (const int N, const int *matrix, const int max_depth, int *iso)
/* N - number of vertex in graph
* matrix - connectivity matrix
* max_depth - maximum depth for check_cycle_size
* iso - isomorphism class
*/
{
int cycle, depth, i, *n_cycle, tail;
/* cycle - number of cycles
* depth - depth for check_cycle_size
* n_cycle - number of cycles of certain size
* tail - number of tails
int depth, i, j, *n_cycle, p, *pn;
/* depth - depth for check_cycle_size
* n_cycle - number of cycle
* p - current weight
* pn - total weight
*/
if (max_depth > N)
depth = N;
else
depth = max_depth;
n_cycle = (int *) malloc ((max_depth-2) * sizeof (int));
// convert to matrix of weight
pn = (int *) malloc (N * sizeof (int));
n_cycle = (int *) malloc ((depth - 2) * sizeof (int));
for (i=0; i<N; i++)
pn[i] = 0;
for (i=0; i<N; i++)
{
p = 0;
for (j=0; j<N; j++)
p += matrix[i*N+j];
pn[p]++;
}
tail = check_tail (N, matrix);
cycle = check_cycle (N, matrix);
if (cycle > 0)
iso[0] = check_tail (pn);
iso[1] = check_cycle (N, pn);
for (i=2; i<max_depth; i++)
iso[i] = 0;
if (iso[1] > 0)
{
check_cycle_size (N, matrix, depth, n_cycle);
for (i=0; i<depth-2; i++)
n_cycle[i] = check_cycle_size (N, matrix, i+3);
else
for (i=0; i<depth-2; i++)
n_cycle[i] = 0;
for (i=depth-2; i<max_depth-2; i++)
n_cycle[i] = 0;
sprintf (iso, "%i.%i.%i", N, tail, cycle);
for (i=0; i<max_depth-2; i++)
sprintf (iso, "%s.%i", iso, n_cycle[i]);
iso[i+2] = n_cycle[i];
}
free (n_cycle);
free (pn);
return 1;
}