From b6150ab95ceb736c43fbcd0371e3a2b6916d781c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Slobodan=20Jeli=C4=87?= Date: Tue, 24 Dec 2024 16:23:07 +0100 Subject: [PATCH] pc --- .vscode/settings.json | 3 +- example1.txt | 501 ++++++++++++++++++++++++++++++++++++++++ include/random_graph.h | 1 + include/random_groups.h | 16 ++ src/main.c | 18 +- src/random_groups.c | 87 +++++++ 6 files changed, 624 insertions(+), 2 deletions(-) create mode 100644 example1.txt create mode 100644 include/random_groups.h create mode 100644 src/random_groups.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 9ed3626..9d395b2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "random_graph.h": "c" + "random_graph.h": "c", + "random_groups.h": "c" } } \ No newline at end of file diff --git a/example1.txt b/example1.txt new file mode 100644 index 0000000..4cb9f90 --- /dev/null +++ b/example1.txt @@ -0,0 +1,501 @@ +Generated graph edges: +Edge 0 -> 2 +Edge 0 -> 3 +Edge 0 -> 7 +Edge 0 -> 11 +Edge 0 -> 16 +Edge 0 -> 17 +Edge 0 -> 25 +Edge 0 -> 28 +Edge 0 -> 29 +Edge 0 -> 30 +Edge 0 -> 32 +Edge 0 -> 33 +Edge 0 -> 34 +Edge 0 -> 37 +Edge 0 -> 42 +Edge 0 -> 43 +Edge 0 -> 52 +Edge 0 -> 59 +Edge 1 -> 5 +Edge 1 -> 6 +Edge 1 -> 11 +Edge 1 -> 13 +Edge 1 -> 18 +Edge 1 -> 20 +Edge 1 -> 23 +Edge 1 -> 29 +Edge 1 -> 35 +Edge 1 -> 36 +Edge 1 -> 45 +Edge 1 -> 53 +Edge 1 -> 54 +Edge 1 -> 57 +Edge 1 -> 58 +Edge 1 -> 59 +Edge 2 -> 3 +Edge 2 -> 8 +Edge 2 -> 11 +Edge 2 -> 13 +Edge 2 -> 16 +Edge 2 -> 26 +Edge 2 -> 27 +Edge 2 -> 29 +Edge 2 -> 30 +Edge 2 -> 31 +Edge 2 -> 32 +Edge 2 -> 36 +Edge 2 -> 41 +Edge 2 -> 43 +Edge 2 -> 45 +Edge 2 -> 46 +Edge 2 -> 48 +Edge 2 -> 50 +Edge 2 -> 52 +Edge 3 -> 6 +Edge 3 -> 18 +Edge 3 -> 19 +Edge 3 -> 21 +Edge 3 -> 23 +Edge 3 -> 25 +Edge 3 -> 28 +Edge 3 -> 38 +Edge 3 -> 39 +Edge 3 -> 41 +Edge 3 -> 42 +Edge 3 -> 43 +Edge 3 -> 54 +Edge 3 -> 57 +Edge 4 -> 5 +Edge 4 -> 7 +Edge 4 -> 8 +Edge 4 -> 12 +Edge 4 -> 13 +Edge 4 -> 14 +Edge 4 -> 16 +Edge 4 -> 17 +Edge 4 -> 21 +Edge 4 -> 25 +Edge 4 -> 30 +Edge 4 -> 33 +Edge 4 -> 35 +Edge 4 -> 42 +Edge 4 -> 43 +Edge 4 -> 45 +Edge 4 -> 48 +Edge 4 -> 51 +Edge 4 -> 54 +Edge 4 -> 59 +Edge 5 -> 10 +Edge 5 -> 12 +Edge 5 -> 13 +Edge 5 -> 16 +Edge 5 -> 17 +Edge 5 -> 30 +Edge 5 -> 36 +Edge 5 -> 43 +Edge 5 -> 47 +Edge 5 -> 48 +Edge 5 -> 49 +Edge 5 -> 51 +Edge 5 -> 54 +Edge 5 -> 59 +Edge 6 -> 7 +Edge 6 -> 8 +Edge 6 -> 9 +Edge 6 -> 11 +Edge 6 -> 14 +Edge 6 -> 17 +Edge 6 -> 20 +Edge 6 -> 21 +Edge 6 -> 34 +Edge 6 -> 35 +Edge 6 -> 37 +Edge 6 -> 41 +Edge 6 -> 51 +Edge 6 -> 54 +Edge 6 -> 56 +Edge 6 -> 57 +Edge 6 -> 59 +Edge 7 -> 9 +Edge 7 -> 13 +Edge 7 -> 14 +Edge 7 -> 16 +Edge 7 -> 30 +Edge 7 -> 35 +Edge 7 -> 39 +Edge 7 -> 40 +Edge 7 -> 43 +Edge 7 -> 46 +Edge 7 -> 47 +Edge 7 -> 48 +Edge 7 -> 49 +Edge 8 -> 9 +Edge 8 -> 10 +Edge 8 -> 17 +Edge 8 -> 20 +Edge 8 -> 24 +Edge 8 -> 27 +Edge 8 -> 30 +Edge 8 -> 32 +Edge 8 -> 33 +Edge 8 -> 34 +Edge 8 -> 37 +Edge 8 -> 40 +Edge 8 -> 42 +Edge 8 -> 43 +Edge 8 -> 45 +Edge 8 -> 49 +Edge 8 -> 52 +Edge 8 -> 57 +Edge 8 -> 58 +Edge 9 -> 10 +Edge 9 -> 11 +Edge 9 -> 16 +Edge 9 -> 20 +Edge 9 -> 22 +Edge 9 -> 23 +Edge 9 -> 24 +Edge 9 -> 31 +Edge 9 -> 34 +Edge 9 -> 35 +Edge 9 -> 38 +Edge 9 -> 40 +Edge 9 -> 41 +Edge 9 -> 44 +Edge 9 -> 48 +Edge 9 -> 49 +Edge 9 -> 52 +Edge 10 -> 16 +Edge 10 -> 19 +Edge 10 -> 22 +Edge 10 -> 23 +Edge 10 -> 25 +Edge 10 -> 28 +Edge 10 -> 30 +Edge 10 -> 32 +Edge 10 -> 41 +Edge 10 -> 43 +Edge 10 -> 46 +Edge 10 -> 50 +Edge 10 -> 51 +Edge 10 -> 52 +Edge 10 -> 57 +Edge 11 -> 16 +Edge 11 -> 17 +Edge 11 -> 18 +Edge 11 -> 20 +Edge 11 -> 25 +Edge 11 -> 29 +Edge 11 -> 33 +Edge 11 -> 37 +Edge 11 -> 42 +Edge 11 -> 47 +Edge 11 -> 48 +Edge 12 -> 13 +Edge 12 -> 18 +Edge 12 -> 19 +Edge 12 -> 21 +Edge 12 -> 27 +Edge 12 -> 31 +Edge 12 -> 32 +Edge 12 -> 33 +Edge 12 -> 35 +Edge 12 -> 44 +Edge 12 -> 46 +Edge 12 -> 49 +Edge 12 -> 53 +Edge 12 -> 58 +Edge 12 -> 59 +Edge 13 -> 19 +Edge 13 -> 32 +Edge 13 -> 33 +Edge 13 -> 43 +Edge 13 -> 45 +Edge 13 -> 49 +Edge 13 -> 50 +Edge 13 -> 55 +Edge 14 -> 17 +Edge 14 -> 18 +Edge 14 -> 19 +Edge 14 -> 20 +Edge 14 -> 27 +Edge 14 -> 28 +Edge 14 -> 34 +Edge 14 -> 35 +Edge 14 -> 38 +Edge 14 -> 43 +Edge 14 -> 46 +Edge 15 -> 20 +Edge 15 -> 25 +Edge 15 -> 28 +Edge 15 -> 29 +Edge 15 -> 31 +Edge 15 -> 37 +Edge 15 -> 38 +Edge 15 -> 40 +Edge 15 -> 41 +Edge 15 -> 47 +Edge 15 -> 50 +Edge 15 -> 53 +Edge 16 -> 26 +Edge 16 -> 31 +Edge 16 -> 33 +Edge 16 -> 38 +Edge 16 -> 39 +Edge 16 -> 42 +Edge 16 -> 44 +Edge 16 -> 47 +Edge 16 -> 48 +Edge 16 -> 50 +Edge 16 -> 51 +Edge 16 -> 57 +Edge 17 -> 18 +Edge 17 -> 22 +Edge 17 -> 28 +Edge 17 -> 29 +Edge 17 -> 36 +Edge 17 -> 39 +Edge 17 -> 40 +Edge 17 -> 44 +Edge 17 -> 46 +Edge 17 -> 48 +Edge 17 -> 49 +Edge 17 -> 55 +Edge 17 -> 56 +Edge 18 -> 20 +Edge 18 -> 31 +Edge 18 -> 33 +Edge 18 -> 36 +Edge 18 -> 37 +Edge 18 -> 38 +Edge 18 -> 39 +Edge 18 -> 47 +Edge 18 -> 48 +Edge 18 -> 58 +Edge 19 -> 21 +Edge 19 -> 22 +Edge 19 -> 26 +Edge 19 -> 28 +Edge 19 -> 33 +Edge 19 -> 35 +Edge 19 -> 36 +Edge 19 -> 42 +Edge 19 -> 43 +Edge 19 -> 44 +Edge 19 -> 47 +Edge 19 -> 53 +Edge 19 -> 54 +Edge 20 -> 23 +Edge 20 -> 25 +Edge 20 -> 27 +Edge 20 -> 32 +Edge 20 -> 35 +Edge 20 -> 38 +Edge 20 -> 40 +Edge 20 -> 44 +Edge 20 -> 51 +Edge 20 -> 58 +Edge 21 -> 25 +Edge 21 -> 30 +Edge 21 -> 33 +Edge 21 -> 38 +Edge 21 -> 39 +Edge 21 -> 51 +Edge 21 -> 52 +Edge 21 -> 55 +Edge 21 -> 56 +Edge 21 -> 57 +Edge 21 -> 58 +Edge 22 -> 50 +Edge 22 -> 51 +Edge 22 -> 54 +Edge 23 -> 25 +Edge 23 -> 27 +Edge 23 -> 34 +Edge 23 -> 36 +Edge 23 -> 47 +Edge 23 -> 48 +Edge 23 -> 51 +Edge 23 -> 54 +Edge 23 -> 55 +Edge 23 -> 56 +Edge 23 -> 59 +Edge 24 -> 26 +Edge 24 -> 27 +Edge 24 -> 32 +Edge 24 -> 33 +Edge 24 -> 41 +Edge 24 -> 45 +Edge 24 -> 47 +Edge 24 -> 48 +Edge 24 -> 49 +Edge 24 -> 52 +Edge 24 -> 53 +Edge 24 -> 58 +Edge 25 -> 26 +Edge 25 -> 31 +Edge 25 -> 34 +Edge 25 -> 36 +Edge 25 -> 37 +Edge 25 -> 39 +Edge 25 -> 48 +Edge 25 -> 53 +Edge 25 -> 54 +Edge 25 -> 55 +Edge 25 -> 57 +Edge 26 -> 33 +Edge 26 -> 39 +Edge 26 -> 41 +Edge 26 -> 43 +Edge 26 -> 47 +Edge 26 -> 50 +Edge 27 -> 29 +Edge 27 -> 30 +Edge 27 -> 31 +Edge 27 -> 32 +Edge 27 -> 38 +Edge 27 -> 39 +Edge 27 -> 43 +Edge 27 -> 44 +Edge 27 -> 46 +Edge 27 -> 51 +Edge 27 -> 55 +Edge 27 -> 59 +Edge 28 -> 29 +Edge 28 -> 30 +Edge 28 -> 31 +Edge 28 -> 36 +Edge 28 -> 44 +Edge 28 -> 45 +Edge 28 -> 58 +Edge 29 -> 32 +Edge 29 -> 34 +Edge 29 -> 41 +Edge 29 -> 42 +Edge 29 -> 45 +Edge 29 -> 47 +Edge 29 -> 51 +Edge 29 -> 52 +Edge 29 -> 59 +Edge 30 -> 31 +Edge 30 -> 38 +Edge 30 -> 43 +Edge 30 -> 44 +Edge 30 -> 52 +Edge 30 -> 57 +Edge 31 -> 35 +Edge 31 -> 39 +Edge 31 -> 44 +Edge 31 -> 45 +Edge 31 -> 51 +Edge 31 -> 58 +Edge 31 -> 59 +Edge 32 -> 34 +Edge 32 -> 35 +Edge 32 -> 36 +Edge 32 -> 42 +Edge 32 -> 52 +Edge 32 -> 55 +Edge 33 -> 34 +Edge 33 -> 35 +Edge 33 -> 37 +Edge 33 -> 41 +Edge 33 -> 53 +Edge 33 -> 55 +Edge 34 -> 35 +Edge 34 -> 36 +Edge 34 -> 37 +Edge 34 -> 38 +Edge 34 -> 43 +Edge 34 -> 59 +Edge 35 -> 36 +Edge 35 -> 37 +Edge 35 -> 39 +Edge 35 -> 41 +Edge 35 -> 47 +Edge 35 -> 51 +Edge 35 -> 52 +Edge 35 -> 56 +Edge 36 -> 39 +Edge 36 -> 43 +Edge 36 -> 45 +Edge 36 -> 47 +Edge 36 -> 52 +Edge 36 -> 53 +Edge 36 -> 55 +Edge 36 -> 59 +Edge 37 -> 39 +Edge 37 -> 44 +Edge 37 -> 48 +Edge 37 -> 51 +Edge 37 -> 56 +Edge 37 -> 57 +Edge 38 -> 40 +Edge 38 -> 43 +Edge 38 -> 46 +Edge 38 -> 56 +Edge 38 -> 58 +Edge 39 -> 40 +Edge 39 -> 46 +Edge 39 -> 49 +Edge 39 -> 51 +Edge 39 -> 52 +Edge 39 -> 55 +Edge 39 -> 57 +Edge 40 -> 47 +Edge 40 -> 48 +Edge 40 -> 52 +Edge 40 -> 54 +Edge 41 -> 50 +Edge 41 -> 52 +Edge 41 -> 57 +Edge 41 -> 58 +Edge 42 -> 46 +Edge 42 -> 48 +Edge 42 -> 56 +Edge 42 -> 59 +Edge 43 -> 48 +Edge 43 -> 53 +Edge 43 -> 54 +Edge 43 -> 59 +Edge 44 -> 47 +Edge 44 -> 51 +Edge 44 -> 52 +Edge 44 -> 54 +Edge 44 -> 55 +Edge 45 -> 48 +Edge 45 -> 49 +Edge 45 -> 51 +Edge 45 -> 52 +Edge 45 -> 54 +Edge 45 -> 57 +Edge 45 -> 58 +Edge 46 -> 47 +Edge 46 -> 50 +Edge 46 -> 54 +Edge 46 -> 55 +Edge 47 -> 48 +Edge 47 -> 52 +Edge 47 -> 59 +Edge 48 -> 52 +Edge 48 -> 53 +Edge 48 -> 56 +Edge 48 -> 58 +Edge 49 -> 53 +Edge 49 -> 54 +Edge 49 -> 59 +Edge 50 -> 52 +Edge 50 -> 53 +Edge 50 -> 57 +Edge 50 -> 59 +Edge 51 -> 54 +Edge 51 -> 58 +Edge 52 -> 54 +Edge 52 -> 55 +Edge 52 -> 59 +Edge 54 -> 57 +Edge 54 -> 58 +Edge 56 -> 57 +Edge 56 -> 58 +Edge 57 -> 58 diff --git a/include/random_graph.h b/include/random_graph.h index 706ca50..6d1abe8 100644 --- a/include/random_graph.h +++ b/include/random_graph.h @@ -12,6 +12,7 @@ typedef struct Edge { } Edge; // Function prototypes +int cmpInt(const void *a, const void *b); unsigned int uv2index(unsigned int u, unsigned int v, unsigned int n); unsigned int min(unsigned int u, unsigned int v); unsigned int max(unsigned int u, unsigned int v); diff --git a/include/random_groups.h b/include/random_groups.h new file mode 100644 index 0000000..8c291f9 --- /dev/null +++ b/include/random_groups.h @@ -0,0 +1,16 @@ +#ifndef RANDOM_GROUPS_H +#define RANDOM_GROUPS_H + +#include +#include + +// Struct for representing groups +typedef struct { + unsigned int *group; // Pointer to an array of group members + unsigned int size; // Number of members in the group +} Group; + +// Function to generate random groups +void generate_random_groups(unsigned int n, unsigned int k, Group **groups); + +#endif // RANDOM_GROUPS_H diff --git a/src/main.c b/src/main.c index c25a1f7..ec43c65 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,10 @@ #include #include #include "random_graph.h" +#include "random_groups.h" int main() { - unsigned int n = 5000, m = 10000, seed = 0; + unsigned int n = 20, m = 80, k = 4, seed = 5; Edge *edges = malloc(m * sizeof(Edge)); if (!edges) { fprintf(stderr, "Error: Memory allocation of EDGE array: edges FAILED.\n"); @@ -17,6 +18,21 @@ int main() { printf("Edge %d -> %d\n", edges[i].src, edges[i].dest); } + + Group *groups; + generate_random_groups(n, k, &groups); + + // Print groups + printf("Random groups:\n"); + for (unsigned int i = 0; i < k; i++) { + printf("Group %u: ", i); + for (unsigned int j = 0; j < groups[i].size; j++) { + printf("%u ", groups[i].group[j]); + } + printf("\n"); + free(groups[i].group); // Free each group's array + } + free(groups); // Free the groups array free(edges); return 0; } diff --git a/src/random_groups.c b/src/random_groups.c new file mode 100644 index 0000000..53c0af3 --- /dev/null +++ b/src/random_groups.c @@ -0,0 +1,87 @@ +#include "random_groups.h" +#include "random_graph.h" + +// Helper function to shuffle an array +void shuffle(unsigned int *array, unsigned int size) { + for (unsigned int i = size - 1; i > 0; i--) { + unsigned int j = rand() % (i + 1); + unsigned int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +} + + +void generate_random_groups(unsigned int n, unsigned int k, Group **groups_out) { + if (k > n) { + fprintf(stderr, "Error: Number of groups (k) cannot exceed the number of vertices (n).\n"); + exit(EXIT_FAILURE); + } + + // Allocate memory for array N (vertices) and initialize + unsigned int *N = (unsigned int *)malloc(n * sizeof(unsigned int)); + if (!N) { + fprintf(stderr, "Error: Memory allocation failed for array N.\n"); + exit(EXIT_FAILURE); + } + for (unsigned int i = 0; i < n; i++) { + N[i] = i; + } + + // Allocate memory for groups + Group *groups = (Group *)malloc(k * sizeof(Group)); + if (!groups) { + fprintf(stderr, "Error: Memory allocation failed for groups.\n"); + free(N); + exit(EXIT_FAILURE); + } + for (unsigned int i = 0; i < k; i++) { + groups[i].group = (unsigned int *)malloc(n * sizeof(unsigned int)); + groups[i].size = 0; + if (!groups[i].group) { + fprintf(stderr, "Error: Memory allocation failed for group %u.\n", i); + for (unsigned int j = 0; j < i; j++) { + free(groups[j].group); + } + free(groups); + free(N); + exit(EXIT_FAILURE); + } + } + + // Generate a random float f in the range (0, 0.5) + float f; + do { + f = (float)rand() / RAND_MAX; + } while (f <= 0.0 || f >= 0.5); + + unsigned int offset = (unsigned int)(f * n); + + // Shuffle the array N + shuffle(N, n); + + // Distribute elements to the first part of each group + for (unsigned int i = 0; i < k; i++) { + groups[i].group[groups[i].size++] = N[offset + i]; + } + + // Distribute remaining elements to groups + for (unsigned int i = 0; i < k; i++) { + unsigned int start = offset + k; + unsigned int end = offset + k + (n - offset) / k - 1; + shuffle(N + start, end - start + 1); + + for (unsigned int j = start; j <= end; j++) { + groups[i].group[groups[i].size++] = N[j]; + } + } + + // Sort each group + for (unsigned int i = 0; i < k; i++) { + qsort(groups[i].group, groups[i].size, sizeof(unsigned int), cmpInt); + } + + // Clean up + free(N); + *groups_out = groups; +}