Anyview:离散数学(第六章)

前言(用前说明)

以下所有答案皆为作者本人所做,自认为写的是一坨,仅供参考,切勿抄袭(懂得都懂),也希望各位可以写下评论给出更好的解法,谢谢。

1

#include "allinclude.h"
pCartersianSet CartesianProduct(pOriginalSet pA, pOriginalSet pB)
{  // Add your code here
   pCartersianSet pC=createNullCartersianSet();
   if(isNullOriginalSet(pA)==true||isNullOriginalSet(pB)==true)
   {
      return pC;
   }
   for(resetOriginalSet(pA);!isEndOfOriginalSet(pA);nextOriginalSetPos(pA))
   {
      for(resetOriginalSet(pB);!isEndOfOriginalSet(pB);nextOriginalSetPos(pB))
      {
         OrderedCoupleInsertToCartersianSet(pC,createOrderedCouple(getCurrentOriginalSetElem(pA),getCurrentOriginalSetElem(pB)));
      }
   }
   return pC; // This is temporary code. Modify it if necessary.
}

2

#include "allinclude.h"
boolean isBinaryRelation(pOriginalSet pA, pOriginalSet pB, pCartersianSet pC)
{  // Add your code here
    pCartersianSet pD=createNullCartersianSet();
    /*if((isNullOriginalSet(pA)==true||isNullOriginalSet(pB)==true)&&isNullCartersianSet(pC)==false)
    {
        return false;
    }*/
    for(resetOriginalSet(pA);!isEndOfOriginalSet(pA);nextOriginalSetPos(pA))
    {
        for(resetOriginalSet(pB);!isEndOfOriginalSet(pB);nextOriginalSetPos(pB))
        {
            OrderedCoupleInsertToCartersianSet(pD,createOrderedCouple(getCurrentOriginalSetElem(pA),getCurrentOriginalSetElem(pB)));
        }
    }
    for(resetCartersianSet(pC);!isEndOfCartersianSet(pC);nextCartersianSetPos(pC))
    {
        if(isInCartersianSet(pD,getCurrentCartersianSetElem(pC))==false)
        {
            return false;
        }
    }
    return true;
}

3

#include "allinclude.h"
pCartersianSet IdentityRelation(pOriginalSet pSet)
{  // Add your code here
	pCartersianSet pC=createNullCartersianSet();
   for(resetOriginalSet(pSet);!isEndOfOriginalSet(pSet);nextOriginalSetPos(pSet))
   {
      pOrderedCouple pD=createOrderedCouple(getCurrentOriginalSetElem(pSet),getCurrentOriginalSetElem(pSet));
      OrderedCoupleInsertToCartersianSet(pC,pD);
   }
   return pC; // This is temporary code. Modify it if necessary.
}

4

#include "allinclude.h"
pCartersianSet CompositeOperation(pCartersianSet pA, pCartersianSet pB)
{  // Add your code here
    pCartersianSet pC=createNullCartersianSet();//创建一个空的卡氏积集合
    for(resetCartersianSet(pA);!isEndOfCartersianSet(pA);nextCartersianSetPos(pA))//遍历第一个pA卡氏积集合
    {
        pOrderedCouple pA1=getCurrentCartersianSetElem(pA);//获取其中的序偶
        for(resetCartersianSet(pB);!isEndOfCartersianSet(pB);nextCartersianSetPos(pB))//遍历第二个pB的卡氏积集合
        {
            pOrderedCouple pB1=getCurrentCartersianSetElem(pB);//获取pB中的序偶
            if(isEqualOriginalSetElem(getSecondElemOfOrderedCouple(pA1),getFirstElemOfOrderedCouple(pB1)))//判断第一个序偶的第二个元素和第二个序偶的第二个元素是否相等
            {
                pOrderedCouple p=createOrderedCouple(getFirstElemOfOrderedCouple(pA1),getSecondElemOfOrderedCouple(pB1));//将第一个序偶的第一个元素和第二个序偶的第二个元素组成一个序偶
                if(isInCartersianSet(pC,p)==false)//判断此序偶是否已经存在于pC卡氏积集合中
                {
                    OrderedCoupleInsertToCartersianSet(pC,p);//将序偶插入卡氏积集合中
                }
            }
        }
    }
    return pC;// This is Temporary code. Modify it if necessary.
}

5

#include "allinclude.h"
pBoolMatrix CompositeOperation(pBoolMatrix pMatrixA, pBoolMatrix pMatrixB)
{  // Add your code here
    int pArow=getRow(pMatrixA),pAColum=getColumn(pMatrixA);//获取A矩阵的行数和列数
    int pBrow=getRow(pMatrixB),pBColum=getColumn(pMatrixB);//获取B矩阵的行数和列数
    if(pAColum!=pBrow)
    {
        return NULL;//两个矩阵无法进行复合
    }
    pBoolMatrix pC=createBoolMatrixRowMulColumnWith0(pArow,pBColum);//创建一个全为0的矩阵C
    for(int i=1;i<=pArow;i++)
    {
        for(int j=1;j<=pBColum;j++)
        {
            for(int k=1;k<=pAColum;k++)
            {
                if(getValue(pMatrixA,i,k)==true&&getValue(pMatrixB,k,j)==true)
                {
                    setValue(pC,i,j,true);
                }
            }
        }
    }
    return pC;
}

6

#include "allinclude.h"
pCartersianSet InverseOperation(pCartersianSet pA)
{  // Add your code here
   pCartersianSet pB=createNullCartersianSet();//创建一个空的卡氏积集合
   for(resetCartersianSet(pA);!isEndOfCartersianSet(pA);nextCartersianSetPos(pA))
   {
      pOrderedCouple p=getCurrentCartersianSetElem(pA);//获取A中的序偶
      pOrderedCouple p1=createOrderedCouple(getSecondElemOfOrderedCouple(p),getFirstElemOfOrderedCouple(p));//将序偶中的元素交换
      OrderedCoupleInsertToCartersianSet(pB,p1);//将交换后的元素插入新的卡氏积集合中
   }
   return pB;  // This is Temporary code. Modify it if necessary.
}

7

#include "allinclude.h"
pBoolMatrix InverseOperation(pBoolMatrix pMatrixA)
{  // Add your code here
    int pArow=getRow(pMatrixA),pAColumn=getColumn(pMatrixA);
    pBoolMatrix pB=createBoolMatrixRowMulColumnWith0(pAColumn,pArow);
    for(int i=1;i<=pArow;i++)
    {
        for(int j=1;j<=pArow;j++)
        {
            if(getValue(pMatrixA,i,j)==0)
            {
                setValue(pB,j,i,false);
            }
            else{
                setValue(pB,j,i,true);
            }
        }
    }
    return pB;
}

8

#include "allinclude.h"
pCartersianSet CompositeOperation(pCartersianSet pA, pCartersianSet pB)
{  // Add your code here
   pCartersianSet pC=createNullCartersianSet();//创建一个空的卡氏积集合
   for(resetCartersianSet(pA);!isEndOfCartersianSet(pA);nextCartersianSetPos(pA))//遍历第一个pA卡氏积集合
   {
      pOrderedCouple pA1=getCurrentCartersianSetElem(pA);//获取其中的序偶
      for(resetCartersianSet(pB);!isEndOfCartersianSet(pB);nextCartersianSetPos(pB))//遍历第二个pB的卡氏积集合
      {
         pOrderedCouple pB1=getCurrentCartersianSetElem(pB);//获取pB中的序偶
         if(isEqualOriginalSetElem(getSecondElemOfOrderedCouple(pA1),getFirstElemOfOrderedCouple(pB1)))//判断第一个序偶的第二个元素和第二个序偶的第二个元素是否相等
         {
            pOrderedCouple p=createOrderedCouple(getFirstElemOfOrderedCouple(pA1),getSecondElemOfOrderedCouple(pB1));//将第一个序偶的第一个元素和第二个序偶的第二个元素组成一个序偶
            if(isInCartersianSet(pC,p)==false)//判断此序偶是否已经存在于pC卡氏积集合中
            {
               OrderedCoupleInsertToCartersianSet(pC,p);//将序偶插入卡氏积集合中
            }
         }
      }
   }
   return pC;
}
pCartersianSet PowOperation(pOriginalSet pA, pCartersianSet pBinaryRelationR, int n)
{  // Add your code here
   pCartersianSet pC=createNullCartersianSet();
   pC=copyCartersianSet(pBinaryRelationR);//复制新的关系集合
   pCartersianSet pD=createNullCartersianSet();
   if(n==0)
   {
      for(resetOriginalSet(pA);!isEndOfOriginalSet(pA);nextOriginalSetPos(pA))
      {
         OrderedCoupleInsertToCartersianSet(pD,createOrderedCouple(getCurrentOriginalSetElem(pA),getCurrentOriginalSetElem(pA)));
      }
      return pD;
   }
   if(n==1)
   {
      return pC;
   }
   for(int i=2;i<=n;i++)
   {
      pD=copyCartersianSet(pC);
      pC=CompositeOperation(pD,pBinaryRelationR);
   }
   return pC;  // This is Temporary code. Modify it if necessary.
}

9

#include "allinclude.h"
pBoolMatrix CompositeOperation(pBoolMatrix pMatrixA, pBoolMatrix pMatrixB)
{  // Add your code here
    int pArow=getRow(pMatrixA),pAColum=getColumn(pMatrixA);//获取A矩阵的行数和列数
    int pBrow=getRow(pMatrixB),pBColum=getColumn(pMatrixB);//获取B矩阵的行数和列数
    if(pAColum!=pBrow)
    {
        return NULL;//两个矩阵无法进行复合
    }
    pBoolMatrix pC=createBoolMatrixRowMulColumnWith0(pArow,pBColum);//创建一个全为0的矩阵C
    for(int i=1;i<=pArow;i++)
    {
        for(int j=1;j<=pBColum;j++)
        {
            for(int k=1;k<=pAColum;k++)
            {
                if(getValue(pMatrixA,i,k)==true&&getValue(pMatrixB,k,j)==true)
                {
                    setValue(pC,i,j,true);
                }
            }
        }
    }
    return pC;
}
pBoolMatrix PowerOperation(pBoolMatrix pMatrixA, int n)
{  // Add your code here
    int row=getRow(pMatrixA),column=getColumn(pMatrixA);
    pBoolMatrix pB=createBoolMatrixRowMulColumnWith0(row,column);
    for(int i=1;i<=row;i++)
    {
        setValue(pB,i,i,true);
    }
    if(n==1)
    {
        return pMatrixA;
    }
    for(int i=1;i<=n;i++)
    {
        pB=CompositeOperation(pMatrixA,pB);
    }
    return pB;
}

10

#include "allinclude.h"
boolean IsReflexivity(pOriginalSet pA, pCartersianSet pBinaryRelationR)
{  // Add your code here
  for(resetOriginalSet(pA);!isEndOfOriginalSet(pA);nextOriginalSetPos(pA))
  {
    pOrderedCouple p=createOrderedCouple(getCurrentOriginalSetElem(pA),getCurrentOriginalSetElem(pA));
    if(!isInCartersianSet(pBinaryRelationR,p))
    {
    	return false;
    }
  }
  return true;
}

11

#include "allinclude.h"
boolean IsReflexivity(pBoolMatrix pBinaryRelationR)
{  // Add your code here
	int row=getRow(pBinaryRelationR);
    for(int i=1;i<=row;i++)
    {
        if(getValue(pBinaryRelationR,i,i)!=true)
        {
            return false;
        }
    }
    return true;
}

12

#include "allinclude.h"
boolean IsAntiReflexivity(pCartersianSet pBinaryRelationR)
{  // Add your code here
	for(resetCartersianSet(pBinaryRelationR);!isEndOfCartersianSet(pBinaryRelationR);nextCartersianSetPos(pBinaryRelationR))
    {
        pOrderedCouple p=getCurrentCartersianSetElem(pBinaryRelationR);
        if(isEqualOriginalSetElem(getFirstElemOfOrderedCouple(p),getSecondElemOfOrderedCouple(p)))
        {
            return false;
        }
    }
    return true;
}

13

#include "allinclude.h"
boolean IsAntiReflexivity(pBoolMatrix pBinaryRelationR)
{
	int row=getRow(pBinaryRelationR);
    for(int i=1;i<=row;i++)
    {
        if(getValue(pBinaryRelationR,i,i)==true)
        {
            return false;
        }
    }
    return true;
}

14

#include "allinclude.h"
Reflexivity_Type DetermineReflexivity(pOriginalSet pA, pCartersianSet pBinaryRelationR)
{  // Add your code here
    int flag=0;
    for(resetOriginalSet(pA);!isEndOfOriginalSet(pA);nextOriginalSetPos(pA))
    {
        pOrderedCouple p=createOrderedCouple(getCurrentOriginalSetElem(pA),getCurrentOriginalSetElem(pA));
        if(!isInCartersianSet(pBinaryRelationR,p))
        {
    		flag=1;
        }
    }
    if(flag==0)
    {
        return REFLEXIVITY; 
    }
    flag=0;
    for(resetCartersianSet(pBinaryRelationR);!isEndOfCartersianSet(pBinaryRelationR);nextCartersianSetPos(pBinaryRelationR))
    {
        pOrderedCouple p=getCurrentCartersianSetElem(pBinaryRelationR);
        if(isEqualOriginalSetElem(getFirstElemOfOrderedCouple(p),getSecondElemOfOrderedCouple(p)))
        {
            flag=1;
        }
    }
    if(flag==0)
    {
        return ANTI_REFLEXIVITY;
    }
    return NOT_REFLEXIVITY_AND_NOT_ANTI_REFLEXIVITY;
}

15

#include "allinclude.h"
Reflexivity_Type DetermineReflexivity(pBoolMatrix pBinaryRelationR)
{  // Add your code here
    int row=getRow(pBinaryRelationR),flag=0;
    for(int i=1;i<=row;i++)
    {
        if(getValue(pBinaryRelationR,i,i)!=true)
        {
            flag=1;
        }
    }
    if(flag==0)
    {
        return REFLEXIVITY;
    }
    for(int i=1;i<=row;i++)
    {
        if(getValue(pBinaryRelationR,i,i)==true)
        {
            return NOT_REFLEXIVITY_AND_NOT_ANTI_REFLEXIVITY;
        }
    }
    return ANTI_REFLEXIVITY;

}

16

#include "allinclude.h"
Symmetry_Type DetermineSymmetry(pCartersianSet pBinaryRelationR)
{  // Add your code here
    if(getCurrentCartersianSetElem(pBinaryRelationR)==NULL)
    {
        return SYMMETRY_AND_ANTI_SYMMETRY;;
    }
    int flag1=0,flag2=0,flag3=0,s=0;
	for(resetCartersianSet(pBinaryRelationR);!isEndOfCartersianSet(pBinaryRelationR);nextCartersianSetPos(pBinaryRelationR),s++)
	{
        pOrderedCouple p=getCurrentCartersianSetElem(pBinaryRelationR);
        pOrderedCouple q=createOrderedCouple(getSecondElemOfOrderedCouple(p),getFirstElemOfOrderedCouple(p));
        if(isInCartersianSet(pBinaryRelationR,q))
        {
            if(getSecondElemOfOrderedCouple(p)==getFirstElemOfOrderedCouple(p)){
                flag1++;
                flag2++;
                flag3++;
            }
            else{
                flag1++;
            }
        }
        else if(!isInCartersianSet(pBinaryRelationR,q))
        {
            flag2++;
        }
    }
    if(flag3==s)
    {
        return SYMMETRY_AND_ANTI_SYMMETRY;
    }
    else if(flag2==s){
        return ANTI_SYMMETRY;
    }
    else if(flag1==s)
    {
        return SYMMETRY;
    }
    else {
        return NOT_SYMMETRY_AND_NOT_ANTI_SYMMETRY;
    }
}

17

#include "allinclude.h"
Symmetry_Type DetermineSymmetry(pBoolMatrix pBinaryRelationR)
{  // Add your code here
    int flag1=0,flag2=0,flag3=0,flag=0;
    int row=getRow(pBinaryRelationR);
    for(int i=1;i<=row;i++)
    {

        if(getValue(pBinaryRelationR,i,i)==true)
        {
            flag3++;
        }
        for(int j=1;j<=row;j++)
        {
            if(getValue(pBinaryRelationR,i,j)==false)
            {
                flag++;
            }
            if((getValue(pBinaryRelationR,i,j)==false||getValue(pBinaryRelationR,j,i)==false)&&i!=j)
            {
                flag2++;
            }
            if(getValue(pBinaryRelationR,i,j)==getValue(pBinaryRelationR,j,i)&&i!=j)
            {
                flag1++;
            }
            if(i==j)
            {
                flag2++;
                flag1++;
            }
        }
    }
    if(flag==row*row)
    {
        return SYMMETRY_AND_ANTI_SYMMETRY;
    }
    if(flag3==row&&flag2==row*row&&flag1==row*row)
    {
        return SYMMETRY_AND_ANTI_SYMMETRY;
    }
    else if(flag1==row*row&&flag2!=row*row)
    {
        return SYMMETRY;
    }
    else if(flag2==row*row&&flag1!=row*row){
        return ANTI_SYMMETRY;
    }
    else {
        return NOT_SYMMETRY_AND_NOT_ANTI_SYMMETRY;
    }
}

18

#include "allinclude.h"
boolean IsTransitive(pOriginalSet pA, pCartersianSet pBinaryRelationR)
{  // Add your code here
    pCartersianSet pR=createNullCartersianSet();
    for(resetCartersianSet(pBinaryRelationR);!isEndOfCartersianSet(pBinaryRelationR);nextCartersianSetPos(pBinaryRelationR))
    {
        OrderedCoupleInsertToCartersianSet(pR,getCurrentCartersianSetElem(pBinaryRelationR));
    }
    for(resetCartersianSet(pBinaryRelationR);!isEndOfCartersianSet(pBinaryRelationR);nextCartersianSetPos(pBinaryRelationR))
    {
        pOrderedCouple pC1 = getCurrentCartersianSetElem(pBinaryRelationR);
        for(resetCartersianSet(pR);!isEndOfCartersianSet(pR);nextCartersianSetPos(pR))
        {
            pOrderedCouple pC2 = getCurrentCartersianSetElem(pR);
            if(isEqualOriginalSetElem(getSecondElemOfOrderedCouple(pC1),getFirstElemOfOrderedCouple(pC2)))
            {
                if(!isInCartersianSet(pBinaryRelationR,createOrderedCouple(getFirstElemOfOrderedCouple(pC1),getSecondElemOfOrderedCouple(pC2))))
                {
                    return false;
                }
            }
        }
    }
    return true;
}

19

#include "allinclude.h"
pCartersianSet ReflexiveClosure(pOriginalSet pA, pCartersianSet pBinaryRelationR)
{  // Add your code here
	pCartersianSet pC=createNullCartersianSet();
   for(resetOriginalSet(pA);!isEndOfOriginalSet(pA);nextOriginalSetPos(pA))
	{
		pOrderedCouple p=createOrderedCouple(getCurrentOriginalSetElem(pA),getCurrentOriginalSetElem(pA));
      if(isInCartersianSet(pBinaryRelationR,p)==false)
      {
         OrderedCoupleInsertToCartersianSet(pBinaryRelationR,p);
      }
   } 
   return pBinaryRelationR; // This is temporary code. Modify it if necessary.
}

20

#include "allinclude.h"
pBoolMatrix ReflexiveClosure(pBoolMatrix pBinaryRelationR)
{  // Add your code here
	int row=getRow(pBinaryRelationR);
    for(int i=1;i<=row;i++)
    {
        setValue(pBinaryRelationR,i,i,true);
    }
    return pBinaryRelationR;
}

21

#include "allinclude.h"
pCartersianSet SymmetricClosure(pCartersianSet pBinaryRelationR)
{  // Add your code here
   pCartersianSet pC=createNullCartersianSet();
	for(resetCartersianSet(pBinaryRelationR);!isEndOfCartersianSet(pBinaryRelationR);nextCartersianSetPos(pBinaryRelationR))
   {
      pOrderedCouple p1=getCurrentCartersianSetElem(pBinaryRelationR);
      pOrderedCouple p=createOrderedCouple(getSecondElemOfOrderedCouple(p1),getFirstElemOfOrderedCouple(p1));
      OrderedCoupleInsertToCartersianSet(pC,p1);
      if(!isInCartersianSet(pC,p)&&!isInCartersianSet(pBinaryRelationR,p))
      {
		   OrderedCoupleInsertToCartersianSet(pC,p);
      }
   }
   return pC;  // This is Temporary code. Modify it if necessary. 
}

22

#include "allinclude.h"
pBoolMatrix SymmetricClosure(pBoolMatrix pBinaryRelationR)
{  // Add your code here
    int row=getRow(pBinaryRelationR);
    for(int i=1;i<=row;i++)
    {
        for(int j=1;j<=row;j++)
        {
            if(getValue(pBinaryRelationR,i,j)==true)
            {
                setValue(pBinaryRelationR,j,i,true);
            }
        }
    }
    return pBinaryRelationR;
}

23

#include "allinclude.h"
pCartersianSet TransitiveClosure(pOriginalSet pA, pCartersianSet pBinaryRelationR)
{  // Add your code here
    pCartersianSet pR=createNullCartersianSet();
    pCartersianSet pD=createNullCartersianSet();
    for(resetCartersianSet(pBinaryRelationR);!isEndOfCartersianSet(pBinaryRelationR);nextCartersianSetPos(pBinaryRelationR))
    {
        OrderedCoupleInsertToCartersianSet(pR,getCurrentCartersianSetElem(pBinaryRelationR));
        OrderedCoupleInsertToCartersianSet(pD,getCurrentCartersianSetElem(pBinaryRelationR));
    }
    for(resetCartersianSet(pD);!isEndOfCartersianSet(pD);nextCartersianSetPos(pD))
    {
      pOrderedCouple pC1 = getCurrentCartersianSetElem(pD);
      for(resetCartersianSet(pR);!isEndOfCartersianSet(pR);nextCartersianSetPos(pR))
      {
         pOrderedCouple pC2 = getCurrentCartersianSetElem(pR);
         if(isEqualOriginalSetElem(getSecondElemOfOrderedCouple(pC1),getFirstElemOfOrderedCouple(pC2)))
         {
            pOrderedCouple pC3=createOrderedCouple(getFirstElemOfOrderedCouple(pC1),getSecondElemOfOrderedCouple(pC2));
            if(!isInCartersianSet(pD,pC3))
            {
               OrderedCoupleInsertToCartersianSet(pD,pC3);
               OrderedCoupleInsertToCartersianSet(pR,pC3);
               resetCartersianSet(pR);
               resetCartersianSet(pD);
            }
         }
      }
   }
   return pR;  // This is Temporary code. Modify it if necessary.
}

24

#include "allinclude.h"
pMatrix Warshall(pMatrix pM)
{  // Add your code here
    int i,j,a,b;
    int n=getDim(pM);
    pMatrixBase p;
    p=outToBuffer(pM);
    if(n>=1)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                a=converToInt(*(p+(i*n+j)));
                if(a==1)
                {
                    for(b=0;b<n;b++)
                    {
                        if(converToInt(*(p+(j*n+b)))==1)
                        {
                            setValue(p+(i*n+b),1);
                        }
                    }
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                a=converToInt(*(p+(i*n+j)));
                if(a==1)
                {
                    for(b=0;b<n;b++)
                    {
                        if(converToInt(*(p+(j*n+b)))==1)
                        {
                            setValue(p+(i*n+b),1);
                        }
                    }
                }
            }
        }
        return createMatrix(p,n);
    }
    return pM;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sɪʟᴇɴᴛ໊ོ5329

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值