On this page
Package Verada.Containers.Graphs
Verada predefined library: a directed graph, a proof enclave. Its vertices are cells, and each vertex's edges point at the cells of other vertices (or its own), so a vertex is shared by every edge into it and the edges may make cycles, which ownership forbids; the heap engine proves the operations against the graph's model, its vertices' degrees and its edges, so that clients treat a graph as that value. Only memory limits the vertices and a vertex's edges: the table of vertices and each vertex's links double when full, and Add_Vertex and Add_Edge raise Storage_Error when memory cannot hold one more (Verada.Storage).
Vertex_Count_Type
subtype Vertex_Count_Type is Natural;Vertex
subtype Vertex is Positive;Degree_Type
subtype Degree_Type is Natural;Edge
type Edge is record
From, To : Vertex;
end record;an edge, from a vertex to a vertex
Edge_Sequences
package Edge_Sequences is new Verada.Containers.Functional.Sequences (Edge);Degree_Sequences
package Degree_Sequences is new Verada.Containers.Functional.Sequences (Degree_Type);Graph_Model
type Graph_Model is record
Degrees : Degree_Sequences.Sequence;
Edges : Edge_Sequences.Sequence;
end record;a graph's value: the out-degree of each vertex, the vertices being 1 .. the count of degrees; and the edges, each once, in the order they were added
Graph
type Graph is limited private
with Default_Initial_Condition => Degree_Sequences.Length (Model (Graph).Degrees) = 0
and then Edge_Sequences.Length (Model (Graph).Edges) = 0,
Finalizable => (Finalize => Clear, Relaxed_Finalization => True);Model
function Model (G : Graph) return Graph_Model with Ghost;Vertex_Count
function Vertex_Count (G : Graph) return Vertex_Count_Type
with Post => Vertex_Count'Result = Degree_Sequences.Length (Model (G).Degrees)
and then Vertex_Count'Result < Verada.Storage.Length_Bound;Has_Edge
function Has_Edge (M : Graph_Model; From, To : Vertex) return Boolean is
(for some K in 1 .. Edge_Sequences.Length (M.Edges) => Edge_Sequences.Get (M.Edges, K) = Edge'(From, To))
with Ghost;whether a graph's model has an edge from From to To (of the model, not the graph, so that clients see what it says: a function of an enclave object is opaque to them)
Out_Degree
function Out_Degree (G : Graph; V : Vertex) return Degree_Type
with Pre => V <= Vertex_Count (G),
Post => Out_Degree'Result = Degree_Sequences.Get (Model (G).Degrees, V)
and then Out_Degree'Result < Verada.Storage.Length_Bound;how many edges leave V
Adjacent
function Adjacent (G : Graph; From, To : Vertex) return Boolean
with Pre => From <= Vertex_Count (G) and then To <= Vertex_Count (G),
Post => Adjacent'Result = Has_Edge (Model (G), From, To);whether an edge leads from From to To, found by following From's edges
Add_Vertex
procedure Add_Vertex (G : in out Graph; V : out Vertex)
with Post => Model (G).Degrees = Degree_Sequences.Add (Model (G)'Old.Degrees, 0)
and Model (G).Edges = Model (G)'Old.Edges
and V = Vertex_Count (G);a new vertex, with no edges, numbered after the others
Add_Edge
procedure Add_Edge (G : in out Graph; From, To : Vertex)
with Pre => From <= Vertex_Count (G) and then To <= Vertex_Count (G)
and then not Has_Edge (Model (G), From, To),
Post => Model (G).Degrees = Degree_Sequences.Set (Model (G)'Old.Degrees, From, Degree_Sequences.Get (Model (G)'Old.Degrees, From) + 1)
and Model (G).Edges = Edge_Sequences.Add (Model (G)'Old.Edges, Edge'(From, To));an edge from From to To (to itself when they are the same), not there before
Clear
procedure Clear (G : in out Graph)
with Post => Vertex_Count (G) = 0 and then Edge_Sequences.Length (Model (G).Edges) = 0;every vertex and edge removed (and the cells freed: a graph that ends is cleared)