php.cpp
Go to the documentation of this file.
00001 
00006 #include <vector>
00007 #include <string>
00008 #include <fstream>
00009 #include <iostream>
00010 #include <stdlib.h>
00011 #include <time.h>
00012 
00013 using namespace std;
00014 
00015 #include "../cnflib/formula.h"
00016 #include "../cnflib/clause.h"
00017 #include "../cnflib/variableset.h"
00018 #include "../graphlib/bipartitegraph.h"
00019 #include "../graphlib/vertex.h"
00020 
00021 
00030 unsigned int string2uint(std::string str)
00031 {
00032         std::istringstream buffer(str);
00033         unsigned int number;
00034         buffer >> number;
00035         return number;
00036 }
00037 
00038 
00039 
00050 void pigeonAxiom(unsigned int i,
00051         BipartiteGraph G,
00052         Formula * form,
00053         VariableSet * vars)
00054 {
00055         vector<int> clauseVar;
00056         for (unsigned int j=0; j<G.leftVertex(i).order(); j++)
00057                 clauseVar.push_back(  vars->getVarCode\
00058                         (i, G.leftVertex(i).getNeighbours()[j] )  );
00059         Clause pigeon(clauseVar);
00060         form->addClause(pigeon);
00061 }
00062 
00063 
00064 
00065 
00076 void holeAxiom(unsigned int j,
00077         BipartiteGraph G,
00078         Formula * form,
00079         VariableSet * vars)
00080 {
00081         vector<int> clauseVar;
00082         for (unsigned int i1=0; i1<G.rightVertex(j).order(); i1++)
00083                 for (unsigned int i2=i1+1; i2<G.rightVertex(j).order(); i2++)
00084                 {
00085                         clauseVar.clear();
00086                         clauseVar.push_back(  (-1)*vars->\
                                getVarCode(G.rightVertex(j).getNeighbours()[i1], j)  );
00087                         clauseVar.push_back(  (-1)*vars->\
                                getVarCode(G.rightVertex(j).getNeighbours()[i2], j)  );
00088                         Clause hole(clauseVar);
00089                         form->addClause(hole);
00090                 }
00091 }
00092 
00093 
00100 void pigeonHolePrinciple(BipartiteGraph G)
00101 {
00102         string comment ("CNF formula encoding the pigeon principle");
00103         unsigned int m = G.getNleftVertices();
00104         unsigned int n = G.getNrightVertices();
00105         VariableSet vars(m,n);
00106         Formula form(vars);
00107         for (unsigned int i=0; i<m; i++)
00108                 pigeonAxiom(i,G,&form,&vars);
00109         for (unsigned int j=0; j<n; j++)
00110                 holeAxiom(j,G,&form,&vars);
00111         form.toFile(&cout,comment);
00112 }
00113 
00125 void randomNeighbours(unsigned int leftOrder,
00126         unsigned int n)
00127 {
00128         cout<<n+1<<" "<<n;
00129         srand(time(NULL));
00130         for (unsigned int u=1; u<=n+1; u++)
00131         {
00132                 cout<<endl<<u<<":";
00133                 vector<int> neighbours;
00134                 for (unsigned int i=0; i<leftOrder; i++)
00135                 {
00136                         bool ok=false;
00137                         while (!ok)
00138                         // We make sure we don't pick twice the same vertex
00139                         {
00140                                 int rand_index = rand()%n + 1;
00141                                 ok = true;
00142                                 for (unsigned int j=0; j<neighbours.size(); j++)
00143                                         if (rand_index == neighbours[j])
00144                                                 ok = false;
00145                                 if (ok)
00146                                         neighbours.push_back(rand_index);
00147                         }
00148                 }
00149                 for (unsigned int i=0; i<neighbours.size()-1; i++)
00150                         cout<<neighbours[i]<<" ";
00151                 cout<<neighbours[neighbours.size()-1];
00152         }
00153 }
00154 
00163 int main(int argc, char ** argv)
00164 {
00165         BipartiteGraph * G;
00166         if (argc >=4)
00167         {
00168                 string inArg (argv[1]);
00169                 if (inArg.compare("--generate")==0)
00170                 // Outputs the complete graph for n, m as given in the command
00171                 // line arguments
00172                 {
00173                         unsigned int m = (unsigned int)str2num(string (argv[2]) );
00174                         unsigned int n = (unsigned int)str2num(string (argv[3]) );
00175                         cout<<m<<" "<<n;
00176                         for (unsigned int i=0; i<m; i++)
00177                         {
00178                                 cout<<endl<<i+1<<":";
00179                                 for (unsigned int j=0; j<n; j++)
00180                                         cout<<j+1<<" ";
00181                         }
00182                 }
00183                 else if (inArg.compare("--generate-random")==0)
00184                 // Outputs the random graph with n vertices and of left degree d
00185                 // as given in the command line arguments
00186                 {
00187                         unsigned int n = (unsigned int)str2num(string (argv[2]) );
00188                         unsigned int d = (unsigned int)str2num(string (argv[3]) );
00189                         randomNeighbours(d,n);
00190                 }
00191         }
00192         else if (argc >= 2)
00193         // reading graph from file
00194         {
00195                 string inArg (argv[1]);
00196                 ifstream fileIn(inArg.c_str());
00197                 BipartiteGraph G(&fileIn);
00198                 pigeonHolePrinciple(G);
00199         }
00200         else
00201         // reading graph from standard inArg
00202         {
00203                 BipartiteGraph G(&cin);
00204                 pigeonHolePrinciple(G);
00205         }
00206         cout<<endl;
00207 
00208         return 0;
00209 }
00210 
 All Files Functions