This repository has been archived by the owner on Oct 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.pl
315 lines (265 loc) · 7.94 KB
/
graph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
:- module(graph,
[ graph/1,
vertex/2,
arc/4,
new_graph/1,
delete_graph/1,
new_vertex/2,
graph_vertices/2,
list_vertices/1,
new_arc/3,
new_arc/4,
graph_arcs/2,
vertex_neighbors/3,
adjs/3,
list_arcs/1,
list_graph/1,
read_graph/2,
assert_results/2,
write_graph/2,
write_graph/3,
prepare_output/2 ]).
:- use_module(library(csv)).
:- use_module(mst).
/** <module> Graph
Libreria per la gestione di grafi.
@author Davide Costantini
@version 1.0
*/
%! graph(+G:string) is det.
%
% @arg G Nome del grafo
%
% Rappresentazione di un grafo.
:- dynamic graph/1.
%! vertex(+G:string, +V:string) is det.
%
% @arg G Nome del grafo
% @arg V Nome del vertice
%
% Rappresentazione di un vertice.
:- dynamic vertex/2.
%! arc(+G:string, +U:string, +V:string, +W:int) is det.
%
% @arg G Nome del grafo
% @arg U Nome del vertice sorgente
% @arg V Nome del vertice destinazione
% @arg K Peso dell'arco
%
% Rappresentazione di un arco.
:- dynamic arc/4.
%! new_graph(+G:string) is det.
%
% @arg G Nome del grafo
%
% True se G rappresenta un grafo. Se al momento della chiamata G non
% rappresenta un grafo allora verra' asserito un nuovo grafo.
new_graph(G) :-
graph(G),
!.
new_graph(G) :-
assert(graph(G)),
!.
%! delete_graph(+G:string) is det.
%
% @arg G Nome del grafo
%
% True se e' stato possibile ritrattare tutti i fatti relativi al grafo
% (compresi vertici, archi e fatti relativi al suo MST).
delete_graph(G) :-
not(graph(G)),
!.
delete_graph(G) :-
graph(G),
!,
retract(graph(G)),
retractall(vertex(G, _)),
retractall(arc(G, _, _, _)),
delete_mst(G).
%! new_vertex(+G:string, +V:string) is det.
%
% @arg G Nome del grafo
% @arg V Nome del vertice
%
% True se V rappresenta un vertice del grafo V. Se V non rappresenta un
% vertice del grafo V, allora verra' asserito un nuovo vertice. Se G non
% rappresenta un grafo, allora verrà creato un nuovo grafo.
new_vertex(G, V) :-
vertex(G, V),
!.
new_vertex(G, V) :-
new_graph(G),
assert(vertex(G, V)),
!.
%! graph_vertices(+G:string, -Vs:list) is det.
%
% @arg G Nome del grafo
% @arg Vs Lista dei vertici del grafo
%
% True se Vs contiene tutti i vertici relativi al grafo G.
graph_vertices(G, Vs) :-
graph(G),
findall(vertex(G, V), vertex(G, V), Vs).
%! list_vertices(+G:string) is det.
%
% @arg G Nome del grafo
%
% Stampa tutti i vertici relativi al grafo G.
list_vertices(G) :-
graph(G),
listing(vertex(G, _)).
%! new_arc(+G:string, +U:string, +V:string, +W:int) is det.
%
% @arg G Nome del grafo
% @arg U Nome del vertice sorgente
% @arg V Nome del vertice destinazione
% @arg W Peso dell'arco
%
% True se all'interno del grafo e' presente un grafo da U a V con peso W. Se
% al momento della chiamata questo arco non esiste, allora vengono asseriti
% gli archi da U a V e da V ad U. Se e' gia' presente un arco da U a V con
% peso diverso da W allora l'arco viene modificato in modo che il peso sia W.
% Se G non rappresenta un grafo, allora verrà creato un nuovo grafo.
new_arc(G, U, V, Weight) :-
arc(G, U, V, Weight),
!.
new_arc(G, U, V, Weight) :-
arc(G, U, V, _),
!,
retract(arc(G, U, V, _)),
retract(arc(G, V, U, _)),
assert(arc(G, U, V, Weight)),
assert(arc(G, V, U, Weight)).
new_arc(G, U, V, Weight) :-
not(arc(G, U, V, _)),
!,
new_vertex(G, U),
new_vertex(G, V),
assert(arc(G, U, V, Weight)),
assert(arc(G, V, U, Weight)).
%! new_arc(+G:string, +U:string, +V:string) is det.
%
% @arg G Nome del grafo
% @arg U Nome del vertice sorgente
% @arg V Nome del vertice destinazione
%
% True e' true la chiamata a new_arc/4 con W = 1.
new_arc(G, U, V) :-
new_arc(G, U, V, 1).
%! graph_arcs(+G:string, -Es:list) is det.
%
% @arg G Nome del grafo
% @arg Es Lista degli archi del grafo
%
% True se Es contiene tutti gli archi del grafo.
graph_arcs(G, Es) :-
graph(G),
findall(arc(G, U, V, W), arc(G, U, V, W), Es).
%! vertex_neighbors(+G:string, +V:string, -Ns:list) is det.
%
% @arg G Nome del grafo
% @arg V Nome del vertice destinazione
% @arg Ns Lista degli archi che congiungono V agli altri vertici
%
% True se Ns e' la lista degli archi che portano ai vertici N immediatamente
% raggiungibili da V nel grafo G.
vertex_neighbors(G, V, Ns) :-
vertex(G, V),
findall(arc(G, V, N, W), arc(G, V, N, W), Ns).
%! adjs(+G:string, +V:string, -Vs:list) is det.
%
% @arg G Nome del grafo
% @arg V Nome del vertice destinazione
% @arg Vs Lista degi vertici adiacenti a V
%
% True se Vs e' la lista dei vertici adiacenti a V nel grafo G.
adjs(G, V, Vs) :-
vertex(G, V),
findall(vertex(G, U), arc(G, V, U, _), Vs).
%! list_arcs(+G:string) is det.
%
% @arg G Nome del grafo
%
% Stampa tutti gli archi relativi al grafo G.
list_arcs(G) :-
graph(G),
listing(arc(G, _, _, _)).
%! list_graph(+G:string) is det.
%
% @arg G Nome del grafo
%
% Stampa tutti i vertici e gli archi relativi al grafo G.
list_graph(G) :-
list_vertices(G),
list_arcs(G).
%! read_graph(+G:string, +FileName:string) is det.
%
% @arg G Nome del grafo
% @arg FileName Nome del file per l'input
%
% True se e' stato possibile leggere e memorizzare nella base di conoscenza di
% Prolog gli archi relativi al grafo G contenuti in FileName.
% FileName deve essere un CSV che usa \t come separatore e che contiene
% vertice sorgente, vertice destinazione e peso dell'arco. Se al momento della
% chiamata esiste gia' un grafo G, allora verrà sovrascritto.
read_graph(G, FileName) :-
delete_graph(G),
csv_read_file(FileName, Rows, [functor(arc), arity(3), separator(0'\t)]),
new_graph(G),
maplist(assert_results(G), Rows).
%! assert_results(+G:string, +Rows:string) is det.
%
% @arg G Nome del grafo
% @arg Rows Riga letta dal file
%
% Memorizza nella base di conoscenza di Prolog i vertici e gli archi letti
% dal file.
assert_results(G, Rows) :-
Rows =.. [_, U, V, W],
new_vertex(G, U),
new_vertex(G, V),
new_arc(G, U, V, W).
%! write_graph(+G:string, +FileName:string) is det.
%
% @arg G Nome del grafo
% @arg FileName Nome del file per l'output
%
% True se e' stato possibile scrivere su FileName gli archi relativi al grafo
% G. La scrittura viene effettuata chiamando write_graph/3 con Type = graph.
write_graph(G, FileName) :-
write_graph(G, FileName, graph).
%! write_graph(+G:string, +FileName:string, +Type:string) is det.
%! write_graph(+G:list, +FileName:string, +Type:string) is det.
%
% @arg G Nome del grafo o lista degli archi del grafo
% @arg FileName Nome del file per l'output
% @arg Type Modalita' di scrittura del grafo
%
% True se e' stato possibile scrivere su FileName gli archi relativi al grafo
% G.
% Se Type = graph allora G sara' una stringa che rappresenta il nome del
% grafo.
% Se Type = edges allora G sara' una lista che conterra' la lista degli archi
% di G.
% FileName sara' un CSV che usera' \t come separatore e che conterra' vertice
% sorgente, vertice destinazione e peso dell'arco.
write_graph(G, FileName, graph) :-
atom(G),
!,
graph_arcs(G, Es),
maplist(prepare_output, Es, Rows),
csv_write_file(FileName, Rows, [separator(0'\t)]).
write_graph(G, FileName, edges) :-
compound(G),
!,
maplist(prepare_output, G, Rows),
csv_write_file(FileName, Rows, [separator(0'\t)]).
%! prepare_output(+Arc:list, -Row:string) is det.
%
% @arg Arc Rappresentazione di un arco
% @arg Row Rappresentazione di un arco senza il nome del grafo
%
% True se Row contiene la rappresentazione dell'arco senza il nome del grafo.
prepare_output(Arc, Row) :-
Arc =.. [P, _ | Gs],
Row =.. [P | Gs].