https://github.com/saurfang/graphcoloring
Graph Coloring Algorithms for tidygraph
https://github.com/saurfang/graphcoloring
graph-algorithms graph-coloring map-coloring tidygraph
Last synced: 2 months ago
JSON representation
Graph Coloring Algorithms for tidygraph
- Host: GitHub
- URL: https://github.com/saurfang/graphcoloring
- Owner: saurfang
- License: other
- Created: 2018-09-20T08:12:38.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-18T20:06:02.000Z (over 5 years ago)
- Last Synced: 2025-03-27T07:35:54.633Z (3 months ago)
- Topics: graph-algorithms, graph-coloring, map-coloring, tidygraph
- Language: R
- Homepage: https://saurfang.github.io/graphcoloring/
- Size: 1.83 MB
- Stars: 18
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# graphcoloring[](https://travis-ci.org/saurfang/graphcoloring)
[](https://cran.r-project.org/package=graphcoloring)
[](https://www.tidyverse.org/lifecycle/#experimental)
[](https://codecov.io/github/saurfang/graphcoloring?branch=master)`graphcoloring` is a collection of graph coloring algorithms for coloring vertices of a graph such that no two adjacent vertices share the same color. The algorithms
are included via the embedded 'GraphColoring' C++ library, .## Installation
You can install the released version of graphcoloring from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("graphcoloring")
```Development version can be installed with
```r
devtools::install_github("saurfang/graphcoloring")
```## Example
`color_*` functions operate under `tidygraph` family and can be used to color nodes within `mutate` context similar to `group_*` functions in `tidygraph`.
```{r example, message=FALSE}
library(graphcoloring)
library(tidygraph)
library(ggraph)set.seed(42)
play_islands(5, 10, 0.8, 3) %>%
mutate(color = as.factor(color_dsatur())) %>%
ggraph(., layout = 'kk') +
geom_edge_link(aes(alpha = ..index..), show.legend = FALSE) +
geom_node_point(aes(color = color), size = 7) +
theme_graph()
````graph_coloring_*` functions directly take adjacency lists and returns an integer vector of assigned labels.
For example, this can be used with `sf::st_intersects()` to color a feature collection for visualization.```{r sf-example, message=FALSE}
library(graphcoloring)
library(USAboundaries)
library(sf)
library(ggplot2)set.seed(48)
us_states() %>%
filter(!(name %in% c("Alaska", "District of Columbia", "Hawaii", "Puerto Rico"))) %>%
mutate(
color = st_intersects(.) %>%
graph_coloring_dsatur() %>%
as.factor()
) %>%
ggplot() +
geom_sf(aes(fill = color)) +
theme_bw()
```