wgraph.pl -- Weighted graphs, with plotting function via Real

For now the emphasis is on plotting via igraph, qgraph and ggnet2, R libraries via Real.

A weighted graphs is represented as a list of From-To:W edges or Node entries for orphans.

See wgraph_plot/2.

author
- nicos angelopoulos
version
- 0.1 2015/6/12
- 0:2 2016/1/23
- 0.3 2017/3/12
- 0.4 2019/4/21
- 0.5 2019/5/8
- 0.6 2019/5/12
- 0.7 2022/12/29, added wgraph_del_vertices/3, wgraph_vertex_frequencies/2.
license
- MIT
 wgraph(+InGraph, -Graph)
wgraph(+OutF, +Graph)
 wgraph(+OutF, +Graph, +Opts)
Graph is the wgraph/1 canonical representation of InGraph. InGraph, can be either a wgraph or a csv file that has columns 'from', 'to' and 'weight'. The latter case needs pack(mtx). When Graph is given, OutF is taken to be a file to store Graph in. When Opts are given they can change the default column names of OutF.

Opts

cnm_from(From=from)
name for from column
cnm_to(To=to)
name for to column
cnm_weight(Weight=weight)
name for weight column
save(Save=false)
to save wgraph in csv file, defaults to true if Stem is given
stem(Stem=wgraph)
stem for saving
 ?- wgraph( 'wgraph_ex.csv', [a-b:1,a-c:2,b-c:3] ).
 ?- wgraph( 'wgraph_ex.csv', G ), write( graph(G) ), nl.
 ?- wgraph( File, [a-b:1,a-c:2,b-c:3], [stem(wgraph_ex)] ).
 ?- wgraph( File, [a-b:1,a-c:2,b-c:3], [stem(wgraph_ex1)] ).

Requires library(mtx) iff you need to save/restore from csv.

 wgraph_plot(+Wgraph, +Opts)
Display weighted graph Wgraph possibly from an existing layout that may include positions, colours and labels. Wgraph should be in a form accepted by wgraph/3.

Layout (see below) should be an mtx/1 matrix with at least two columns: x, y defining the positions of the nodes. In addition if columns: labels and colours are present but not given in Opts they will be used.

Graph can be an mtx/1 matrix with at least 2 columns: from and to. In addition column weight is also processed. When missing and no weights option is given, all weights are set to 1.

Opts

Ropt = Rval
= paired options are passed to the plotter call, see r_call/2
cnm_colour(Clrs=colour)
= (layout) colours column name
cnm_label(Lbs=label)
(layout) labels column name
cnm_x(Xc=x)
(layout) column name for X position coordinate
cnm_y(Yc=y)
(layout) column name for Y position coordinate
cnm_from(From=from)
(graph) column name for From column
cnm_to(To=to)
(graph) column name for To column
cnm_weight(Wc=weight)
(graph) column name for edge weight
colours(Clrs=white)
colours for the nodes - should be known to R
format(Frm=x11)
output format: pdf, x11 or none (as x11 without explicit <- x11() call). when Plotter is ggnet2 then Frm can x11 or any file extension recognised by your installation of ggsave().
labels(Lbs=[110, 111, 100, 101, 115])
labels for the nodes, _[]_ for none, false for leaving it unset
layout_call(LayG)
layout call (R/qgraph) to use for getting x,y coordinates. currently not implemented
layout_fun(LayF)
name of function in R that can generate coordinates object (see https://www.rdocumentation.org/packages/igraph/versions/1.2.6/topics/layout_) defaults to: layout_with_fr, other values include:
layout_mtx(LayM)
used if layout_call(LayG) is not present
orphan_edge_weight(OEW)
if present an edge is added for every orphan from a medianed node with this weight
plotter(Plotter=qgraph)
also known: igraph and ggnet2
save(Save=true)
to save the layout and graph, defaults to false if no stem is given
stem(Stem)
stem of the output file (def. replaces .csv to .pdf if LayM is a file- else wgraph_plot)
height(H=7)
height of the plot device
width(W=7)
width of the plot device
label_distance(LbD=0.5)
distance for vertex labels
node_size(Vz)
size of nodes, can be prop(Min,Mult)- size being proportional to label length
wgraph(Wgraph)
the weighted graph (wgraph/1)
wgraph_mtx(WgMtx)
the matrix from which to extract the graph if one is not given by Wgraph
w_threshold(Thres=1)
values below that are not ajusted for width
useDingbats(DingB=TRUE)
when format is pdf, setting this to FALSE, turns the homonym R option for pdf() function

Also see wgraph/2 options for saving the graph (save/1 and stem/1).

?- G = [1-2:50,2-3:100], assert( wg(G) ),
?- wg(M), wgraph_plot(M,[]).

?- M = [row(from,to,weight),row(1,2,50),row(2,3,100)], assert(wg0(M) ).
?- wg0(M), wgraph_plot(M,[]).

?- G = [row(from,to,weight),row(1,2,50),row(2,3,100),row(4,'','')], assert(wg1(G) ).
?- wg1(G1), wgraph_plot(G1,true).

?- G = [1-2:200,2-3:400,4], assert(wg1(G) ).
?- wg1(G1), wgraph_plot(G1,true).
?- wg1(G1), wgraph_plot(G1,orphan_edge_weight(0.1) ).
?- wg1(G1), wgraph_plot(G1,plotter(ggnet2) ).
?- G1=[1-2:200,2-3:400,4], wgraph_plot(G1, [format(svg),stem(g1),save(false),plotter(ggnet2)] ).

Produces file: g1.svg

[[../doc/images/g1.svg]]

author
- nicos angelopoulos
version
- 0.1 2014/11/21
- 0.2 2016/01/23
- 0.3 2019/04/21, added ggnet2
 wgraph_clique(+Nodes, +Wgraph)
 wgraph_clique(+Nodes, +Wgraph, -Weights, -Rgraph)
True iff Nodes form a clique in Wgraph. Currently only testing is implemented.

Rgraph is the reduced graph after removing the clique of Nodes, with Weights being the nest list of removed weights.

?- G = [a-b:1,a-c:1,a-d:1,b-c:1,b-d:1,c-d:1,d-e:1], assert( wg(G) ).
?- wg(G), wgraph_clique([a,b,c,d],G).
G = [a-b:1, a-c:1, a-d:1, b-c:1, b-d:1, c-d:1, d-e:1].

?- wg(G), wgraph_clique([a,b,c,d],G,W,R).
G = [a-b:1, a-c:1, a-d:1, b-c:1, b-d:1, c-d:1, d-e:1],
W = [[1, 1, 1], [1, 1], [1], []],
R = [d-e:1].
author
- nicos angelopoulos
version
- 0.1 2015/3/30
 wgraph_ugraph(+Wgraph, -Ugraph)
wgraph_ugraph(-Wgraph, +Ugraph)
 wgraph_ugraph(-Wgraph, +Weights, +Ugraph)
Convert between wgraph and ugraph representations.

When constructing a Wgraph, Weights can be a single atomic, or a list of atomic value and Wgraph edges (Nd1-Nd2:Wgt). If a single atomic value (also works if a singleton atomic is given), then all weights are set to this atomic. When a list given then for each Ugraph Nd1-Nd2, Nd1-Nd2:Wgt is looked for in Weights, and if found Wgt is used. Else the first atomic in the list is used. If no atomic is given in the list, default is 1. Which is also the default if no Weights is given (wgraph_ugraph(-,+).

 ?- wgraph_ugraph( [1-2:1,1-3:2,2-3:4], Ug ).
 Ug = [1-[2, 3], 2-[3], 3-[]].

 ?- wgraph_ugraph( [1-2:1,1-3:2,2-3:1,4], Ug ).
 Ug = [1-[2, 3], 2-[3], 3-[], 4-[]].
 
 ?- wgraph_ugraph( [1-2:1,1-3:2,2-3:1,4], Ug ), wgraph_ugraph( Wg, Ug ).
 Ug = [1-[2, 3], 2-[3], 3-[], 4-[]],
 Wg = [4, 1-2:1, 1-3:1, 2-3:1].

 ?-
    Org = [1-2:1,1-3:2,2-3:1,4],
    wgraph_ugraph( Org, Ug ), del_vertices( Ug, [2], Del ),
    wgraph_ugraph( New, Org, Del ).
 Org = [1-2:1, 1-3:2, 2-3:1, 4],
 Ug = [1-[2, 3], 2-[3], 3-[], 4-[]],
 Del = [1-[3], 3-[], 4-[]],
 New = [4, 1-3:2].
 
 wgraph_vertices(+Wgraph, -Vertices)
Nodes is the set of all nodes in Wgraph (wgraph/1).
 ?- wgraph_vertices( [a-b:3,b-c:4], Nodes ).
 Nodes = [a, b, c].
 ?- wgraph_vertices( [a-b:3,b-c:4,d], Nodes ).
 Nodes = [a, b, c, d].
author
- nicos angelopoulos
version
- 0.1 2014/11/13
 wgraph_adjacency(+Wgraph, +Nodes, +Rmtx)
 wgraph_adjacency(+Wgraph, +Rmtx)
Create and occupy R matrix Rmtx as the weighed adjacency matrix of Wgraph (wgraph/1). When present Nodes is used to define the shape and location of nodes in Rmtx. Else the set of N1 and N2s in Wgraph is used (see wgraph_vertices/2).
 ?- wgraph_adjacency( [a-b:2,b-c:3,c-a:1], wga ).
 ?- <- wga.
      [,1] [,2] [,3]
 [1,]    0    2    0
 [2,]    0    0    3
 [3,]    1    0    0
 true.
author
- nicos angelopoulos
version
- 0.1 2014/11/13
 wgraph_add_edges(+Graph, +Edges, -NewGraph)
Add edges to a graph. NewGraph is sorted.
?- wgraph_add_edges( [], a-b:1, One ), wgraph_add_edges( One, b-c:2, Two ), wgraph_add_edges( Three, a-c:3, Three ).
author
- nicos angelopoulos
version
- 0.1 2015/3/30
 wgraph_add_vertices(+Graph, +Verts, -NewGraph)
?- wgraph_add_vertices( [1-3:1], [2,3], New ).
New = [2, 1-3:1].
 wgraph_known_cliques_replace(+Wgraph, -Cgraph, -Fired, +Opts)
Replace edges of cliques with central nodes from which edges eminate to all present members. Non clique nodes that connect to all present nodes point to the central node, else their connections are untouched. Fired is a list of Clq-Set pairs, where Clq is a new node and Set is the set of all nodes connecting to Clq.

Opts cliques(Cliques) either a Clique-Members list, or /2 predicate holding Member, Clique relations

replace_requires(RR=all_present) i think this is min_pop_present actually

min_pop(MinPop=4) how many of the clique members have to be present for replace to take place

author
- nicos angelopoulos
version
- 0.1 2015/3/30
To be done
- complete(Comp) flag
- alternative implementations of replace_requires(RR) flag
 wgraph_del_vertices(+Graph, +Vertices, -NewGraph)
Remove all Vertices (list) and all edges referring to these Vertices from Graph to produce NewGraph.

The predicate follows name and argument conventions from del_vertices/3. However, Vertices can be a single Vertex here (non-list).

?-
     wgraph_del_vertices( [a,b-c:1,b-d:2], d, G1 ).
G1 = [a, b-c:1].


?-
     wgraph_del_vertices( [a,b-c:1,b-d:2], [c,d], G2 ).
G2 = [a].
author
- nicos angelopoulos
version
- 0:1 2021/02/05
 wgraph_vertex_frequencies(+Wgraph, -Freqs)
Edge frequencies for all nodes in Wgraph.
?-
     wgraph_vertex_frequencies( [a,b-c:1,b-d:2], Freqs1 ).
Freqs1 = [a-0, b-2, c-1, d-1].
author
- nicos angelopoulos
version
- 0:1 2021/02/05
To be done
- add argument/option to add weights rather then counts
 wgraph_vertices_collapse(+Graph, +Verts, +Collapse, -NewGraph)
Replace all Verts in Graph with a single Collapsed node to produce New Graph.

Only


?-
     wgraph_vertices_collapse( [a,b-c:1,b-d:2], [d,c], e, G1 ).
G1 = [a, b-e:2].

?-
     wgraph_vertices_collapse( [a,b-c:1,b-d:2,e], [a,e], f, G2 ).
author
- nicos angelopoulos
version
- 0:1 2021/02/05
To be done
- add rules or difference conflict resolution
 wgraph_version(-Version, -Date)
Version (Mj:Mn:Fx) and Date of publication (date(Y,M,D)).
 ?- wgraph_version( V, D ).
 V = 0:7:0
 D = date(2022, 12, 29)