Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 6b75169

Browse files
committed
Print test modularity
1 parent 23c924c commit 6b75169

12 files changed

+95
-52
lines changed

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ set(CMAKE_C_STANDARD 90)
55
set(GCC_COVERAGE_COMPILE_FLAGS "-ansi -Wall -Wextra -Werror -pedantic-errors")
66
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
77

8-
add_executable(cluster cluster.c spmat.c matrix.h matrix.c graph.h graph.c LinkedList.h LinkedList.c VerticesGroup.h VerticesGroup.c division.h division.c defs.h defs.c ErrorHandler.c ErrorHandler.h quicksort.h quicksort.c tests/graphs/testUtils.c tests/graphs/testUtils.h)
9-
add_executable(tester tests/tester.h tests/tester.c spmat.c matrix.h matrix.c graph.h graph.c LinkedList.h LinkedList.c VerticesGroup.h VerticesGroup.c division.h division.c defs.h defs.c ErrorHandler.c ErrorHandler.h quicksort.h quicksort.c)
8+
add_executable(cluster cluster.c spmat.c matrix.h matrix.c graph.h graph.c LinkedList.h LinkedList.c VerticesGroup.h VerticesGroup.c division.h division.c defs.h defs.c ErrorHandler.c ErrorHandler.h quicksort.h quicksort.c)
9+
add_executable(tester tests/tester.h tests/tester.c spmat.c matrix.h matrix.c graph.h graph.c LinkedList.h LinkedList.c VerticesGroup.h VerticesGroup.c division.h division.c defs.h defs.c ErrorHandler.c ErrorHandler.h quicksort.h quicksort.c tests/testUtils.c tests/testUtils.h)

LinkedList.c

-35
Original file line numberDiff line numberDiff line change
@@ -93,38 +93,3 @@ void removeItem(LinkedList *list, LinkedListNode *item){
9393
list->length = 0;
9494
}
9595
}
96-
97-
/**
98-
* Prints the vertex-coloring a given group-list defines over
99-
* a n-vertices graph.
100-
* @param groupList a partition of graph vertices into groups.
101-
* @param n the number of vertices in the graph.
102-
*/
103-
void printGroupList(LinkedList *groupList, int n){
104-
int L = groupList->length;
105-
int *coloring = malloc(sizeof(int) * n);
106-
int i, j;
107-
LinkedListNode *node = groupList->first;
108-
VerticesGroup *group;
109-
VertexNode *vertex;
110-
assertMemoryAllocation(coloring);
111-
112-
for(i = 0; i < L; ++i){
113-
group = node->pointer;
114-
vertex = group->first;
115-
for(j = 0; j < group->size; ++j){
116-
coloring[vertex->index] = i;
117-
vertex = vertex->next;
118-
}
119-
node = node->next;
120-
}
121-
122-
printf("[");
123-
if(n>0)
124-
printf("%d", coloring[0]);
125-
for(i = 1; i < n; ++i)
126-
printf(",%d", coloring[i]);
127-
printf("]\n");
128-
129-
free(coloring);
130-
}

LinkedList.h

-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ void *insertItem(LinkedList *list, void *pointer);
2323

2424
void removeItem(LinkedList *list, LinkedListNode *item);
2525

26-
void printGroupList(LinkedList *groupList, int n);
27-
2826
#endif

VerticesGroup.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ void calculateModularitySubMatrix(Graph *G, VerticesGroup *group) {
137137
* (otherwise, we'll have to sort it later).
138138
* @param group a non-empty VerticesGroup.
139139
*/
140-
void fillVerticesArr(VerticesGroup *group){
140+
void fillVerticesArr(VerticesGroup *group) {
141141
VertexNode *node = group->first;
142142
int i = 0;
143143
char isSorted = 1;
144144
int prevIndex = node->index;
145145
group->verticesArr = malloc(sizeof(int) * group->size);
146146
assertMemoryAllocation(group->verticesArr);
147147
do {
148-
if(node->index < prevIndex)
148+
if (node->index < prevIndex)
149149
isSorted = 0;
150150
group->verticesArr[i] = node->index;
151151
++i;
@@ -168,6 +168,9 @@ double multiplyModularityByVector(Graph *G, VerticesGroup *group, double *s, dou
168168
double numRes = 0;
169169
/* the common value of all rows of the multiplication of the expectedEdges (K) matrix by s */
170170
double degreesCommon = 0, modularityNorm1 = getModularityMatrixNorm1(group);
171+
if (G->degreeSum == 0) {
172+
return 0;
173+
}
171174
group->edgeSubMatrix->mult(group->edgeSubMatrix, s, res);
172175
for (i = 0; i < group->size; i++) {
173176
degreesCommon += G->degrees[i] * s[i];

cluster.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
#include "VerticesGroup.h"
77
#include "LinkedList.h"
88
#include "division.h"
9+
#include "defs.h"
910

1011
int main() {
1112
int i = 1, j;
1213
LinkedList *groupsLst;
1314
LinkedListNode *node;
1415
VerticesGroup *group;
1516
VertexNode *vNode;
16-
Graph *G = constructGraphFromInput(
17-
"graph.in");
17+
Graph *G = constructGraphFromInput(GRAPH_FILE_PATH);
1818
srand(time(0));
1919
groupsLst = divisionAlgorithm(G);
2020
node = groupsLst->first;

defs.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CLUSTER_DEFS_H
22
#define CLUSTER_DEFS_H
33

4+
#define GRAPH_FILE_PATH "C:\\Users\\royar\\Source\\Workspaces\\Workspace\\C projects\\cproject-cluster\\graph.in"
45
#define IS_POSITIVE(X) ((X)>0.0001)
56

67
#endif

division.c

+26
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,29 @@ void saveOutputToFile(LinkedList *groupLst, char *output_path) {
202202
}
203203
fclose(output_file);
204204
}
205+
206+
207+
/**
208+
* Calculate modularity of a graph's division
209+
* @param groupLst
210+
* @return modularity
211+
*/
212+
double calculateDivisionModularity(Graph *G, LinkedList *groupLst) {
213+
double *s = malloc(sizeof(double) * G->n);
214+
VerticesGroup *group;
215+
double modularity = 0;
216+
int i;
217+
LinkedListNode *node = groupLst->first;
218+
for (i = 0; i < G->n; i++) {
219+
s[i] = 1;
220+
}
221+
if (node != NULL) {
222+
do {
223+
group = node->pointer;
224+
calculateModularitySubMatrix(G, group);
225+
modularity += calculateModularity(G, group, s);
226+
node = node->next;
227+
} while (node != groupLst->first);
228+
}
229+
return modularity;
230+
}

division.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ void divisionAlgRec(Graph *G, VerticesGroup *group, LinkedList *groupsLst, doubl
1818

1919
void saveOutputToFile(LinkedList *groupLst, char *output_path);
2020

21+
double calculateDivisionModularity(Graph *G, LinkedList *groupLst);
22+
2123
#endif

tests/graphs/testUtils.c tests/testUtils.c

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <stdio.h>
2+
#include <stdlib.h>
23
#include "testUtils.h"
3-
#include "../../spmat.h"
4+
#include "../spmat.h"
5+
#include "../ErrorHandler.h"
46

57
/**
68
* Print matrix (python style)
@@ -120,3 +122,38 @@ void printSpmat(spmat *spm) {
120122
printf("\n");
121123
}
122124
}
125+
126+
/**
127+
* Prints the vertex-coloring a given group-list defines over
128+
* a n-vertices graph.
129+
* @param groupList a partition of graph vertices into groups.
130+
* @param n the number of vertices in the graph.
131+
*/
132+
void printGroupList(LinkedList *groupList, int n){
133+
int L = groupList->length;
134+
int *coloring = malloc(sizeof(int) * n);
135+
int i, j;
136+
LinkedListNode *node = groupList->first;
137+
VerticesGroup *group;
138+
VertexNode *vertex;
139+
assertMemoryAllocation(coloring);
140+
141+
for(i = 0; i < L; ++i){
142+
group = node->pointer;
143+
vertex = group->first;
144+
for(j = 0; j < group->size; ++j){
145+
coloring[vertex->index] = i;
146+
vertex = vertex->next;
147+
}
148+
node = node->next;
149+
}
150+
151+
printf("[");
152+
if(n>0)
153+
printf("%d", coloring[0]);
154+
for(i = 1; i < n; ++i)
155+
printf(",%d", coloring[i]);
156+
printf("]\n");
157+
158+
free(coloring);
159+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#ifndef CLUSTER_TESTUTILS_H
22
#define CLUSTER_TESTUTILS_H
33

4-
#include "../../matrix.h"
4+
#include "../matrix.h"
5+
#include "../LinkedList.h"
56

67
void printMatrix(Matrix *mat);
78

89
void printMatrixPy(Matrix *mat);
910

1011
void printVect(double *vector, int length);
1112

13+
void printGroupList(LinkedList *groupList, int n);
14+
1215
#endif

tests/tester.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdlib.h>
22
#include "tester.h"
33
#include "../ErrorHandler.h"
4-
#include "../VerticesGroup.h"
4+
#include "testUtils.h"
55
#include <time.h>
66
#include <stdio.h>
77

@@ -82,21 +82,28 @@ Graph *generateCommunitiesGraph(LinkedList *GroupList, int n, char noise) {
8282
* @param n The number of vertices G consists of.
8383
* @return 1-if the partitions are equivalent. 0-otherwise.
8484
*/
85-
char checkGroupListsEquality(LinkedList *GroupList1, LinkedList *GroupList2, int n) {
86-
int i = 0, j = 0;
85+
char checkGroupListsEquality(LinkedList *GroupList1, testGraph *TG) {
86+
int i = 0, j = 0, n = TG->G->n;
87+
double modularity;
88+
LinkedList *GroupList2 = TG->GroupList;
8789
LinkedListNode *node1 = GroupList1->first, *node2 = GroupList2->first;
8890
VerticesGroup *G1, *G2;
8991
VertexNode *vertex1, *vertex2;
9092
int *coloring1, *coloring2, *colorMapping;
9193
int n1 = 0, n2 = 0;
9294
int numberOfGroups = GroupList1->length;
95+
modularity = calculateDivisionModularity(TG->G, GroupList1);
96+
printf("Found modularity: %f\n", modularity);
97+
modularity = calculateDivisionModularity(TG->G, GroupList2);
98+
printf("Expected modularity: %f\n", modularity);
9399
if (numberOfGroups != GroupList2->length) {
94100
printf("The first partition consists of %d groups, while the second consists of %d groups.\n",
95101
GroupList1->length, GroupList2->length);
96102
printf("Coloring1: ");
97103
printGroupList(GroupList1, n);
98104
printf("Coloring2: ");
99105
printGroupList(GroupList2, n);
106+
100107
return 0;
101108
}
102109
coloring1 = (int *) malloc(n * sizeof(int));
@@ -236,7 +243,7 @@ void destroyTestGraph(testGraph *TG) {
236243
*/
237244
char performTest(testGraph *TG) {
238245
LinkedList *result = divisionAlgorithm(TG->G);
239-
return checkGroupListsEquality(result, TG->GroupList, TG->G->n);
246+
return checkGroupListsEquality(result, TG);
240247
}
241248

242249
/**

tests/tester.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#ifndef CLUSTER_TESTER_H
22
#define CLUSTER_TESTER_H
3+
34
#include "../division.h"
45

5-
typedef struct _testGraph{
6+
typedef struct _testGraph {
67
Graph *G;
78
LinkedList *GroupList;
89
} testGraph;
910

1011
Graph *generateCommunitiesGraph(LinkedList *GroupList, int n, char noise);
1112

12-
char checkGroupListsEquality(LinkedList *GroupList1, LinkedList *GroupList2, int n);
13+
char checkGroupListsEquality(LinkedList *GroupList1, testGraph *TG);
1314

1415
testGraph *createTestGraph(int edges[][2], int length, int n, LinkedList *GroupList);
1516

@@ -19,6 +20,6 @@ char performTest(testGraph *TG);
1920

2021
char testGraphFromFile(char *path);
2122

22-
void printResultsFromOutputFile(char* output_file_path);
23+
void printResultsFromOutputFile(char *output_file_path);
2324

2425
#endif

0 commit comments

Comments
 (0)