Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bron kerbosch #467

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions include/CXXGraph/Graph/Algorithm/BronKerbosch_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/***********************************************************/
/*** ______ ____ ______ _ ***/
/*** / ___\ \/ /\ \/ / ___|_ __ __ _ _ __ | |__ ***/
/*** | | \ / \ / | _| '__/ _` | '_ \| '_ \ ***/
/*** | |___ / \ / \ |_| | | | (_| | |_) | | | | ***/
/*** \____/_/\_\/_/\_\____|_| \__,_| .__/|_| |_| ***/
/*** |_| ***/
/***********************************************************/
/*** Header-Only C++ Library for Graph ***/
/*** Representation and Algorithms ***/
/***********************************************************/
/*** Author: ZigRazor ***/
/*** E-Mail: zigrazor@gmail.com ***/
/***********************************************************/
/*** Collaboration: ----------- ***/
/***********************************************************/
/*** License: AGPL v3.0 ***/
/***********************************************************/

#ifndef __CXXGRAPH_BRONKERBOSCH_IMPL_H__
#define __CXXGRAPH_BRONKERBOSCH_IMPL_H__

#pragma once

#include "CXXGraph/Graph/Graph_decl.h"

namespace CXXGraph {

template <typename T>
const BronKerboschResult<T> Graph<T>::bron_kerbosch() const{
std::vector<T_NodeSet<T>> maximalCliques{};
BronKerboschResult<T> result;

if(!isUndirectedGraph()){
result.success = false;
result.errorMessage = ERR_DIR_GRAPH;
return result;
}

T_NodeSet<T> _X, _R, _P(getNodeSet());


std::function<void(T_NodeSet<T>,T_NodeSet<T>,T_NodeSet<T>)> rec = [&maximalCliques,&rec,this]
(T_NodeSet<T> R, T_NodeSet<T> P, T_NodeSet<T> X){
if(P.empty() && X.empty()){
maximalCliques.push_back(R);
}

auto it = P.begin();
while(it != P.end()){
const auto v = *it;
T_NodeSet<T> X2, R2(R), P2, nbd(inOutNeighbors(v));
R2.insert(v);
for(const auto &u: nbd){
if(X.count(u) > 0 && u != v){X2.insert(u);}
if(P.count(u) > 0 && u != v){P2.insert(u);}
}
rec(R2,P2,X2);
X.insert(v);
it = P.erase(it);
}
};

rec(_R,_P,_X);
result.success = true;
result.maximalCliques = maximalCliques;
return result;
}

} // namespace CXXGraph
#endif
1 change: 1 addition & 0 deletions include/CXXGraph/Graph/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "CXXGraph/Graph/Algorithm/TopologicalSort_impl.hpp"
#include "CXXGraph/Graph/Algorithm/TransitiveReduction_impl.hpp"
#include "CXXGraph/Graph/Algorithm/welshPowellColoring_impl.hpp"
#include "CXXGraph/Graph/Algorithm/BronKerbosch_impl.hpp"

// IO Operation
#include "CXXGraph/Graph/IO/IOUtility_impl.hpp"
Expand Down
4 changes: 4 additions & 0 deletions include/CXXGraph/Graph/Graph_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ class Graph {
* search.
*
*/


virtual const BronKerboschResult<T> bron_kerbosch() const;

virtual const std::vector<Node<T>> breadth_first_search(
const Node<T> &start) const;

Expand Down
9 changes: 9 additions & 0 deletions include/CXXGraph/Utility/Typedef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ struct BestFirstSearchResult_struct {
template <typename T>
using BestFirstSearchResult = BestFirstSearchResult_struct<T>;

template <typename T>
struct BronKerboschResult_struct {
bool success = false;
std::string errorMessage = "";
std::vector<std::unordered_set<shared<const Node<T>>, nodeHash<T>>> maximalCliques = {};
};
template<typename T>
using BronKerboschResult = BronKerboschResult_struct<T>;

///////////////////////////////////////////////////////////////////////////////////
// Using Definition
// ///////////////////////////////////////////////////////////////
Expand Down
147 changes: 147 additions & 0 deletions test/BronKerboschTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <memory>

#include "CXXGraph/CXXGraph.hpp"
#include "gtest/gtest.h"

// Smart pointers alias
template <typename T>
using unique = std::unique_ptr<T>;
template <typename T>
using shared = std::shared_ptr<T>;

using std::make_shared;
using std::make_unique;


TEST(BronKerboschTest, test_1) {
CXXGraph::Node<int> node0("0", 0);
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
CXXGraph::Node<int> node4("4", 4);

CXXGraph::UndirectedEdge<int> edge0(0,node0,node1);
CXXGraph::UndirectedEdge<int> edge1(0,node0,node2);
CXXGraph::UndirectedEdge<int> edge2(0,node2,node1);
CXXGraph::UndirectedEdge<int> edge3(0,node2,node3);
CXXGraph::UndirectedEdge<int> edge4(0,node2,node4);
CXXGraph::UndirectedEdge<int> edge5(0,node3,node4);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge0));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge4));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge5));

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::BronKerboschResult<int> res = graph.bron_kerbosch();

ASSERT_TRUE(res.success);
ASSERT_EQ(res.maximalCliques.size(),2);
ASSERT_EQ(res.errorMessage, "");
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node0),make_shared<CXXGraph::Node<int>>(node1),make_shared<CXXGraph::Node<int>>(node2) })!= res.maximalCliques.end());
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node2),make_shared<CXXGraph::Node<int>>(node3),make_shared<CXXGraph::Node<int>>(node4) }) != res.maximalCliques.end());
}

//Empty graph E_5
TEST(BronKerboschTest,test_2){
CXXGraph::Node<int> node0("0",0);
CXXGraph::Node<int> node1("1",1);
CXXGraph::Node<int> node2("2",2);
CXXGraph::Node<int> node3("3",3);
CXXGraph::Node<int> node4("4",4);

CXXGraph::T_EdgeSet<int> edgeSet;
CXXGraph::Graph<int> graph(edgeSet);

graph.addNode(&node0);
graph.addNode(&node1);
graph.addNode(&node2);
graph.addNode(&node3);
graph.addNode(&node4);

CXXGraph::BronKerboschResult<int> res = graph.bron_kerbosch();
ASSERT_TRUE(res.success);
ASSERT_EQ(res.maximalCliques.size(),5);
ASSERT_EQ(res.errorMessage, "");
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node0) }) != res.maximalCliques.end());
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node1) }) != res.maximalCliques.end());
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node2) }) != res.maximalCliques.end());
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node3) }) != res.maximalCliques.end());
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node4) }) != res.maximalCliques.end());

}

//Complete graph K_5
TEST(BronKerboschTest,test_3){
CXXGraph::Node<int> node0("0",0);
CXXGraph::Node<int> node1("1",1);
CXXGraph::Node<int> node2("2",2);
CXXGraph::Node<int> node3("3",3);
CXXGraph::Node<int> node4("4",4);

CXXGraph::UndirectedEdge<int> edge0(0,node0,node1);
CXXGraph::UndirectedEdge<int> edge1(1,node0,node2);
CXXGraph::UndirectedEdge<int> edge2(2,node0,node3);
CXXGraph::UndirectedEdge<int> edge3(3,node0,node4);
CXXGraph::UndirectedEdge<int> edge4(4,node1,node2);
CXXGraph::UndirectedEdge<int> edge5(5,node1,node3);
CXXGraph::UndirectedEdge<int> edge6(6,node1,node4);
CXXGraph::UndirectedEdge<int> edge7(7,node2,node3);
CXXGraph::UndirectedEdge<int> edge8(8,node2,node4);
CXXGraph::UndirectedEdge<int> edge9(9,node3,node4);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge0));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge4));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge5));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge6));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge7));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge8));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge9));

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::BronKerboschResult<int> res = graph.bron_kerbosch();

ASSERT_TRUE(res.success);
ASSERT_EQ(res.maximalCliques.size(),1);
ASSERT_EQ(res.errorMessage, "");
ASSERT_TRUE(std::find(res.maximalCliques.begin(),res.maximalCliques.end(),CXXGraph::T_NodeSet<int>{ make_shared<CXXGraph::Node<int>>(node0),make_shared<CXXGraph::Node<int>>(node1),make_shared<CXXGraph::Node<int>>(node2),make_shared<CXXGraph::Node<int>>(node3),make_shared<CXXGraph::Node<int>>(node4) }) != res.maximalCliques.end());
}


//Graph with a directed edge
TEST(BronKerboschTest,test_4){
CXXGraph::Node<int> node0("0", 0);
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
CXXGraph::Node<int> node4("4", 4);

CXXGraph::DirectedEdge<int> edge0(0,node0,node1);
CXXGraph::UndirectedEdge<int> edge1(0,node0,node2);
CXXGraph::UndirectedEdge<int> edge2(0,node2,node1);
CXXGraph::UndirectedEdge<int> edge3(0,node2,node3);
CXXGraph::UndirectedEdge<int> edge4(0,node2,node4);
CXXGraph::UndirectedEdge<int> edge5(0,node3,node4);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge0));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge4));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge5));

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::BronKerboschResult<int> res = graph.bron_kerbosch();

ASSERT_FALSE(res.success);
ASSERT_EQ(res.errorMessage,"Graph is directed");
}

1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ if(TEST)

add_test(test_bellmanford test_exe --gtest_filter=BellmanFord*)
add_test(test_boruvka test_exe --gtest_filter=Boruvka*)
add_test(test_bron_kerbosch test_exe --gtest_filter=BronKerbosch*)
add_test(test_connectivity test_exe --gtest_filter=Connectivity*)
add_test(test_euler_path test_exe --gtest_filter=EulerPath*)
add_test(test_fordfulkerson test_exe --gtest_filter=FordFulkerson*)
Expand Down
Loading