@@ -135,7 +135,7 @@ defmodule Graph do
135135 NOTE: Currently this function assumes graphs are directed graphs, but in the future
136136 it will support undirected graphs as well.
137137
138- NOTE 2: To avoid to overwrite vertices with the same label, output is
138+ NOTE 2: To avoid to overwrite vertices with the same label, output is
139139 generated using the internal numeric ID as vertex label.
140140 Original label is expressed as `id[label="<label>"]`.
141141
@@ -1494,6 +1494,40 @@ defmodule Graph do
14941494 def topsort ( % __MODULE__ { type: :undirected } ) , do: false
14951495 def topsort ( % __MODULE__ { } = g ) , do: Graph.Directed . topsort ( g )
14961496
1497+ @ doc """
1498+ Returns a batch topological ordering of the vertices of graph `g`, if such an ordering exists, otherwise it
1499+ returns false. For each vertex in the returned list, no out-neighbors occur earlier in the list. This differs
1500+ from `topsort/1` in that this function returns a list of lists where each sublist can be concurrently evaluated
1501+ without worrying about elements in the sublist depending on eachother.
1502+
1503+ Multiple edges between two vertices are considered a single edge for purposes of this sort.
1504+
1505+ ## Example
1506+
1507+ iex> g = Graph.new |> Graph.add_vertices([:a, :b, :c])
1508+ ...> g = Graph.add_edges(g, [{:a, :b}, {:a, :c}])
1509+ ...> Graph.batch_topsort(g)
1510+ [[:a], [:b, :c]]
1511+
1512+ iex> g = Graph.new |> Graph.add_vertices([:a, :b, :c, :d])
1513+ ...> g = Graph.add_edges(g, [{:a, :b}, {:a, :c}, {:b, :c}, {:c, :d}])
1514+ ...> Graph.batch_topsort(g)
1515+ [[:a], [:b], [:c], [:d]]
1516+
1517+ iex> g = Graph.new |> Graph.add_vertices([:a, :b, :c, :d, :x, :y, :z])
1518+ ...> g = Graph.add_edges(g, [{:a, :b}, {:a, :c}, {:c, :d}, {:x, :y}, {:x, :z}])
1519+ ...> Graph.batch_topsort(g)
1520+ [[:a, :x], [:b, :c, :y, :z], [:d]]
1521+
1522+ iex> g = Graph.new |> Graph.add_vertices([:a, :b, :c, :d, :x, :y, :z])
1523+ ...> g = Graph.add_edges(g, [{:a, :b}, {:a, :c}, {:b, :c}, {:c, :d}, {:c, :a}])
1524+ ...> Graph.batch_topsort(g)
1525+ false
1526+ """
1527+ @ spec batch_topsort ( t ) :: [ vertex ] | false
1528+ def batch_topsort ( % __MODULE__ { type: :undirected } ) , do: false
1529+ def batch_topsort ( % __MODULE__ { } = g ) , do: Graph.Directed . batch_topsort ( g )
1530+
14971531 @ doc """
14981532 Returns a list of connected components, where each component is a list of vertices.
14991533
0 commit comments