LightGraphs basic usage

Noob question. How do I add a vertex to a graph and get its value, as in the following code fragment?

add_vertex!(g)
v = maximum(vertices(g)) # better way to do this?
add_edge!(g, cur, v)
cur = v

That works for simple cases where no vertices have been removed, but probably isn’t very efficient. Is there a canonical way to do it?

just use nv(g):

add_vertex!(g)
v = nv(g)
add_edge!(g, cur, v)
cur = v

The “more correct” way to do this is to understand that add_vertex! adds to to the end of the range that describes vertices, and use last(vertices(g)) instead of nv, but they’re essentially the same in terms of complexity.

2 Likes