using System;
using System.Collections.Generic;
class Relation {
public bool checkEquivalence(HashSet<int> A, HashSet<Tuple<int, int>> R) {
bool transitive = checkTransitive(R);
bool symmetric = checkSymmetric(R);
bool reflexive = checkReflexive(A, R);
return (transitive && symmetric && reflexive);
}
bool checkTransitive(HashSet<Tuple<int, int>> R) {
if (R.Count == 0) {
return true;
}
Dictionary<int, HashSet<int>> tup = new Dictionary<int, HashSet<int>>();
foreach (var i in R)
{
if (!tup.ContainsKey(i.Item1))
{
HashSet<int> temp = new HashSet<int>();
temp.Add(i.Item2);
tup.Add(i.Item1, temp);
}
else {
tup[i.Item1].Add(i.Item2);
}
}
foreach (var a in tup) {
HashSet<int> all_b_in_aRb = tup[a.Key];
foreach (int b in all_b_in_aRb) {
if (tup.ContainsKey(b) && a.Key != b) {
HashSet<int> all_c_in_bRc = tup[b];
foreach (int c in all_c_in_bRc) {
if (!all_b_in_aRb.Contains(c)) {
return false;
}
}
}
}
}
return true;
}
bool checkSymmetric(HashSet<Tuple<int, int>> R) {
if (R.Count == 0) {
return true;
}
foreach (var i in R)
{
Tuple<int, int> temp = new Tuple<int, int>(i.Item2, i.Item1);
if (!R.Contains(temp)) {
return false;
}
}
return true;
}
bool checkReflexive(HashSet<int> A, HashSet<Tuple<int, int>> R) {
if (A.Count > 0 && R.Count == 0) {
return false;
}
else if (A.Count == 0) {
return true;
}
foreach (int i in A)
{
Tuple<int, int> temp = new Tuple<int, int>(i, i);
if (!R.Contains(temp)) {
return false;
}
}
return true;
}
}
public class GFG
{
static public void Main ()
{
HashSet<int> A = new HashSet<int>(){ 1, 2, 3, 4 };
HashSet<Tuple<int, int>> R = new HashSet<Tuple<int, int>>();
R.Add(new Tuple<int, int>(1, 1));
R.Add(new Tuple<int, int>(1, 3));
R.Add(new Tuple<int, int>(2, 2));
R.Add(new Tuple<int, int>(3, 3));
R.Add(new Tuple<int, int>(3, 1));
R.Add(new Tuple<int, int>(3, 4));
R.Add(new Tuple<int, int>(4, 4));
R.Add(new Tuple<int, int>(4, 3));
Relation obj = new Relation();
if (obj.checkEquivalence(A, R)) {
Console.WriteLine("Equivalence Relation");
}
else {
Console.WriteLine("Not a Equivalence Relation");
}
}
}