57 |
58 | `graph_coloring_*` functions directly take adjacency lists and returns
59 | an integer vector of assigned labels. For example, this can be used with
60 | `sf::st_intersects()` to color a feature collection for visualization.
61 |
62 | ``` r
63 | library(graphcoloring)
64 | library(USAboundaries)
65 | library(sf)
66 | library(ggplot2)
67 |
68 | set.seed(48)
69 |
70 | us_states() %>%
71 | filter(!(name %in% c("Alaska", "District of Columbia", "Hawaii", "Puerto Rico"))) %>%
72 | mutate(
73 | color = st_intersects(.) %>%
74 | graph_coloring_dsatur() %>%
75 | as.factor()
76 | ) %>%
77 | ggplot() +
78 | geom_sf(aes(fill = color)) +
79 | theme_bw()
80 | ```
81 |
82 |
83 |
--------------------------------------------------------------------------------
/README.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | output: github_document
3 | ---
4 |
5 |
6 |
7 | ```{r setup, include = FALSE}
8 | knitr::opts_chunk$set(
9 | collapse = TRUE,
10 | comment = "#>",
11 | fig.path = "man/figures/README-",
12 | out.width = "100%"
13 | )
14 | ```
15 | # graphcoloring
16 |
17 | [](https://travis-ci.org/saurfang/graphcoloring)
18 | [](https://cran.r-project.org/package=graphcoloring)
19 | [](https://www.tidyverse.org/lifecycle/#experimental)
20 | [](https://codecov.io/github/saurfang/graphcoloring?branch=master)
21 |
22 | `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
23 | are included via the embedded 'GraphColoring' C++ library, YEAR: 2018 113 | COPYRIGHT HOLDER: Forest Fang; Brian Crites 114 |115 | 116 |
NEWS.md
111 | See magrittr::%>% for details.
lhs %>% rhs124 | 125 | 126 |
122 | All functions123 | 124 | |
125 | |
|---|---|
|
129 |
|
131 | Color nodes using Graph Coloring Algorithm |
132 |
|
135 |
|
137 | Graph Coloring over Adjacency List |
138 |
R/graphcoloring-package.R
116 | graphcoloring-package.RdA collection of graph coloring algorithms for coloring vertices of 122 | a graph such that no two adjacent vertices share the same color. The algorithms 123 | are included via the embedded 'GraphColoring' C++ library, <https://github.com/brrcrites/GraphColoring>.
124 | 125 |Useful links:
Report bugs at https://github.com/saurfang/graphcoloring/issues
These functions are tidygraph wrapper around the various graph coloring algorithms.
123 | They automatically use the graph that is being computed on, and
124 | otherwise passes on its arguments to the relevant coloring function. The return value is always
125 | a integer vector of assigned color index so that neighboring nodes never share the same color.
color_dsatur() 130 | 131 | color_msc() 132 | 133 | color_lmxrlf() 134 | 135 | color_hybrid_lmxrlf_tabucol() 136 | 137 | color_hybrid_dsatur_tabucol() 138 | 139 | color_tabucol(k)140 | 141 |
| k | 146 |number of colors to use for graph coloring |
147 |
|---|
color_dsatur: Color graph using graph_coloring_dsatur()
color_msc: Color graph using graph_coloring_msc()
color_lmxrlf: Color graph using graph_coloring_lmxrlf()
WARNING Algorithm is unstable and requires additional testing
color_hybrid_lmxrlf_tabucol: Color graph using graph_coloring_hybrid_lmxrlf_tabucol()
WARNING Algorithm is unstable and requires additional testing
color_hybrid_dsatur_tabucol: Color graph using graph_coloring_hybrid_dsatur_tabucol()
color_tabucol: Color graph using graph_coloring_tabucol()
183 |library(tidygraph)#> 166 | #>#> 167 | #> 168 | #>#> 169 | #> 170 | #>171 | if (requireNamespace("ggraph", quietly = TRUE)) { 172 | library(ggraph) 173 | set.seed(42) 174 | 175 | play_islands(5, 10, 0.8, 3) %>% 176 | mutate(color = as.factor(color_dsatur())) %>% 177 | ggraph(layout = 'kk') + 178 | geom_edge_link(aes(alpha = ..index..), show.legend = FALSE) + 179 | geom_node_point(aes(color = color), size = 7) + 180 | theme_graph("") 181 | }#>#> 182 | #>
R/RcppExports.R, R/color.R
118 | graph_coloring.RdThese functions perform graph coloring using various algorithms over an adjacency list.
124 |In graph theory, graph coloring is a special case of graph labeling; 125 | it is an assignment of labels traditionally called "colors" to elements of a graph subject 126 | to certain constraints. In its simplest form, it is a way of coloring the vertices of a graph 127 | such that no two adjacent vertices share the same color; this is called a vertex coloring.
128 | 129 |graph_coloring_dsatur(adj_list) 132 | 133 | graph_coloring_msc(adj_list) 134 | 135 | graph_coloring_lmxrlf(adj_list) 136 | 137 | graph_coloring_hybrid_dsatur_tabucol(adj_list) 138 | 139 | graph_coloring_hybrid_lmxrlf_tabucol(adj_list) 140 | 141 | graph_coloring_tabucol(adj_list, k)142 | 143 |
| adj_list | 148 |an adjacency list in the format of |
150 |
|---|---|
| k | 153 |number of colors to use for graph coloring |
154 |
graph_coloring_hybrid_dsatur_tabucol() and graph_coloring_hybrid_lmxrlf_tabucol() use a hybrid approach
160 | to run DSATUR and lmXRLF first to determine an upper bound for the graph chromatic number. It then searches
161 | better solutions by running lowered chromatic number through TabuCol to check if the graph can be colored
162 | with less colors.
graph_coloring_dsatur: Color graph using DSATUR algorithm
168 | (Brélaz 1979)
graph_coloring_msc: Color graph using Maximum Cardinality Search(MCS) algorithm
170 | (Palsberg 2007)
graph_coloring_lmxrlf: Color graph using Least-constraining Most-constrained eXtended RLF(lmXRLF) algorithm
172 | (Kirovski et al. 1998)
graph_coloring_hybrid_dsatur_tabucol: Color graph using a hybrid of DASTUR and TabuCol algorithm
174 | (Kirovski et al. 1998; Brélaz 1979; Hertz and de
175 | Werra 1987)
graph_coloring_hybrid_lmxrlf_tabucol: Color graph using a hybrid of lmXRLF and TabuCol algorithm
177 | (Kirovski et al. 1998; Hertz and de
178 | Werra 1987)
graph_coloring_tabucol: Color graph using TabuCol algorithm
180 | (Hertz and de
181 | Werra 1987)
https://en.wikipedia.org/wiki/Graph_coloring
187 |https://github.com/brrcrites/GraphColoring
188 |Brélaz D (1979). 189 | “New Methods to Color the Vertices of a Graph.” 190 | Commun. ACM, 22(4), 251--256. 191 | ISSN 0001-0782, doi: 10.1145/359094.359101 192 | , http://doi.acm.org/10.1145/359094.359101.
193 |Palsberg J (2007). 194 | “Register Allocation via Coloring of Chordal Graphs.” 195 | In Proceedings of the Thirteenth Australasian Symposium on Theory of Computing - Volume 65, series CATS '07, 3--3. 196 | ISBN 1-920-68246-5, http://dl.acm.org/citation.cfm?id=1273694.1273695.
197 |Kirovski D, Potkonjak M, Potkonjak M (1998). 198 | “Efficient Coloring of a Large Spectrum of Graphs.” 199 | In Proceedings of the 35th Annual Design Automation Conference, series DAC '98, 427--432. 200 | ISBN 0-89791-964-5, doi: 10.1145/277044.277165 201 | , http://doi.acm.org/10.1145/277044.277165.
202 |Hertz A, de 203 | Werra D (1987). 204 | “Using Tabu Search Techniques for Graph Coloring.” 205 | Computing, 39(4), 345--351. 206 | ISSN 0010-485X, doi: 10.1007/BF02239976 207 | , http://dx.doi.org/10.1007/BF02239976.
208 | 209 |230 |library(tidygraph) 216 | 217 | if (requireNamespace("sf", quietly = TRUE) && requireNamespace("USAboundaries", quietly = TRUE)) { 218 | library(sf) 219 | library(USAboundaries) 220 | 221 | us_states() %>% 222 | filter(!(name %in% c("Alaska", "District of Columbia", "Hawaii", "Puerto Rico"))) %>% 223 | transmute( 224 | color = st_intersects(.) %>% 225 | graph_coloring_dsatur() %>% 226 | as.factor() 227 | ) %>% 228 | plot() 229 | }#>#>#>
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, https://github.com/brrcrites/GraphColoring.
You can install the released version of graphcoloring from CRAN with:
97 | 98 |Development version can be installed with
99 | 100 |color_* functions operate under tidygraph family and can be used to color nodes within mutate context similar to group_* functions in tidygraph.
library(graphcoloring)
106 | library(tidygraph)
107 | library(ggraph)
108 |
109 | set.seed(42)
110 |
111 | play_islands(5, 10, 0.8, 3) %>%
112 | mutate(color = as.factor(color_dsatur())) %>%
113 | ggraph(., layout = 'kk') +
114 | geom_edge_link(aes(alpha = ..index..), show.legend = FALSE) +
115 | geom_node_point(aes(color = color), size = 7) +
116 | 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.
library(graphcoloring)
120 | library(USAboundaries)
121 | library(sf)
122 | library(ggplot2)
123 |
124 | set.seed(48)
125 |
126 | us_states() %>%
127 | filter(!(name %in% c("Alaska", "District of Columbia", "Hawaii", "Puerto Rico"))) %>%
128 | mutate(
129 | color = st_intersects(.) %>%
130 | graph_coloring_dsatur() %>%
131 | as.factor()
132 | ) %>%
133 | ggplot() +
134 | geom_sf(aes(fill = color)) +
135 | theme_bw()
vignettes/graph-coloring.Rmd
88 | graph-coloring.Rmdgraphcoloring 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, https://github.com/brrcrites/GraphColoring. The package provide two sets of functions, color_* and graph_coloring_*, which operate on a tidygraph and adjavency lists respectively. Both sets of functions covers all algorithms found in the C++ GraphColoring library.
Here is the list of algorithms found in graphcoloring package:
tidygraph
111 | tidygraph is a powerful abstraction for graph datasets. It envisions a graph as two tidy tables, nodes and edges, and provides ways to activate either set and apply dplyr verbs for manipulation.
color_* functions operate under tidygraph family and can be used to color nodes within mutate context similar to group_* functions in tidygraph. They automatically use the graph that is being computed on, and otherwise passes on its arguments to the relevant coloring function. The return value is always a integer vector of assigned color index so that neighboring nodes never share the same color.
library(graphcoloring)
115 | library(tidygraph)
116 | library(ggraph)
117 |
118 | set.seed(42)
119 |
120 | play_islands(5, 10, 0.8, 3) %>%
121 | mutate(color = as.factor(color_dsatur())) %>%
122 | ggraph(., layout = 'kk') +
123 | geom_edge_link(aes(alpha = ..index..), show.legend = FALSE) +
124 | geom_node_point(aes(color = color), size = 7) +
125 | theme_graph()
graph_coloring_* functions directly take adjacency lists and returns an integer vector of assigned labels.
Here is a 3-coloring of the famous Petersen Graph:
133 |library(graphcoloring)
134 | library(igraph)
135 | library(dplyr)
136 |
137 | # create graph
138 | petersen_graph <- graph.famous("Petersen")
139 | # get adjacency list
140 | petersen_edges <- as_adj_list(petersen_graph)
141 | # color the graph with 3 colors
142 | set.seed(10737312)
143 | petersen_colors <- graph_coloring_tabucol(petersen_edges, 3)
144 |
145 | # arrange vertices for layout
146 | petersen_positions <- data_frame(
147 | theta = (0:4) * 2 * pi / 5 + pi / 2,
148 | r = 2
149 | ) %>%
150 | bind_rows(
151 | mutate(., r = r / 2)
152 | ) %>%
153 | transmute(
154 | x = r * cos(theta),
155 | y = r * sin(theta)
156 | )
157 |
158 | petersen_graph %>%
159 | as_tbl_graph() %>%
160 | mutate(color = as.factor(petersen_colors)) %>%
161 | ggraph(., layout = "manual", node.positions = petersen_positions) +
162 | geom_edge_link() +
163 | geom_node_point(aes(color = color), size = 7, show.legend = FALSE) +
164 | theme_graph()
One common use case for graph coloring is to visualize geographical dataset to color contiguous groupings. For example, this can be used with sf::st_intersects() to color a feature collection for visualization.
Here we look at Bureau of Economic Analysis regions which group US states into 8 regions:
168 |library(graphcoloring)
169 | library(USAboundaries)
170 | library(sf)
171 | library(ggplot2)
172 | library(rvest)
173 |
174 | # retrieve Bureau of Economic Analysis regions
175 | bea_regions <- read_html("https://apps.bea.gov/regional/docs/regions.cfm") %>%
176 | html_node(".table") %>%
177 | html_table()
178 |
179 | # 48 states
180 | states_sf <- us_states() %>%
181 | filter(!(name %in% c("Alaska", "District of Columbia", "Hawaii", "Puerto Rico"))) %>%
182 | left_join(bea_regions, c("state_name" = "State or Region name"))
183 |
184 | # color regions
185 | set.seed(48)
186 |
187 | region_colors <- states_sf %>%
188 | group_by(`Region code`) %>%
189 | summarise() %>% {
190 | colors <- st_intersects(.) %>%
191 | graph_coloring_dsatur() %>%
192 | as.factor()
193 |
194 | data_frame(
195 | `Region code` = .$`Region code`,
196 | color = colors
197 | )
198 | }
199 |
200 | states_sf %>%
201 | left_join(region_colors, "Region code") %>%
202 | ggplot() +
203 | geom_sf(aes(fill = color), show.legend = FALSE) +
204 | theme_bw()
It might be better to choose an 8-color palette in this case but graph coloring can be particularly useful when the number of colors get exceedingly big.
207 |Graph coloring is commonly used in Scheduling and Register Allocation. It can also be used to solve Sudoku puzzles!
212 |A Sudoku puzzle plays on a 9x9 grid where some entries are pre-filled with numbers from 1 to 9. The goal is the fill the entire grid with 1 to 9 such that:
213 |![]()
A Sudoku puzzle can be converted into a graph by modeling the 9x9 cells into 81 vertices where a pair of vertices are connected if and only if they are on the same row, column, or 3x3 block. Each valid Sudoku solution is therefore a 9-coloring of the Sudoku graph.
220 |generate_sudoku <- function(n, seed) {
221 | set.seed(seed)
222 |
223 | sudoku_graph(n) %>%
224 | mutate(color = as.factor(color_tabucol(4))) %>%
225 | ggraph(., layout = "grid") +
226 | geom_edge_link() +
227 | geom_node_point(aes(color = color), size = 7, show.legend = TRUE) +
228 | theme_graph() +
229 | theme(legend.position = "bottom")
230 | }
231 |
232 | generate_sudoku(2, 432)
233 | generate_sudoku(2, 45)
