Calculate index and add to feedback arc set

This commit is contained in:
zenon 2018-06-06 17:25:30 +02:00
parent 7766b09f65
commit 9983d25742

View File

@ -200,30 +200,48 @@ int findIndex(int array[], int value, int size)
return (index == size ? -1 : index);
}
void genSolution(int permutation[])
void genSolution(int permutation[], int V)
{
int r;
// initialize array to indices
for (int i = 0; i < graph.V; i++) {
for (int i = 0; i < V; i++) {
permutation[i] = i;
}
// loop from end to start, choose random and switch
// with number at current index.
for (int i = (graph.V - 1); i > 0; i--) {
for (int i = (V - 1); i > 0; i--) {
r = rand() % (i+1);
int tmp = permutation[r];
permutation[r] = permutation[i];
permutation[i] = tmp;
}
for (int u = 0; u < graph.V; u++) {
int edges[MAX_ITEMS] = {-1};
int count = 0;
for (int u = 0; u < V; u++) {
int idx = findIndex(permutation, u, V);
struct AdjListNode *pCrawl = graph.array[u].head;
while (pCrawl) {
int idx_v = findIndex(permutation, pCrawl->dest, V);
if (idx > idx_v) {
edges[count++] = u;
edges[count++] = pCrawl->dest;
if (count > MAX_ITEMS)
return;
break;
}
pCrawl = pCrawl->next;
}
}
printf("[");
for (int i = 0; i < (count - 1); i++) {
printf("%d, ", edges[i]);
}
}
printf("%d]\n", edges[count-1]);
}
int main(int argc, char *argv[])
@ -262,18 +280,14 @@ int main(int argc, char *argv[])
parse(argc, argv);
srand(time(NULL));
// seed differently for each process
srand(time(NULL) * getpid());
int permutation[graph.V];
while (quit == 0) {
for (int i = 0; i < 10; i++) {
genSolution(permutation);
printf("[");
for (int i = 0; i < (graph.V - 1); i++) {
printf("%d, ", permutation[i]);
}
printf("%d]\n", permutation[graph.V-1]);
for (int i = 0; i < 1000000; i++) {
genSolution(permutation, graph.V);
}
break;
}