2015-11-18 15:06:25 +00:00
|
|
|
/**
|
|
|
|
* @file undgraphs.yap
|
|
|
|
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
|
|
|
|
* @date 2006
|
|
|
|
*
|
|
|
|
* @brief Directed Graph Processing Utilities.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2006-04-10 20:24:52 +01:00
|
|
|
% File : dgraphs.yap
|
|
|
|
% Author : Vitor Santos Costa
|
|
|
|
% Updated: 2006
|
2015-11-18 15:06:25 +00:00
|
|
|
% Purpose:
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2006-04-10 20:24:52 +01:00
|
|
|
:- module( undgraphs,
|
|
|
|
[
|
|
|
|
undgraph_add_edge/4,
|
|
|
|
undgraph_add_edges/3,
|
|
|
|
undgraph_add_vertices/3,
|
|
|
|
undgraph_del_edge/4,
|
|
|
|
undgraph_del_edges/3,
|
|
|
|
undgraph_del_vertex/3,
|
|
|
|
undgraph_del_vertices/3,
|
|
|
|
undgraph_edges/2,
|
2006-04-14 02:01:55 +01:00
|
|
|
undgraph_neighbors/3,
|
|
|
|
undgraph_neighbours/3,
|
2008-03-26 14:41:45 +00:00
|
|
|
undgraph_components/2,
|
2006-04-20 16:28:08 +01:00
|
|
|
undgraph_min_tree/2]).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
/** @defgroup undgraphs Undirected Graphs
|
2015-01-04 23:58:23 +00:00
|
|
|
@ingroup library
|
2014-09-11 20:06:57 +01:00
|
|
|
@{
|
|
|
|
|
|
|
|
The following graph manipulation routines use the red-black tree graph
|
|
|
|
library to implement undirected graphs. Mostly, this is done by having
|
|
|
|
two directed edges per undirected edge.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pred undgraph_new(+ _Graph_)
|
|
|
|
|
|
|
|
|
|
|
|
Create a new directed graph. This operation must be performed before
|
|
|
|
trying to use the graph.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/** @pred undgraph_complement(+ _Graph_, - _NewGraph_)
|
|
|
|
|
|
|
|
|
|
|
|
Unify _NewGraph_ with the graph complementary to _Graph_.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
/** @pred undgraph_vertices(+ _Graph_, - _Vertices_)
|
|
|
|
|
|
|
|
|
|
|
|
Unify _Vertices_ with all vertices appearing in graph
|
|
|
|
_Graph_.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2007-12-05 12:17:25 +00:00
|
|
|
:- reexport( library(dgraphs),
|
|
|
|
[
|
|
|
|
dgraph_new/1 as undgraph_new,
|
|
|
|
dgraph_add_vertex/3 as undgraph_add_vertex,
|
|
|
|
dgraph_vertices/2 as undgraph_vertices,
|
|
|
|
dgraph_complement/2 as undgraph_complement,
|
|
|
|
dgraph_symmetric_closure/2 as dgraph_to_undgraph,
|
2008-06-26 14:09:15 +01:00
|
|
|
dgraph_edge/3 as undgraph_edge,
|
|
|
|
dgraph_reachable/3 as undgraph_reachable
|
2007-12-05 12:17:25 +00:00
|
|
|
]).
|
|
|
|
|
|
|
|
|
2006-04-10 20:24:52 +01:00
|
|
|
:- use_module( library(dgraphs),
|
|
|
|
[
|
|
|
|
dgraph_add_edge/4,
|
|
|
|
dgraph_add_edges/3,
|
|
|
|
dgraph_add_vertices/3,
|
|
|
|
dgraph_del_edge/4,
|
|
|
|
dgraph_del_edges/3,
|
|
|
|
dgraph_del_vertex/3,
|
|
|
|
dgraph_del_vertices/3,
|
|
|
|
dgraph_edges/2,
|
2006-04-14 02:01:55 +01:00
|
|
|
dgraph_neighbors/3,
|
2007-12-05 12:17:25 +00:00
|
|
|
dgraph_neighbours/3]).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
2006-04-20 16:28:08 +01:00
|
|
|
:- use_module(library(wundgraphs), [
|
|
|
|
undgraph_to_wundgraph/2,
|
|
|
|
wundgraph_min_tree/3,
|
|
|
|
wundgraph_max_tree/3,
|
|
|
|
wundgraph_to_undgraph/2]).
|
|
|
|
|
2006-04-10 20:24:52 +01:00
|
|
|
:- use_module(library(ordsets),
|
|
|
|
[ ord_del_element/3,
|
|
|
|
ord_union/3,
|
|
|
|
ord_subtract/3]).
|
|
|
|
|
|
|
|
:- use_module(library(rbtrees),
|
2006-04-14 02:01:55 +01:00
|
|
|
[ rb_delete/4,
|
2009-06-08 20:13:31 +01:00
|
|
|
rb_delete/3,
|
2008-03-26 14:41:45 +00:00
|
|
|
rb_insert/4,
|
|
|
|
rb_in/3,
|
2006-04-14 02:01:55 +01:00
|
|
|
rb_partial_map/4
|
|
|
|
]).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
2007-12-05 12:17:25 +00:00
|
|
|
undgraph_add_edge(Vs0,V1,V2,Vs2) :-
|
2006-04-10 20:24:52 +01:00
|
|
|
dgraphs:dgraph_new_edge(V1,V2,Vs0,Vs1),
|
|
|
|
dgraphs:dgraph_new_edge(V2,V1,Vs1,Vs2).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred undgraph_add_edges(+ _Graph_, + _Edges_, - _NewGraph_)
|
|
|
|
|
|
|
|
|
|
|
|
Unify _NewGraph_ with a new graph obtained by adding the list of
|
|
|
|
edges _Edges_ to the graph _Graph_.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2007-12-05 12:17:25 +00:00
|
|
|
undgraph_add_edges(G0, Edges, GF) :-
|
|
|
|
dup_edges(Edges, DupEdges),
|
|
|
|
dgraph_add_edges(G0, DupEdges, GF).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
|
|
|
dup_edges([],[]).
|
|
|
|
dup_edges([E1-E2|Edges], [E1-E2,E2-E1|DupEdges]) :-
|
|
|
|
dup_edges(Edges, DupEdges).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred undgraph_add_vertices(+ _Graph_, + _Vertices_, - _NewGraph_)
|
|
|
|
|
|
|
|
|
|
|
|
Unify _NewGraph_ with a new graph obtained by adding the list of
|
|
|
|
vertices _Vertices_ to the graph _Graph_.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2007-12-05 12:17:25 +00:00
|
|
|
undgraph_add_vertices(G, [], G).
|
|
|
|
undgraph_add_vertices(G0, [V|Vs], GF) :-
|
|
|
|
dgraph_add_vertex(G0, V, GI),
|
|
|
|
undgraph_add_vertices(GI, Vs, GF).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred undgraph_edges(+ _Graph_, - _Edges_)
|
|
|
|
|
|
|
|
|
|
|
|
Unify _Edges_ with all edges appearing in graph
|
|
|
|
_Graph_.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2006-04-10 20:24:52 +01:00
|
|
|
undgraph_edges(Vs,Edges) :-
|
|
|
|
dgraph_edges(Vs,DupEdges),
|
|
|
|
remove_dups(DupEdges,Edges).
|
|
|
|
|
|
|
|
remove_dups([],[]).
|
|
|
|
remove_dups([V1-V2|DupEdges],NEdges) :- V1 @< V2, !,
|
|
|
|
NEdges = [V1-V2|Edges],
|
|
|
|
remove_dups(DupEdges,Edges).
|
|
|
|
remove_dups([_|DupEdges],Edges) :-
|
|
|
|
remove_dups(DupEdges,Edges).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred undgraph_neighbours(+ _Vertex_, + _Graph_, - _Vertices_)
|
|
|
|
|
|
|
|
|
|
|
|
Unify _Vertices_ with the list of neighbours of vertex _Vertex_
|
|
|
|
in _Graph_.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2006-04-10 20:24:52 +01:00
|
|
|
undgraph_neighbours(V,Vertices,Children) :-
|
|
|
|
dgraph_neighbours(V,Vertices,Children0),
|
2006-04-14 02:01:55 +01:00
|
|
|
(
|
|
|
|
ord_del_element(Children0,V,Children)
|
|
|
|
->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
Children = Children0
|
|
|
|
).
|
2006-04-10 20:24:52 +01:00
|
|
|
undgraph_neighbors(V,Vertices,Children) :-
|
|
|
|
dgraph_neighbors(V,Vertices,Children0),
|
2006-04-14 02:01:55 +01:00
|
|
|
(
|
|
|
|
ord_del_element(Children0,V,Children)
|
|
|
|
->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
Children = Children0
|
|
|
|
).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
2007-12-05 12:17:25 +00:00
|
|
|
undgraph_del_edge(Vs0,V1,V2,VsF) :-
|
|
|
|
dgraph_del_edge(Vs0,V1,V2,Vs1),
|
|
|
|
dgraph_del_edge(Vs1,V2,V1,VsF).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred undgraph_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.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2007-12-05 12:17:25 +00:00
|
|
|
undgraph_del_edges(G0, Edges, GF) :-
|
|
|
|
dup_edges(Edges,DupEdges),
|
|
|
|
dgraph_del_edges(G0, DupEdges, GF).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
2007-12-05 12:17:25 +00:00
|
|
|
undgraph_del_vertex(Vs0, V, Vsf) :-
|
2006-04-10 20:24:52 +01:00
|
|
|
rb_delete(Vs0, V, BackEdges, Vsi),
|
2006-04-14 02:01:55 +01:00
|
|
|
(
|
|
|
|
ord_del_element(BackEdges,V,RealBackEdges)
|
|
|
|
->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
BackEdges = RealBackEdges
|
|
|
|
),
|
2006-04-10 20:24:52 +01:00
|
|
|
rb_partial_map(Vsi, RealBackEdges, del_edge(V), Vsf).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred undgraph_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_.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2007-12-05 12:17:25 +00:00
|
|
|
undgraph_del_vertices(G0, Vs, GF) :-
|
|
|
|
sort(Vs,SortedVs),
|
|
|
|
delete_all(SortedVs, [], BackEdges, G0, GI),
|
|
|
|
ord_subtract(BackEdges, SortedVs, TrueBackEdges),
|
|
|
|
delete_remaining_edges(SortedVs, TrueBackEdges, GI, GF).
|
2006-04-10 20:24:52 +01:00
|
|
|
|
|
|
|
% it would be nice to be able to delete a set of elements from an RB tree
|
|
|
|
% but I don't how to do it yet.
|
|
|
|
delete_all([], BackEdges, BackEdges) --> [].
|
|
|
|
delete_all([V|Vs], BackEdges0, BackEdgesF, Vs0,Vsf) :-
|
|
|
|
rb_delete(Vs0, V, NewEdges, Vsi),
|
|
|
|
ord_union(NewEdges,BackEdges0,BackEdgesI),
|
|
|
|
delete_all(Vs, BackEdgesI ,BackEdgesF, Vsi,Vsf).
|
|
|
|
|
|
|
|
delete_remaining_edges(SortedVs, TrueBackEdges, Vs0,Vsf) :-
|
|
|
|
rb_partial_map(Vs0, TrueBackEdges, del_edges(SortedVs), Vsf).
|
|
|
|
|
|
|
|
del_edges(ToRemove,E0,E) :-
|
|
|
|
ord_subtract(E0,ToRemove,E).
|
|
|
|
|
2006-04-14 02:01:55 +01:00
|
|
|
del_edge(ToRemove,E0,E) :-
|
2006-04-10 20:24:52 +01:00
|
|
|
ord_del_element(E0,ToRemove,E).
|
|
|
|
|
2006-04-20 16:28:08 +01:00
|
|
|
undgraph_min_tree(G, T) :-
|
|
|
|
undgraph_to_wundgraph(G, WG),
|
|
|
|
wundgraph_min_tree(WG, WT, _),
|
|
|
|
wundgraph_to_undgraph(WT, T).
|
|
|
|
|
|
|
|
undgraph_max_tree(G, T) :-
|
|
|
|
undgraph_to_wundgraph(G, WG),
|
|
|
|
wundgraph_max_tree(WG, WT, _),
|
|
|
|
wundgraph_to_undgraph(WT, T).
|
|
|
|
|
2008-03-26 14:41:45 +00:00
|
|
|
undgraph_components(Graph,[Map|Gs]) :-
|
|
|
|
pick_node(Graph,Node,Children,Graph1), !,
|
|
|
|
undgraph_new(Map0),
|
|
|
|
rb_insert(Map0, Node, Children, Map1),
|
|
|
|
expand_component(Children, Map1, Map, Graph1, NGraph),
|
|
|
|
undgraph_components(NGraph,Gs).
|
|
|
|
undgraph_components(_,[]).
|
|
|
|
|
|
|
|
expand_component([], Map, Map, Graph, Graph).
|
|
|
|
expand_component([C|Children], Map1, Map, Graph1, NGraph) :-
|
|
|
|
rb_delete(Graph1, C, Edges, Graph2), !,
|
|
|
|
rb_insert(Map1, C, Edges, Map2),
|
|
|
|
expand_component(Children, Map2, Map3, Graph2, Graph3),
|
|
|
|
expand_component(Edges, Map3, Map, Graph3, NGraph).
|
2008-09-30 11:46:49 +01:00
|
|
|
expand_component([_|Children], Map1, Map, Graph1, NGraph) :-
|
|
|
|
expand_component(Children, Map1, Map, Graph1, NGraph).
|
2008-03-26 14:41:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
pick_node(Graph,Node,Children,Graph1) :-
|
|
|
|
rb_in(Node,Children,Graph), !,
|
|
|
|
rb_delete(Graph, Node, Graph1).
|