-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.pl
More file actions
44 lines (37 loc) · 983 Bytes
/
graph.pl
File metadata and controls
44 lines (37 loc) · 983 Bytes
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
:- dynamic graph/1.
:- dynamic vertex/2.
:- dynamic edge/4.
% checks if graph already exists
new_graph(Graph) :-
graph(Graph),
!.
% if graph does not exist, adds to the knowledge base
new_graph(Graph) :-
assert(graph(Graph)),
!.
% checks if vertex already exists
add_vertex(Graph, Vertex) :-
nonvar(Graph),
nonvar(Vertex),
graph(Graph),
vertex(Graph, Vertex),
!.
% if vertex does not exist, adds to the knowledge base
add_vertex(Graph, Vertex) :-
assert(vertex(Graph, Vertex)),
!.
% checks if edge already exists
add_edge(Graph, Origin, Destination, Weight) :-
nonvar(Graph),
nonvar(Origin),
nonvar(Destination),
nonvar(Weight),
graph(Graph),
edge(Graph, Origin, Destination, _),
retractall(edge(Graph, Origin, Destination, _)),
add_edge(Graph, Origin, Destination, Weight),
!.
% if edge does not exist, adds to the knowledge base
add_edge(Graph, Origin, Destination, Weight) :-
assert(edge(Graph, Origin, Destination, Weight)),
!.