Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mbojan/rgraph6

Encoding graphs in graph6, sparse6, and digraph6 formats
https://github.com/mbojan/rgraph6

network-analysis r r-package

Last synced: 12 days ago
JSON representation

Encoding graphs in graph6, sparse6, and digraph6 formats

Awesome Lists containing this project

README

        

---
output:
github_document:
html_preview: false
editor_options:
chunk_output_type: console
---

```{r setup, include = FALSE}
suppressPackageStartupMessages({
library(rgraph6)
library(dplyr)
library(ggraph)
library(igraph)
})

knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# `rgraph6`: Representing Graphs as graph6, digraph6 or sparse6 Strings

[![R-CMD-check](https://github.com/mbojan/rgraph6/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/mbojan/rgraph6/actions/workflows/R-CMD-check.yaml)
[![rstudio mirror downloads](https://cranlogs.r-pkg.org/badges/rgraph6?color=2ED968)](https://cranlogs.r-pkg.org/)
[![cran version](http://www.r-pkg.org/badges/version/rgraph6)](https://cran.r-project.org/package=rgraph6)
[![rgraph6 status badge](https://mbojan.r-universe.dev/badges/rgraph6)](https://mbojan.r-universe.dev)

Functions in this package allow for encoding network data as strings of printable ASCII characters and back using 'graph6', 'sparse6', and 'digraph6' formats. This is convenient in a number of contexts, especially when working with large number of graphs. Provided functions allow to directly encode and decode graph data in the form of adjacency matrices, edgelists, network objects and igraph objects to and from these three formats.

## What are 'graph6', 'sparse6' and 'digraph6' formats?

'graph6', 'sparse6' and 'digraph6' are formats for encoding graphs as strings of printable ASCII characters due to [Brendan McKay](https://en.wikipedia.org/wiki/Brendan_McKay). See [here](http://users.cecs.anu.edu.au/~bdm/data/formats.txt) for format specification. Formats 'graph6' and 'sparse6' are for undirected graphs. Format 'digraph6' is for directed graphs.

## Functions

Main functions for encoding network data are:

- `as_graph6()`
- `as_sparse6()`
- `as_digraph6()`

Main functions for decoding are:

- `adjacency_from_text()`
- `edgelist_from_text()`
- `igraph_from_text()`
- `network_from_text()`

Low-level functions are shown on the following graph:

```{r functions-low, echo=FALSE,fig.width=10,fig.height=5}
u <- c(
"adjacency", "edgelist", "network", "igraph", "digraph6",
"sparse6", "graph6"
)

d <- tidyr::crossing(
from = u,
to = u
) %>%
filter(from != to) %>%
mutate(
fun = paste0(to, "_from_", from)
) %>%
mutate(
fun = case_when(
to == "graph6" & (from %in% c("adjacency", "network", "igraph")) ~ "as_graph6",
to == "sparse6" & (from %in% c("edgelist", "network", "igraph")) ~ "as_sparse6",
to == "digraph6" & (from %in% c("adjacency", "network", "igraph")) ~ "as_digraph6",
TRUE ~ fun
),
ok = purrr::map_lgl(fun, exists, where = asNamespace("rgraph6"))
)

g <- d %>%
filter(ok) %>%
transmute(
from, to,
fun = paste0(fun, "()")
) %>%
tidygraph::as_tbl_graph()

x <- c(1.7619, 0.8201, -2.019, 0.8277, -0.0565, -0.0451, -0.9931)
y <- c(-9e-04, -0.4761, 0.0281, 0.4898, -0.4738, 0.4898, 0.0206)
E(g)$cross <- FALSE
E(g)$cross[c(13, 5, 11, 8)] <- TRUE
noncross_pos <- c(5, -5, 5, -5, 5, 5, -5, -5, 5, -5, 5, -5, -5, 5)
cross_pos <- c(-5, -5, 5, 5)
cross_push <- c(-27, -27, 27, 27)

ggraph(g, layout = "manual", x = x, y = y) +
geom_edge_parallel(
aes(label = fun, filter = !cross),
start_cap = circle(1),
end_cap = circle(1),
label_dodge = unit(noncross_pos, "mm"),
label_push = unit(0, "mm"),
angle_calc = "along",
arrow = arrow(length = unit(3, "mm"), type = "closed")
) +
geom_edge_parallel(
aes(label = fun, filter = cross),
start_cap = circle(1),
end_cap = circle(1),
label_dodge = unit(cross_pos, "mm"),
label_push = unit(cross_push, "mm"),
angle_calc = "along",
arrow = arrow(length = unit(3, "mm"), type = "closed")
) +
geom_node_label(aes(label = name)) +
coord_cartesian(clip = "off") +
scale_y_continuous(expand = expansion(add = .1)) +
theme_void()
```

## Examples

### Encode list of igraph objects

Generate a list of igraph objects:

```{r}
set.seed(666)
igraph_list <- replicate(5, igraph::sample_gnp(10, 0.1, directed = FALSE),
simplify = FALSE
)
```

Encode as 'graph6' symbols:

```{r}
as_graph6(igraph_list)
```

Encode as 'sparse6' symbols:

```{r}
as_sparse6(igraph_list)
```

### Decode a vector of different types of symbols

Using example data `g6`, `d6`, and `s6` provided with the package:

```{r example-mixed}
# Create a vector with a mixture of 'graph6', 'digraph6' and 'sparse6' symbols
x <- c(g6[1], s6[2], d6[3])
x

# Parse to igraph objects (package igraph required)
igraph_from_text(x)

# Parse to network objects (package network required)
network_from_text(x)
```

### Tidy graph databases

The formats shine if we need to store large number of graphs in a data frame. Let's generate a list of random graphs as igraph objects and store them in a data frame column of graph6 symbols:

```{r}
library("dplyr")

# Generate list of igraph objects
set.seed(666)

d <- tibble::tibble(
g6 = replicate(
10,
igraph::sample_gnp(sample(3:12, 1, replace = TRUE), p = .5, directed = FALSE),
simplify = FALSE
) %>%
as_graph6()
)
d
```

Nice and compact. We can go further by doing some computations and saving the results together with the graph data, and even save it to a simple CSV file!

```{r}
d %>%
dplyr::mutate(
igraphs = igraph_from_text(g6),
vc = purrr::map_dbl(igraphs, igraph::vcount),
ec = purrr::map_dbl(igraphs, igraph::ecount),
density = purrr::map_dbl(igraphs, igraph::edge_density)
) %>%
dplyr::select(-igraphs) %>%
write.csv(row.names = FALSE)
```

## Installation

Install development version from GitHub with:

```{r install-gh, eval=FALSE}
# install.packages("remotes")
remotes::install_github("mbojan/rgraph6", build_vignettes = TRUE)
```

Nightly Windows and MacOS binaries are available on [R Universe](https://mbojan.r-universe.dev):

```{r install-runiv, eval=FALSE}
install.packages("rgraph6", repos = "https://mbojan.r-universe.dev")
```

## Authors, contributors and citation

```{r persons, echo=FALSE}
pd <- read.dcf("DESCRIPTION")
p <- eval(parse(text = pd[, "Authors@R"]))
is_maintainer <- purrr::map_lgl(p, ~ "cre" %in% .x$role)
is_author <- purrr::map_lgl(p, ~ "aut" %in% .x$role)
```

**Author and maintainer**: `r format(p[is_maintainer], include = c("given", "family", "email", "comment"))`.

**Co-authors**: `r paste(format(p[is_author & !is_maintainer], include = c("given", "family", "comment")), collapse=", ")`

To cite this package please use the following two entries:

Bojanowski M, Schoch D (2024). _rgraph6: Representing Graphs as 'graph6', 'digraph6' or 'sparse6' Strings_. R package version 2.0-4, .

McKay, B. D., & Piperno, A. (2014). Practical graph isomorphism, II. _Journal of Symbolic Computation_, 60, 94-112.