Skip to content

Instantly share code, notes, and snippets.

@RobertTalbert
Created February 13, 2015 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertTalbert/b6ab8af3fd53bf3db446 to your computer and use it in GitHub Desktop.
Save RobertTalbert/b6ab8af3fd53bf3db446 to your computer and use it in GitHub Desktop.
Relations: Problems and Proofs input-output pairs

Here are some sample input-output pairs for you to try out and compare your work on the Relations: Proofs and Programs learning module.

Option 1: Relation Properties

relation1 = [(0,0), (0,1), (1,1), (2,1), (2,2)]
pairs2dict(relation1)
{0: [0, 1], 1:[1], 2: [1, 2]}

relation2 = {0: [1, 2], 1: [3, 4], 2: [2,3], 3: [ ], 4: [ ]}
relation3 = {'Jerry': ['George', 'Elaine'], 'Elaine':['Kramer']} 

dict2pairs(relation2)
[(0,1), (0,2), (1,3), (1,4), (2,2), (2,3)]

dict2pairs(relation3)
[('Jerry', 'George'), ('Jerry', 'Elaine'), ('Elaine', 'Kramer')] 
# All these functions should work regardless of whether the relation
# uses integers, strings, or whatever. 

is_reflexive(relation1)
True

is_reflexive(relation2)
False

is_symmetric(relation1)
False

is_symmetric(relation3)
False

is_antisymmetric(relation1)
True

is_antisymmetric(relation2)
True

is_transitive(relation1)
True     # Draw the digraph to see if you can tell why. 

is_transitive(relation2)
False

Option 2: Equivalence Relations

See above for sample input-output pairs for reflexivity, symmetry, and transitivity and the definitions of relation1, relation2, and relation3.

is_equivalence_relation(relation1)
False   # Not symmetric

relation4 = {0:[0,1,2], 1:[0,1,2], 2:[0,1,2], 3:[3]}

is_equivalence_relation(relation4)
True

class_of(relation1, 0)
'Error: The relation is not an equivalence relation'

class_of(relation4, 1)
[0,1,2]

class_of(relation4, 3)
[3] 

Option 3: Partial and total orderings

See above for sample input-output pairs for reflexivity, antisymmetry, and transitivity and the definitions of relation1, relation2, and relation3.

is_partial_ordering(relation1)
True

is_partial_ordering(relation3)
False

is_minimal_element(relation1, 0)
True

is_minimal_element(relation1, 1)
False

is_minimal_element(relation1, 2)
True

is_minimal_element(relation3, 'Jerry')
'Whoa there bro -- that's not a partial order'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment