#include #include #include #include #include #include using namespace std; struct Edge { int i; int j; }; struct Side { int i; int j; int k; }; class Graph { protected: int Nn; int En; int Sn; Edge *E; Side *S; void randomE(); void randomS(); public: Graph(int Nn_, int En_, int Sn_); ~Graph(); void prG(char* fname); void saveG(); double degree_variance(); void kring_half(int s1, int s2); void kring_sides(int s1, int s2, int s3); void torus_sides(); void prGtorus(); }; class Receive: public Graph { int *nodes; int *edges; int *sides; int *packets; int pn; int newp(); FILE * ps; FILE * eps; char fout[256]; static const double di_R = 200; static const double di_r = 5; static const double di_margin = 1; int exploreE(); int exploreS(); void fprR(FILE *f); protected: int Pn; public: Receive(int Nn_, int En_, int Sn_); ~Receive(); void init(); int rx(); void propen(char *fname); void prR(); void prclose(); int decode(); }; class Rate: public Receive { int excess; int *succ; int passes; double perf; void compperf(); public: Rate(int Nn_, int En_, int Sn_); ~Rate(); void rate_and_draw(int T, char* fname); void rate(int T); void prsucc(char *fname); double getperf(); void saveChart(); }; void randseed(); void subset(int *S, int N, int n); main() { randseed(); Rate* R; const int Nn=18; const int Sn=2*Nn; const int En=0; R=new Rate(Nn,En,Sn); R->torus_sides(); R->rate(10000); cout << "perf = " << R->getperf() << endl; cout << "degree variance = " << R->degree_variance() << endl; R->saveG(); R->prGtorus(); R->saveChart(); delete R; } void Graph::prGtorus() { if( !(En==0 && Sn==2*Nn) ) return; int n=(int)sqrt((double)Nn/2); if( 2*n*n != Nn ) return; const double R = 20; const double r = 4; const double margin = 1; double x[Nn]; double y[Nn]; double x0=0; double y0=n*R; double x1=R*sin(M_PI/3); double y1=R/2+n*R; double dy=R; double dx=R*2*sin(M_PI/3); double he = y1; double wi = dx*n; for(int i=0;i mkpdf.log", 2*(di_R+di_r+di_margin),2*(di_R+di_r+di_margin),fout,fout); system(mkgif); system(mkpdf); } void Receive::prR() { fprR(ps); fprR(eps); } void Receive::fprR(FILE *f) { const float width1=0.6; const float graye1=0.4; const float grayl1=0.9; const float grayf1=0.98; const float width2=1.8; const float graye2=0; const float grayl2=0.8; const float grayf2=0.92; double x[Nn]; double y[Nn]; for(int i=0;i=Pn) return -1; int i = rand()%(Pn-pn); int j = -1; for(;i>=0;i--) { j++; for(;packets[j];j++) ; } packets[j]=1; pn++; return j; } void Receive::init() { for(int i=0;i-1;i++) for(int j=i+1;j-1;j++) for(int k=j+1;k-1;k++,n--) if(n==0) { S[l].i=i; S[l].j=j; S[l].k=k; l++; } } } void Graph::randomE() { const int K = Nn*(Nn-1)/2; int Set[K]; subset(Set,K,En); int l=0; for(int m=0;m-1;i++) for(int j=i+1;j-1;j++,n--) { if(n==0) { E[l].i=i; E[l].j=j; l++; } } } } Graph::Graph(int Nn_, int En_, int Sn_) { Nn=Nn_; En=En_; Sn=Sn_; /* cout << "constructing a Graph with " << Nn << " nodes, " << En << " edges and " << Sn << " sides" << endl; */ E=new Edge[En]; S=new Side[Sn]; randomE(); randomS(); } Graph::~Graph() { //cout << "destructing the Graph" << endl; delete[] E; delete[] S; } void subset(int *Set, int N, int n) { for(int i=0;i=0;i--) { j++; for(;Set[j];j++) ; } Set[j]=1; } } void randseed() { int ti=time(0); printf("srand=%d\r\n",ti); srand(ti); } void Graph::prG(char* fname) { const double R = 70; const double r = 4; const double margin = 1; double x[Nn]; double y[Nn]; for(int i=0;i