Implement random cluster generation
This commit is contained in:
123
src/geometry.c
Normal file
123
src/geometry.c
Normal file
@@ -0,0 +1,123 @@
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <limits.h>
|
||||
#include "geometry.h"
|
||||
#include "util.h"
|
||||
|
||||
/* function for creating a random set of points in unit square */
|
||||
|
||||
int generate_random_points_2d(
|
||||
int node_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates)
|
||||
{
|
||||
int rval = 0, i, j, winner, x, y;
|
||||
int **hit = 0, *hit_count = 0;
|
||||
|
||||
hit = (int **) malloc(grid_size * sizeof(int *));
|
||||
ABORT_IF (!hit, "out of memory for hit\n");
|
||||
|
||||
for (i = 0; i < grid_size; i++)
|
||||
hit[i] = 0;
|
||||
|
||||
hit_count = (int *) malloc(grid_size * sizeof(int));
|
||||
ABORT_IF(!hit_count, "out of memory for hit_count\n");
|
||||
|
||||
for (i = 0; i < grid_size; i++)
|
||||
hit_count[i] = 0;
|
||||
|
||||
for (i = 0; i < node_count; i++)
|
||||
{
|
||||
winner = 0;
|
||||
do
|
||||
{
|
||||
x = rand() % grid_size;
|
||||
y = rand() % grid_size;
|
||||
|
||||
/* check to see if (x,y) is a duplicate point */
|
||||
for (j = 0; j < hit_count[x]; j++)
|
||||
if (hit[x][j] == y) break;
|
||||
|
||||
if (j == hit_count[x])
|
||||
{
|
||||
void *tmp_ptr = (void *) hit[x];
|
||||
tmp_ptr = realloc(tmp_ptr, (hit_count[x] + 1) * sizeof(int));
|
||||
ABORT_IF (!tmp_ptr, "could not reallocate hit_count\n");
|
||||
|
||||
hit[x] = (int *) tmp_ptr;
|
||||
hit[x][hit_count[x]] = y;
|
||||
hit_count[x]++;
|
||||
winner = 1;
|
||||
}
|
||||
} while (!winner);
|
||||
|
||||
x_coordinates[i] = (double) x;
|
||||
y_coordinates[i] = (double) y;
|
||||
}
|
||||
|
||||
CLEANUP:
|
||||
|
||||
if (hit)
|
||||
{
|
||||
for (i = 0; i < grid_size; i++)
|
||||
if (hit[i]) free(hit[i]);
|
||||
|
||||
free(hit);
|
||||
}
|
||||
if (hit_count) free(hit_count);
|
||||
return rval;
|
||||
}
|
||||
|
||||
int generate_random_clusters_2d(
|
||||
int node_count,
|
||||
int cluster_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int *clusters)
|
||||
{
|
||||
int rval = 0;
|
||||
|
||||
rval = generate_random_points_2d(node_count, grid_size, x_coordinates,
|
||||
y_coordinates);
|
||||
ABORT_IF(rval, "generate_random_points_2d failed");
|
||||
|
||||
for (int i = 0; i < cluster_count; i++)
|
||||
clusters[i] = i;
|
||||
|
||||
for (int i = cluster_count; i < node_count; i++)
|
||||
{
|
||||
int closest_point = 0;
|
||||
int closest_distance = INT_MAX;
|
||||
|
||||
for (int j = 0; j < cluster_count; j++)
|
||||
{
|
||||
int distance =
|
||||
get_euclidean_distance(x_coordinates, y_coordinates, i, j);
|
||||
|
||||
if (distance < closest_distance)
|
||||
{
|
||||
closest_distance = distance;
|
||||
closest_point = j;
|
||||
}
|
||||
}
|
||||
|
||||
clusters[i] = closest_point;
|
||||
}
|
||||
|
||||
CLEANUP:
|
||||
return rval;
|
||||
}
|
||||
|
||||
int get_euclidean_distance(
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int p1_index,
|
||||
int p2_index)
|
||||
{
|
||||
double t1 = x_coordinates[p1_index] - x_coordinates[p2_index];
|
||||
double t2 = y_coordinates[p1_index] - y_coordinates[p2_index];
|
||||
return (int) (sqrt(t1 * t1 + t2 * t2) + 0.5);
|
||||
}
|
||||
24
src/geometry.h
Normal file
24
src/geometry.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef _PROJECT_GEOMETRY_H_
|
||||
#define _PROJECT_GEOMETRY_H_
|
||||
|
||||
int generate_random_points_2d(
|
||||
int node_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates);
|
||||
|
||||
int generate_random_clusters_2d(
|
||||
int node_count,
|
||||
int cluster_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int *clusters);
|
||||
|
||||
int get_euclidean_distance(
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int p1_index,
|
||||
int p2_index);
|
||||
|
||||
#endif //_PROJECT_GEOMETRY_H_
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <malloc.h>
|
||||
#include <math.h>
|
||||
#include "main.h"
|
||||
#include "graph.h"
|
||||
#include "util.h"
|
||||
@@ -102,12 +101,6 @@ int graph_build(int node_count, int edge_count, int *edge_list, struct Graph *G)
|
||||
return rval;
|
||||
}
|
||||
|
||||
int euclid_edgelen(int i, int j, double *x, double *y)
|
||||
{
|
||||
double t1 = x[i] - x[j], t2 = y[i] - y[j];
|
||||
return (int) (sqrt(t1 * t1 + t2 * t2) + 0.5);
|
||||
}
|
||||
|
||||
void get_delta(
|
||||
int island_node_count,
|
||||
int *island_nodes,
|
||||
|
||||
@@ -37,8 +37,6 @@ void graph_free(struct Graph *G);
|
||||
int graph_build
|
||||
(int node_count, int edge_count, int *edge_list, struct Graph *G);
|
||||
|
||||
int euclid_edgelen(int i, int j, double *x, double *y);
|
||||
|
||||
void get_delta(
|
||||
int nsize,
|
||||
int *nlist,
|
||||
|
||||
87
src/util.c
87
src/util.c
@@ -1,10 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/resource.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#include "util.h"
|
||||
|
||||
double get_current_time(void)
|
||||
double get_current_time()
|
||||
{
|
||||
struct rusage ru;
|
||||
|
||||
@@ -14,88 +14,9 @@ double get_current_time(void)
|
||||
+ ((double) ru.ru_utime.tv_usec) / 1000000.0;
|
||||
}
|
||||
|
||||
/* function for creating a random set of points in unit square */
|
||||
|
||||
int build_random_2d_points(
|
||||
int node_count, double *x_list, double *y_list, int grid_size)
|
||||
double get_real_time()
|
||||
{
|
||||
int rval = 0, i, j, winner, x, y;
|
||||
int **hit = (int **) NULL, *hitcount = (int *) NULL;
|
||||
|
||||
printf("Random %d point set, grid_size = %d\n", node_count, grid_size);
|
||||
fflush(stdout);
|
||||
|
||||
hit = (int **) malloc(grid_size * sizeof(int *));
|
||||
if (!hit)
|
||||
{
|
||||
fprintf(stderr, "out of memory for hit\n");
|
||||
rval = 1;
|
||||
goto CLEANUP;
|
||||
}
|
||||
for (i = 0; i < grid_size; i++) hit[i] = (int *) NULL;
|
||||
|
||||
hitcount = (int *) malloc(grid_size * sizeof(int));
|
||||
if (!hitcount)
|
||||
{
|
||||
fprintf(stderr, "out of memory for hitcount\n");
|
||||
rval = 1;
|
||||
goto CLEANUP;
|
||||
}
|
||||
for (i = 0; i < grid_size; i++) hitcount[i] = 0;
|
||||
|
||||
for (i = 0; i < node_count; i++)
|
||||
{
|
||||
winner = 0;
|
||||
do
|
||||
{
|
||||
x = (int) (rand() % grid_size);
|
||||
y = (int) (rand() % grid_size);
|
||||
|
||||
/* check to see if (x,y) is a duplicate point */
|
||||
|
||||
for (j = 0; j < hitcount[x]; j++)
|
||||
{
|
||||
if (hit[x][j] == y) break;
|
||||
}
|
||||
if (j == hitcount[x])
|
||||
{
|
||||
void *tmp_ptr = (void *) hit[x];
|
||||
tmp_ptr = realloc(tmp_ptr, (hitcount[x] + 1) * sizeof(int));
|
||||
if (!tmp_ptr)
|
||||
{
|
||||
fprintf(stderr, "out of member in realloc of hit\n");
|
||||
rval = 1;
|
||||
goto CLEANUP;
|
||||
}
|
||||
hit[x] = (int *) tmp_ptr;
|
||||
hit[x][hitcount[x]] = y;
|
||||
hitcount[x]++;
|
||||
winner = 1;
|
||||
}
|
||||
if (!winner)
|
||||
{
|
||||
printf("X");
|
||||
fflush(stdout);
|
||||
}
|
||||
} while (!winner);
|
||||
x_list[i] = (double) x;
|
||||
y_list[i] = (double) y;
|
||||
}
|
||||
|
||||
CLEANUP:
|
||||
|
||||
printf("\n");
|
||||
|
||||
if (hit)
|
||||
{
|
||||
for (i = 0; i < grid_size; i++)
|
||||
{
|
||||
if (hit[i]) free(hit[i]);
|
||||
}
|
||||
free(hit);
|
||||
}
|
||||
if (hitcount) free(hitcount);
|
||||
return rval;
|
||||
return (double) time (0);
|
||||
}
|
||||
|
||||
static double initial_time = 0;
|
||||
|
||||
@@ -6,10 +6,7 @@
|
||||
|
||||
double get_current_time(void);
|
||||
|
||||
int build_random_2d_points
|
||||
(int node_count, double *x_list, double *y_list, int grid_size);
|
||||
|
||||
double get_current_time(void);
|
||||
double get_real_time();
|
||||
|
||||
void time_printf(const char *fmt, ...);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user