map_bfs_back function

Apply a function to nodes in the reverse order of a breath first search

Apply a function to nodes in the reverse order of a breath first search

These functions allow you to map over the nodes in a graph, by first performing a breath first search on the graph and then mapping over each node in the reverse order they are visited. The mapping function will have access to the result and search statistics for all the nodes following itself in the search. To map over the nodes in the original direction use map_bfs().

map_bfs_back(root, mode = "out", unreachable = FALSE, .f, ...) map_bfs_back_lgl(root, mode = "out", unreachable = FALSE, .f, ...) map_bfs_back_chr(root, mode = "out", unreachable = FALSE, .f, ...) map_bfs_back_int(root, mode = "out", unreachable = FALSE, .f, ...) map_bfs_back_dbl(root, mode = "out", unreachable = FALSE, .f, ...)

Arguments

  • root: The node to start the search from
  • mode: How should edges be followed? 'out' only follows outbound edges, 'in' only follows inbound edges, and 'all' follows all edges. This parameter is ignored for undirected graphs.
  • unreachable: Should the search jump to an unvisited node if the search is completed without visiting all nodes.
  • .f: A function to map over all nodes. See Details
  • ...: Additional parameters to pass to .f

Returns

map_bfs_back() returns a list of the same length as the number of nodes in the graph, in the order matching the node order in the graph (that is, not in the order they are called). map_bfs_back_*() tries to coerce its result into a vector of the classes logical (map_bfs_back_lgl), character (map_bfs_back_chr), integer (map_bfs_back_int), or double

(map_bfs_back_dbl). These functions will throw an error if they are unsuccesful, so they are type safe.

Details

The function provided to .f will be called with the following arguments in addition to those supplied through ...:

  • graph: The full tbl_graph object
  • node: The index of the node currently mapped over
  • rank: The rank of the node in the search
  • parent: The index of the node that led to the current node
  • before: The index of the node that was visited before the current node
  • after: The index of the node that was visited after the current node.
  • dist: The distance of the current node from the root
  • path: A table containing node, rank, parent, before, after, dist, and result columns giving the values for each node reached from the current node. The result column will contain the result of the mapping of each node in a list.

Instead of spelling out all of these in the function it is possible to simply name the ones needed and use ... to catch the rest.

Examples

# Collect values from children create_tree(40, children = 3, directed = TRUE) %>% mutate(value = round(runif(40)*100)) %>% mutate(child_acc = map_bfs_back_dbl(node_is_root(), .f = function(node, path, ...) { if (nrow(path) == 0) .N()$value[node] else { sum(unlist(path$result[path$parent == node])) } }))

See Also

Other node map functions: map_bfs(), map_dfs(), map_dfs_back()