do_dfs: Use the depth-first search (dfs) algorithm

Description Usage Arguments Value Examples

View source: R/do_dfs.R

Description

With a chosen or random node serving as the starting point, perform a depth-first search of the whole graph and return the node ID values visited. The dfs algorithm differs from breadth-first search (bfs) in that dfs will follow tree branches as far as possible until terminating at leaf node (bfs traverses branches one level at a time).

Usage

1
do_dfs(graph, node = NULL, direction = "all")

Arguments

graph

a graph object of class dgr_graph that is created using create_graph.

node

an optional node ID value to specify a single starting point for the dfs. If not provided, a random node from the graph will be chosen.

direction

using all (the default), the bfs will ignore edge direction while traversing through the graph. With out and in, traversals between adjacent nodes will respect the edge direction.

Value

a vector containing node ID values for nodes visited during the depth-first search. The order of the node IDs corresponds to the order visited.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Create a graph containing
# two balanced trees
graph <-
  create_graph() %>%
  add_balanced_tree(
    k = 2, h = 2) %>%
  add_balanced_tree(
  k = 3, h = 2)

# Perform a depth-first
# search of the graph,
# beginning at the root
# node `1` (the default
# `direction = "all"`
# doesn't take edge
# direction into account)
graph %>%
  do_dfs(node = 1)

# If not specifying a
# starting node, the function
# will begin the search
# from a random node
graph %>%
  do_dfs()

# It's also possible to
# perform dfs while taking
# into account edge direction;
# using `direction = "in"`
# causes the dfs routine to
# visit nodes along inward edges
graph %>%
  do_dfs(
    node = 1,
    direction = "in")

# Using `direction = "out"`
# results in the dfs moving
# along solely outward edges
graph %>%
  do_dfs(
    node = 1,
    direction = "out")

Example output

Warning messages:
1: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
2: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
3: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
 [1]  1  2  4  5  3  6  7  8  9 12 13 14 10 15 16 17 11 18 19 20
 [1]  8  9 12 13 14 10 15 16 17 11 18 19 20  1  2  4  5  3  6  7
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
 [1]  1  2  4  5  3  6  7  8  9 12 13 14 10 15 16 17 11 18 19 20

DiagrammeR documentation built on March 18, 2018, 1:25 p.m.