index | submit | rank | book

rel-member – Relation members

Write a program that is able to read and store relations and to perform membership/relationship queries.

Relation members

In this exercise, by convention, we deal with relations over the finite set of natural numbers from 0 to n-1.

Input and output

Input will consist of several commands. Each command may be a relationship matrix declaration, a relationship list declaration or a member / relationship query.

Each matrix declaration begins with a line containing the word matrix and number n indicating the size of the domain and co-domain. This is followed by n lines with a matrix representation.

Each list declaration begins with a line containing the word list and number n. This is followed by zero or more lines with the ordered pairs of the relation followed by a line containing a dash -.

Each member query consist of the word member followed by two numbers x and y.

For each member query, your program should print whether x is related to y in the latest given relation (either by matrix or list declarations).

Example input

matrix 3
0 1 1
0 0 1
0 0 0
member 0 0
member 1 2
list 4
1 1
2 2
-
member 0 0
member 1 1

Example output

no
yes
no
yes

Scoring

try first: rel-ltom rel-mtol set-member

try next: rel-properties rel-composition

index | submit | rank | book

Copyright © 2020-2021 Rudy Matela
All rights reserved