aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/api/graph.c
blob: 82994c84f0ff50a568d434ec82a422640d57010f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* graph.c (basic graph routines) */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "avl.h"
#include "dmp.h"
#include "env.h"
#include "glpk.h"

/* CAUTION: DO NOT CHANGE THE LIMITS BELOW */

#define NV_MAX 100000000 /* = 100*10^6 */
/* maximal number of vertices in the graph */

#define NA_MAX 500000000 /* = 500*10^6 */
/* maximal number of arcs in the graph */

/***********************************************************************
*  NAME
*
*  glp_create_graph - create graph
*
*  SYNOPSIS
*
*  glp_graph *glp_create_graph(int v_size, int a_size);
*
*  DESCRIPTION
*
*  The routine creates a new graph, which initially is empty, i.e. has
*  no vertices and arcs.
*
*  The parameter v_size specifies the size of data associated with each
*  vertex of the graph (0 to 256 bytes).
*
*  The parameter a_size specifies the size of data associated with each
*  arc of the graph (0 to 256 bytes).
*
*  RETURNS
*
*  The routine returns a pointer to the graph created. */

static void create_graph(glp_graph *G, int v_size, int a_size)
{     G->pool = dmp_create_pool();
      G->name = NULL;
      G->nv_max = 50;
      G->nv = G->na = 0;
      G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *));
      G->index = NULL;
      G->v_size = v_size;
      G->a_size = a_size;
      return;
}

glp_graph *glp_create_graph(int v_size, int a_size)
{     glp_graph *G;
      if (!(0 <= v_size && v_size <= 256))
         xerror("glp_create_graph: v_size = %d; invalid size of vertex "
            "data\n", v_size);
      if (!(0 <= a_size && a_size <= 256))
         xerror("glp_create_graph: a_size = %d; invalid size of arc dat"
            "a\n", a_size);
      G = xmalloc(sizeof(glp_graph));
      create_graph(G, v_size, a_size);
      return G;
}

/***********************************************************************
*  NAME
*
*  glp_set_graph_name - assign (change) graph name
*
*  SYNOPSIS
*
*  void glp_set_graph_name(glp_graph *G, const char *name);
*
*  DESCRIPTION
*
*  The routine glp_set_graph_name assigns a symbolic name specified by
*  the character string name (1 to 255 chars) to the graph.
*
*  If the parameter name is NULL or an empty string, the routine erases
*  the existing symbolic name of the graph. */

void glp_set_graph_name(glp_graph *G, const char *name)
{     if (G->name != NULL)
      {  dmp_free_atom(G->pool, G->name, strlen(G->name)+1);
         G->name = NULL;
      }
      if (!(name == NULL || name[0] == '\0'))
      {  int j;
         for (j = 0; name[j] != '\0'; j++)
         {  if (j == 256)
               xerror("glp_set_graph_name: graph name too long\n");
            if (iscntrl((unsigned char)name[j]))
               xerror("glp_set_graph_name: graph name contains invalid "
                  "character(s)\n");
         }
         G->name = dmp_get_atom(G->pool, strlen(name)+1);
         strcpy(G->name, name);
      }
      return;
}

/***********************************************************************
*  NAME
*
*  glp_add_vertices - add new vertices to graph
*
*  SYNOPSIS
*
*  int glp_add_vertices(glp_graph *G, int nadd);
*
*  DESCRIPTION
*
*  The routine glp_add_vertices adds nadd vertices to the specified
*  graph. New vertices are always added to the end of the vertex list,
*  so ordinal numbers of existing vertices remain unchanged.
*
*  Being added each new vertex is isolated (has no incident arcs).
*
*  RETURNS
*
*  The routine glp_add_vertices returns an ordinal number of the first
*  new vertex added to the graph. */

int glp_add_vertices(glp_graph *G, int nadd)
{     int i, nv_new;
      if (nadd < 1)
         xerror("glp_add_vertices: nadd = %d; invalid number of vertice"
            "s\n", nadd);
      if (nadd > NV_MAX - G->nv)
         xerror("glp_add_vertices: nadd = %d; too many vertices\n",
            nadd);
      /* determine new number of vertices */
      nv_new = G->nv + nadd;
      /* increase the room, if necessary */
      if (G->nv_max < nv_new)
      {  glp_vertex **save = G->v;
         while (G->nv_max < nv_new)
         {  G->nv_max += G->nv_max;
            xassert(G->nv_max > 0);
         }
         G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *));
         memcpy(&G->v[1], &save[1], G->nv * sizeof(glp_vertex *));
         xfree(save);
      }
      /* add new vertices to the end of the vertex list */
      for (i = G->nv+1; i <= nv_new; i++)
      {  glp_vertex *v;
         G->v[i] = v = dmp_get_atom(G->pool, sizeof(glp_vertex));
         v->i = i;
         v->name = NULL;
         v->entry = NULL;
         if (G->v_size == 0)
            v->data = NULL;
         else
         {  v->data = dmp_get_atom(G->pool, G->v_size);
            memset(v->data, 0, G->v_size);
         }
         v->temp = NULL;
         v->in = v->out = NULL;
      }
      /* set new number of vertices */
      G->nv = nv_new;
      /* return the ordinal number of the first vertex added */
      return nv_new - nadd + 1;
}

/**********************************************************************/

void glp_set_vertex_name(glp_graph *G, int i, const char *name)
{     /* assign (change) vertex name */
      glp_vertex *v;
      if (!(1 <= i && i <= G->nv))
         xerror("glp_set_vertex_name: i = %d; vertex number out of rang"
            "e\n", i);
      v = G->v[i];
      if (v->name != NULL)
      {  if (v->entry != NULL)
         {  xassert(G->index != NULL);
            avl_delete_node(G->index, v->entry);
            v->entry = NULL;
         }
         dmp_free_atom(G->pool, v->name, strlen(v->name)+1);
         v->name = NULL;
      }
      if (!(name == NULL || name[0] == '\0'))
      {  int k;
         for (k = 0; name[k] != '\0'; k++)
         {  if (k == 256)
               xerror("glp_set_vertex_name: i = %d; vertex name too lon"
                  "g\n", i);
            if (iscntrl((unsigned char)name[k]))
               xerror("glp_set_vertex_name: i = %d; vertex name contain"
                  "s invalid character(s)\n", i);
         }
         v->name = dmp_get_atom(G->pool, strlen(name)+1);
         strcpy(v->name, name);
         if (G->index != NULL)
         {  xassert(v->entry == NULL);
            v->entry = avl_insert_node(G->index, v->name);
            avl_set_node_link(v->entry, v);
         }
      }
      return;
}

/***********************************************************************
*  NAME
*
*  glp_add_arc - add new arc to graph
*
*  SYNOPSIS
*
*  glp_arc *glp_add_arc(glp_graph *G, int i, int j);
*
*  DESCRIPTION
*
*  The routine glp_add_arc adds a new arc to the specified graph.
*
*  The parameters i and j specify the ordinal numbers of, resp., tail
*  and head vertices of the arc. Note that self-loops and multiple arcs
*  are allowed.
*
*  RETURNS
*
*  The routine glp_add_arc returns a pointer to the arc added. */

glp_arc *glp_add_arc(glp_graph *G, int i, int j)
{     glp_arc *a;
      if (!(1 <= i && i <= G->nv))
         xerror("glp_add_arc: i = %d; tail vertex number out of range\n"
            , i);
      if (!(1 <= j && j <= G->nv))
         xerror("glp_add_arc: j = %d; head vertex number out of range\n"
            , j);
      if (G->na == NA_MAX)
         xerror("glp_add_arc: too many arcs\n");
      a = dmp_get_atom(G->pool, sizeof(glp_arc));
      a->tail = G->v[i];
      a->head = G->v[j];
      if (G->a_size == 0)
         a->data = NULL;
      else
      {  a->data = dmp_get_atom(G->pool, G->a_size);
         memset(a->data, 0, G->a_size);
      }
      a->temp = NULL;
      a->t_prev = NULL;
      a->t_next = G->v[i]->out;
      if (a->t_next != NULL) a->t_next->t_prev = a;
      a->h_prev = NULL;
      a->h_next = G->v[j]->in;
      if (a->h_next != NULL) a->h_next->h_prev = a;
      G->v[i]->out = G->v[j]->in = a;
      G->na++;
      return a;
}

/***********************************************************************
*  NAME
*
*  glp_del_vertices - delete vertices from graph
*
*  SYNOPSIS
*
*  void glp_del_vertices(glp_graph *G, int ndel, const int num[]);
*
*  DESCRIPTION
*
*  The routine glp_del_vertices deletes vertices along with all
*  incident arcs from the specified graph. Ordinal numbers of vertices
*  to be deleted should be placed in locations num[1], ..., num[ndel],
*  ndel > 0.
*
*  Note that deleting vertices involves changing ordinal numbers of
*  other vertices remaining in the graph. New ordinal numbers of the
*  remaining vertices are assigned under the assumption that the
*  original order of vertices is not changed. */

void glp_del_vertices(glp_graph *G, int ndel, const int num[])
{     glp_vertex *v;
      int i, k, nv_new;
      /* scan the list of vertices to be deleted */
      if (!(1 <= ndel && ndel <= G->nv))
         xerror("glp_del_vertices: ndel = %d; invalid number of vertice"
            "s\n", ndel);
      for (k = 1; k <= ndel; k++)
      {  /* take the number of vertex to be deleted */
         i = num[k];
         /* obtain pointer to i-th vertex */
         if (!(1 <= i && i <= G->nv))
            xerror("glp_del_vertices: num[%d] = %d; vertex number out o"
               "f range\n", k, i);
         v = G->v[i];
         /* check that the vertex is not marked yet */
         if (v->i == 0)
            xerror("glp_del_vertices: num[%d] = %d; duplicate vertex nu"
               "mbers not allowed\n", k, i);
         /* erase symbolic name assigned to the vertex */
         glp_set_vertex_name(G, i, NULL);
         xassert(v->name == NULL);
         xassert(v->entry == NULL);
         /* free vertex data, if allocated */
         if (v->data != NULL)
            dmp_free_atom(G->pool, v->data, G->v_size);
         /* delete all incoming arcs */
         while (v->in != NULL)
            glp_del_arc(G, v->in);
         /* delete all outgoing arcs */
         while (v->out != NULL)
            glp_del_arc(G, v->out);
         /* mark the vertex to be deleted */
         v->i = 0;
      }
      /* delete all marked vertices from the vertex list */
      nv_new = 0;
      for (i = 1; i <= G->nv; i++)
      {  /* obtain pointer to i-th vertex */
         v = G->v[i];
         /* check if the vertex is marked */
         if (v->i == 0)
         {  /* it is marked, delete it */
            dmp_free_atom(G->pool, v, sizeof(glp_vertex));
         }
         else
         {  /* it is not marked, keep it */
            v->i = ++nv_new;
            G->v[v->i] = v;
         }
      }
      /* set new number of vertices in the graph */
      G->nv = nv_new;
      return;
}

/***********************************************************************
*  NAME
*
*  glp_del_arc - delete arc from graph
*
*  SYNOPSIS
*
*  void glp_del_arc(glp_graph *G, glp_arc *a);
*
*  DESCRIPTION
*
*  The routine glp_del_arc deletes an arc from the specified graph.
*  The arc to be deleted must exist. */

void glp_del_arc(glp_graph *G, glp_arc *a)
{     /* some sanity checks */
      xassert(G->na > 0);
      xassert(1 <= a->tail->i && a->tail->i <= G->nv);
      xassert(a->tail == G->v[a->tail->i]);
      xassert(1 <= a->head->i && a->head->i <= G->nv);
      xassert(a->head == G->v[a->head->i]);
      /* remove the arc from the list of incoming arcs */
      if (a->h_prev == NULL)
         a->head->in = a->h_next;
      else
         a->h_prev->h_next = a->h_next;
      if (a->h_next == NULL)
         ;
      else
         a->h_next->h_prev = a->h_prev;
      /* remove the arc from the list of outgoing arcs */
      if (a->t_prev == NULL)
         a->tail->out = a->t_next;
      else
         a->t_prev->t_next = a->t_next;
      if (a->t_next == NULL)
         ;
      else
         a->t_next->t_prev = a->t_prev;
      /* free arc data, if allocated */
      if (a->data != NULL)
         dmp_free_atom(G->pool, a->data, G->a_size);
      /* delete the arc from the graph */
      dmp_free_atom(G->pool, a, sizeof(glp_arc));
      G->na--;
      return;
}

/***********************************************************************
*  NAME
*
*  glp_erase_graph - erase graph content
*
*  SYNOPSIS
*
*  void glp_erase_graph(glp_graph *G, int v_size, int a_size);
*
*  DESCRIPTION
*
*  The routine glp_erase_graph erases the content of the specified
*  graph. The effect of this operation is the same as if the graph
*  would be deleted with the routine glp_delete_graph and then created
*  anew with the routine glp_create_graph, with exception that the
*  handle (pointer) to the graph remains valid. */

static void delete_graph(glp_graph *G)
{     dmp_delete_pool(G->pool);
      xfree(G->v);
      if (G->index != NULL) avl_delete_tree(G->index);
      return;
}

void glp_erase_graph(glp_graph *G, int v_size, int a_size)
{     if (!(0 <= v_size && v_size <= 256))
         xerror("glp_erase_graph: v_size = %d; invalid size of vertex d"
            "ata\n", v_size);
      if (!(0 <= a_size && a_size <= 256))
         xerror("glp_erase_graph: a_size = %d; invalid size of arc data"
            "\n", a_size);
      delete_graph(G);
      create_graph(G, v_size, a_size);
      return;
}

/***********************************************************************
*  NAME
*
*  glp_delete_graph - delete graph
*
*  SYNOPSIS
*
*  void glp_delete_graph(glp_graph *G);
*
*  DESCRIPTION
*
*  The routine glp_delete_graph deletes the specified graph and frees
*  all the memory allocated to this program object. */

void glp_delete_graph(glp_graph *G)
{     delete_graph(G);
      xfree(G);
      return;
}

/**********************************************************************/

void glp_create_v_index(glp_graph *G)
{     /* create vertex name index */
      glp_vertex *v;
      int i;
      if (G->index == NULL)
      {  G->index = avl_create_tree(avl_strcmp, NULL);
         for (i = 1; i <= G->nv; i++)
         {  v = G->v[i];
            xassert(v->entry == NULL);
            if (v->name != NULL)
            {  v->entry = avl_insert_node(G->index, v->name);
               avl_set_node_link(v->entry, v);
            }
         }
      }
      return;
}

int glp_find_vertex(glp_graph *G, const char *name)
{     /* find vertex by its name */
      AVLNODE *node;
      int i = 0;
      if (G->index == NULL)
         xerror("glp_find_vertex: vertex name index does not exist\n");
      if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
      {  node = avl_find_node(G->index, name);
         if (node != NULL)
            i = ((glp_vertex *)avl_get_node_link(node))->i;
      }
      return i;
}

void glp_delete_v_index(glp_graph *G)
{     /* delete vertex name index */
      int i;
      if (G->index != NULL)
      {  avl_delete_tree(G->index), G->index = NULL;
         for (i = 1; i <= G->nv; i++) G->v[i]->entry = NULL;
      }
      return;
}

/* eof */