Graphviz 14.1.2~dev.20260118.2044
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#include "config.h"
12
13#define STANDALONE
15#include <math.h>
16#include "power.h"
17#include <stdbool.h>
18#include <time.h>
19
20static 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
30static 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
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
99 int n = A->m;
100
101 clock_t start = clock();
102 assert(A->m == A->n);
104 const int *const ia = A2->ia;
105 const int *const ja = A2->ja;
106
107 /* Laplacian */
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 }
119 }
120 {
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 */
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);
148}
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, size_t 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 double get_local_12_norm(int n, int i, const int *ia, const int *ja, const int *p)
static void get_12_norm(int n, const int *ia, const int *ja, int *p, double *norm)
static double norm(int n, const double *x)
static long seed
Definition exeval.c:1008
#define A(n, t)
Definition expr.h:76
void vector_ordering(int n, double *v, int **p)
Definition general.c:93
static bool Verbose
Definition gml2gv.c:26
static attrs_t * L
Definition gmlparse.c:94
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:198
double * power_method(void *A, int n, int random_seed)
Definition power.c:23