Graph.Reducers.Bfs (libgraph v0.16.0)
This reducer traverses the graph using Breadth-First Search.
Link to this section Summary
Functions
Performs a breadth-first traversal of the graph, applying the provided mapping function to each new vertex encountered.
Performs a breadth-first traversal of the graph, applying the provided reducer function to each new vertex encountered and the accumulator.
Link to this section Functions
map(g, fun)
Performs a breadth-first traversal of the graph, applying the provided mapping function to each new vertex encountered.
NOTE: The algorithm will follow lower-weighted edges first.
Returns a list of values returned from the mapper in the order they were encountered.
example
Example
iex> g = Graph.new |> Graph.add_vertices([1, 2, 3, 4])
...> g = Graph.add_edges(g, [{1, 3}, {1, 4}, {3, 2}, {2, 3}])
...> Elixir.Graph.Reducers.Bfs.map(g, fn v -> v end)
[1, 3, 4, 2]
reduce(g, acc, fun)
Performs a breadth-first traversal of the graph, applying the provided reducer function to each new vertex encountered and the accumulator.
NOTE: The algorithm will follow lower-weighted edges first.
The result will be the state of the accumulator after the last reduction.
example
Example
iex> g = Graph.new |> Graph.add_vertices([1, 2, 3, 4])
...> g = Graph.add_edges(g, [{1, 3}, {1, 4}, {3, 2}, {2, 3}])
...> Elixir.Graph.Reducers.Bfs.reduce(g, [], fn v, acc -> {:next, [v|acc]} end)
[2, 4, 3, 1]
iex> g = Graph.new |> Graph.add_vertices([1, 2, 3, 4])
...> g = Graph.add_edges(g, [{1, 3}, {1, 4}, {3, 2}, {2, 3}, {4, 5}])
...> Elixir.Graph.Reducers.Bfs.reduce(g, [], fn 5, acc -> {:skip, acc}; v, acc -> {:next, [v|acc]} end)
[2, 4, 3, 1]
iex> g = Graph.new |> Graph.add_vertices([1, 2, 3, 4])
...> g = Graph.add_edges(g, [{1, 3}, {1, 4}, {3, 2}, {2, 3}, {4, 5}])
...> Elixir.Graph.Reducers.Bfs.reduce(g, [], fn 4, acc -> {:halt, acc}; v, acc -> {:next, [v|acc]} end)
[3, 1]