From 9d51ef6fbd98d7202238993ea57f141c80d74512 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Wed, 26 Jul 2023 23:09:15 +0200 Subject: [PATCH] implemented cliques in a graph --- .gitignore | 1 + graph.cc | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++- graph.h | 17 +++++++ t.cc | 22 ++++++++- 4 files changed, 166 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 556c524..421c9a7 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,7 @@ libla.la version.cc t test +fi tX *.tar.bz2 .*.swp diff --git a/graph.cc b/graph.cc index 189fd73..419221e 100644 --- a/graph.cc +++ b/graph.cc @@ -24,7 +24,133 @@ #include #include - namespace LA { +template +NRVec neighbors(const G &adjacency, const int v) +{ +if(v<0||v>=adjacency.nrows()) laerror("invalid vertex number in neighbors"); +int n=0; +for(int i=0; i r(n); +for(int i=0; i +NRVec nonneighbors(const G &adjacency, const int v) +{ +if(v<0||v>=adjacency.nrows()) laerror("invalid vertex number in nonneighbors"); +int n=0; +for(int i=0; i r(n); +for(int i=0; i +NRVec findclique(const G &adjacency, const int v) +{ +NRVec r(0); +if(adjacency.nrows()) + { + if(v<0||v>=adjacency.nrows()) laerror("invalid vertex number in findclique"); + NRVec neighb = neighbors(adjacency,v); + NRVec nonneighb = nonneighbors(adjacency,v); + NRVec c1(0),c2(0); + if(neighb.size()) + { + G subg1 = adjacency.submatrix(neighb); + c1 = findclique(subg1); + //reindex to original graph + for(int i=0; i=c2.size()) r=c1; else r=c2; + } +return (r); +} + + +template +NRVec complement(const G &adjacency, const NRVec & vertexlist) +{ +int n=adjacency.nrows(); +NRVec occ(n); +occ.clear(); +for(int i=0; i=n) laerror("illegal vertex in complement"); + occ[vertexlist[i]]=1; + } +int m=0; +for(int i=0; i r(m); +m=0; +for(int i=0; i +NRVec cliquecover(const G &adjacency) +{ +int n=adjacency.nrows(); +NRVec r(n); r.clear(); +G graph = adjacency; +int cliquenumber=1; +int remaining=n; +NRVec cliques(0); +NRVec notincliques(n); +for(int i=0; i clique = findclique(graph); + NRVec cliqueorignum(clique.size()); + for(int i=0; i neighbors(const NRSMat &adjacency, const int v); \ +template NRVec nonneighbors(const NRSMat &adjacency, const int v); \ +template NRVec findclique(const NRSMat &adjacency, const int v); \ +template NRVec complement(const NRSMat &adjacency, const NRVec & vertexlist); \ +template NRVec cliquecover(const NRSMat &adjacency); \ + + + +INSTANTIZE(char) + }//namespace diff --git a/graph.h b/graph.h index 827416b..d8a583c 100644 --- a/graph.h +++ b/graph.h @@ -30,6 +30,23 @@ namespace LA { +//presently I am not sure what the best general class for a graph would be, so leave it purely templated algorithms +//where the class G is adjacency supporting operator() and submatrix() operations + +template +NRVec neighbors(const G &adjacency, const int v); + +template +NRVec nonneighbors(const G &adjacency, const int v); + +template +NRVec findclique(const G &adjacency, const int v=0); + +template +NRVec complement(const G &adjacency, const NRVec & vertexlist); + +template +NRVec cliquecover(const G &adjacency); diff --git a/t.cc b/t.cc index 56239f6..6f23cdf 100644 --- a/t.cc +++ b/t.cc @@ -26,6 +26,8 @@ #include "polynomial.h" #include "contfrac.h" #include "simple.h" +#include "graph.h" + using namespace std; using namespace LA_Vecmat3; @@ -1954,7 +1956,7 @@ cin >>v; cout < v,r,tmp,q,q1; cin >>q; @@ -2746,5 +2748,23 @@ if(0) laerror("test exception"); } +if(1) +{ +NRSMat adj; +cin >>adj; +NRVec clique = findclique(adj); +cout <<"First clique is "< cover = cliquecover(adj); +cout <<"Clique cover is "< p(cover.size()); +cover.sort(0,p); +cout<<"permutation to disentabgle the cliques = "< perm1(p.size()); +for(int i=0; i adjperm = adj.submatrix(perm1); +cout <<"resorted graph = "<