Starting from:

$30

CSE211-Homework 2 Solved

Problem 3: Relations                                                                                                                                                 

Remember that a relation R on a set A can have the properties reflexive, symmetric, anti-symmetric and transitive.

•   Reflexive: R is reflexive if (a, a) ∈ R, ∀ a ∈ A.

•   Symmetric: R is symmetric if (b, a) ∈ R whenever (a, b) ∈ R, ∀ a, b ∈ A.

•   Anti-symmetric: R is antisymmetric if ∀ a, b ∈ A, (a, b) ∈ R and (b, a) ∈ R implies that a = b.

•   Transitive: R is transitive if ∀ a, b, c ∈ A, (a, b) ∈ R and (b, c) ∈ R implies that (a, c) ∈ R.

For the details about the properties, please check the 4th lecture slide on Moodle.

As we solved the problem 3 in PS4 document - which is available on Moodle - in the problem session, we can determine any given relation if it is reflexive, symmetric, anti-symmetric, and transitive.

Write an algorithm to determine if a given relation R is reflexive, symmetric, anti-symmetric, and transitive.

Your code should meet the following requirements, standards and tasks.

•   Read the relations in the text file ”input.txt”.

•   Let R be a relation on a set A where ∃a,b ∈ A,(a,b) ∈ R. Each relation R is represented with the lines in the file:

1.    The first line says how many relations in R.

2.    The second line gives the elements of the set A.

3.    The following lines give each relation in R.

•   After determining each relation in input.txt whether it is reflexive, symmetric, anti-symmetric and transitive with your algorithm, write its result to the file which is called ”output.txt” with the following format.

•   output.txt:

1.    Start a new line with ”n” which indicates a new relation.

2.    The set of R

3.    Reflexive: Yes or No, explain the reason if No (e.g. ”(a, a) is not found”).

4.    Symmetric: Yes or No, explain the reason if No (e.g. ”(b, a) is not found whereas (a, b) is found.”)

5.    Antisymmetric: Yes or No, explain the reason if No (e.g. ”(b, a) and (a, b) are found.”)

6.    Transitive: Yes or No, explain the reason if No (e.g. ”(a, c) is not found whereas (a, b) and (b, c) are found.”)

•   An example of the output format is given in ”exampleoutput.txt”. The file has the result of the first relation in ”input.txt”.

•   When explaining why a property does not exist in the relation, one reason is enough to explain if there are more. For example, in ”exampleoutput.txt”, the relation is not symmetric because (b, a) and (e, a) are not found. Detecting one of them is enough to explain the reason.

•   Bonus (20 points): If you can explain why a property exists in the relation, it brings you bonus of 20 points.

•   Your code is responsible to provide exception and error handling. The input file may be given with a wrong information, then your code must be prepared to detect them. For instance, ”The element b of the relation (1, b) is not found in the set A = {1,2,3,4}.”.

•   You can implement your algorithm in Python, Java, C or C++.

•   Important: Put comments almost for each line of your code to describe what the line is going to do.

•   You should put your source code file (file name is problem1.{.c,.java,.py,.cpp}) and output.txt into your homework zip file (check Course Policy).

More products