| 1 | /* glpnet09.c */cxx . . $ `<file>/external/glpk/glpnet09.c`/ |
| 2 | |
| 3 | /*********************************************************************** |
| 4 | * This code is part of GLPK (GNU Linear Programming Kit). |
| 5 | * |
| 6 | * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
| 7 | * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, |
| 8 | * Moscow Aviation Institute, Moscow, Russia. All rights reserved. |
| 9 | * E-mail: <[email protected]>. |
| 10 | * |
| 11 | * GLPK is free software: you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * GLPK is distributed in the hope that it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 18 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 19 | * License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with GLPK. If not, see <http://www.gnu.org/licenses/>. |
| 23 | ***********************************************************************/ |
| 24 | |
| 25 | #include "glpapi.h"cxx . . $ `<file>/external/glpk/glpapi.h`/ |
| 26 | #include "glpnet.h"cxx . . $ `<file>/external/glpk/glpnet.h`/ |
| 27 | |
| 28 | /*********************************************************************** |
| 29 | * NAME |
| 30 | * |
| 31 | * kellerman - cover edges by cliques with Kellerman's heuristic |
| 32 | * |
| 33 | * SYNOPSIS |
| 34 | * |
| 35 | * #include "glpnet.h" |
| 36 | * int kellerman(int n, int (*func)(void *info, int i, int ind[]), |
| 37 | * void *info, glp_graph *H); |
| 38 | * |
| 39 | * DESCRIPTION |
| 40 | * |
| 41 | * The routine kellerman implements Kellerman's heuristic algorithm |
| 42 | * to find a minimal set of cliques which cover all edges of specified |
| 43 | * graph G = (V, E). |
| 44 | * |
| 45 | * The parameter n specifies the number of vertices |V|, n >= 0. |
| 46 | * |
| 47 | * Formal routine func specifies the set of edges E in the following |
| 48 | * way. Running the routine kellerman calls the routine func and passes |
| 49 | * to it parameter i, which is the number of some vertex, 1 <= i <= n. |
| 50 | * In response the routine func should store numbers of all vertices |
| 51 | * adjacent to vertex i to locations ind[1], ind[2], ..., ind[len] and |
| 52 | * return the value of len, which is the number of adjacent vertices, |
| 53 | * 0 <= len <= n. Self-loops are allowed, but ignored. Multiple edges |
| 54 | * are not allowed. |
| 55 | * |
| 56 | * The parameter info is a transit pointer (magic cookie) passed to the |
| 57 | * formal routine func as its first parameter. |
| 58 | * |
| 59 | * The result provided by the routine kellerman is the bipartite graph |
| 60 | * H = (V union C, F), which defines the covering found. (The program |
| 61 | * object of type glp_graph specified by the parameter H should be |
| 62 | * previously created with the routine glp_create_graph. On entry the |
| 63 | * routine kellerman erases the content of this object with the routine |
| 64 | * glp_erase_graph.) Vertices of first part V correspond to vertices of |
| 65 | * the graph G and have the same ordinal numbers 1, 2, ..., n. Vertices |
| 66 | * of second part C correspond to cliques and have ordinal numbers |
| 67 | * n+1, n+2, ..., n+k, where k is the total number of cliques in the |
| 68 | * edge covering found. Every edge f in F in the program object H is |
| 69 | * represented as arc f = (i->j), where i in V and j in C, which means |
| 70 | * that vertex i of the graph G is in clique C[j], 1 <= j <= k. (Thus, |
| 71 | * if two vertices of the graph G are in the same clique, these vertices |
| 72 | * are adjacent in G, and corresponding edge is covered by that clique.) |
| 73 | * |
| 74 | * RETURNS |
| 75 | * |
| 76 | * The routine Kellerman returns k, the total number of cliques in the |
| 77 | * edge covering found. |
| 78 | * |
| 79 | * REFERENCE |
| 80 | * |
| 81 | * For more details see: glpk/doc/notes/keller.pdf (in Russian). */ |
| 82 | |
| 83 | struct setcxx . . $ set# |
| 84 | { /* set of vertices */ |
| 85 | int sizecxx . . $ set#size.; |
| 86 | /* size (cardinality) of the set, 0 <= card <= n */ |
| 87 | int *listcxx . . $ set#list.; /* int list[1+n]; */ |
| 88 | /* the set contains vertices list[1,...,size] */ |
| 89 | int *poscxx . . $ set#pos.; /* int pos[1+n]; */ |
| 90 | /* pos[i] > 0 means that vertex i is in the set and |
| 91 | list[pos[i]] = i; pos[i] = 0 means that vertex i is not in |
| 92 | the set */ |
| 93 | }; |
| 94 | |
| 95 | int kellermancxx . . $ `external/glpk/glpnet.h:53:9`!(int n, int (*func)(void *info, int i, int ind[]), |
| 96 | void *info, void /* glp_graph */ *H_) |
| 97 | { glp_graphcxx . . $ glp_graph# *H = H_; |
| 98 | struct setcxx . . $ set# W_, *W = &W_, V_, *V = &V_; |
| 99 | glp_arccxx . . $ glp_arc# *a; |
| 100 | int i, j, k, m, t, len, card, best; |
| 101 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(n >= 0); |
| 102 | /* H := (V, 0; 0), where V is the set of vertices of graph G */ |
| 103 | glp_erase_graphcxx . . $ glp_erase_graph(fd6d4ec766750ee3).(H, H->v_sizecxx . . $ _glp_graph#v_size., H->a_sizecxx . . $ _glp_graph#a_size.); |
| 104 | glp_add_verticescxx . . $ glp_add_vertices(9d5fec13eee488ca).(H, n); |
| 105 | /* W := 0 */ |
| 106 | W->sizecxx . . $ set#size. = 0; |
| 107 | W->listcxx . . $ set#list. = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int)); |
| 108 | W->poscxx . . $ set#pos. = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int)); |
| 109 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&W->poscxx . . $ set#pos.[1], 0, sizeof(int) * n); |
| 110 | /* V := 0 */ |
| 111 | V->sizecxx . . $ set#size. = 0; |
| 112 | V->listcxx . . $ set#list. = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int)); |
| 113 | V->poscxx . . $ set#pos. = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int)); |
| 114 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&V->poscxx . . $ set#pos.[1], 0, sizeof(int) * n); |
| 115 | /* main loop */ |
| 116 | for (i = 1; i <= n; i++) |
| 117 | { /* W must be empty */ |
| 118 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(W->size == 0); |
| 119 | /* W := { j : i > j and (i,j) in E } */ |
| 120 | len = func(info, i, W->listcxx . . $ set#list.); |
| 121 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(0 <= len && len <= n); |
| 122 | for (t = 1; t <= len; t++) |
| 123 | { j = W->listcxx . . $ set#list.[t]; |
| 124 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(1 <= j && j <= n); |
| 125 | if (j >= i) continue; |
| 126 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(W->pos[j] == 0); |
| 127 | W->listcxx . . $ set#list.[++W->sizecxx . . $ set#size.] = j, W->poscxx . . $ set#pos.[j] = W->sizecxx . . $ set#size.; |
| 128 | } |
| 129 | /* on i-th iteration we need to cover edges (i,j) for all |
| 130 | j in W */ |
| 131 | /* if W is empty, it is a special case */ |
| 132 | if (W->sizecxx . . $ set#size. == 0) |
| 133 | { /* set k := k + 1 and create new clique C[k] = { i } */ |
| 134 | k = glp_add_verticescxx . . $ glp_add_vertices(9d5fec13eee488ca).(H, 1) - n; |
| 135 | glp_add_arccxx . . $ glp_add_arc(e54dd140876db879).(H, i, n + k); |
| 136 | continue; |
| 137 | } |
| 138 | /* try to include vertex i into existing cliques */ |
| 139 | /* V must be empty */ |
| 140 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(V->size == 0); |
| 141 | /* k is the number of cliques found so far */ |
| 142 | k = H->nvcxx . . $ _glp_graph#nv. - n; |
| 143 | for (m = 1; m <= k; m++) |
| 144 | { /* do while V != W; since here V is within W, we can use |
| 145 | equivalent condition: do while |V| < |W| */ |
| 146 | if (V->sizecxx . . $ set#size. == W->sizecxx . . $ set#size.) break; |
| 147 | /* check if C[m] is within W */ |
| 148 | for (a = H->vcxx . . $ _glp_graph#v.[n + m]->incxx . . $ _glp_vertex#in.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->h_nextcxx . . $ _glp_arc#h_next.) |
| 149 | { j = a->tailcxx . . $ _glp_arc#tail.->icxx . . $ _glp_vertex#i.; |
| 150 | if (W->poscxx . . $ set#pos.[j] == 0) break; |
| 151 | } |
| 152 | if (a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) continue; |
| 153 | /* C[m] is within W, expand clique C[m] with vertex i */ |
| 154 | /* C[m] := C[m] union {i} */ |
| 155 | glp_add_arccxx . . $ glp_add_arc(e54dd140876db879).(H, i, n + m); |
| 156 | /* V is a set of vertices whose incident edges are already |
| 157 | covered by existing cliques */ |
| 158 | /* V := V union C[m] */ |
| 159 | for (a = H->vcxx . . $ _glp_graph#v.[n + m]->incxx . . $ _glp_vertex#in.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->h_nextcxx . . $ _glp_arc#h_next.) |
| 160 | { j = a->tailcxx . . $ _glp_arc#tail.->icxx . . $ _glp_vertex#i.; |
| 161 | if (V->poscxx . . $ set#pos.[j] == 0) |
| 162 | V->listcxx . . $ set#list.[++V->sizecxx . . $ set#size.] = j, V->poscxx . . $ set#pos.[j] = V->sizecxx . . $ set#size.; |
| 163 | } |
| 164 | } |
| 165 | /* remove from set W the vertices whose incident edges are |
| 166 | already covered by existing cliques */ |
| 167 | /* W := W \ V, V := 0 */ |
| 168 | for (t = 1; t <= V->sizecxx . . $ set#size.; t++) |
| 169 | { j = V->listcxx . . $ set#list.[t], V->poscxx . . $ set#pos.[j] = 0; |
| 170 | if (W->poscxx . . $ set#pos.[j] != 0) |
| 171 | { /* remove vertex j from W */ |
| 172 | if (W->poscxx . . $ set#pos.[j] != W->sizecxx . . $ set#size.) |
| 173 | { int jj = W->listcxx . . $ set#list.[W->sizecxx . . $ set#size.]; |
| 174 | W->listcxx . . $ set#list.[W->poscxx . . $ set#pos.[j]] = jj; |
| 175 | W->poscxx . . $ set#pos.[jj] = W->poscxx . . $ set#pos.[j]; |
| 176 | } |
| 177 | W->sizecxx . . $ set#size.--, W->poscxx . . $ set#pos.[j] = 0; |
| 178 | } |
| 179 | } |
| 180 | V->sizecxx . . $ set#size. = 0; |
| 181 | /* now set W contains only vertices whose incident edges are |
| 182 | still not covered by existing cliques; create new cliques |
| 183 | to cover remaining edges until set W becomes empty */ |
| 184 | while (W->sizecxx . . $ set#size. > 0) |
| 185 | { /* find clique C[m], 1 <= m <= k, which shares maximal |
| 186 | number of vertices with W; to break ties choose clique |
| 187 | having smallest number m */ |
| 188 | m = 0, best = -1; |
| 189 | k = H->nvcxx . . $ _glp_graph#nv. - n; |
| 190 | for (t = 1; t <= k; t++) |
| 191 | { /* compute cardinality of intersection of W and C[t] */ |
| 192 | card = 0; |
| 193 | for (a = H->vcxx . . $ _glp_graph#v.[n + t]->incxx . . $ _glp_vertex#in.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->h_nextcxx . . $ _glp_arc#h_next.) |
| 194 | { j = a->tailcxx . . $ _glp_arc#tail.->icxx . . $ _glp_vertex#i.; |
| 195 | if (W->poscxx . . $ set#pos.[j] != 0) card++; |
| 196 | } |
| 197 | if (best < card) |
| 198 | m = t, best = card; |
| 199 | } |
| 200 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(m > 0); |
| 201 | /* set k := k + 1 and create new clique: |
| 202 | C[k] := (W intersect C[m]) union { i }, which covers all |
| 203 | edges incident to vertices from (W intersect C[m]) */ |
| 204 | k = glp_add_verticescxx . . $ glp_add_vertices(9d5fec13eee488ca).(H, 1) - n; |
| 205 | for (a = H->vcxx . . $ _glp_graph#v.[n + m]->incxx . . $ _glp_vertex#in.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->h_nextcxx . . $ _glp_arc#h_next.) |
| 206 | { j = a->tailcxx . . $ _glp_arc#tail.->icxx . . $ _glp_vertex#i.; |
| 207 | if (W->poscxx . . $ set#pos.[j] != 0) |
| 208 | { /* vertex j is in both W and C[m]; include it in new |
| 209 | clique C[k] */ |
| 210 | glp_add_arccxx . . $ glp_add_arc(e54dd140876db879).(H, j, n + k); |
| 211 | /* remove vertex j from W, since edge (i,j) will be |
| 212 | covered by new clique C[k] */ |
| 213 | if (W->poscxx . . $ set#pos.[j] != W->sizecxx . . $ set#size.) |
| 214 | { int jj = W->listcxx . . $ set#list.[W->sizecxx . . $ set#size.]; |
| 215 | W->listcxx . . $ set#list.[W->poscxx . . $ set#pos.[j]] = jj; |
| 216 | W->poscxx . . $ set#pos.[jj] = W->poscxx . . $ set#pos.[j]; |
| 217 | } |
| 218 | W->sizecxx . . $ set#size.--, W->poscxx . . $ set#pos.[j] = 0; |
| 219 | } |
| 220 | } |
| 221 | /* include vertex i to new clique C[k] to cover edges (i,j) |
| 222 | incident to all vertices j just removed from W */ |
| 223 | glp_add_arccxx . . $ glp_add_arc(e54dd140876db879).(H, i, n + k); |
| 224 | } |
| 225 | } |
| 226 | /* free working arrays */ |
| 227 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(W->listcxx . . $ set#list.); |
| 228 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(W->poscxx . . $ set#pos.); |
| 229 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(V->listcxx . . $ set#list.); |
| 230 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(V->poscxx . . $ set#pos.); |
| 231 | /* return the number of cliques in the edge covering found */ |
| 232 | return H->nvcxx . . $ _glp_graph#nv. - n; |
| 233 | } |
| 234 | |
| 235 | /* eof */ |