Big one code discrete mathematics set relation determination

The judgment of the relation between big one code and discrete mathematics set

1. Program design ideas

1. Find the transfer closure: for any vertex i and j, if the two vertices are connected but not adjacent to each other and contain the first n points as the middle nodes of the path, add the corresponding m[i][j] as 1.
2. Equivalence: if it satisfies reflexivity, symmetry and transitivity, it is an equivalence relationship.
3. Equivalence class: for the related points, output the related points: if m[i][j]==1, output each j element.
4. Partition: output equivalence class. If the same equivalence class has been output, the equivalence class of this element will not be output.

2. Problems encountered in the process of programming and debugging and their solutions

1. The repetitive output of the same equivalent class during partition readjusts the running order of the program.
2. Output redundant {} and readjust the running order of the program.
3. Check and solve some careless problems.

3. Program code and operation results

#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#define MAXE 16
using namespace std;
bool is_eql,m_saved,m_added;//sign
int x=0;//Terminator
class Relation
{
public:
    Relation();
    void clrmem();
    int Getpnum(){return pnum;}
    int Checkp(int p);
    int Read();////
    void Write();//Reading and writing
    void EnterMtx();
    void Enter();
    int CtrlZ();//Undo closure
    void ShowSet();
    void ShowMtx();
    bool Judge();//Judge relationship
    void GetClosure(bool gain[]);//Compute closure
    void Closure();//Closure menu
    void EqualClass();//Show equivalence class
    void ShowPart();//Display division
private:
    int sets[MAXE];
    bool matrix[MAXE][MAXE];//Matrix used
    bool msave[MAXE][MAXE];//Matrix of backups
    bool madd[MAXE][MAXE];//Relation added in closure operation, stored in matrix form
    int pnum;////
    int rnum;//Number of points and relationships
    int radd;
}rs1;

int ckerror()
{//Error checking
    if(cin.fail())
    {
        cin.clear();
        cin.sync();
        return 1;//Wrong
    }
    return 0;//No mistake
}

Relation::Relation()
{////Initialization
    memset(sets,0,MAXE*sizeof(int));
    memset(matrix,false,(MAXE*MAXE)*sizeof(bool));
    memset(msave,false,(MAXE*MAXE)*sizeof(bool));
    memset(madd,false,(MAXE*MAXE)*sizeof(bool));
    pnum=0,rnum=0;
}

void Relation::clrmem()
{//Empty relationship
    memset(sets,0,MAXE*sizeof(int));
    memset(matrix,false,(MAXE*MAXE)*sizeof(bool));
    pnum=0,rnum=0;
}

int Relation::Checkp(int p)
{//Check element exists
    for(int i=0;i<pnum;i++)
        if(sets[i]==p) return i;
    return -1;//No corresponding element found
}

int Relation::Read()
{//Read in
    ifstream fin("mtx.txt");
    if(!fin.is_open()) return 1;//No recorded data / not opened successfully
    clrmem();
    fin>>x>>pnum;
    for(int i=0;i<pnum;i++)
        fin>>sets[i];
    rnum=pnum*pnum;
    for(int i=0;i<pnum;i++)
        for(int j=0;j<pnum;j++)
            fin>>matrix[i][j];
    fin.close();
    return 0;//Successfully read in
}

void Relation::Write()
{//preservation
    fstream fout;
    fout.open("mtx.txt",ios::out);
    fout<<x<<'\r'<<pnum<<'\r';
    for(int i=0;i<pnum;i++)
        fout<<sets[i]<<' ';
    fout<<'\r';
    for(int i=0;i<pnum;i++)
    {
        for(int j=0;j<pnum;j++)
            fout<<matrix[i][j]<<' ';
        fout<<'\r';
    }
    fout<<"//The stored data are: end identifier, number of set elements, set, relation matrix“;
    fout.close();
}

void Relation::EnterMtx()
{//Modify relation matrix
    int xx,yy;
    cout<<"Please enter a relationship to“"<<x<<' '<<x<<""end"<<endl;
    cout<<"For example, "1 4 2 3 "<<x<<' '<<x<<""representative M={<1,4>,<2,3>}"<<endl;
    while(1)
        {
            cin>>xx>>yy;
            if(ckerror()){cout<<"Wrong input";exit(0);}
            if(xx==x&&yy==x) break;
            xx=Checkp(xx);
            yy=Checkp(yy);
            if(xx==-1||yy==-1) continue;//If the input does not exist, ignore this input -- checkp returns - 1, then it does not exist
            matrix[xx][yy]=1;
        }
    Write();
}
void Relation::Enter()
{//input
    /*Input set section*/
    clrmem();
    cout<<"Please enter elements in the collection separated by spaces to"<<x<<"end:";
    for(int i=0;i<MAXE;i++)
    {
        cin>>sets[i];
        if(ckerror()) {cout<<"Wrong input";exit(0);}
        for(int j=0;j<i;j++)
            if(sets[j]==sets[i])
            {
                sets[i--]=0;
                break;
            }
        if(sets[i]==x){pnum=i;break;}
    }
    rnum=pnum*pnum;
    ShowSet();
    /*Input matrix part*/
    EnterMtx();
    Write();//Save after input
}

int Relation::CtrlZ()
{
    memcpy(matrix,msave,(MAXE*MAXE)*sizeof(bool));
    memset(msave,false,(MAXE*MAXE)*sizeof(bool));
    m_saved=false;
    Write();
    return 1;
}

void Relation::ShowSet()
{//Show collection
    cout<<"Set as";
    if(pnum==0) {cout<<"empty set         |"<<endl;return;}
    cout<<'{';
    for(int i=0;i<pnum;i++)
    {
        cout<<sets[i];
        if(i!=pnum-1) cout<<',';
    }
    cout<<'}'<<endl;
}

void Relation::ShowMtx()
{//Display matrix
    if(pnum==0) {cout<<"No elements in collection     |"<<endl;return;}//No return
        cout<<"matrix M="<<endl;
        for(int i=0;i<pnum;i++)
        {
            for(int j=0;j<pnum;j++)
                cout<<matrix[i][j]<<' ';
            cout<<endl;
        }
}

bool Relation::Judge()
{//Nature judgment
    bool F_reflex=true,F_ireflex=true,F_symme=true,F_isymme=true,F_trans=true;
    if(pnum==0) return false;
    for(int i=0;i<pnum;i++)
        if(!matrix[i][i]) //If there is 0 on the diagonal
            F_reflex=false;
    if(F_reflex) cout<<"Reflexivity   |"<<endl;
    for(int i=0;i<pnum;i++)
        if(matrix[i][i]) //1 on the diagonal
            F_ireflex=false;
    if(F_ireflex) cout<<"Have reflexivity |"<<endl;
    for(int i=0;i<pnum;i++)
        for(int j=0;j<pnum;j++)
            if((matrix[i][j] && !matrix[j][i]))//If there is < I, J >, there is no < J, I >
                F_symme=false;
    if(F_symme) cout<<"Have symmetry   |"<<endl;
    for(int i=0;i<pnum;i++)
        for(int j=0;j<pnum;j++)
            if(matrix[i][j] && matrix[j][i] && i != j) //If < i, J > and < J, i > exist, and i is not j
                F_isymme=false;
    if(F_isymme) cout<<"Antisymmetry |"<<endl;
    for(int i=0;i<pnum;i++)
        for(int j=0;j<pnum;j++)//Ergodic matrix, for any two points in the set i,k
            if(matrix[i][j]==1)//If there is a midway point j, there is < I, j >
                for(int k=0;k<pnum;k++)
                    if(matrix[j][k]&&!matrix[i][k]) //And < J, k >, if there is no < I, k >, then there is no transitivity
                        F_trans=false;
    if(F_trans) cout<<"Transitivity   |"<<endl;
    is_eql=(F_reflex&&F_symme&&F_trans);
    if(is_eql) cout<<"Is equivalence   |"<<endl;
    return true;
}

void Relation::GetClosure(bool gain[4])
{//Compute closure
    /*Storage matrix, clear relevant data*/
    radd=0;
    m_added=false;
    m_saved=false;
    memcpy(msave,matrix,(MAXE*MAXE)*sizeof(bool));
    memset(madd,false,(MAXE*MAXE)*sizeof(bool));
    m_saved=true;
    /*Calculation part*/
    bool t;//Pass closure intermediate variable
    if(gain[0]){//Reflexive closure
    for(int i=0;i<pnum;i++)
        if(matrix[i][i]==0)
            {madd[i][i]=matrix[i][i]=1;radd++;}
    }
    if(gain[1]){//Symmetric closure
    for(int i=0;i<pnum;i++)
        for(int j=0;j<pnum;j++)
            if(matrix[i][j]==1&&matrix[j][i]==0)
                {madd[j][i]=matrix[j][i]=1;radd++;}
    }
    if(gain[2]){//Pass closure
    for(int n=0;n<pnum;n++)//If the nth vertex is included as the middle vertex
        for(int i=0;i<pnum;i++)
            for(int j=0;j<pnum;j++)//Ergodic matrix, if i,j is connected but not adjacent, and contains the nth point as the intermediate node, then add the relation < i,j >
            {
                t=matrix[i][n]*matrix[n][j];
                if(t&&!matrix[i][j]) madd[i][j]=matrix[i][j]=t;//If changed
            }
        }
}

void Relation::Closure()
{//Closure operation menu
    /*Interactive part*/
    int n;
    char asks[5];
    bool gain[4]={false};
    cout<<"1.Reflexive closure 2.Symmetric closure 3.Pass closure 4.Equivalent closure"<<endl;
    cout<<"Please enter the sequence number of the closure operation you need to do (enter multiple closures at the same time, for example, 12 is to find reflexive and symmetric closures)"<<endl;
    cin>>asks;
    ckerror();
    for(int i=0;asks[i]!='\0'&&i<5;i++)
        if(asks[i]<'1'||asks[i]>'4')
        {//debugging
            cout<<"Your input is wrong. You have returned"<<endl;
            return;
        }
    //At this point, we get an array containing only the numeric characters corresponding to the options
    for(int i=0;asks[i]!='\0'&&i<5;i++)
    {
        n=asks[i]-49;//The number is - 48, followed by - 1 as the subscript
        gain[n]=true;
    }
    if(gain[3]) memset(gain,true,3*sizeof(bool));
    GetClosure(gain);
    if(radd) m_added=true;
    Write();
    if(!m_added) return;
    cout<<"do M∪M`Operations, M`={";
    for(int i=0;i<pnum;i++)
        for(int j=0;j<pnum;j++)
            if(madd[i][j]==1) cout<<'<'<<sets[i]<<','<<sets[j]<<">,";
        cout<<'}'<<endl;
    system("pause");
}

void Relation::EqualClass()
{
    for(int i=0;i<pnum;i++)
    {
        cout<<'['<<sets[i]<<"]R={";
        for(int j=0;j<pnum;j++)
            if(matrix[i][j])cout<<sets[j]<<',';
        cout<<"}"<<endl;
    }
    system("pause");
}

void Relation::ShowPart()
{
    int temp[MAXE]={0};
    cout<<"The set is divided into{";
    for(int i=0;i<pnum;i++)
    {
        if(temp[i]==0)
        {
            cout<<'{';
            for(int j=0;j<pnum;j++)
                if(matrix[i][j]&&temp[j]==0) {cout<<sets[j]<<',';temp[j]=1;}
            cout<<"\b}";
        }
    }
    cout<<'}'<<endl;
}

void NoData()
{//initialization
    if(rs1.Read())
    {
        cout<<"Please enter the identifier to end the input:";
        cin>>x;
        ckerror();
        rs1.Enter();//If not, record
        rs1.Write();
    }
}

int Functions()
{//Show body
    int choice;
    while(1)
    {
        system("cls");
        rs1.Read();
        cout<<"The ending character is:"<<x<<endl;
        cout<<"-----------------@"<<endl;
        rs1.ShowSet();
        rs1.ShowMtx();
        if(!rs1.Getpnum()) cout<<"-----------------@"<<endl;
        if(rs1.Getpnum()) cout<<"-------------@"<<endl;
        rs1.Judge();//Here is the operation part
        if(rs1.Getpnum()) cout<<"-------------@"<<endl;
        if(is_eql) rs1.ShowPart();
        cout<<"\n Please select:"<<endl;
        cout<<"1.Reenter data"<<endl;
        cout<<"2.Reenter Terminator"<<endl;
        cout<<"3.Add relationship"<<endl;
        cout<<"4.Perform closure operation"<<endl;
        if(m_saved)cout<<"5.Undo last closure"<<endl;
        if(is_eql) cout<<"6.Show equivalence class"<<endl;
        cout<<"0.Save and exit"<<endl;
        cin>>choice;
        ckerror();
        switch(choice)
        {
        case 1:
            rs1.Enter();
            m_saved=0;
            break;
        case 2:
            cin>>x;
            ckerror();
            rs1.Write();
            break;
        case 3:
            rs1.EnterMtx();
            break;
        case 4:
            rs1.Closure();
            break;
        case 5:
            if(m_saved)
                if(rs1.CtrlZ())
                    cout<<"recovery was successful"<<endl;
            break;
        case 6:
            if(is_eql) rs1.EqualClass();
            break;
        case 0:
            rs1.Write();
            return 0;
            break;
        default:
            break;
        }
    }
}
int main()
{
    NoData();
    Functions();
    return 0;
}

Operation result of the program:

4 Summary (experience)

I find that the advantage of computers is that they can do a lot of fast and repeated calculations according to certain programs,
I also have a further understanding of discrete mathematics.

5. References

Warshell algorithm learning and Implementation: https://www.cnblogs.com/lpshou/archive/2012/04/27/2473109.html
360 Encyclopedia Warshell algorithm: https://baike.so.com/doc/5312044-5547048.html

Tags: Programming iOS

Posted on Sat, 13 Jun 2020 21:34:12 -0400 by phdatabase