digraph(3)

πŸ“¦
version 29.0.6 Β· 3 versions
, in ubuntu stonking

🌸 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/1 is called, or when the process that created the digraph terminates. So yeah, they hang around until you deal with them. 😭
  • digraph is 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] from V1 to V2 in G.
  • remove all edges of G emanating from v[i] and incident to v[i+1] for 1 <= i < k (including multiple edges, yeah all of them).
  • repeat until there is no path between V1 and V2.

✨ 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 through V gets 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}, where Cyclicity is cyclic or acyclic, according to the options given to new.
  • {memory, NoWords}, where NoWords is the number of words allocated to the ETS tables.
  • {protection, Protection}, where Protection is protected or private, according to the options given to new.

✨ 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 option T is specified or Type is not a proper list, a badarg exception 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 from stdlib 8.0.4, section digraph(3). credit where credit is due, slay. πŸ‘‘βœ¨

go build something gorgeous with it. mwah. πŸ’…πŸ«ΆπŸ’œ

πŸ“¦ where this page comes from