]>
wirehaze git hosting - graph-theory.git/blob - graph/prim.c
1 #include "graph/prim.h"
2 #include "graph/search.h"
7 * todo priority queue, currently its broken
11 prim_ (struct graph
*g
, struct graph
*pt
, struct state
*vs
, unsigned int ri
,
12 unsigned int (*w
) (unsigned int, unsigned int))
16 struct state
*rs
, *ns
;
21 if (!(sq
.q
= calloc (g
->nv
, sizeof *sq
.q
)))
31 ri
= vertex_id (g
, r
);
35 while (e
&& (n
= e
->to
))
37 ni
= vertex_id (g
, n
);
45 if ((err
= add_arc (pt
, ri
, ni
)))
58 if (ns
->w
< w (ri
, ni
))
61 del_arc (pt
, vertex_id (g
, ns
->from
), ni
);
63 if ((err
= add_arc (pt
, ri
, ni
)))
87 prim (struct graph
*g
, unsigned int (*w
) (unsigned int, unsigned int))
93 if (!(pt
= new_graph (g
->nv
)))
96 if (!(vs
= calloc (g
->nv
, sizeof *vs
)))
99 for (vi
= 0; vi
< g
->nv
; ++vi
)
100 if (vs
[vi
].c
== WHITE
)
101 if (prim_ (g
, pt
, vs
, vi
, w
))