YAP 7.1.0
Unweighted Graphs

The following graph manipulation routines are based in code originally written by Richard O'Keefe. More...

Detailed Description

The following graph manipulation routines are based in code originally written by Richard O'Keefe.

The code was then extended to be compatible with the SICStus Prolog ugraphs library The routines assume directed graphs, undirected graphs may be implemented by using two edges Graphs are represented in one of two ways:

The S-representation of a graph is a list of (vertex-neighbors) pairs, where the pairs are in standard order (as produced by keysort) and the neighbors of each vertex are also in standard order (as produced by sort) This form is convenient for many calculations

These built-ins are available once included with the use_module(library(ugraphs)) command


Class Documentation

◆ vertices_edges_to_ugraph/3

class vertices_edges_to_ugraph/3

vertices_edges_to_ugraph(+ Vertices, + Edges, - Graph)

Given a graph with a set of vertices Vertices and a set of edges Edges, Graph must unify with the corresponding s-representation Note that the vertices without edges will appear in Vertices but not in Edges Moreover, it is sufficient for a vertex to appear in Edges

?- vertices_edges_to_ugraph([],[1-3,2-4,4-5,1-5],L).
L = [1-[3,5],2-[4],3-[],4-[5],5-[]] ?
vertices_edges_to_ugraph(+ Vertices, + Edges, - Graph)

In this case all edges are defined implicitly The next example shows three unconnected edges:

?- vertices_edges_to_ugraph([6,7,8],[1-3,2-4,4-5,1-5],L).
L = [1-[3,5],2-[4],3-[],4-[5],5-[],6-[],7-[],8-[]] ?

◆ add_edges/3

class add_edges/3

add_edges(+ Graph, + Edges, - NewGraph)

Unify NewGraph with a new graph obtained by adding the list of edges Edges to the graph Graph In the next example:

?- add_edges([1-[3,5],2-[4],3-[],4-[5],5-[],6-[],
7-[],8-[]],[1-6,2-3,3-2,5-7,3-2,4-5],NL).
NL = [1-[3,5,6],2-[3,4],3-[2],4-[5],5-[7],6-[],7-[],8-[]]
add_edges(+ Graph, + Edges, - NewGraph)

◆ add_vertices/3

class add_vertices/3

add_vertices(+ Graph, + Vertices, - NewGraph)

Unify NewGraph with a new graph obtained by adding the list of vertices Vertices to the graph Graph In the next example:

?- add_vertices([1-[3,5],2-[4],3-[],4-[5],
5-[],6-[],7-[],8-[]],
[0,2,9,10,11],
NG).
NG = [0-[],1-[3,5],2-[4],3-[],4-[5],5-[],
6-[],7-[],8-[],9-[],10-[],11-[]]
add_vertices(+ Graph, + Vertices, - NewGraph)

◆ complement/2

class complement/2

complement(+ Graph, - NewGraph)

Unify NewGraph with the graph complementary to Graph In the next example:

?- complement([1-[3,5],2-[4],3-[],
4-[1,2,7,5],5-[],6-[],7-[],8-[]], NL).
NL = [1-[2,4,6,7,8],2-[1,3,5,6,7,8],3-[1,2,4,5,6,7,8],
4-[3,5,6,8],5-[1,2,3,4,6,7,8],6-[1,2,3,4,5,7,8],
7-[1,2,3,4,5,6,8],8-[1,2,3,4,5,6,7]]
complement(+ Graph, - NewGraph)

◆ compose/3

class compose/3

compose(+ LeftGraph, + RightGraph, - NewGraph)

Compose the graphs LeftGraph and RightGraph to form NewGraph In the next example:

?- compose([1-[2],2-[3]],[2-[4],3-[1,2,4]],L).
L = [1-[4],2-[1,2,4],3-[]]
compose(+ LeftGraph, + RightGraph, - NewGraph)

◆ del_edges/3

class del_edges/3

del_edges(+ Graph, + Edges, - NewGraph)

Unify NewGraph with a new graph obtained by removing the list of edges Edges from the graph Graph Notice that no vertices are deleted In the next example:

?- del_edges([1-[3,5],2-[4],3-[],4-[5],5-[],
6-[],7-[],8-[]],
[1-6,2-3,3-2,5-7,3-2,4-5,1-3],NL).
NL = [1-[5],2-[4],3-[],4-[],5-[],6-[],7-[],8-[]]
del_edges(+ Graph, + Edges, - NewGraph)

◆ del_vertices/3

class del_vertices/3

del_vertices(+ Graph, + Vertices, - NewGraph)

Unify NewGraph with a new graph obtained by deleting the list of vertices Vertices and all the edges that start from or go to a vertex in Vertices to the graph Graph In the next example:

?- del_vertices([2,1],[1-[3,5],2-[4],3-[],
4-[5],5-[],6-[],7-[2,6],8-[]],NL).
NL = [3-[],4-[5],5-[],6-[],7-[6],8-[]]
del_vertices(+ Graph, + Vertices, - NewGraph)

◆ edges/2

class edges/2

edges(+ Graph, - Edges)

Unify Edges with all edges appearing in graph Graph In the next example:

?- vertices([1-[3,5],2-[4],3-[],4-[5],5-[]], V).
L = [1,2,3,4,5]
vertices(+ Graph, - Vertices)

◆ neighbors/3

class neighbors/3

neighbors(+ Vertex, + Graph, - Vertices)

Unify Vertices with the list of neighbors of vertex Vertex in Graph If the vertice is not in the graph fail In the next example:

?- neighbors(4,[1-[3,5],2-[4],3-[],
4-[1,2,7,5],5-[],6-[],7-[],8-[]],
NL).
NL = [1,2,7,5]
neighbors(+ Vertex, + Graph, - Vertices)

◆ neighbours/3

class neighbours/3

neighbours(+ Vertex, + Graph, - Vertices)

Unify Vertices with the list of neighbours of vertex Vertex in Graph In the next example:

?- neighbours(4,[1-[3,5],2-[4],3-[],
4-[1,2,7,5],5-[],6-[],7-[],8-[]], NL).
NL = [1,2,7,5]
neighbours(+ Vertex, + Graph, - Vertices)

◆ reachable/3

class reachable/3

reachable(+ Node, + Graph, - Vertices)

Unify Vertices with the set of all vertices in graph Graph that are reachable from Node In the next example:

?- reachable(1,[1-[3,5],2-[4],3-[],4-[5],5-[]],V).
V = [1,3,5]
reachable(+ Node, + Graph, - Vertices)

◆ top_sort/3

class top_sort/3

top_sort(+ Graph, - Sort0, - Sort)

Generate the difference list Sort- Sort0 as a topological sorting of graph Graph, if one is possible

◆ top_sort/2

class top_sort/2

top_sort(+ Graph, - Sort)

Generate the set of nodes Sort as a topological sorting of graph Graph, if one is possible In the next example we show how topological sorting works for a linear graph:

?- top_sort([_138-[_219],_219-[_139], _139-[]],L).
L = [_138,_219,_139]
top_sort(+ Graph, - Sort)

◆ transitive_closure/2

class transitive_closure/2

transitive_closure(+ Graph, + Closure)

Generate the graph Closure as the transitive closure of graph Graph In the next example:

?- transitive_closure([1-[2,3],2-[4,5],4-[6]],L).
L = [1-[2,3,4,5,6],2-[4,5,6],4-[6]]
transitive_closure(+ Graph, + Closure)

◆ vertices/2

class vertices/2

vertices(+ Graph, - Vertices)

Unify Vertices with all vertices appearing in graph Graph In the next example:

?- vertices([1-[3,5],2-[4],3-[],4-[5],5-[]], V).
L = [1,2,3,4,5]

◆ transpose/2

class transpose/2

transpose(+ Graph, - NewGraph)

Unify NewGraph with a new graph obtained from Graph by replacing all edges of the form V1-V2 by edges of the form V2-V1 The cost is O(|V|^2) In the next example:

?- transpose([1-[3,5],2-[4],3-[],
4-[5],5-[],6-[],7-[],8-[]], NL).
NL = [1-[],2-[],3-[1],4-[2],5-[1,4],6-[],7-[],8-[]]
transpose(+ Graph, - NewGraph)

Notice that an undirected graph is its own transpose

◆ raakau/3

class raakau/3

raakau(Vertices, InitialValue, Tree)

NOT USED AFTER ALL takes an ordered list of verticies and an initial value, and makes a very special sort of tree out of them, which represents a function sending each vertex to the initial value Note that in the third clause for raakau/6 Z can never be 0, this means that it doesn't matter what "greatest member" is reported for empty trees