1 /* glpapi15.c (basic graph and network routines) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
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: <mao@gnu.org>.
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.
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.
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 ***********************************************************************/
27 /* CAUTION: DO NOT CHANGE THE LIMITS BELOW */
29 #define NV_MAX 100000000 /* = 100*10^6 */
30 /* maximal number of vertices in the graph */
32 #define NA_MAX 500000000 /* = 500*10^6 */
33 /* maximal number of arcs in the graph */
35 /***********************************************************************
38 * glp_create_graph - create graph
42 * glp_graph *glp_create_graph(int v_size, int a_size);
46 * The routine creates a new graph, which initially is empty, i.e. has
47 * no vertices and arcs.
49 * The parameter v_size specifies the size of data associated with each
50 * vertex of the graph (0 to 256 bytes).
52 * The parameter a_size specifies the size of data associated with each
53 * arc of the graph (0 to 256 bytes).
57 * The routine returns a pointer to the graph created. */
59 static void create_graph(glp_graph *G, int v_size, int a_size)
60 { G->pool = dmp_create_pool();
64 G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *));
71 glp_graph *glp_create_graph(int v_size, int a_size)
73 if (!(0 <= v_size && v_size <= 256))
74 xerror("glp_create_graph: v_size = %d; invalid size of vertex "
76 if (!(0 <= a_size && a_size <= 256))
77 xerror("glp_create_graph: a_size = %d; invalid size of arc dat"
79 G = xmalloc(sizeof(glp_graph));
80 create_graph(G, v_size, a_size);
84 /***********************************************************************
87 * glp_set_graph_name - assign (change) graph name
91 * void glp_set_graph_name(glp_graph *G, const char *name);
95 * The routine glp_set_graph_name assigns a symbolic name specified by
96 * the character string name (1 to 255 chars) to the graph.
98 * If the parameter name is NULL or an empty string, the routine erases
99 * the existing symbolic name of the graph. */
101 void glp_set_graph_name(glp_graph *G, const char *name)
102 { if (G->name != NULL)
103 { dmp_free_atom(G->pool, G->name, strlen(G->name)+1);
106 if (!(name == NULL || name[0] == '\0'))
108 for (j = 0; name[j] != '\0'; j++)
110 xerror("glp_set_graph_name: graph name too long\n");
111 if (iscntrl((unsigned char)name[j]))
112 xerror("glp_set_graph_name: graph name contains invalid "
115 G->name = dmp_get_atom(G->pool, strlen(name)+1);
116 strcpy(G->name, name);
121 /***********************************************************************
124 * glp_add_vertices - add new vertices to graph
128 * int glp_add_vertices(glp_graph *G, int nadd);
132 * The routine glp_add_vertices adds nadd vertices to the specified
133 * graph. New vertices are always added to the end of the vertex list,
134 * so ordinal numbers of existing vertices remain unchanged.
136 * Being added each new vertex is isolated (has no incident arcs).
140 * The routine glp_add_vertices returns an ordinal number of the first
141 * new vertex added to the graph. */
143 int glp_add_vertices(glp_graph *G, int nadd)
146 xerror("glp_add_vertices: nadd = %d; invalid number of vertice"
148 if (nadd > NV_MAX - G->nv)
149 xerror("glp_add_vertices: nadd = %d; too many vertices\n",
151 /* determine new number of vertices */
152 nv_new = G->nv + nadd;
153 /* increase the room, if necessary */
154 if (G->nv_max < nv_new)
155 { glp_vertex **save = G->v;
156 while (G->nv_max < nv_new)
157 { G->nv_max += G->nv_max;
158 xassert(G->nv_max > 0);
160 G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *));
161 memcpy(&G->v[1], &save[1], G->nv * sizeof(glp_vertex *));
164 /* add new vertices to the end of the vertex list */
165 for (i = G->nv+1; i <= nv_new; i++)
167 G->v[i] = v = dmp_get_atom(G->pool, sizeof(glp_vertex));
174 { v->data = dmp_get_atom(G->pool, G->v_size);
175 memset(v->data, 0, G->v_size);
178 v->in = v->out = NULL;
180 /* set new number of vertices */
182 /* return the ordinal number of the first vertex added */
183 return nv_new - nadd + 1;
186 /**********************************************************************/
188 void glp_set_vertex_name(glp_graph *G, int i, const char *name)
189 { /* assign (change) vertex name */
191 if (!(1 <= i && i <= G->nv))
192 xerror("glp_set_vertex_name: i = %d; vertex number out of rang"
196 { if (v->entry != NULL)
197 { xassert(G->index != NULL);
198 avl_delete_node(G->index, v->entry);
201 dmp_free_atom(G->pool, v->name, strlen(v->name)+1);
204 if (!(name == NULL || name[0] == '\0'))
206 for (k = 0; name[k] != '\0'; k++)
208 xerror("glp_set_vertex_name: i = %d; vertex name too lon"
210 if (iscntrl((unsigned char)name[k]))
211 xerror("glp_set_vertex_name: i = %d; vertex name contain"
212 "s invalid character(s)\n", i);
214 v->name = dmp_get_atom(G->pool, strlen(name)+1);
215 strcpy(v->name, name);
216 if (G->index != NULL)
217 { xassert(v->entry == NULL);
218 v->entry = avl_insert_node(G->index, v->name);
219 avl_set_node_link(v->entry, v);
225 /***********************************************************************
228 * glp_add_arc - add new arc to graph
232 * glp_arc *glp_add_arc(glp_graph *G, int i, int j);
236 * The routine glp_add_arc adds a new arc to the specified graph.
238 * The parameters i and j specify the ordinal numbers of, resp., tail
239 * and head vertices of the arc. Note that self-loops and multiple arcs
244 * The routine glp_add_arc returns a pointer to the arc added. */
246 glp_arc *glp_add_arc(glp_graph *G, int i, int j)
248 if (!(1 <= i && i <= G->nv))
249 xerror("glp_add_arc: i = %d; tail vertex number out of range\n"
251 if (!(1 <= j && j <= G->nv))
252 xerror("glp_add_arc: j = %d; head vertex number out of range\n"
255 xerror("glp_add_arc: too many arcs\n");
256 a = dmp_get_atom(G->pool, sizeof(glp_arc));
262 { a->data = dmp_get_atom(G->pool, G->a_size);
263 memset(a->data, 0, G->a_size);
267 a->t_next = G->v[i]->out;
268 if (a->t_next != NULL) a->t_next->t_prev = a;
270 a->h_next = G->v[j]->in;
271 if (a->h_next != NULL) a->h_next->h_prev = a;
272 G->v[i]->out = G->v[j]->in = a;
277 /***********************************************************************
280 * glp_del_vertices - delete vertices from graph
284 * void glp_del_vertices(glp_graph *G, int ndel, const int num[]);
288 * The routine glp_del_vertices deletes vertices along with all
289 * incident arcs from the specified graph. Ordinal numbers of vertices
290 * to be deleted should be placed in locations num[1], ..., num[ndel],
293 * Note that deleting vertices involves changing ordinal numbers of
294 * other vertices remaining in the graph. New ordinal numbers of the
295 * remaining vertices are assigned under the assumption that the
296 * original order of vertices is not changed. */
298 void glp_del_vertices(glp_graph *G, int ndel, const int num[])
301 /* scan the list of vertices to be deleted */
302 if (!(1 <= ndel && ndel <= G->nv))
303 xerror("glp_del_vertices: ndel = %d; invalid number of vertice"
305 for (k = 1; k <= ndel; k++)
306 { /* take the number of vertex to be deleted */
308 /* obtain pointer to i-th vertex */
309 if (!(1 <= i && i <= G->nv))
310 xerror("glp_del_vertices: num[%d] = %d; vertex number out o"
313 /* check that the vertex is not marked yet */
315 xerror("glp_del_vertices: num[%d] = %d; duplicate vertex nu"
316 "mbers not allowed\n", k, i);
317 /* erase symbolic name assigned to the vertex */
318 glp_set_vertex_name(G, i, NULL);
319 xassert(v->name == NULL);
320 xassert(v->entry == NULL);
321 /* free vertex data, if allocated */
323 dmp_free_atom(G->pool, v->data, G->v_size);
324 /* delete all incoming arcs */
325 while (v->in != NULL)
326 glp_del_arc(G, v->in);
327 /* delete all outgoing arcs */
328 while (v->out != NULL)
329 glp_del_arc(G, v->out);
330 /* mark the vertex to be deleted */
333 /* delete all marked vertices from the vertex list */
335 for (i = 1; i <= G->nv; i++)
336 { /* obtain pointer to i-th vertex */
338 /* check if the vertex is marked */
340 { /* it is marked, delete it */
341 dmp_free_atom(G->pool, v, sizeof(glp_vertex));
344 { /* it is not marked, keep it */
349 /* set new number of vertices in the graph */
354 /***********************************************************************
357 * glp_del_arc - delete arc from graph
361 * void glp_del_arc(glp_graph *G, glp_arc *a);
365 * The routine glp_del_arc deletes an arc from the specified graph.
366 * The arc to be deleted must exist. */
368 void glp_del_arc(glp_graph *G, glp_arc *a)
369 { /* some sanity checks */
371 xassert(1 <= a->tail->i && a->tail->i <= G->nv);
372 xassert(a->tail == G->v[a->tail->i]);
373 xassert(1 <= a->head->i && a->head->i <= G->nv);
374 xassert(a->head == G->v[a->head->i]);
375 /* remove the arc from the list of incoming arcs */
376 if (a->h_prev == NULL)
377 a->head->in = a->h_next;
379 a->h_prev->h_next = a->h_next;
380 if (a->h_next == NULL)
383 a->h_next->h_prev = a->h_prev;
384 /* remove the arc from the list of outgoing arcs */
385 if (a->t_prev == NULL)
386 a->tail->out = a->t_next;
388 a->t_prev->t_next = a->t_next;
389 if (a->t_next == NULL)
392 a->t_next->t_prev = a->t_prev;
393 /* free arc data, if allocated */
395 dmp_free_atom(G->pool, a->data, G->a_size);
396 /* delete the arc from the graph */
397 dmp_free_atom(G->pool, a, sizeof(glp_arc));
402 /***********************************************************************
405 * glp_erase_graph - erase graph content
409 * void glp_erase_graph(glp_graph *G, int v_size, int a_size);
413 * The routine glp_erase_graph erases the content of the specified
414 * graph. The effect of this operation is the same as if the graph
415 * would be deleted with the routine glp_delete_graph and then created
416 * anew with the routine glp_create_graph, with exception that the
417 * handle (pointer) to the graph remains valid. */
419 static void delete_graph(glp_graph *G)
420 { dmp_delete_pool(G->pool);
422 if (G->index != NULL) avl_delete_tree(G->index);
426 void glp_erase_graph(glp_graph *G, int v_size, int a_size)
427 { if (!(0 <= v_size && v_size <= 256))
428 xerror("glp_erase_graph: v_size = %d; invalid size of vertex d"
430 if (!(0 <= a_size && a_size <= 256))
431 xerror("glp_erase_graph: a_size = %d; invalid size of arc data"
434 create_graph(G, v_size, a_size);
438 /***********************************************************************
441 * glp_delete_graph - delete graph
445 * void glp_delete_graph(glp_graph *G);
449 * The routine glp_delete_graph deletes the specified graph and frees
450 * all the memory allocated to this program object. */
452 void glp_delete_graph(glp_graph *G)
458 /**********************************************************************/
460 void glp_create_v_index(glp_graph *G)
461 { /* create vertex name index */
464 if (G->index == NULL)
465 { G->index = avl_create_tree(avl_strcmp, NULL);
466 for (i = 1; i <= G->nv; i++)
468 xassert(v->entry == NULL);
470 { v->entry = avl_insert_node(G->index, v->name);
471 avl_set_node_link(v->entry, v);
478 int glp_find_vertex(glp_graph *G, const char *name)
479 { /* find vertex by its name */
482 if (G->index == NULL)
483 xerror("glp_find_vertex: vertex name index does not exist\n");
484 if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
485 { node = avl_find_node(G->index, name);
487 i = ((glp_vertex *)avl_get_node_link(node))->i;
492 void glp_delete_v_index(glp_graph *G)
493 { /* delete vertex name index */
495 if (G->index != NULL)
496 { avl_delete_tree(G->index), G->index = NULL;
497 for (i = 1; i <= G->nv; i++) G->v[i]->entry = NULL;
502 /***********************************************************************
505 * glp_read_graph - read graph from plain text file
509 * int glp_read_graph(glp_graph *G, const char *fname);
513 * The routine glp_read_graph reads a graph from a plain text file.
517 * If the operation was successful, the routine returns zero. Otherwise
518 * it prints an error message and returns non-zero. */
520 int glp_read_graph(glp_graph *G, const char *fname)
523 int nv, na, i, j, k, ret;
524 glp_erase_graph(G, G->v_size, G->a_size);
525 xprintf("Reading graph from `%s'...\n", fname);
526 data = glp_sdf_open_file(fname);
535 glp_sdf_set_jump(data, jump);
536 nv = glp_sdf_read_int(data);
538 glp_sdf_error(data, "invalid number of vertices\n");
539 na = glp_sdf_read_int(data);
541 glp_sdf_error(data, "invalid number of arcs\n");
542 xprintf("Graph has %d vert%s and %d arc%s\n",
543 nv, nv == 1 ? "ex" : "ices", na, na == 1 ? "" : "s");
544 if (nv > 0) glp_add_vertices(G, nv);
545 for (k = 1; k <= na; k++)
546 { i = glp_sdf_read_int(data);
547 if (!(1 <= i && i <= nv))
548 glp_sdf_error(data, "tail vertex number out of range\n");
549 j = glp_sdf_read_int(data);
550 if (!(1 <= j && j <= nv))
551 glp_sdf_error(data, "head vertex number out of range\n");
552 glp_add_arc(G, i, j);
554 xprintf("%d lines were read\n", glp_sdf_line(data));
556 done: if (data != NULL) glp_sdf_close_file(data);
560 /***********************************************************************
563 * glp_write_graph - write graph to plain text file
567 * int glp_write_graph(glp_graph *G, const char *fname).
571 * The routine glp_write_graph writes the specified graph to a plain
576 * If the operation was successful, the routine returns zero. Otherwise
577 * it prints an error message and returns non-zero. */
579 int glp_write_graph(glp_graph *G, const char *fname)
584 xprintf("Writing graph to `%s'...\n", fname);
585 fp = xfopen(fname, "w"), count = 0;
587 { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg());
591 xfprintf(fp, "%d %d\n", G->nv, G->na), count++;
592 for (i = 1; i <= G->nv; i++)
594 for (a = v->out; a != NULL; a = a->t_next)
595 xfprintf(fp, "%d %d\n", a->tail->i, a->head->i), count++;
599 { xprintf("Write error on `%s' - %s\n", fname, xerrmsg());
603 xprintf("%d lines were written\n", count);
605 done: if (fp != NULL) xfclose(fp);