1#include <vector>2#include <cassert>3#include <iostream>4 using namespacestd;5 classgraph{6 Private:7 intN;8 int**graph;9 Public:TenGraph (intN) { One This->n =N; AGraph =New int*[n]; - for(intI=0; i<n;i++) -Graph[i] =New int[n]; the for(intI=0; i<n;i++) - for(intj=0; j<n;j++) -GRAPH[I][J] =0; - } + voidAddedge (intAintb) { -ASSERT (a>0&&a<=n); +ASSERT (b>0&&b<=n); A if(Connected (A-1, B-1)) at return; -graph[a-1][b-1] =1; - } - voidRemoveedge (intAintb) { -ASSERT (a>0&&a<=n); -ASSERT (b>0&&b<=n); in if(!connected (A, b)) - return; tograph[a-1][b-1] =0; + } - BOOLConnectedintAintb) { the returngraph[a][b]==1; * } $ voidPlusintA1,intB1,intA2,intB2) {Panax Notoginseng if(graph[a1][b1]==0) -graph[a1][b1]+=GRAPH[A2][B2]; the Else + return; A } the voiddisplay () { +cout<<"Matrix Form:"<<Endl; - for(intI=0; i<n;i++){ $ for(intj =0; j<n;j++) $cout<<graph[i][j]<<" "; -cout<<Endl; - } thecout<<"Front:"; - for(intI=0; i<n;i++)Wuyi for(intj=0; j<n;j++){ the if(Graph[i][j]) -cout<<"<"<<i+1<<","<<j+1<<">"; Wu } -cout<<Endl; About } $ }; - - classrelationship{ - Private: A BOOLPrint; + intnum_points; the graph graph; - Public: $Relationship (intnum): graph (num) { thePrint =true; the This->num_points =num; the } the - voidAdd_rs (intAintb) { in Graph.addedge (A, b); the } the About voidRemove_rs (intAintb) { the Graph.removeedge (A, b); the } the + voidTransfer () {//a matrix as a transitive closure operation - for(inti =0; i<num_points;i++){ the for(intj =0; j<num_points;j++){Bayi if(graph.connected (j,i)) { the for(intK =0; k<num_points;k++) the Graph.plus (j,k,i,k); - } - } the } thePrint =true; the } the - voidShow () { the Graph.display (); the } the };94 the intMain () { thecout<<"Please enter the number of elements you want:"<<Endl; the intN;98 while(cin>>N) { AboutRelationship op =relationship (n); - intnum;101cout<<"How many pairs of relationships do you want to contact? "<<Endl;102Cin>>num;103 while(num--){104cout<<"Please enter [1,"<<n<<"] Any pair of sequence pairs between"<<Endl; the intb;106Cin>>a>>b;107 Op.add_rs (A, b);108 }109cout<<"Do you want to perform a transitive closure operation? (yes or no)"<<Endl; the strings;111Cin>>s; the if(s=="Yes")113 Op.transfer (); the op.show (); the } the}
C + + realizes the relationship class of discrete mathematics and supports transitive closure operations