digraph(3)
πΈ NAME πΈ
digraph β so this is the Erlang module definition page for digraph(3), bestie. This module hands you a version of labeled directed graphs, aka "digraphs". That's the whole vibe. π
π¦ DESCRIPTION π¦
okay so here's the tea: the digraphs that this module manages live inside ETS tables. And that means the following, no cap:
- Only the process that created the digraph is allowed to update it. Like, it's their digraph, nobody else gets to touch it. π
- Digraphs will not be garbage collected. The ETS tables used for a digraph will only be deleted when
delete/1is called, or when the process that created the digraph terminates. So yeah, they hang around until you deal with them. π digraphis a mutable data structure. It changes. It grows. It's giving main character energy. β¨
What makes the graphs provided here non-proper directed graphs is that multiple edges between vertices are allowed. However, the customary definition of directed graphs is used here. (Yes, both things are true at once, we contain multitudes. π«Ά)
- A directed graph (or just "digraph") is a pair (V, E) of a finite set V of vertices and a finite set E of directed edges (or just "edges"). The set of edges E is a subset of V Γ V (the Cartesian product of V with itself).
In this module, V is allowed to be empty. The so obtained unique digraph is called the empty digraph. Both vertices and edges are represented by unique Erlang terms.
- 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. Labels are Erlang terms.
- An edge e = (v, w) is said to emanate from vertex v and to be incident on vertex w.
- The out-degree of a vertex is the number of edges emanating from that vertex.
- The in-degree of a vertex is the number of edges incident on that vertex.
- If an edge is emanating from v and incident on w, then w is said to be an out-neighbor of v, and v is said to be an in-neighbor of w.
- A path P from v[1] to v[k] in a digraph (V, E) is a non-empty sequence v[1], v[2], ..., v[k] of vertices in V such that there is an edge (v[i],v[i+1]) in E for 1 <= i < k.
- The length of path P is k-1.
- Path P is simple if all vertices are distinct, except that the first and the last vertices can be the same.
- Path P is a cycle if the length of P is not zero and v[1] = v[k].
- A loop is a cycle of length one.
- A simple cycle is a path that is both a cycle and simple.
- An acyclic digraph is a digraph without cycles.
π See Also π
digraph_utils, ets
𧬠DATA TYPES π§¬
add_edge_err_rsn()
add_edge_err_rsn() ::
{bad_edge, Path :: [vertex()]} | {bad_vertex, V :: vertex()}.
The error reason for when an edge could not be added to a graph. Sad times, but at least it tells you why. π
If the edge would create a cycle in an acyclic digraph, {error, {bad_edge, Path}} is returned. If G already has an edge with value E connecting a different pair of vertices, {error, {bad_edge, [V1, V2]}} is returned. If either of V1 or V2 is not a vertex of digraph G, {error, {bad_vertex, V}} is returned, V = V1 or V = V2.
d_cyclicity()
d_cyclicity() :: acyclic | cyclic.
d_protection()
d_protection() :: private | protected.
d_type()
d_type() :: d_cyclicity() | d_protection().
edge()
edge() :: term().
Serves as the identifier or "name" of an edge. This is distinct from an edge "label" which attaches ancillary information to the edge rather than identifying the edge itself. Name vs. label, they're not the same thing, iykyk. π
-opaque graph()
-opaque graph()
A digraph as returned by new/0,1. It's opaque, so don't go poking at its insides unless you enjoy chaos. π
label()
label() :: term().
vertex()
vertex() :: term().
and that's the first half, bestie β more of this module is still coming π
π FUNCTIONS π
okay bestie, here's the whole toolbox. every single one of these, no skipping, no scrolling past the "boring" ones (they're all bangers actually). π«Ά
β¨ add_edge/4 β¨
add_edge(G, V1, V2, Label) -> edge() | {error, add_edge_err_rsn()}
when
G :: graph(),
V1 :: vertex(),
V2 :: vertex(),
Label :: label().
literally just add_edge(G, E, V1, V2, Label), except the E gets made for you. lazy girl summer, we love it. π
the edge it makes looks like the term ['$e' | N], where N is an integer >= 0.
wanna know what can go wrong? go peek at add_edge_err_rsn/0, it has the whole β¨ drama β¨ lined up.
β¨ add_edge/5 β¨
add_edge(G, E, V1, V2, Label) ->
edge() | {error, add_edge_err_rsn()}
when
G :: graph(),
E :: edge(),
V1 :: vertex(),
V2 :: vertex(),
Label :: label().
makes (or edits!) an edge with the id E in digraph G, and Label becomes the (new) label of that edge. the edge emanates from V1 and is incident on V2 (yes those are the real words, iykyk π). returns E.
again, possible oopsies live in add_edge_err_rsn/0. π₯
β¨ add_vertex/1 β¨
add_vertex(G) -> vertex() when G :: graph().
makes a vertex with the empty list as its label and hands it back to you, cute and ready. π
the vertex it makes is represented by the term ['$v' | N], where N is an integer >= 0.
β¨ add_vertex/3 β¨
add_vertex(G, V, Label) -> vertex()
when G :: graph(), V :: vertex(), Label :: label().
makes (or modifies π
) vertex V of digraph G, with Label as the (new) label of that vertex. returns the new vertex V. slay.
β¨ del_edge/2 β¨
del_edge(G, E) -> true when G :: graph(), E :: edge().
bye bye π β deletes edge E from digraph G.
β¨ del_edges/2 β¨
del_edges(G, Edges) -> true when G :: graph(), Edges :: [edge()].
mass deletion era π deletes every edge in the list Edges from digraph G.
β¨ del_path/3 β¨
del_path(G, V1, V2) -> true
when G :: graph(), V1 :: vertex(), V2 :: vertex().
keeps deleting edges from digraph G until there is literally no path left from vertex V1 to vertex V2. periodt.
here's a lil sketch of how it does it:
- find an arbitrary simple path
v[1], v[2], ..., v[k]fromV1toV2inG. - remove all edges of
Gemanating fromv[i]and incident tov[i+1]for1 <= i < k(including multiple edges, yeah all of them). - repeat until there is no path between
V1andV2.
β¨ del_vertex/2 β¨
del_vertex(G, V) -> true when G :: graph(), V :: vertex().
deletes vertex V from digraph G. and bestie, heads up: any edges emanating from V or incident on V get deleted too. it does not go out quietly. π
β¨ del_vertices/2 β¨
del_vertices(G, Vertices) -> true
when G :: graph(), Vertices :: [vertex()].
deletes all the vertices in the list Vertices from digraph G. group deletion, very main character.
β¨ delete/1 β¨
delete(G) -> true when G :: graph().
deletes digraph G entirely.
ngl this call is important β digraphs are implemented with ETS, and there is no garbage collection of ETS tables. sad but true. π« but the digraph is deleted if the process that created the digraph terminates, so if you're dying anyway you're off the hook.
β¨ edge/2 β¨
edge(G, E) -> {E, V1, V2, Label} | false
when
G :: graph(),
E :: edge(),
V1 :: vertex(),
V2 :: vertex(),
Label :: label().
returns {E, V1, V2, Label}, where Label is the label of edge E emanating from V1 and incident on V2 in digraph G. if no such edge E exists in digraph G, you get false. π
β¨ edges/1 β¨
edges(G) -> Edges when G :: graph(), Edges :: [edge()].
returns a list of all the edges of digraph G, in some unspecified order β so don't come for me about the ordering, it's vibes-based. π
β¨ edges/2 β¨
edges(G, V) -> Edges
when G :: graph(), V :: vertex(), Edges :: [edge()].
returns a list of all edges emanating from or incident on V of digraph G, in some unspecified order. both directions, bi energy. π
β¨ get_cycle/2 β¨
get_cycle(G, V) -> Vertices | false
when
G :: graph(),
V :: vertex(),
Vertices :: [vertex(), ...].
if a simple cycle of length two or more exists through vertex V, you get the cycle as a list [V, ..., V] of vertices. if there's a loop through V, you get the loop as a list [V]. if literally no cycles through V exist, false, sorry bestie. π
get_path/3 is what finds a simple cycle through V.
β¨ get_path/3 β¨
get_path(G, V1, V2) -> Vertices | false
when
G :: graph(),
V1 :: vertex(),
V2 :: vertex(),
Vertices :: [vertex(), ...].
tries to find a simple path from vertex V1 to vertex V2 of digraph G. returns the path as a list [V1, ..., V2] of vertices, or false if no simple path from V1 to V2 of length one or more exists. π
digraph G gets traversed in a depth-first manner, and the first found path is what's returned. no take-backs, no exploring the other options.
β¨ get_short_cycle/2 β¨
get_short_cycle(G, V) -> Vertices | false
when
G :: graph(),
V :: vertex(),
Vertices :: [vertex(), ...].
tries to find an as short as possible simple cycle through vertex V of digraph G. returns the cycle as a list [V, ..., V] of vertices, or false if no simple cycle through V exists. π₯Ί
heads up: a loop throughVgets returned as the list[V, V]here. not[V]. don't mix it up.
get_short_path/3 is used for finding a simple cycle through V.
β¨ get_short_path/3 β¨
get_short_path(G, V1, V2) -> Vertices | false
when
G :: graph(),
V1 :: vertex(),
V2 :: vertex(),
Vertices :: [vertex(), ...].
tries to find an as short as possible simple path from vertex V1 to vertex V2 of digraph G. returns the path as a list [V1, ..., V2] of vertices, or false if no simple path from V1 to V2 of length one or more exists.
digraph G gets traversed in a breadth-first manner this time, and the first found path is returned. glow up from the depth-first version. β¨
β¨ in_degree/2 β¨
in_degree(G, V) -> non_neg_integer()
when G :: graph(), V :: vertex().
returns the in-degree of vertex V of digraph G. how many edges are coming at you, periodt. π
β¨ in_edges/2 β¨
in_edges(G, V) -> Edges
when G :: graph(), V :: vertex(), Edges :: [edge()].
returns a list of all the edges incident on V of digraph G, in some unspecified order.
β¨ in_neighbours/2 β¨
in_neighbours(G, V) -> Vertex
when
G :: graph(),
V :: vertex(),
Vertex :: [vertex()].
returns a list of all the in-neighbors of V of digraph G, in some unspecified order. your inner circle fr fr. π«Ά
β¨ info/1 β¨
info(G) -> InfoList
when
G :: graph(),
InfoList ::
[{cyclicity, Cyclicity :: d_cyclicity()} |
{memory, NoWords :: non_neg_integer()} |
{protection, Protection :: d_protection()}].
returns a list of {Tag, Value} pairs describing digraph G. here's exactly which pairs come back:
{cyclicity, Cyclicity}, whereCyclicityiscyclicoracyclic, according to the options given tonew.{memory, NoWords}, whereNoWordsis the number of words allocated to the ETS tables.{protection, Protection}, whereProtectionisprotectedorprivate, according to the options given tonew.
β¨ new/1 β¨
new(Type) -> graph() when Type :: [d_type()].
returns an empty digraph with properties according to the options in Type:
cyclicβ allows cycles in the digraph (default). let it spin, let it twirl. πacyclicβ the digraph is to be kept acyclic.protectedβ other processes can read the digraph (default). sharing is caring.privateβ the digraph can be read and modified by the creating process only. exclusive, very possessive gf energy. π
π¨ if an unrecognized type optionTis specified orTypeis not a proper list, abadargexception is raised. no exceptions (except that one). π
β¨ no_edges/1 β¨
no_edges(G) -> non_neg_integer() when G :: graph().
returns the number of edges of digraph G.
β¨ no_vertices/1 β¨
no_vertices(G) -> non_neg_integer() when G :: graph().
returns the number of vertices of digraph G.
β¨ out_degree/2 β¨
out_degree(G, V) -> non_neg_integer()
when G :: graph(), V :: vertex().
returns the out-degree of vertex V of digraph G. how many edges are leaving your property. π
β¨ out_edges/2 β¨
out_edges(G, V) -> Edges
when G :: graph(), V :: vertex(), Edges :: [edge()].
returns a list of all the edges emanating from V of digraph G, in some unspecified order.
β¨ out_neighbours/2 β¨
out_neighbours(G, V) -> Vertices
when
G :: graph(),
V :: vertex(),
Vertices :: [vertex()].
returns a list of all the out-neighbors of V of digraph G, in some unspecified order.
β¨ sink_vertices/1 β¨
sink_vertices(graph()) -> [vertex()].
returns a list of all the vertices of graph G with out-degree zero. they take, they don't give. sink behavior honestly. π«
β¨ source_vertices/1 β¨
source_vertices(graph()) -> [vertex()].
returns a list of all the vertices of graph G with in-degree zero. givers, not takers. π
β¨ vertex/2 β¨
vertex(G, V) -> {V, Label} | false
when G :: graph(), V :: vertex(), Label :: label().
returns {V, Label}, where Label is the label of vertex V of digraph G. or false if no vertex V of digraph G exists. π
β¨ vertices/1 β¨
vertices(G) -> Vertices when G :: graph(), Vertices :: [vertex()].
returns a list of all the vertices of digraph G, in some unspecified order. the whole squad. π
okay bestie that's the entire toolbox, every last one of them, nothing skipped. π€ i genuinely love this module, it's lowkey iconic.
that was Ericsson AB's work β this page is fromstdlib 8.0.4, sectiondigraph(3). credit where credit is due, slay. πβ¨
go build something gorgeous with it. mwah. π π«Άπ
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms