# Combine two graphs in lightgraph

**URL:** <https://discourse.julialang.org/t/combine-two-graphs-in-lightgraph/53462>\
**Category:** General Usage\
**Created:** [January 17, 2021, 12:52am UTC](https://discourse.julialang.org/t/combine-two-graphs-in-lightgraph/53462 "2021-01-17T00:52:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [January 17, 2021, 12:52am UTC](https://discourse.julialang.org/t/combine-two-graphs-in-lightgraph/53462/1 "2021-01-17T00:52:45Z")

</div>

Hi,

I have two graphs that represent SQL tables :

```julia
f = star_graph(5)
nodelabel = ["T1","C1","C2","C3","C4"]

h = star_graph(4)
nodelabel = ["T2","C1.2","C2.2","C3.2"]

```

I want to context both graphs on “C2” and “C3.2” how do I do that in lightgraph?

---

<div class="post-metadata">

**Author:** ![tmthyln](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tmthyln/32/21607_2.png) [@tmthyln](https://discourse.julialang.org/u/tmthyln)\
**Post date:** [January 17, 2021, 1:51am UTC](https://discourse.julialang.org/t/combine-two-graphs-in-lightgraph/53462/2 "2021-01-17T01:51:26Z")

</div>

Do you want the resulting graph to have C2 and C3.2 be the same vertex, or have an edge between them in their union? To create the combined graph, you can use [`blockdiag`](https://juliagraphs.org/LightGraphs.jl/latest/operators/#SparseArrays.blockdiag-Union%7BTuple%7BAG%7D,%20Tuple%7BAG,AG%7D%7D%20where%20AG%3C:AbstractGraph), and then slightly adjust the resulting graph.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [January 17, 2021, 10:30am UTC](https://discourse.julialang.org/t/combine-two-graphs-in-lightgraph/53462/3 "2021-01-17T10:30:11Z")

</div>

hi,

I want an edge between C2 & C3.2. I will check out blockdiag!

thanks!

EDIT:  
I tried this function but now got a gplot with the two graphs in one picture but they are not unioned. I tried the join function which joins the whole two graphs to eachother?

The other challenge is how can I merge the nodelabels of both so I clearly see which graph is which?

Thanks in advance!

best,

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [January 17, 2021, 12:48pm UTC](https://discourse.julialang.org/t/combine-two-graphs-in-lightgraph/53462/4 "2021-01-17T12:48:17Z")

</div>

> [@AlexanderChen](#):
>
> The other challenge is how can I merge the nodelabels of both so I clearly see which graph is which?

If you do `blockdiag(f, h)`, the vertices and edges of `h` are appended to those of `f`, preserving their order, so you can also append (`vcat`) the vectors of node labels, e.g.

```julia
f = star_graph(5)
f_nodelabel = ["T1","C1","C2","C3","C4"]

h = star_graph(4)
h_nodelabel = ["T2","C1.2","C2.2","C3.2"]

fh = blockdiag(f, h)
fh_nodelabel = [f_nodelabel; h_nodelabel]

```

To join the graphs through C2 and C3.2 (the third node of the `f` and the fourth of `h`), this is an option:

```julia
add_edge!(fh, 3, nv(f)+4)

```

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [January 19, 2021, 6:44am UTC](https://discourse.julialang.org/t/combine-two-graphs-in-lightgraph/53462/5 "2021-01-19T06:44:40Z")

</div>

Ah, so `blockdiag` merges two seperate graphs into one disjointed graph and from there you can start adding edges. Thank you!
