/* LA: linear algebra C++ interface library Copyright (C) 2023 Jiri Pittner or This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ //this is a haphazard collection of graph theoretical algorithms I happened to need #include "graph.h" #include #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 *ncliques) { int n=adjacency.nrows(); NRVec r(n); r.clear(); G graph = adjacency; int cliquenumber=0; 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, int *ncliques); \ INSTANTIZE(char) }//namespace