Create a (Di)graph from an iterator over edges with a certain number of vertices

Given that I have a set of edges, e.g:

using Graphs
g = Graphs.grid([5, 5])
es = edges(g)

How do I create a new Graph, using those edges, but wit ha specific number of vertices? In this case I can do:


However, this does not permit to create graphs with vertices that are unconnected to an edge, e.g.

g = SimpleGraph(10)
es = edges(g)

creates an empty graph. Is there some way to specify that the graph should have a certain number of vertices? I can always create a new graph and add edges manually, but wanted to check whenever there was something better.

I don’t think it is possible, since there was already a similar pull request (which I haven’t had time to look at): New SimpleGraph constructor from tuples by GiggleLiu · Pull Request #252 · JuliaGraphs/Graphs.jl · GitHub

Instead of creating an empty graph and adding edges, I think the more efficient way right now would be to create a graph with all the edges and then add the required number of vertices:

g = SimpleDiGraphFromIterator(es)
add_vertices!(g, n_target - nv(g))

That seems like the way to go, thanks :slight_smile:

1 Like