Interfacing with the igraph R package

The igraph package (link) is the R interface to the `igraph` library (link) for network analysis. It implements an extensive selection of algorithms for creating and generating directed and undirected graphs, manipulating nodes and arcs, and it provides highly customizable plotting facilities. For this reason, bnlearn implements functions to import and export network structures to igraph's native objects, which are themselves of class `igraph`.

Exporting a network structure to igraph

Exporting objects is achieved with the conversion method `as.igraph()`.

```> library(bnlearn)
>
> dag.bnlearn = random.graph(LETTERS[1:10])
> dag.bnlearn
```
```
Random/Generated Bayesian network

model:
[A][D][E][H][B|A][F|E][J|A:D][C|A:B][G|F][I|B:E:F:G]
nodes:                                 10
arcs:                                  11
undirected arcs:                     0
directed arcs:                       11
average markov blanket size:           3.20
average neighbourhood size:            2.20
average branching factor:              1.10

generation algorithm:                  Full Ordering
arc sampling probability:              0.2222222
```
```> dag.igraph = as.igraph(dag.bnlearn)
> dag.igraph
```
```IGRAPH 536fd9a DN-- 10 11 --
+ attr: name (v/c)
+ edges from 536fd9a (vertex names):
[1] A->B A->C A->J B->C B->I D->J E->F E->I F->G F->I G->I
```

`as.igraph()` accepts objects of class `bn.fit` as well as of class `bn`, and it implitly call `bn.net()` (link) on the former.

```> dag.test = hc(learning.test)
> dag.test
```
```
Bayesian network learned via Score-based methods

model:
[A][C][F][B|A][D|A:C][E|B:F]
nodes:                                 6
arcs:                                  5
undirected arcs:                     0
directed arcs:                       5
average markov blanket size:           2.33
average neighbourhood size:            1.67
average branching factor:              0.83

learning algorithm:                    Hill-Climbing
score:                                 BIC (disc.)
penalization coefficient:              4.258597
tests used in the learning procedure:  40
optimized:                             TRUE
```
```> fitted = bn.fit(dag.test, learning.test)
> as.igraph(fitted)
```
```IGRAPH ba653cc DN-- 6 5 --
+ attr: name (v/c)
+ edges from ba653cc (vertex names):
[1] A->B A->D B->E C->D F->E
```

Undirected arcs are exported as a pair of directed arcs, that is, Xi — Xj becomes {Xi → Xj, Xi ← Xj}.

```> as.igraph(skeleton(dag.test))
```
```IGRAPH 946cb0d DN-- 6 10 --
+ attr: name (v/c)
+ edges from 946cb0d (vertex names):
[1] A->B A->D B->A B->E C->D D->A D->C E->B E->F F->E
```

Importing a network structure from igraph

Importing network structures works in the same way, with a conversion method `as.bn()`.

```> as.bn(dag.igraph)
```
```
Random/Generated Bayesian network

model:
[A][D][E][H][B|A][F|E][J|A:D][C|A:B][G|F][I|B:E:F:G]
nodes:                                 10
arcs:                                  11
undirected arcs:                     0
directed arcs:                       11
average markov blanket size:           3.20
average neighbourhood size:            2.20
average branching factor:              1.10

generation algorithm:                  Empty
```

Note that as graphs are imported with `as.bn()` they are checked to be acyclic, unless the user specifies `check.cycles = FALSE`.

```> library(igraph)
>
> cyclic.graph = make_graph(~ A-+B-+C-+D-+A)
> cyclic.graph
```
```IGRAPH 547f41b DN-- 4 4 --
+ attr: name (v/c)
+ edges from 547f41b (vertex names):
[1] A->B B->C C->D D->A
```
```> as.bn(cyclic.graph)
```
```## Error: the igraph object contains directed cycles.
```
```> as.bn(cyclic.graph, check.cycles = FALSE)
```
```
Random/Generated Bayesian network

model:
[A|D][B|A][C|B][D|C]
nodes:                                 4
arcs:                                  4
undirected arcs:                     0
directed arcs:                       4
average markov blanket size:           2.00
average neighbourhood size:            2.00
average branching factor:              1.00

generation algorithm:                  Empty
```
Last updated on `Wed Nov 9 16:41:15 2022` with bnlearn `4.9-20221107` and `R version 4.2.2 (2022-10-31)`.