-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm4.c
166 lines (132 loc) · 4.31 KB
/
algorithm4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* algorithm4.c - Source File
* Implementation of the functions declared in the header file
*/
#define POSITIVE(X) ((X) > 0.00001)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "algorithm4.h"
#include "graph.h"
#include "BHatMatrix.h"
#include "errorHandler.h"
/* --------Functions Deceleration--------- */
void algorithm4(BHatMatrix*, graph*, double*);
void nullifyVector(int*, int);
/* --------Functions Implementation---------*/
void algorithm4(BHatMatrix *B,graph *G, double *s)
{
/*Variables deceleration*/
int i, j, n, maxScoreIteration = 0, maxImproveIteration = 0, lastMovedIndex, currDeg, currNodeValue;
int *nodes, *indices, *unmoved;
double maxScore, maxImprove, currScoreValue, dQ, improve, valueInDMatrix;
double *score, *Bg_s;
n = G -> n;
nodes = G -> graph_nodes;
dQ = 0;
/*Initializing vectors */
score = (double *)malloc(n * sizeof(double));
if(score == NULL) returnErrorByType(4);
indices = (int *)malloc(n * sizeof(int));
if(indices == NULL) returnErrorByType(4);
Bg_s = (double *)malloc(n * sizeof(double));
if(Bg_s == NULL) returnErrorByType(4);
unmoved = (int *)malloc(n * sizeof(int));
if(unmoved == NULL) returnErrorByType(4);
/*Iterating until no further improvement is found*/
do {
/*Setting (-inf) initial value to maxImprove variable*/
maxImprove = -HUGE_VAL;
/*Nullifying the unmoved list*/
nullifyVector(unmoved, n);
/*Trying to find an improvement of the current partition defined by s*/
for(i = 0; i < n; i++) {
/*Computing DQ for the move of each unmoved vertex*/
maxScore = -HUGE_VAL;
/*Setting initial values to the score array, only in the first iteration (i == 0)*/
if(i == 0)
{
/*Performing multiplication of B[g] * s */
BgMult(B, G, s, Bg_s);
/*Initializing values to the score array, according to B[g] * s vector*/
for(j = 0; j < n; j++)
{
currNodeValue = *nodes;
currDeg = *(B -> degrees + currNodeValue);
valueInDMatrix = B -> constM * currDeg * currDeg;
/*Calculating the following: score[j] = -2 * (s[j] * Bg_s[j] + D[j][j])*/
currScoreValue = -2 * (*(s + currNodeValue) * (*Bg_s) + valueInDMatrix);
*score = currScoreValue;
/*Finding the maximum score, and the corresponding node index*/
if(currScoreValue > maxScore){
maxScore = currScoreValue;
maxScoreIteration = j;
}
score++;
nodes++;
Bg_s++;
}
score -= n;
nodes -= n;
Bg_s -= n;
}
else
{
/*Updating the score array*/
for (j = 0; j < n; j++) {
if (*unmoved == 0) {
currNodeValue = *(nodes + j);
/* Calculating the following:
* k is the index of the last moved node.
* score[j] = score[j] - 4 * (s[j] * s[k] * B_g[j][k])*/
currScoreValue = *score - 4 * ( *(s + currNodeValue) * (*(s + *(nodes + lastMovedIndex))) *
calcBgPlace(B, currNodeValue, *(nodes + lastMovedIndex)));
*score = currScoreValue;
/*Finding the maximum score, and the corresponding node index*/
if(currScoreValue > maxScore) {
maxScore = currScoreValue;
maxScoreIteration = j;
}
}
score++;
unmoved++;
}
score -= n;
unmoved -= n;
}
/*Moving the vertex corresponding to the maximal score*/
*(s + *(nodes + maxScoreIteration)) *= -1;
/*Updating the indices array, according to the previous node*/
*(indices + i) = maxScoreIteration;
/*Updating the last moved index*/
lastMovedIndex = maxScoreIteration;
/*Marking the maxScoreIteration node as moved*/
*(unmoved + maxScoreIteration) = 1;
/*Updating the current improvement*/
improve = i == 0 ? maxScore : improve + maxScore;
/*Updating the maximal improvement in the algorithm*/
if(improve > maxImprove) {
maxImprove = improve;
maxImproveIteration = i;
}
}
/*Updating the "s" vector representing the current division, according to the indices array*/
for(i = n - 1; i > maxImproveIteration; i--)
*(s + *(nodes + *(indices + i))) *= -1;
/*Updating the DQ value according to the current iteration*/
dQ = maxImproveIteration == n - 1 ? 0 : maxImprove;
} while(POSITIVE(dQ));
/*Frees all the allocated vectors*/
free(unmoved);
free(Bg_s);
free(score);
free(indices);
}
void nullifyVector(int *unmoved, int n){
int i;
for(i = 0; i <n; i++)
{
*unmoved = 0;
unmoved++;
}
}