Summary
Graphs are how you model things and the relationships between them when a list or a map can no longer keep the relationships straight. In LFE, you get them from two standard-library modules — digraph for building and querying, digraph_utils for the real algorithms — and the whole chapter reduces to a handful of moves worth carrying away:
- Conjure and dispose.
digraph:new/0,1creates a graph (defaultcyclicandprotected; passacyclicto forbid cycles at insertion time,privateto lock other processes out).digraph:delete/1frees it. The second is not optional. - Vertices are the things.
add_vertex,vertex,vertices,del_vertex,no_vertices, plus the degree and neighbour functions. A vertex is any term; a label is a note pinned to it, not its identity. - Edges are the directed lines between.
add_edge,edge(returning the canonical#(E V1 V2 Label)),edges,del_edge,no_edges. Direction matters; multiple edges and labels are allowed; anacyclicgraph refuses an edge that would close a loop, handing you the offending path. - Paths and cycles.
get_path(depth-first, a path) versusget_short_path(breadth-first, the short path);get_cycleandget_short_cyclefor loops;del_pathfor severance. - The power tools.
topsortfor build order (falseon a cycle),is_acyclicfor a quick verdict,reachable/reachingfor impact analysis,components/strong_componentsfor structure, and a drawer of specialists for the rarer days.
And above all, the one fact that makes graphs different from everything else in this Part: a digraph mutates. It is a reference to shared, ETS-backed state, not an immutable value — owned by one process, invisible to the garbage collector, and yours to delete. Hold the handle, respect the ownership, clean up when you’re done, and digraph is a superb tool. Forget, and it will wait for you at 3 a.m. with the patience of a thing that knows it has all the time in the world.
The other data structures in this book ask nothing of you but that you understand them. The graph asks that, and one thing more: that you remember to draw the line, walk it, and then — like any good guest — turn off the lights on your way out. Don’t Panic. Just don’t forget to delete.