digraph_utils(3erl)
πΈ NAME πΈ
digraph_utils(3erl) is the Erlang Module Definition page for algorithms on directed graphs. Bestie, we're in graph-algorithm territory and it's giving iconic depth-first traversal energy. π
β¨
β¨ DESCRIPTION β¨
Okay so this module provides algorithms based on depth-first traversal of directed graphs. For basic functions on directed graphs, see the digraph(3erl) module. π¦
- Directed graph (or just digraph): a pair
(V, E)of a finite setVof vertices and a finite setEof directed edges (or just edges). The set of edgesEis a subset ofV x V(the Cartesian product ofVwith itself). π - Labeled digraph: digraphs can be annotated with more information. Such information can be attached to the vertices and to the edges of the digraph. An annotated digraph is called a labeled digraph, and the information attached to a vertex or an edge is called a label.
- An edge
e = (v, w)is said to emanate from vertexvand to be incident on vertexw. - If an edge is emanating from
vand incident onw, thenwis said to be an out-neighbor ofv, andvis said to be an in-neighbor ofw. - A path
Pfromv[1]tov[k]in a digraph(V, E)is a non-empty sequencev[1], v[2], ..., v[k]of vertices inVsuch that there is an edge(v[i],v[i+1])inEfor1 <= i < k. - The length of path
Pisk-1. - Path
Pis a cycle if the length ofPis not zero andv[1] = v[k]. - A loop is a cycle of length one.
- An acyclic digraph is a digraph without cycles.
- A depth-first traversal of a directed digraph can be viewed as a process that visits all vertices of the digraph. Initially, all vertices are marked as unvisited. The traversal starts with an arbitrarily chosen vertex, which is marked as visited, and follows an edge to an unmarked vertex, marking that vertex. The search then proceeds from that vertex in the same fashion, until there is no edge leading to an unvisited vertex. At that point the process backtracks, and the traversal continues as long as there are unexamined edges. If unvisited vertices remain when all edges from the first vertex have been examined, some so far unvisited vertex is chosen, and the process is repeated. π
- A partial ordering of a set
Sis a transitive, antisymmetric, and reflexive relation between the objects ofS. - The problem of topological sorting is to find a total ordering of
Sthat is a superset of the partial ordering. A digraphG = (V, E)is equivalent to a relationEonV(we neglect that the version of directed graphs provided by the digraph module allows multiple edges between vertices). If the digraph has no cycles of length two or more, the reflexive and transitive closure ofEis a partial ordering. - A subgraph
G'ofGis a digraph whose vertices and edges form subsets of the vertices and edges ofG. G'is maximal with respect to a propertyPif all other subgraphs that include the vertices ofG'do not have propertyP.- A strongly connected component is a maximal subgraph such that there is a path between each pair of vertices.
- A connected component is a maximal subgraph such that there is a path between each pair of vertices, considering all edges undirected.
- An arborescence is an acyclic digraph with a vertex
V, the root, such that there is a unique path fromVto every other vertex ofG. - A tree is an acyclic non-empty digraph such that there is a unique path between every pair of vertices, considering all edges undirected.
π EXPORTS π
These are the functions, bestie. Every signature and type line is kept exactly as-is, no cap. π«Ά
π arborescence_root(Digraph) -> no | {yes, Root}
Types:
Digraph = digraph:graph()
Root = digraph:vertex()
Returns {yes, Root} if Root is the root of the arborescence Digraph, otherwise no.
π components(Digraph) -> [Component]
Types:
Digraph = digraph:graph()
Component = [digraph:vertex()]
Returns a list of connected components. Each component is represented by its vertices. The order of the vertices and the order of the components are arbitrary. Each vertex of digraph Digraph occurs in exactly one component.
π condensation(Digraph) -> CondensedDigraph
Types:
Digraph = CondensedDigraph = digraph:graph()
Creates a digraph where the vertices are the strongly connected components of Digraph as returned by strong_components/1. If X and Y are two different strongly connected components, and vertices x and y exist in X and Y, respectively, such that there is an edge emanating from x and incident on y, then an edge emanating from X and incident on Y is created.
The created digraph has the same type as Digraph. All vertices and edges have the default label [].
Each cycle is included in some strongly connected component, which implies that a topological ordering of the created digraph always exists.
π cyclic_strong_components(Digraph) -> [StrongComponent]
Types:
Digraph = digraph:graph()
StrongComponent = [digraph:vertex()]
Returns a list of strongly connected components. Each strongly component is represented by its vertices. The order of the vertices and the order of the components are arbitrary. Only vertices that are included in some cycle in Digraph are returned, otherwise the returned list is equal to that returned by strong_components/1.
π is_acyclic(Digraph) -> boolean()
Types:
Digraph = digraph:graph()
Returns true if and only if digraph Digraph is acyclic.
π is_arborescence(Digraph) -> boolean()
Types:
Digraph = digraph:graph()
Returns true if and only if digraph Digraph is an arborescence.
π is_tree(Digraph) -> boolean()
Types:
Digraph = digraph:graph()
Returns true if and only if digraph Digraph is a tree.
π loop_vertices(Digraph) -> Vertices
Types:
Digraph = digraph:graph()
Vertices = [digraph:vertex()]
Returns a list of all vertices of Digraph that are included in some loop.
π postorder(Digraph) -> Vertices
Types:
Digraph = digraph:graph()
Vertices = [digraph:vertex()]
Returns all vertices of digraph Digraph. The order is given by a depth-first traversal of the digraph, collecting visited vertices in postorder. More precisely, the vertices visited while searching from an arbitrarily chosen vertex are collected in postorder, and all those collected vertices are placed before the subsequently visited vertices.
π preorder(Digraph) -> Vertices
Types:
Digraph = digraph:graph()
Vertices = [digraph:vertex()]
Returns all vertices of digraph Digraph. The order is given by a depth-first traversal of the digraph, collecting visited vertices in preorder.
π reachable(Vertices, Digraph) -> Reachable
Types:
Digraph = digraph:graph()
Vertices = Reachable = [digraph:vertex()]
Returns an unsorted list of digraph vertices such that for each vertex in the list, there is a path in Digraph from some vertex of Vertices to the vertex. In particular, as paths can have length zero, the vertices of Vertices are included in the returned list.
π reachable_neighbours(Vertices, Digraph) -> Reachable
Types:
Digraph = digraph:graph()
Vertices = Reachable = [digraph:vertex()]
Returns an unsorted list of digraph vertices such that for each vertex in the list, there is a path in Digraph of length one or more from some vertex of Vertices to the vertex. As a consequence, only those vertices of Vertices that are included in some cycle are returned.
π reaching(Vertices, Digraph) -> Reaching
Types:
Digraph = digraph:graph()
Vertices = Reaching = [digraph:vertex()]
Returns an unsorted list of digraph vertices such that for each vertex in the list, there is a path from the vertex to some vertex of Vertices. In particular, as paths can have length zero, the vertices of Vertices are included in the returned list.
π reaching_neighbours(Vertices, Digraph) -> Reaching
Types:
Digraph = digraph:graph()
Vertices = Reaching = [digraph:vertex()]
Returns an unsorted list of digraph vertices such that for each vertex in the list, there is a path of length one or more from the vertex to some vertex of Vertices. Therefore only those vertices of Vertices that are included in some cycle are returned.
π strong_components(Digraph) -> [StrongComponent]
Types:
Digraph = digraph:graph()
StrongComponent = [digraph:vertex()]
Returns a list of strongly connected components. Each strongly component is represented by its vertices. The order of the vertices and the order of the components are arbitrary. Each vertex of digraph Digraph occurs in exactly one strong component.
π subgraph(Digraph, Vertices) -> SubGraph / subgraph(Digraph, Vertices, Options) -> SubGraph
Types:
Digraph = SubGraph = digraph:graph()
Vertices = [digraph:vertex()]
Options = [{type, SubgraphType} | {keep_labels, boolean()}]
SubgraphType = inherit | [digraph:d_type()]
Creates a maximal subgraph of Digraph having as vertices those vertices of Digraph that are mentioned in Vertices.
If the value of option type is inherit, which is the default, the type of Digraph is used for the subgraph as well. Otherwise the option value of type is used as argument to digraph:new/1.
If the value of option keep_labels is true, which is the default, the labels of vertices and edges of Digraph are used for the subgraph as well. If the value is false, default label [] is used for the vertices and edges of the subgroup.
subgraph(Digraph, Vertices) is equivalent to subgraph(Digraph, Vertices, []).
β οΈ If any of the arguments are invalid, a badarg exception is raised. That's a hard no, bestie.
π topsort(Digraph) -> Vertices | false
Types:
Digraph = digraph:graph()
Vertices = [digraph:vertex()]
Returns a topological ordering of the vertices of digraph Digraph if such an ordering exists, otherwise false. For each vertex in the returned list, no out-neighbors occur earlier in the list.
π SEE ALSO π
digraph(3erl)β the other module in this whole graph situation, and likeβ¦ you gotta know it, bestie π the utils here don't exist in a vacuum, so go read that one too. iykyk.
okay that's literally the whole page, we're done, go build some graphs ππ
this lil masterpiece was brought to you by Ericsson AB β¨, living in stdlib 4.3.1.3 π, and it goes by digraph_utils(3erl) β say it with me: digraph underscore utils, paren, three, erl. ate, no crumbs π«ΆπΈ
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms