Tuesday, October 18, 2022
HomeWordPress DevelopmentTransitive Relation on a Set

Transitive Relation on a Set


#embrace <bits/stdc++.h>

utilizing namespace std;

  

class Relation {

public:

    bool checkTransitive(set<pair<int, int> > R)

    {

        

        if (R.measurement() == 0) {

            return true;

        }

  

        

        

        map<int, set<int> > tup;

  

        

        

        for (auto i = R.start(); i != R.finish(); i++) {

            if (tup.discover(i->first) == tup.finish()) {

                set<int> temp;

                temp.insert(i->second);

                tup.insert(

                    pair<int, set<int> >(i->first, temp));

            }

            else {

                tup.at(i->first).insert(i->second);

            }

        }

  

        for (auto a = tup.start(); a != tup.finish(); a++) {

  

            

            set<int> all_b_in_aRb = tup.at(a->first);

  

            

            for (int b : all_b_in_aRb) {

                if (tup.discover(b) != tup.finish()

                    && a->first != b) {

  

                    

                    set<int> all_c_in_bRc = tup.at(b);

  

                    

                    

                    for (int c : all_c_in_bRc) {

                        if (all_b_in_aRb.discover(c)

                            == all_b_in_aRb.finish()) {

                            return false;

                        }

                    }

                }

            }

        }

  

        

        return true;

    }

};

  

int principal()

{

    set<pair<int, int> > R;

  

    

    R.insert(make_pair(1, 1));

    R.insert(make_pair(1, 2));

    R.insert(make_pair(2, 1));

    R.insert(make_pair(2, 2));

    R.insert(make_pair(1, 3));

    R.insert(make_pair(2, 3));

    R.insert(make_pair(3, 4));

    R.insert(make_pair(1, 4));

  

    Relation obj;

  

    

    if (obj.checkTransitive(R)) {

        cout << "Transitive Relation" << endl;

    }

    else {

        cout << "Not a Transitive Relation" << endl;

    }

  

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments