Graphviz 14.1.3~dev.20260207.0611
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 <stdlib.h>
19#include <time.h>
20
21static double get_local_12_norm(int n, int i, const int *ia, const int *ja,
22 const int *p) {
23 double norm = n;
24 for (int j = ia[i]; j < ia[i+1]; j++){
25 if (ja[j] == i) continue;
26 norm = fmin(norm, abs(p[i] - p[ja[j]]));
27 }
28 return norm;
29}
30
31static void get_12_norm(int n, const int *ia, const int *ja, int *p,
32 double *norm) {
33 /* norm[0] := antibandwidth
34 norm[1] := (\sum_{i\in V} (Min_{{j,i}\in E} |p[i] - p[j]|)/|V|
35 */
36 norm[0] = n; norm[1] = 0;
37 for (int i = 0; i < n; i++){
38 double tmp = n;
39 for (int j = ia[i]; j < ia[i+1]; j++){
40 if (ja[j] == i) continue;
41 norm[0] = fmin(norm[0], abs(p[i] - p[ja[j]]));
42 tmp = fmin(tmp, abs(p[i] - p[ja[j]]));
43 }
44 norm[1] += tmp;
45 }
46 norm[1] /= n;
47}
48
50 int cnt = 1, n = A->m, *ia = A->ia, *ja = A->ja;
51 double norm1[2];
52 clock_t start = clock();
53 FILE *fp = NULL;
54
55 if (Verbose){
56 fprintf(stderr,"saving timing vs antiband data to timing_greedy\n");
57 fp = fopen("timing_greedy","w");
58 }
59 assert(SparseMatrix_is_symmetric(A, true));
60 for (bool improved = true; improved; ) {
61 improved = false;
62 for (int i = 0; i < n; i++) {
63 norm1[0] = get_local_12_norm(n, i, ia, ja, p);
64 for (int j = 0; j < n; j++) {
65 if (j == i) continue;
66 const double norm2 = get_local_12_norm(n, j, ia, ja, p);
67 const int pi = p[i];
68 const int pj = p[j];
69 p[i] = pj;
70 p[j] = pi;
71 const double norm11 = get_local_12_norm(n, i, ia, ja, p);
72 const double norm22 = get_local_12_norm(n, j, ia, ja, p);
73 if (fmin(norm11, norm22) > fmin(norm1[0], norm2)){
74 improved = true;
75 norm1[0] = norm11;
76 continue;
77 }
78 p[i] = pi;
79 p[j] = pj;
80 }
81 if (i%100 == 0 && Verbose) {
82 get_12_norm(n, ia, ja, p, norm1);
83 fprintf(fp, "%f %f %f\n", ((double)(clock() - start)) / CLOCKS_PER_SEC,
84 norm1[0], norm1[1]);
85 }
86 }
87 if (Verbose) {
88 get_12_norm(n, ia, ja, p, norm1);
89 fprintf(stderr, "[%d] aband = %f, aband_avg = %f\n", cnt++, norm1[0], norm1[1]);
90 fprintf(fp,"%f %f %f\n", ((double)(clock() - start)) / CLOCKS_PER_SEC,
91 norm1[0], norm1[1]);
92 }
93 }
94 if (fp != NULL) {
95 fclose(fp);
96 }
97}
98
100 int n = A->m;
101
102 clock_t start = clock();
103 assert(A->m == A->n);
105 const int *const ia = A2->ia;
106 const int *const ja = A2->ja;
107
108 /* Laplacian */
110 for (int i = 0; i < n; i++){
111 double nrow = 0.;
112 for (int j = ia[i]; j < ia[i+1]; j++){
113 const int jj = ja[j];
114 if (jj != i){
115 nrow ++;
116 L = SparseMatrix_coordinate_form_add_entry(L, i, jj, &(double){-1});
117 }
118 }
120 }
121 {
124 L = new;
125 }
126
127 /* largest eigen vector */
128 double *v = power_method(L, L->n, seed);
129
130 vector_ordering(n, v, p);
131 free(v);
132 if (Verbose)
133 fprintf(stderr, "cpu time for spectral ordering (before greedy) = %f\n",
134 ((double)(clock() - start)) / CLOCKS_PER_SEC);
135
136 clock_t start2 = clock();
137 /* swapping */
139 if (Verbose) {
140 fprintf(stderr, "cpu time for greedy refinement = %f\n",
141 ((double)(clock() - start2)) / CLOCKS_PER_SEC);
142
143 fprintf(stderr, "cpu time for spectral + greedy = %f\n",
144 ((double)(clock() - start)) / CLOCKS_PER_SEC);
145
146 }
147
148 if (A2 != A) SparseMatrix_delete(A2);
150}
SparseMatrix SparseMatrix_from_coordinate_format(SparseMatrix A)
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, bool pattern_symmetric_only)
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
#define SparseMatrix_coordinate_form_add_entry(A, irn, jcn, val)
wrap SparseMatrix_coordinate_form_add_entry_ for type safety
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
void free(void *)
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