1% Generated by roxygen2: do not edit by hand 2% Please edit documentation in R/do_bfs.R 3\name{do_bfs} 4\alias{do_bfs} 5\title{Use the breadth-first search (bfs) algorithm} 6\usage{ 7do_bfs(graph, node = NULL, direction = "all") 8} 9\arguments{ 10\item{graph}{A graph object of class \code{dgr_graph}.} 11 12\item{node}{An optional node ID value to specify a single starting point for 13the bfs. If not provided, a random node from the graph will be chosen.} 14 15\item{direction}{Using \code{all} (the default), the bfs will ignore edge 16direction while traversing through the graph. With \code{out} and \verb{in}, 17traversals between adjacent nodes will respect the edge direction.} 18} 19\value{ 20A vector containing node ID values for nodes visited during the 21breadth-first search. The order of the node IDs corresponds to the order 22visited. 23} 24\description{ 25With a chosen or random node serving as the starting point, perform a 26breadth-first search of the whole graph and return the node ID values 27visited. The bfs algorithm differs from depth-first search (dfs) in that bfs 28will follow tree branches branches one level at a time until terminating at 29leaf node (dfs traverses branches as far as possible). 30} 31\examples{ 32# Create a graph containing 33# two balanced trees 34graph <- 35 create_graph() \%>\% 36 add_balanced_tree( 37 k = 2, h = 2) \%>\% 38 add_balanced_tree( 39 k = 3, h = 2) 40 41# Perform a breadth-first 42# search of the graph, 43# beginning at the root node 44# `1` (the default 45# `direction = "all"` doesn't 46# take edge direction into 47# account) 48graph \%>\% 49 do_bfs(node = 1) 50 51# If not specifying a 52# starting node, the function 53# will begin the search from 54# a random node 55graph \%>\% 56 do_bfs() 57 58# It's also possible to 59# perform bfs while taking 60# into account edge direction; 61# using `direction = "in"` 62# causes the bfs routine to 63# visit nodes along inward edges 64graph \%>\% 65 do_bfs( 66 node = 1, 67 direction = "in") 68 69# Using `direction = "out"` 70# results in the bfs moving 71# along solely outward edges 72graph \%>\% 73 do_bfs( 74 node = 1, 75 direction = "out") 76 77} 78