Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
country_graph_coloring.c
Go to the documentation of this file.
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#define STANDALONE
13#include <math.h>
14#include "power.h"
15#include <stdbool.h>
16#include <time.h>
17
18static void get_local_12_norm(int n, int i, const int *ia, const int *ja,
19 const int *p, double *norm){
20 int j, nz = 0;
21 norm[0] = n; norm[1] = 0;
22 for (j = ia[i]; j < ia[i+1]; j++){
23 if (ja[j] == i) continue;
24 norm[0] = fmin(norm[0], abs(p[i] - p[ja[j]]));
25 nz++;
26 norm[1] += abs(p[i] - p[ja[j]]);
27 }
28 if (nz > 0) norm[1] /= nz;
29}
30static void get_12_norm(int n, int *ia, int *ja, int *p, double *norm){
31 /* norm[0] := antibandwidth
32 norm[1] := (\sum_{{i,j}\in E} |p[i] - p[j]|)/|E|
33 norm[2] := (\sum_{i\in V} (Min_{{j,i}\in E} |p[i] - p[j]|)/|V|
34 */
35 int i, j, nz = 0;
36 double tmp;
37 norm[0] = n; norm[1] = 0; norm[2] = 0;
38 for (i = 0; i < n; i++){
39 tmp = n;
40 for (j = ia[i]; j < ia[i+1]; j++){
41 if (ja[j] == i) continue;
42 norm[0] = fmin(norm[0], abs(p[i] - p[ja[j]]));
43 norm[1] += abs(p[i] - p[ja[j]]);
44 tmp = fmin(tmp, abs(p[i] - p[ja[j]]));
45 nz++;
46 }
47 norm[2] += tmp;
48 }
49 norm[2] /= n;
50 norm[1] /= nz;
51}
52
54 bool improved = true;
55 int cnt = 1, n = A->m, i, j, *ia = A->ia, *ja = A->ja;
56 double norm1[3], norm2[3], norm11[3], norm22[3];
57 clock_t start = clock();
58 FILE *fp = NULL;
59
60 if (Verbose){
61 fprintf(stderr,"saving timing vs antiband data to timing_greedy\n");
62 fp = fopen("timing_greedy","w");
63 }
64 assert(SparseMatrix_is_symmetric(A, true));
65 while (improved){
66 improved = false;
67 for (i = 0; i < n; i++){
68 get_local_12_norm(n, i, ia, ja, p, norm1);
69 for (j = 0; j < n; j++){
70 if (j == i) continue;
71 get_local_12_norm(n, j, ia, ja, p, norm2);
72 const int pi = p[i];
73 const int pj = p[j];
74 (p)[i] = pj;
75 (p)[j] = pi;
76 get_local_12_norm(n, i, ia, ja, p, norm11);
77 get_local_12_norm(n, j, ia, ja, p, norm22);
78 if (fmin(norm11[0], norm22[0]) > fmin(norm1[0], norm2[0])){
79 improved = true;
80 norm1[0] = norm11[0];
81 norm1[1] = norm11[1];
82 continue;
83 }
84 (p)[i] = pi;
85 (p)[j] = pj;
86 }
87 if (i%100 == 0 && Verbose) {
88 get_12_norm(n, ia, ja, p, norm1);
89 fprintf(fp, "%f %f %f\n", ((double)(clock() - start)) / CLOCKS_PER_SEC,
90 norm1[0], norm1[2]);
91 }
92 }
93 if (Verbose) {
94 get_12_norm(n, ia, ja, p, norm1);
95 fprintf(stderr, "[%d] aband = %f, aband_avg = %f\n", cnt++, norm1[0], norm1[2]);
96 fprintf(fp,"%f %f %f\n", ((double)(clock() - start)) / CLOCKS_PER_SEC,
97 norm1[0], norm1[2]);
98 }
99 }
100 if (fp != NULL) {
101 fclose(fp);
102 }
103}
104
106 int n = A->m, i, j, jj;
107 SparseMatrix L, A2;
108 int *ia = A->ia, *ja = A->ja;
109 int a = -1;
110 double nrow;
111 double norm1[3];
112 clock_t start, start2;
113
114 start = clock();
115 assert(A->m == A->n);
116 A2 = SparseMatrix_symmetrize(A, true);
117 ia = A2->ia; ja = A2->ja;
118
119 /* Laplacian */
121 for (i = 0; i < n; i++){
122 nrow = 0.;
123 for (j = ia[i]; j < ia[i+1]; j++){
124 jj = ja[j];
125 if (jj != i){
126 nrow ++;
128 }
129 }
131 }
132 {
135 L = new;
136 }
137
138 /* largest eigen vector */
139 double *v = power_method(L, L->n, seed);
140
141 vector_ordering(n, v, p);
142 if (Verbose)
143 fprintf(stderr, "cpu time for spectral ordering (before greedy) = %f\n",
144 ((double)(clock() - start)) / CLOCKS_PER_SEC);
145
146 start2 = clock();
147 /* swapping */
149 if (Verbose) {
150 fprintf(stderr, "cpu time for greedy refinement = %f\n",
151 ((double)(clock() - start2)) / CLOCKS_PER_SEC);
152
153 fprintf(stderr, "cpu time for spectral + greedy = %f\n",
154 ((double)(clock() - start)) / CLOCKS_PER_SEC);
155
156 }
157 get_12_norm(n, ia, ja, *p, norm1);
158
159 if (A2 != A) SparseMatrix_delete(A2);
161}
SparseMatrix SparseMatrix_from_coordinate_format(SparseMatrix A)
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, bool pattern_symmetric_only)
SparseMatrix SparseMatrix_coordinate_form_add_entry(SparseMatrix A, int irn, int jcn, const void *val)
bool SparseMatrix_is_symmetric(SparseMatrix A, bool test_pattern_symmetry_only)
void SparseMatrix_delete(SparseMatrix A)
SparseMatrix SparseMatrix_new(int m, int n, int nz, int type, int format)
@ MATRIX_TYPE_REAL
@ FORMAT_COORD
void country_graph_coloring(int seed, SparseMatrix A, int **p)
void improve_antibandwidth_by_swapping(SparseMatrix A, int *p)
static void get_12_norm(int n, int *ia, int *ja, int *p, double *norm)
static void get_local_12_norm(int n, int i, const int *ia, const int *ja, const int *p, double *norm)
static double norm(int n, const double *x)
static long seed
Definition exeval.c:1035
#define A(n, t)
Definition expr.h:76
void vector_ordering(int n, double *v, int **p)
Definition general.c:115
static bool Verbose
Definition gml2gv.c:23
static attrs_t * L
Definition gmlparse.c:93
node NULL
Definition grammar.y:163
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:210
double * power_method(void *A, int n, int random_seed)
Definition power.c:21