]>
wirehaze git hosting - graph-theory.git/blob - graph/adj.c
6 new_graph (unsigned int nv
)
17 if (!(g
= malloc (sizeof *g
)))
25 if (!(g
->v
= calloc (nv
, sizeof *g
->v
)))
42 add_arc (struct graph
*g
, unsigned int vi
, unsigned int vj
)
46 if (vi
>= g
->nv
|| vj
>= g
->nv
)
47 return -(errno
= EINVAL
);
49 if (!(e
= malloc (sizeof *e
)))
53 e
->next
= g
->v
[vi
].adj
;
60 add_edge (struct graph
*g
, unsigned int vi
, unsigned int vj
)
70 if ((err
= add_arc (g
, vi
, vj
)))
73 if ((err
= add_arc (g
, vj
, vi
)))
82 return -(errno
= -err
);
86 del_arc (struct graph
*g
, unsigned int vi
, unsigned int vj
)
91 if (vi
>= g
->nv
|| vj
>= g
->nv
)
100 while (e
&& e
->to
!= j
)
106 if (!e
) /* not found */
118 del_edge (struct graph
*g
, unsigned int vi
, unsigned int vj
)
125 free_graph (struct graph
*g
)
133 for (i
= 0; i
< g
->nv
; ++i
)
138 del_arc (g
, i
, vertex_id (g
, v
->adj
->to
)); /* lazy */