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

**URL:** <https://discourse.julialang.org/t/create-a-di-graph-from-an-iterator-over-edges-with-a-certain-number-of-vertices/102751>\
**Category:** Graphs\
**Tags:** graphs\
**Created:** [August 12, 2023, 6:41pm UTC](https://discourse.julialang.org/t/create-a-di-graph-from-an-iterator-over-edges-with-a-certain-number-of-vertices/102751 "2023-08-12T18:41:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [August 12, 2023, 6:41pm UTC](https://discourse.julialang.org/t/create-a-di-graph-from-an-iterator-over-edges-with-a-certain-number-of-vertices/102751/1 "2023-08-12T18:41:25Z")

</div>

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

```julia
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:

```julia
SimpleDiGraphFromIterator(es)

```

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

```julia
g = SimpleGraph(10)
es = edges(g)
SimpleDiGraphFromIterator(es)

```

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.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [August 13, 2023, 7:21am UTC](https://discourse.julialang.org/t/create-a-di-graph-from-an-iterator-over-edges-with-a-certain-number-of-vertices/102751/2 "2023-08-13T07:21:50Z")

</div>

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](https://github.com/JuliaGraphs/Graphs.jl/pull/252)

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:

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

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [August 13, 2023, 3:20pm UTC](https://discourse.julialang.org/t/create-a-di-graph-from-an-iterator-over-edges-with-a-certain-number-of-vertices/102751/3 "2023-08-13T15:20:02Z")

</div>

That seems like the way to go, thanks 🙂
