protocol

# `graph_protocol`

Common protocol for all types of graphs. Graphs are represented using a dictionary where keys are vertices and values are sorted lists of neighbors (implicitly defining edges).

Availability:
`logtalk_load(graphs(loader))`
Author: Paulo Moura
Version: 1:0:0
Date: 2026-02-25
Compilation flags:
`static`
Dependencies:
(none)
Remarks:
(none)
Inherited public predicates:
(none)

## Public predicates

### `new/1`

Creates a new empty graph.

Compilation flags:
`static`
Template:
`new(Graph)`
Mode and number of proofs:
`new(-graph)` - `one`

### `new/2`

Creates a new graph from a list of edges. Vertices are defined implicitly by edges.

Compilation flags:
`static`
Template:
`new(Edges,Graph)`
Mode and number of proofs:
`new(+list(edge),-graph)` - `one`

### `new/3`

Creates a new graph from a list of vertices and a list of edges. Vertices may also be defined implicitly by edges.

Compilation flags:
`static`
Template:
`new(Vertices,Edges,Graph)`
Mode and number of proofs:
`new(+list(vertex),+list(edge),-graph)` - `one`

### `empty/1`

True iff the given graph is empty.

Compilation flags:
`static`
Template:
`empty(Graph)`
Mode and number of proofs:
`empty(@graph)` - `zero_or_one`

### `vertices/2`

Unifies `Vertices` with a sorted list of all vertices in the graph.

Compilation flags:
`static`
Template:
`vertices(Graph,Vertices)`
Mode and number of proofs:
`vertices(+graph,-list(vertex))` - `one`

### `edges/2`

Unifies `Edges` with a list of all edges in the graph.

Compilation flags:
`static`
Template:
`edges(Graph,Edges)`
Mode and number of proofs:
`edges(+graph,-list(edge))` - `one`

### `add_vertex/3`

Adds a vertex to the graph. If the vertex already exists, the graph is unchanged.

Compilation flags:
`static`
Template:
`add_vertex(Graph,Vertex,NewGraph)`
Mode and number of proofs:
`add_vertex(+graph,+vertex,-graph)` - `one`

### `add_vertices/3`

Adds a list of vertices to the graph.

Compilation flags:
`static`
Template:
`add_vertices(Graph,Vertices,NewGraph)`
Mode and number of proofs:
`add_vertices(+graph,+list(vertex),-graph)` - `one`

### `delete_vertex/3`

Deletes a vertex and all edges incident to it from the graph. If the vertex does not exist, the graph is unchanged.

Compilation flags:
`static`
Template:
`delete_vertex(Graph,Vertex,NewGraph)`
Mode and number of proofs:
`delete_vertex(+graph,+vertex,-graph)` - `one`

### `delete_vertices/3`

Deletes a list of vertices and all edges incident to them from the graph.

Compilation flags:
`static`
Template:
`delete_vertices(Graph,Vertices,NewGraph)`
Mode and number of proofs:
`delete_vertices(+graph,+list(vertex),-graph)` - `one`

### `add_edges/3`

Adds a list of edges to the graph. Vertices referenced by edges are added implicitly.

Compilation flags:
`static`
Template:
`add_edges(Graph,Edges,NewGraph)`
Mode and number of proofs:
`add_edges(+graph,+list(edge),-graph)` - `one`

### `delete_edges/3`

Deletes a list of edges from the graph. Vertices are not deleted. Non-existing edges are silently ignored.

Compilation flags:
`static`
Template:
`delete_edges(Graph,Edges,NewGraph)`
Mode and number of proofs:
`delete_edges(+graph,+list(edge),-graph)` - `one`

### `neighbors/3`

Unifies `Neighbors` with a sorted list of the neighbors of `Vertex` in the graph. Fails if `Vertex` is not in the graph.

Compilation flags:
`static`
Template:
`neighbors(Vertex,Graph,Neighbors)`
Mode and number of proofs:
`neighbors(+vertex,+graph,-list(vertex))` - `zero_or_one`

### `reachable/3`

Unifies `Vertices` with a sorted list of vertices reachable from `Vertex` (including `Vertex` itself). Fails if `Vertex` is not in the graph.

Compilation flags:
`static`
Template:
`reachable(Vertex,Graph,Vertices)`
Mode and number of proofs:
`reachable(+vertex,+graph,-list(vertex))` - `zero_or_one`

### `breadth_first_order/3`

Unifies `Vertices` with the breadth-first traversal order rooted at `Vertex`. Fails if `Vertex` is not in the graph.

Compilation flags:
`static`
Template:
`breadth_first_order(Vertex,Graph,Vertices)`
Mode and number of proofs:
`breadth_first_order(+vertex,+graph,-list(vertex))` - `zero_or_one`

### `depth_first_order/3`

Unifies `Vertices` with the depth-first traversal order rooted at `Vertex`. Fails if `Vertex` is not in the graph.

Compilation flags:
`static`
Template:
`depth_first_order(Vertex,Graph,Vertices)`
Mode and number of proofs:
`depth_first_order(+vertex,+graph,-list(vertex))` - `zero_or_one`

### `number_of_vertices/2`

Unifies `N` with the number of vertices in the graph.

Compilation flags:
`static`
Template:
`number_of_vertices(Graph,N)`
Mode and number of proofs:
`number_of_vertices(+graph,-integer)` - `one`

### `number_of_edges/2`

Unifies `N` with the number of edges in the graph.

Compilation flags:
`static`
Template:
`number_of_edges(Graph,N)`
Mode and number of proofs:
`number_of_edges(+graph,-integer)` - `one`

### `path/3`

Returns a maximal path (list of vertices) rooted at `Vertex`, enumerating different paths on backtracking. Fails if `Vertex` is not in the graph.

Compilation flags:
`static`
Template:
`path(Vertex,Graph,Path)`
Mode and number of proofs:
`path(+vertex,+graph,-list(vertex))` - `zero_or_more`

### `has_path/3`

True iff there is a path from `Vertex1` to `Vertex2` in the graph.

Compilation flags:
`static`
Template:
`has_path(Vertex1,Vertex2,Graph)`
Mode and number of proofs:
`has_path(+vertex,+vertex,+graph)` - `zero_or_one`

### `min_path/5`

Finds the minimum cost path from `Vertex1` to `Vertex2`. For unweighted graphs, cost is the number of edges. Fails if no path exists.

Compilation flags:
`static`
Template:
`min_path(Vertex1,Vertex2,Graph,Path,Cost)`
Mode and number of proofs:
`min_path(+vertex,+vertex,+graph,-list(vertex),-number)` - `zero_or_one`

### `max_path/5`

Finds the maximum cost acyclic path from `Vertex1` to `Vertex2`. For unweighted graphs, cost is the number of edges. Fails if no path exists.

Compilation flags:
`static`
Template:
`max_path(Vertex1,Vertex2,Graph,Path,Cost)`
Mode and number of proofs:
`max_path(+vertex,+vertex,+graph,-list(vertex),-number)` - `zero_or_one`

### `min_distances/3`

Computes minimum path costs from `Vertex` to all reachable vertices. Returns a list of `Target-Cost` pairs including `Vertex-0`.

Compilation flags:
`static`
Template:
`min_distances(Vertex,Graph,Distances)`
Mode and number of proofs:
`min_distances(+vertex,+graph,-list(pair))` - `zero_or_one`

### `min_predecessors/3`

Computes predecessor links for minimum paths rooted at `Vertex`. Returns a list of `Target-Predecessor` pairs; the source predecessor is `none`.

Compilation flags:
`static`
Template:
`min_predecessors(Vertex,Graph,Predecessors)`
Mode and number of proofs:
`min_predecessors(+vertex,+graph,-list(pair))` - `zero_or_one`

### `all_pairs_min_paths/2`

Computes minimum path costs for all ordered pairs of vertices with a path between them. Returns a list of `(Vertex1-Vertex2)-Cost` terms.

Compilation flags:
`static`
Template:
`all_pairs_min_paths(Graph,Pairs)`
Mode and number of proofs:
`all_pairs_min_paths(+graph,-list(pair))` - `one`

### `all_pairs_min_predecessors/2`

Computes predecessor links for minimum paths for all ordered source-target pairs with a path. Returns a list of `(Vertex1-Vertex2)-Predecessor` terms.

Compilation flags:
`static`
Template:
`all_pairs_min_predecessors(Graph,Pairs)`
Mode and number of proofs:
`all_pairs_min_predecessors(+graph,-list(pair))` - `one`

### `is_complete/1`

True iff every pair of distinct vertices in the graph is connected by an edge.

Compilation flags:
`static`
Template:
`is_complete(Graph)`
Mode and number of proofs:
`is_complete(+graph)` - `zero_or_one`

### `is_bipartite/1`

True iff the graph is bipartite, i.e. its vertices can be partitioned into two sets such that every edge connects a vertex in one set to a vertex in the other.

Compilation flags:
`static`
Template:
`is_bipartite(Graph)`
Mode and number of proofs:
`is_bipartite(+graph)` - `zero_or_one`

### `is_sparse/1`

True iff the graph is sparse, i.e. the number of edges is at most `|V| * log2(|V|)`. The cutoff `|E| = |V| * log2(|V|)` separates sparse graphs (where adjacency lists are efficient) from dense graphs (where adjacency matrix representations may be preferable).

Compilation flags:
`static`
Template:
`is_sparse(Graph)`
Mode and number of proofs:
`is_sparse(+graph)` - `zero_or_one`

## Protected predicates

(none)

## Private predicates

(none)

## Operators

(none)