15
1
Challenge description
Let's start with some definitions:
- a relation is a set of ordered pairs of elements (in this challenge, we'll be using integers)
For instance, [(1, 2), (5, 1), (-9, 12), (0, 0), (3, 2)] is a relation.
a relation is called transitive if for any two pairs of elements
(a, b)and(b, c)in this relation, a pair(a, c)is also present,[(1, 2), (2, 4), (6, 5), (1, 4)]is transitive, because it contains(1, 2)and(2, 4), but(1, 4)as well,[(7, 8), (9, 10), (15, -5)]is transitive, because there aren't any two pairs(a, b),(c, d)present such thatb=c.[(5, 9), (9, 54), (0, 0)]is not transitive, because it contains(5, 9)and(9, 54), but not(5, 54)
Given a list of pairs of integers, determine if a relation is transitive or not.
Input / output
You will be given a list of pairs of integers in any reasonable format. Consider a relation
[(1, 6), (9, 1), (6, 5), (0, 0)]
The following formats are equivalent:
[(1, 6), (9, 1), (6, 5), (0, 0)] # list of pairs (2-tuples)
[1, 9, 6, 0], [6, 1, 5, 0] # two lists [x1, x2, ..., xn] [y1, y2, ..., yn]
[[1, 6], [9, 1], [6, 5], [0, 0] # two-dimentional int array
[4, 1, 6, 9, 1, 6, 5, 0, 0] # (n, x1, y1, ..., xn, yn)
[1+6i, 9+i, 6+5i, 0+0i] # list of complex numbers
... many others, whatever best suits golfing purposes
Output: a truthy value for a transitive relation, falsy otherwise. You may assume that the input will consist of at least one pair, and that the pairs are unique.
Does the input have to be a list-like format, or can it be an adjacency--matrix-like format? – xnor – 2016-11-21T21:25:32.357
You should have a test case that is only transitive because the pairs are ordered. E.g.
(1,3) (2,1) (3,4) (1,4) (2,4). If the pairs weren't ordered, this wouldn't be transitive because(2,3)is missing. – Martin Ender – 2016-11-21T21:26:09.3901@MartinEnder I think you misinterpreted "ordered pairs". I don't think it means the pairs in an order - I think it means each pair has an order, first then second. – isaacg – 2016-11-22T01:09:37.507
@isaacg that's what I meant. In other words, my test case is only truthy because the relation isn't implicitly symmetric. – Martin Ender – 2016-11-22T05:49:40.650
Should the third test case (
[(7, 8), (9, 10), (15, -5)]) be not transitive? – wnnmaw – 2016-11-22T14:08:50.450@MartinEnder What I'm saying is it's not transitive -
(2, 1)and(1, 3)are present, and(2, 3)is not, so it's not transitive. An example of what you're saying would be(1, 2), (1, 3). – isaacg – 2016-11-22T16:01:27.807@isaacg oh, completely overlooked that one. You're right, your test case is better. – Martin Ender – 2016-11-22T16:03:23.240
@wnnmaw: No. The definition is "a relation
ris transitive if for any a, b, c inr[(a, b) in r and (b, c) in r] => (a, c) in r. Since the left side of the implication is false, the whole expression evaluates to true. – shooqie – 2016-11-22T19:02:12.880Can the truthy value depend on the relation? e.g. output the relation for true, output 0 for false? – ngenisis – 2017-01-04T13:49:04.390
@ngenisis: sure, those are considered truthy and falsy – shooqie – 2017-01-04T18:22:27.150