Skip to content

Commit

Permalink
Add eulerian path algos
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Nov 17, 2023
1 parent c0ab7d7 commit e79d044
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export(edge_is_loop)
export(edge_is_multiple)
export(edge_is_mutual)
export(edge_is_to)
export(edge_rank_eulerian)
export(ends_with)
export(everything)
export(filter)
Expand All @@ -200,6 +201,7 @@ export(graph_is_complete)
export(graph_is_connected)
export(graph_is_dag)
export(graph_is_directed)
export(graph_is_eulerian)
export(graph_is_forest)
export(graph_is_isomorphic_to)
export(graph_is_simple)
Expand Down Expand Up @@ -504,6 +506,8 @@ importFrom(igraph,edge_connectivity)
importFrom(igraph,ego)
importFrom(igraph,ego_size)
importFrom(igraph,eigen_centrality)
importFrom(igraph,eulerian_cycle)
importFrom(igraph,eulerian_path)
importFrom(igraph,girth)
importFrom(igraph,gorder)
importFrom(igraph,graph_attr)
Expand All @@ -513,6 +517,8 @@ importFrom(igraph,graph_from_data_frame)
importFrom(igraph,graph_from_edgelist)
importFrom(igraph,graph_from_incidence_matrix)
importFrom(igraph,gsize)
importFrom(igraph,has_eulerian_cycle)
importFrom(igraph,has_eulerian_path)
importFrom(igraph,hub_score)
importFrom(igraph,induced_subgraph)
importFrom(igraph,is.directed)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
igraph
* `graph_mean_dist()` now supports edge weights through a new `weights` argument
* Added `to_largest_component()` morpher
* Added `graph_is_eulerian()` and `edge_rank_eulerian()` for eulerian path
calculations

# tidygraph 1.2.3

Expand Down
36 changes: 36 additions & 0 deletions R/edge_rank.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' Calculate edge ranking
#'
#' This set of functions tries to calculate a ranking of the edges in a graph so
#' that edges sharing certain topological traits are in proximity in the
#' resulting order.
#'
#' @param cyclic should the eulerian path start and end at the same node
#'
#' @return An integer vector giving the position of each edge in the ranking
#'
#' @rdname edge_rank
#' @name edge_rank
#'
#' @examples
#' graph <- create_notable('meredith') %>%
#' activate(edges) %>%
#' mutate(rank = edge_rank_eulerian())
#'
NULL

#' @describeIn edge_rank Calculcate ranking as the visit order of a eulerian
#' path or cycle. If no such path or cycle exist it will return a vector of
#' `NA`s
#' @importFrom igraph eulerian_path eulerian_cycle
#' @export
edge_rank_eulerian <- function(cyclic = FALSE) {
expect_edges()
alg <- if (cyclic) eulerian_cycle else eulerian_path
rlang::try_fetch({
compress_rank(match(focus_ind(.G(), 'edges'), alg(.G())$epath))
},
error = function(...) {
rep_len(NA_integer_, length(focus_ind(.G(), 'edges')))
}
)
}
13 changes: 12 additions & 1 deletion R/graph_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#' properties and will all return a logical scalar.
#'
#' @param graph The graph to compare structure to
#'
#' @param method The algorithm to use for comparison
#' @param cyclic should the eulerian path start and end at the same node
#'
#' @param ... Arguments passed on to the comparison methods. See
#' [igraph::is_isomorphic_to()] and [igraph::is_subgraph_isomorphic_to()]
Expand Down Expand Up @@ -86,3 +86,14 @@ graph_is_isomorphic_to <- function(graph, method = 'auto', ...) {
graph_is_subgraph_isomorphic_to <- function(graph, method = 'auto', ...) {
is_subgraph_isomorphic_to(.G(), graph, method, ...)
}
#' @describeIn graph_types Can all the edges in the graph be reaches by a single
#' path or cycle that only goes through each edge once
#' @importFrom igraph has_eulerian_cycle has_eulerian_path
#' @export
graph_is_eulerian <- function(cyclic = FALSE) {
if (cyclic) {
has_eulerian_cycle(.G())
} else {
has_eulerian_path(.G())
}
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ reference:
for topological features such as which nodes they link, etc.
contents:
- edge_is_mutual
- edge_rank_eulerian
- title: Graph measures
desc: >
The graph under investigation can also have properties of its own that can
Expand Down
33 changes: 33 additions & 0 deletions man/edge_rank.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/graph_types.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e79d044

Please sign in to comment.