| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#define STANDALONE |
| 14 |
#include "country_graph_coloring.h" |
| 15 |
#include <math.h> |
| 16 |
#include "power.h" |
| 17 |
#include <stdbool.h> |
| 18 |
#include <time.h> |
| 19 |
|
| 20 |
static double get_local_12_norm(int n, int i, const int *ia, const int *ja, |
| 21 |
const int *p) { |
| 22 |
double norm = n; |
| 23 |
for (int j = ia[i]; j < ia[i+1]; j++){ |
| 24 |
if (ja[j] == i) continue; |
| 25 |
norm = fmin(norm, abs(p[i] - p[ja[j]])); |
| 26 |
} |
| 27 |
return norm; |
| 28 |
} |
| 29 |
|
| 30 |
static void get_12_norm(int n, const int *ia, const int *ja, int *p, |
| 31 |
double *norm) { |
| 32 |
/* norm[0] := antibandwidth |
| 33 |
norm[1] := (\sum_{i\in V} (Min_{{j,i}\in E} |p[i] - p[j]|)/|V| |
| 34 |
*/ |
| 35 |
norm[0] = n; norm[1] = 0; |
| 36 |
for (int i = 0; i < n; i++){ |
| 37 |
double tmp = n; |
| 38 |
for (int j = ia[i]; j < ia[i+1]; j++){ |
| 39 |
if (ja[j] == i) continue; |
| 40 |
norm[0] = fmin(norm[0], abs(p[i] - p[ja[j]])); |
| 41 |
tmp = fmin(tmp, abs(p[i] - p[ja[j]])); |
| 42 |
} |
| 43 |
norm[1] += tmp; |
| 44 |
} |
| 45 |
norm[1] /= n; |
| 46 |
} |
| 47 |
|
| 48 |
void improve_antibandwidth_by_swapping(SparseMatrix A, int *p){ |
| 49 |
int cnt = 1, n = A->m, *ia = A->ia, *ja = A->ja; |
| 50 |
double norm1[2]; |
| 51 |
clock_t start = clock(); |
| 52 |
FILE *fp = NULL; |
| 53 |
|
| 54 |
if (Verbose){ |
| 55 |
fprintf(stderr,"saving timing vs antiband data to timing_greedy\n"); |
| 56 |
fp = fopen("timing_greedy","w"); |
| 57 |
} |
| 58 |
assert(SparseMatrix_is_symmetric(A, true)); |
| 59 |
for (bool improved = true; improved; ) { |
| 60 |
improved = false; |
| 61 |
for (int i = 0; i < n; i++) { |
| 62 |
norm1[0] = get_local_12_norm(n, i, ia, ja, p); |
| 63 |
for (int j = 0; j < n; j++) { |
| 64 |
if (j == i) continue; |
| 65 |
const double norm2 = get_local_12_norm(n, j, ia, ja, p); |
| 66 |
const int pi = p[i]; |
| 67 |
const int pj = p[j]; |
| 68 |
p[i] = pj; |
| 69 |
p[j] = pi; |
| 70 |
const double norm11 = get_local_12_norm(n, i, ia, ja, p); |
| 71 |
const double norm22 = get_local_12_norm(n, j, ia, ja, p); |
| 72 |
if (fmin(norm11, norm22) > fmin(norm1[0], norm2)){ |
| 73 |
improved = true; |
| 74 |
norm1[0] = norm11; |
| 75 |
continue; |
| 76 |
} |
| 77 |
p[i] = pi; |
| 78 |
p[j] = pj; |
| 79 |
} |
| 80 |
if (i%100 == 0 && Verbose) { |
| 81 |
get_12_norm(n, ia, ja, p, norm1); |
| 82 |
fprintf(fp, "%f %f %f\n", ((double)(clock() - start)) / CLOCKS_PER_SEC, |
| 83 |
norm1[0], norm1[1]); |
| 84 |
} |
| 85 |
} |
| 86 |
if (Verbose) { |
| 87 |
get_12_norm(n, ia, ja, p, norm1); |
| 88 |
fprintf(stderr, "[%d] aband = %f, aband_avg = %f\n", cnt++, norm1[0], norm1[1]); |
| 89 |
fprintf(fp,"%f %f %f\n", ((double)(clock() - start)) / CLOCKS_PER_SEC, |
| 90 |
norm1[0], norm1[1]); |
| 91 |
} |
| 92 |
} |
| 93 |
if (fp != NULL) { |
| 94 |
fclose(fp); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void country_graph_coloring(int seed, SparseMatrix A, int **p) { |
| 99 |
int n = A->m; |
| 100 |
|
| 101 |
clock_t start = clock(); |
| 102 |
assert(A->m == A->n); |
| 103 |
SparseMatrix A2 = SparseMatrix_symmetrize(A, true); |
| 104 |
const int *const ia = A2->ia; |
| 105 |
const int *const ja = A2->ja; |
| 106 |
|
| 107 |
/* Laplacian */ |
| 108 |
SparseMatrix L = SparseMatrix_new(n, n, 1, MATRIX_TYPE_REAL, FORMAT_COORD); |
| 109 |
for (int i = 0; i < n; i++){ |
| 110 |
double nrow = 0.; |
| 111 |
for (int j = ia[i]; j < ia[i+1]; j++){ |
| 112 |
const int jj = ja[j]; |
| 113 |
if (jj != i){ |
| 114 |
nrow ++; |
| 115 |
L = SparseMatrix_coordinate_form_add_entry(L, i, jj, &(int){-1}); |
| 116 |
} |
| 117 |
} |
| 118 |
L = SparseMatrix_coordinate_form_add_entry(L, i, i, &nrow); |
| 119 |
} |
| 120 |
{ |
| 121 |
SparseMatrix new = SparseMatrix_from_coordinate_format(L); |
| 122 |
SparseMatrix_delete(L); |
| 123 |
L = new; |
| 124 |
} |
| 125 |
|
| 126 |
/* largest eigen vector */ |
| 127 |
double *v = power_method(L, L->n, seed); |
| 128 |
|
| 129 |
vector_ordering(n, v, p); |
| 130 |
if (Verbose) |
| 131 |
fprintf(stderr, "cpu time for spectral ordering (before greedy) = %f\n", |
| 132 |
((double)(clock() - start)) / CLOCKS_PER_SEC); |
| 133 |
|
| 134 |
clock_t start2 = clock(); |
| 135 |
/* swapping */ |
| 136 |
improve_antibandwidth_by_swapping(A2, *p); |
| 137 |
if (Verbose) { |
| 138 |
fprintf(stderr, "cpu time for greedy refinement = %f\n", |
| 139 |
((double)(clock() - start2)) / CLOCKS_PER_SEC); |
| 140 |
|
| 141 |
fprintf(stderr, "cpu time for spectral + greedy = %f\n", |
| 142 |
((double)(clock() - start)) / CLOCKS_PER_SEC); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
if (A2 != A) SparseMatrix_delete(A2); |
| 147 |
SparseMatrix_delete(L); |
| 148 |
} |