index | submit | rank | book

rel-ltom – Relation, list to matrix

Write a program that converts between representations of binary relations: from a set-of-ordered-pairs representation to a matrix representation.

Relation, list to matrix

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 contain several relations R represented as a list of pairs. Each representation begins with a line containing a number n followed zero or more lines each containing a pair of numbers x and y followed by a line containing a dash -. The number n represents the size of the domain and co-domain. Each pair x and y indicates that x is related to y (xRy). Input is given so that 0 ≤ x, y < n < 100.

For each relation given in the input, output should contain a 0-1 matrix representation of the given relation followed by a blank line.

Example input

3
0 1
1 2
0 2
-
4
1 1
2 2
-

Example output

0 1 1
0 0 1
0 0 0

0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0

Scoring

try first: set-member

try also: rel-mtol

try next: rel-member

index | submit | rank | book

Copyright © 2020-2023 Rudy Matela
All rights reserved