-
Notifications
You must be signed in to change notification settings - Fork 17
/
dfs_1_example.pi
99 lines (81 loc) · 2.23 KB
/
dfs_1_example.pi
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
import util.
import ordset.
%%% 'import' must appear before all definitions.
/*
graph([
('02', '22', 'none'),
('12', '22', 'none'),
('22', '31', 'a001'),
('22', '72', 'none'),
('31', '41', 'none'),
('41', '52', 'a002'),
('41', '61', 'none'),
('52', '72', 'none'),
('61', '71', 'none'),
('61', '72', 'a003'),
('71', '80', '#bno'),
('72', '80', '#dro')
]).
*/
index (-,-,-)
arc('02', '22', 'none').
arc('12', '22', 'none').
arc('22', '31', 'a001').
arc('22', '72', 'none').
arc('31', '41', 'none').
arc('41', '52', 'a002').
arc('41', '61', 'none').
arc('52', '72', 'none').
arc('61', '71', 'none').
arc('61', '72', 'a003').
arc('71', '80', '#bno').
arc('72', '80', '#dro').
main ?=>
Init_Node= '02',
End_Node = '80',
L_Nodes = all_nodes(), %%%% to be used in the future
printf("\n Start Node: %w\t End Node: %w\n All Nodes: %w",Init_Node, End_Node, L_Nodes),
dfs_connected(Init_Node, End_Node, [Init_Node], [], Sol),
printf("\n Path: %w", reverse(Sol[1])),
printf("\n Edges: %w\n", reverse(Sol[2])).
main => printf("\n End of main with fail ... NO SOLUTION").
dfs_connected(Node, End_Node, Visitados, L_Arestas, Sol) ?=>
Node == End_Node,
Sol = {Visitados, L_Arestas},
printf("\n Theres is a path!"), !.
dfs_connected(Node, End_Node, Partial_Caminho, Partial_Arestas, Sol) =>
arc(Node, Prox, Aresta),
not member(Prox,Partial_Caminho),
append([Prox], Partial_Caminho, L_Visitados),
append([Aresta], Partial_Arestas, L_Arestas),
dfs_connected(Prox, End_Node, L_Visitados, L_Arestas, Sol).
all_nodes() = Set =>
L1 = findall(X, arc(X,Y,_)),
L2 = findall(Y, arc(X,Y,_)),
new_ordset(L1) = Set_1,
new_ordset(L2) = Set_2,
Set = union(Set_1, Set_2).
/*
% Executa DFS para verificar conexidade
% Verifica se todos os nós foram visitados
(Visited == Nodes ->
println("O grafo é conexo");
println("O grafo não é conexo")
).
graph(Graph) =>
Graph =
[[1, 2],
import ordse....t
[4, 8]].
data(edge,Graph,D) =>
Graph = {{1,2},
{1,3},
{1,4},
{2,3},
{2,4},
{3,4}},
D = [3,2,1,0].
connect(a, [b, j, k]).
connect(b, [a, c, p]).
connect(c, [b, d, l]).
*/