Title: Finding a path
1Finding a path
Let G be a graph, and A and Z two nodes in G. We
are interested in a relation path(A, Z,
G, P)
where P is an acyclic path from A to Z in G.
X
Z
A
Y
To find an acyclic path, P, between A and Z in a
graph GIf AZ then P A, otherwise find an
acyclic path, P1, from node Y to Z, and find a
path from A to Y avoiding thenodes in P1
2Finding a path (contd)
path1
X
Z
A
Y
path
To find an acyclic path, P, between A and Z in a
graph GIf AZ then P A, otherwise find an
acyclic path, P1, from node Y to Z, and find a
path from A to Y avoiding thenodes in P1
This formulation implies another relation find a
path under the restriction of avoiding some
subset of nodes (P1 above) path1(A, Path1, G,
Path).
3Finding a path (contd)
path1
X
Z
A
Y
path
- path1(A, Path1, G, Path).
- The arguments are
- A is a node,
- G is a graph,
- Path1 is a path in G,
- Path is an acyclic path in G that goes from A to
the beginning of Path1and continues along Path1
up to its end. - The relation between path and path1 is
path(A,Z,G,Path) - path1(A,Z, G, Path).
4Finding a path (contd)
path1
X
Z
A
Y
path
- path(A,Z,G,Path) - path1(A,Z, G, Path).
- The definition of path1 is recursive. The
boundary case arises when the start node of Path1
(Y in the above figure) coincides with the
start node of Path, A. - If the start nodes do not coincide then there
must be a node , X, such that - Y is adjacent to X, and
- X is not in path1, and
- path must satisfy the relation
path1(A,Xpath1, G, Path).
5Finding a path Example
path(A, Z, Graph, Path) - path1(A,
Z, Graph, Path). path1(A, APath1, Graph,
APath1). path1(A, YPath1, graph(Nodes,
Edges), Path) - member(c(X,Y),
Edges), not_member(X, Path1),
path1(A, X,YPath1, graph(Nodes, Edges),
Path). not_member(X, ). not_member(X, YL)
- not_member(X, L), X \ Y.
solve(Path) - path(a,d,graph(a,b,c,d,e,
c(a,b),c(b,c),c(d,c),c(b,d),c(d,b),c(d,a),c(a,
e),c(e,d),c(c,d)), Path).
a
e
b
d
c
graph(a,b,c,d,e, c(a,b),c(b,c),
c(d,c),c(b,d), c(d,b),c(d,a),
c(a,e),c(e,d), c(c,d))
?- solve(X). X a, b, d X a, e, d X
a, b, c, d No