0

Consider the relations: edge(X,Y), Red(X,Y), Blue(X,Y). These relations are representing a graph whose edges can be colored red or blue(or no color).

provide safe datalog rules(with negation if necessary) for the following queries.

Q1. find the pairs of nodes X and Y where there is path (a sequence of linked edges) from X to Y ?

my attempt:- reachable(X,Y) :- edge(X,Y)

reachable(X,Y) :- edge(X,Z), reachable(Z,Y)

Q2. Find the pairs of nodes X and Y where there is path (a sequence of linked edges) of even length from X to Y with alternating red and blue colors?

my attempt:

I have made odd even datalog program and a red/blue program, but don't know how to combine the both to get even length alternate red/blue node

ODD(x,y):- edge(x,y)

ODD(x,y):- edge(x,z), EVEN(z,y)
EVEN(x,y):- edge(x,z), ODD(z,y).

path(X,Y) :- Red(X,Y)

path(X,Y) :- path(X,Z), Blue(Z,W), path(W,Y)

Katie
  • 452
  • 7
  • 18

1 Answers1

1

From your questions I get the impression that you're trying to answer these assignments without testing your potential solutions. That's a very difficult way to work. I would strongly suggest to make little examples to test whether your solution is correct and run it in a Datalog engine (easiest is something online like http://www.iris-reasoner.org/demo or https://developer.logicblox.com/playground/ ).

In this particular case, it is easy to see that if you have an edge("a", b"), edge("b", c") and edge("c", "d"), that your solution will not find a path from "a" to "d".

This query can only be solved with recursion. This path query is pretty basic recursion, search for ancestor or transitive closure.

  • Martin: thank you for the guidance, I will use these pages to test my solutions. Also I have modified the question and shared my attempt, but stuck in Q2. – Katie Sep 28 '16 at 05:42
  • could you plz have a look at my attempt – Katie Sep 29 '16 at 03:26