alpar@1: /* glpapi15.c (basic graph and network routines) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpapi.h" alpar@1: alpar@1: /* CAUTION: DO NOT CHANGE THE LIMITS BELOW */ alpar@1: alpar@1: #define NV_MAX 100000000 /* = 100*10^6 */ alpar@1: /* maximal number of vertices in the graph */ alpar@1: alpar@1: #define NA_MAX 500000000 /* = 500*10^6 */ alpar@1: /* maximal number of arcs in the graph */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_create_graph - create graph alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * glp_graph *glp_create_graph(int v_size, int a_size); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine creates a new graph, which initially is empty, i.e. has alpar@1: * no vertices and arcs. alpar@1: * alpar@1: * The parameter v_size specifies the size of data associated with each alpar@1: * vertex of the graph (0 to 256 bytes). alpar@1: * alpar@1: * The parameter a_size specifies the size of data associated with each alpar@1: * arc of the graph (0 to 256 bytes). alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine returns a pointer to the graph created. */ alpar@1: alpar@1: static void create_graph(glp_graph *G, int v_size, int a_size) alpar@1: { G->pool = dmp_create_pool(); alpar@1: G->name = NULL; alpar@1: G->nv_max = 50; alpar@1: G->nv = G->na = 0; alpar@1: G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *)); alpar@1: G->index = NULL; alpar@1: G->v_size = v_size; alpar@1: G->a_size = a_size; alpar@1: return; alpar@1: } alpar@1: alpar@1: glp_graph *glp_create_graph(int v_size, int a_size) alpar@1: { glp_graph *G; alpar@1: if (!(0 <= v_size && v_size <= 256)) alpar@1: xerror("glp_create_graph: v_size = %d; invalid size of vertex " alpar@1: "data\n", v_size); alpar@1: if (!(0 <= a_size && a_size <= 256)) alpar@1: xerror("glp_create_graph: a_size = %d; invalid size of arc dat" alpar@1: "a\n", a_size); alpar@1: G = xmalloc(sizeof(glp_graph)); alpar@1: create_graph(G, v_size, a_size); alpar@1: return G; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_set_graph_name - assign (change) graph name alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_set_graph_name(glp_graph *G, const char *name); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_set_graph_name assigns a symbolic name specified by alpar@1: * the character string name (1 to 255 chars) to the graph. alpar@1: * alpar@1: * If the parameter name is NULL or an empty string, the routine erases alpar@1: * the existing symbolic name of the graph. */ alpar@1: alpar@1: void glp_set_graph_name(glp_graph *G, const char *name) alpar@1: { if (G->name != NULL) alpar@1: { dmp_free_atom(G->pool, G->name, strlen(G->name)+1); alpar@1: G->name = NULL; alpar@1: } alpar@1: if (!(name == NULL || name[0] == '\0')) alpar@1: { int j; alpar@1: for (j = 0; name[j] != '\0'; j++) alpar@1: { if (j == 256) alpar@1: xerror("glp_set_graph_name: graph name too long\n"); alpar@1: if (iscntrl((unsigned char)name[j])) alpar@1: xerror("glp_set_graph_name: graph name contains invalid " alpar@1: "character(s)\n"); alpar@1: } alpar@1: G->name = dmp_get_atom(G->pool, strlen(name)+1); alpar@1: strcpy(G->name, name); alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_add_vertices - add new vertices to graph alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_add_vertices(glp_graph *G, int nadd); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_add_vertices adds nadd vertices to the specified alpar@1: * graph. New vertices are always added to the end of the vertex list, alpar@1: * so ordinal numbers of existing vertices remain unchanged. alpar@1: * alpar@1: * Being added each new vertex is isolated (has no incident arcs). alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_add_vertices returns an ordinal number of the first alpar@1: * new vertex added to the graph. */ alpar@1: alpar@1: int glp_add_vertices(glp_graph *G, int nadd) alpar@1: { int i, nv_new; alpar@1: if (nadd < 1) alpar@1: xerror("glp_add_vertices: nadd = %d; invalid number of vertice" alpar@1: "s\n", nadd); alpar@1: if (nadd > NV_MAX - G->nv) alpar@1: xerror("glp_add_vertices: nadd = %d; too many vertices\n", alpar@1: nadd); alpar@1: /* determine new number of vertices */ alpar@1: nv_new = G->nv + nadd; alpar@1: /* increase the room, if necessary */ alpar@1: if (G->nv_max < nv_new) alpar@1: { glp_vertex **save = G->v; alpar@1: while (G->nv_max < nv_new) alpar@1: { G->nv_max += G->nv_max; alpar@1: xassert(G->nv_max > 0); alpar@1: } alpar@1: G->v = xcalloc(1+G->nv_max, sizeof(glp_vertex *)); alpar@1: memcpy(&G->v[1], &save[1], G->nv * sizeof(glp_vertex *)); alpar@1: xfree(save); alpar@1: } alpar@1: /* add new vertices to the end of the vertex list */ alpar@1: for (i = G->nv+1; i <= nv_new; i++) alpar@1: { glp_vertex *v; alpar@1: G->v[i] = v = dmp_get_atom(G->pool, sizeof(glp_vertex)); alpar@1: v->i = i; alpar@1: v->name = NULL; alpar@1: v->entry = NULL; alpar@1: if (G->v_size == 0) alpar@1: v->data = NULL; alpar@1: else alpar@1: { v->data = dmp_get_atom(G->pool, G->v_size); alpar@1: memset(v->data, 0, G->v_size); alpar@1: } alpar@1: v->temp = NULL; alpar@1: v->in = v->out = NULL; alpar@1: } alpar@1: /* set new number of vertices */ alpar@1: G->nv = nv_new; alpar@1: /* return the ordinal number of the first vertex added */ alpar@1: return nv_new - nadd + 1; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: void glp_set_vertex_name(glp_graph *G, int i, const char *name) alpar@1: { /* assign (change) vertex name */ alpar@1: glp_vertex *v; alpar@1: if (!(1 <= i && i <= G->nv)) alpar@1: xerror("glp_set_vertex_name: i = %d; vertex number out of rang" alpar@1: "e\n", i); alpar@1: v = G->v[i]; alpar@1: if (v->name != NULL) alpar@1: { if (v->entry != NULL) alpar@1: { xassert(G->index != NULL); alpar@1: avl_delete_node(G->index, v->entry); alpar@1: v->entry = NULL; alpar@1: } alpar@1: dmp_free_atom(G->pool, v->name, strlen(v->name)+1); alpar@1: v->name = NULL; alpar@1: } alpar@1: if (!(name == NULL || name[0] == '\0')) alpar@1: { int k; alpar@1: for (k = 0; name[k] != '\0'; k++) alpar@1: { if (k == 256) alpar@1: xerror("glp_set_vertex_name: i = %d; vertex name too lon" alpar@1: "g\n", i); alpar@1: if (iscntrl((unsigned char)name[k])) alpar@1: xerror("glp_set_vertex_name: i = %d; vertex name contain" alpar@1: "s invalid character(s)\n", i); alpar@1: } alpar@1: v->name = dmp_get_atom(G->pool, strlen(name)+1); alpar@1: strcpy(v->name, name); alpar@1: if (G->index != NULL) alpar@1: { xassert(v->entry == NULL); alpar@1: v->entry = avl_insert_node(G->index, v->name); alpar@1: avl_set_node_link(v->entry, v); alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_add_arc - add new arc to graph alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * glp_arc *glp_add_arc(glp_graph *G, int i, int j); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_add_arc adds a new arc to the specified graph. alpar@1: * alpar@1: * The parameters i and j specify the ordinal numbers of, resp., tail alpar@1: * and head vertices of the arc. Note that self-loops and multiple arcs alpar@1: * are allowed. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine glp_add_arc returns a pointer to the arc added. */ alpar@1: alpar@1: glp_arc *glp_add_arc(glp_graph *G, int i, int j) alpar@1: { glp_arc *a; alpar@1: if (!(1 <= i && i <= G->nv)) alpar@1: xerror("glp_add_arc: i = %d; tail vertex number out of range\n" alpar@1: , i); alpar@1: if (!(1 <= j && j <= G->nv)) alpar@1: xerror("glp_add_arc: j = %d; head vertex number out of range\n" alpar@1: , j); alpar@1: if (G->na == NA_MAX) alpar@1: xerror("glp_add_arc: too many arcs\n"); alpar@1: a = dmp_get_atom(G->pool, sizeof(glp_arc)); alpar@1: a->tail = G->v[i]; alpar@1: a->head = G->v[j]; alpar@1: if (G->a_size == 0) alpar@1: a->data = NULL; alpar@1: else alpar@1: { a->data = dmp_get_atom(G->pool, G->a_size); alpar@1: memset(a->data, 0, G->a_size); alpar@1: } alpar@1: a->temp = NULL; alpar@1: a->t_prev = NULL; alpar@1: a->t_next = G->v[i]->out; alpar@1: if (a->t_next != NULL) a->t_next->t_prev = a; alpar@1: a->h_prev = NULL; alpar@1: a->h_next = G->v[j]->in; alpar@1: if (a->h_next != NULL) a->h_next->h_prev = a; alpar@1: G->v[i]->out = G->v[j]->in = a; alpar@1: G->na++; alpar@1: return a; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_del_vertices - delete vertices from graph alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_del_vertices(glp_graph *G, int ndel, const int num[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_del_vertices deletes vertices along with all alpar@1: * incident arcs from the specified graph. Ordinal numbers of vertices alpar@1: * to be deleted should be placed in locations num[1], ..., num[ndel], alpar@1: * ndel > 0. alpar@1: * alpar@1: * Note that deleting vertices involves changing ordinal numbers of alpar@1: * other vertices remaining in the graph. New ordinal numbers of the alpar@1: * remaining vertices are assigned under the assumption that the alpar@1: * original order of vertices is not changed. */ alpar@1: alpar@1: void glp_del_vertices(glp_graph *G, int ndel, const int num[]) alpar@1: { glp_vertex *v; alpar@1: int i, k, nv_new; alpar@1: /* scan the list of vertices to be deleted */ alpar@1: if (!(1 <= ndel && ndel <= G->nv)) alpar@1: xerror("glp_del_vertices: ndel = %d; invalid number of vertice" alpar@1: "s\n", ndel); alpar@1: for (k = 1; k <= ndel; k++) alpar@1: { /* take the number of vertex to be deleted */ alpar@1: i = num[k]; alpar@1: /* obtain pointer to i-th vertex */ alpar@1: if (!(1 <= i && i <= G->nv)) alpar@1: xerror("glp_del_vertices: num[%d] = %d; vertex number out o" alpar@1: "f range\n", k, i); alpar@1: v = G->v[i]; alpar@1: /* check that the vertex is not marked yet */ alpar@1: if (v->i == 0) alpar@1: xerror("glp_del_vertices: num[%d] = %d; duplicate vertex nu" alpar@1: "mbers not allowed\n", k, i); alpar@1: /* erase symbolic name assigned to the vertex */ alpar@1: glp_set_vertex_name(G, i, NULL); alpar@1: xassert(v->name == NULL); alpar@1: xassert(v->entry == NULL); alpar@1: /* free vertex data, if allocated */ alpar@1: if (v->data != NULL) alpar@1: dmp_free_atom(G->pool, v->data, G->v_size); alpar@1: /* delete all incoming arcs */ alpar@1: while (v->in != NULL) alpar@1: glp_del_arc(G, v->in); alpar@1: /* delete all outgoing arcs */ alpar@1: while (v->out != NULL) alpar@1: glp_del_arc(G, v->out); alpar@1: /* mark the vertex to be deleted */ alpar@1: v->i = 0; alpar@1: } alpar@1: /* delete all marked vertices from the vertex list */ alpar@1: nv_new = 0; alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { /* obtain pointer to i-th vertex */ alpar@1: v = G->v[i]; alpar@1: /* check if the vertex is marked */ alpar@1: if (v->i == 0) alpar@1: { /* it is marked, delete it */ alpar@1: dmp_free_atom(G->pool, v, sizeof(glp_vertex)); alpar@1: } alpar@1: else alpar@1: { /* it is not marked, keep it */ alpar@1: v->i = ++nv_new; alpar@1: G->v[v->i] = v; alpar@1: } alpar@1: } alpar@1: /* set new number of vertices in the graph */ alpar@1: G->nv = nv_new; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_del_arc - delete arc from graph alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_del_arc(glp_graph *G, glp_arc *a); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_del_arc deletes an arc from the specified graph. alpar@1: * The arc to be deleted must exist. */ alpar@1: alpar@1: void glp_del_arc(glp_graph *G, glp_arc *a) alpar@1: { /* some sanity checks */ alpar@1: xassert(G->na > 0); alpar@1: xassert(1 <= a->tail->i && a->tail->i <= G->nv); alpar@1: xassert(a->tail == G->v[a->tail->i]); alpar@1: xassert(1 <= a->head->i && a->head->i <= G->nv); alpar@1: xassert(a->head == G->v[a->head->i]); alpar@1: /* remove the arc from the list of incoming arcs */ alpar@1: if (a->h_prev == NULL) alpar@1: a->head->in = a->h_next; alpar@1: else alpar@1: a->h_prev->h_next = a->h_next; alpar@1: if (a->h_next == NULL) alpar@1: ; alpar@1: else alpar@1: a->h_next->h_prev = a->h_prev; alpar@1: /* remove the arc from the list of outgoing arcs */ alpar@1: if (a->t_prev == NULL) alpar@1: a->tail->out = a->t_next; alpar@1: else alpar@1: a->t_prev->t_next = a->t_next; alpar@1: if (a->t_next == NULL) alpar@1: ; alpar@1: else alpar@1: a->t_next->t_prev = a->t_prev; alpar@1: /* free arc data, if allocated */ alpar@1: if (a->data != NULL) alpar@1: dmp_free_atom(G->pool, a->data, G->a_size); alpar@1: /* delete the arc from the graph */ alpar@1: dmp_free_atom(G->pool, a, sizeof(glp_arc)); alpar@1: G->na--; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_erase_graph - erase graph content alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_erase_graph(glp_graph *G, int v_size, int a_size); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_erase_graph erases the content of the specified alpar@1: * graph. The effect of this operation is the same as if the graph alpar@1: * would be deleted with the routine glp_delete_graph and then created alpar@1: * anew with the routine glp_create_graph, with exception that the alpar@1: * handle (pointer) to the graph remains valid. */ alpar@1: alpar@1: static void delete_graph(glp_graph *G) alpar@1: { dmp_delete_pool(G->pool); alpar@1: xfree(G->v); alpar@1: if (G->index != NULL) avl_delete_tree(G->index); alpar@1: return; alpar@1: } alpar@1: alpar@1: void glp_erase_graph(glp_graph *G, int v_size, int a_size) alpar@1: { if (!(0 <= v_size && v_size <= 256)) alpar@1: xerror("glp_erase_graph: v_size = %d; invalid size of vertex d" alpar@1: "ata\n", v_size); alpar@1: if (!(0 <= a_size && a_size <= 256)) alpar@1: xerror("glp_erase_graph: a_size = %d; invalid size of arc data" alpar@1: "\n", a_size); alpar@1: delete_graph(G); alpar@1: create_graph(G, v_size, a_size); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_delete_graph - delete graph alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * void glp_delete_graph(glp_graph *G); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_delete_graph deletes the specified graph and frees alpar@1: * all the memory allocated to this program object. */ alpar@1: alpar@1: void glp_delete_graph(glp_graph *G) alpar@1: { delete_graph(G); alpar@1: xfree(G); alpar@1: return; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: void glp_create_v_index(glp_graph *G) alpar@1: { /* create vertex name index */ alpar@1: glp_vertex *v; alpar@1: int i; alpar@1: if (G->index == NULL) alpar@1: { G->index = avl_create_tree(avl_strcmp, NULL); alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: xassert(v->entry == NULL); alpar@1: if (v->name != NULL) alpar@1: { v->entry = avl_insert_node(G->index, v->name); alpar@1: avl_set_node_link(v->entry, v); alpar@1: } alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: int glp_find_vertex(glp_graph *G, const char *name) alpar@1: { /* find vertex by its name */ alpar@1: AVLNODE *node; alpar@1: int i = 0; alpar@1: if (G->index == NULL) alpar@1: xerror("glp_find_vertex: vertex name index does not exist\n"); alpar@1: if (!(name == NULL || name[0] == '\0' || strlen(name) > 255)) alpar@1: { node = avl_find_node(G->index, name); alpar@1: if (node != NULL) alpar@1: i = ((glp_vertex *)avl_get_node_link(node))->i; alpar@1: } alpar@1: return i; alpar@1: } alpar@1: alpar@1: void glp_delete_v_index(glp_graph *G) alpar@1: { /* delete vertex name index */ alpar@1: int i; alpar@1: if (G->index != NULL) alpar@1: { avl_delete_tree(G->index), G->index = NULL; alpar@1: for (i = 1; i <= G->nv; i++) G->v[i]->entry = NULL; alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_read_graph - read graph from plain text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_read_graph(glp_graph *G, const char *fname); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_read_graph reads a graph from a plain text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_read_graph(glp_graph *G, const char *fname) alpar@1: { glp_data *data; alpar@1: jmp_buf jump; alpar@1: int nv, na, i, j, k, ret; alpar@1: glp_erase_graph(G, G->v_size, G->a_size); alpar@1: xprintf("Reading graph from `%s'...\n", fname); alpar@1: data = glp_sdf_open_file(fname); alpar@1: if (data == NULL) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: if (setjmp(jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: glp_sdf_set_jump(data, jump); alpar@1: nv = glp_sdf_read_int(data); alpar@1: if (nv < 0) alpar@1: glp_sdf_error(data, "invalid number of vertices\n"); alpar@1: na = glp_sdf_read_int(data); alpar@1: if (na < 0) alpar@1: glp_sdf_error(data, "invalid number of arcs\n"); alpar@1: xprintf("Graph has %d vert%s and %d arc%s\n", alpar@1: nv, nv == 1 ? "ex" : "ices", na, na == 1 ? "" : "s"); alpar@1: if (nv > 0) glp_add_vertices(G, nv); alpar@1: for (k = 1; k <= na; k++) alpar@1: { i = glp_sdf_read_int(data); alpar@1: if (!(1 <= i && i <= nv)) alpar@1: glp_sdf_error(data, "tail vertex number out of range\n"); alpar@1: j = glp_sdf_read_int(data); alpar@1: if (!(1 <= j && j <= nv)) alpar@1: glp_sdf_error(data, "head vertex number out of range\n"); alpar@1: glp_add_arc(G, i, j); alpar@1: } alpar@1: xprintf("%d lines were read\n", glp_sdf_line(data)); alpar@1: ret = 0; alpar@1: done: if (data != NULL) glp_sdf_close_file(data); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * glp_write_graph - write graph to plain text file alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int glp_write_graph(glp_graph *G, const char *fname). alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine glp_write_graph writes the specified graph to a plain alpar@1: * text file. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If the operation was successful, the routine returns zero. Otherwise alpar@1: * it prints an error message and returns non-zero. */ alpar@1: alpar@1: int glp_write_graph(glp_graph *G, const char *fname) alpar@1: { XFILE *fp; alpar@1: glp_vertex *v; alpar@1: glp_arc *a; alpar@1: int i, count, ret; alpar@1: xprintf("Writing graph to `%s'...\n", fname); alpar@1: fp = xfopen(fname, "w"), count = 0; alpar@1: if (fp == NULL) alpar@1: { xprintf("Unable to create `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xfprintf(fp, "%d %d\n", G->nv, G->na), count++; alpar@1: for (i = 1; i <= G->nv; i++) alpar@1: { v = G->v[i]; alpar@1: for (a = v->out; a != NULL; a = a->t_next) alpar@1: xfprintf(fp, "%d %d\n", a->tail->i, a->head->i), count++; alpar@1: } alpar@1: xfflush(fp); alpar@1: if (xferror(fp)) alpar@1: { xprintf("Write error on `%s' - %s\n", fname, xerrmsg()); alpar@1: ret = 1; alpar@1: goto done; alpar@1: } alpar@1: xprintf("%d lines were written\n", count); alpar@1: ret = 0; alpar@1: done: if (fp != NULL) xfclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /* eof */