Skip to content

Commit

Permalink
Implement naive GFD validation algorithm
Browse files Browse the repository at this point in the history
GFD validation algorithm that uses VF2 algorithm to find a subgraph.
  • Loading branch information
AntonChern committed Dec 4, 2023
1 parent 68d6693 commit ef78fe1
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
128 changes: 128 additions & 0 deletions src/core/algorithms/gfd/naivegfd_validation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include "naivegfd_validation.h"

#include <iostream>
#include <set>

#include <boost/graph/vf2_sub_graph_iso.hpp>

#include "gfd.h"

namespace algos {

struct CheckCallback {
private:
const graph_t& query;
const graph_t& graph;
const std::vector<Literal> premises;
const std::vector<Literal> conclusion;
bool& res;
int& amount;

public:
CheckCallback(const graph_t& query_, const graph_t& graph_,
const std::vector<Literal>& premises_, const std::vector<Literal>& conclusion_,
bool& res_, int& amount_)
: query(query_),
graph(graph_),
premises(premises_),
conclusion(conclusion_),
res(res_),
amount(amount_) {}

template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) const {
amount++;
auto satisfied = [this, &f](const std::vector<Literal>& literals) {
for (const Literal& l : literals) {
auto fst_token = l.first;
auto snd_token = l.second;
std::string fst;
std::string snd;
if (fst_token.first == -1) {
fst = fst_token.second;
} else {
vertex_t v;
vertex_t u = boost::vertex(fst_token.first, query);
v = get(f, u);
auto attrs = graph[v].attributes;
if (attrs.find(fst_token.second) == attrs.end()) {
return false;
}
fst = attrs.at(fst_token.second);
}
if (snd_token.first == -1) {
snd = snd_token.second;
} else {
vertex_t v;
vertex_t u = boost::vertex(fst_token.first, query);
v = get(f, u);
auto attrs = graph[v].attributes;
if (attrs.find(snd_token.second) == attrs.end()) {
return false;
}
fst = attrs.at(snd_token.second);
}
if (fst != snd) {
return false;
}
}
return true;
};

if (!satisfied(premises)) {
return true;
}
if (!satisfied(conclusion)) {
res = false;
return false;
}
return true;
}
};

bool NaiveGfdValidation::Validate(const graph_t& graph, const Gfd& gfd) {
graph_t pattern = gfd.GetPattern();

struct VCompare {
const graph_t& pattern;
const graph_t& graph;

bool operator()(vertex_t fr, vertex_t to) const {
return pattern[fr].attributes.at("label") == graph[to].attributes.at("label");
}
} vcompare{pattern, graph};

struct ECompare {
const graph_t& pattern;
const graph_t& graph;

bool operator()(edge_t fr, edge_t to) const {
return pattern[fr].label == graph[to].label;
}
} ecompare{pattern, graph};

bool res = true;
int amount = 0;
CheckCallback callback(pattern, graph, gfd.GetPremises(), gfd.GetConclusion(), res, amount);

bool found = boost::vf2_subgraph_iso(
pattern, graph, callback, get(boost::vertex_index, pattern),
get(boost::vertex_index, graph), vertex_order_by_mult(pattern), ecompare, vcompare);
std::cout << "Checked embeddings: " << amount << std::endl;
if (!found) {
return true;
}
return res;
}

std::vector<Gfd> NaiveGfdValidation::GenerateSatisfiedGfds(const graph_t& graph,
const std::vector<Gfd>& gfds) {
for (auto& gfd : gfds) {
if (this->Validate(graph, gfd)) {
this->result.push_back(gfd);
}
}
return this->result;
}

} // namespace algos
21 changes: 21 additions & 0 deletions src/core/algorithms/gfd/naivegfd_validation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <vector>

#include "algorithms/algorithm.h"
#include "algorithms/gfd/gfd_handler.h"
#include "gfd.h"

namespace algos {

class NaiveGfdValidation : public GfdHandler {
private:
bool Validate(const graph_t& graph, const Gfd& gfd);

public:
std::vector<Gfd> GenerateSatisfiedGfds(const graph_t& graph, const std::vector<Gfd>& gfds);

NaiveGfdValidation() : GfdHandler(){};
NaiveGfdValidation(graph_t graph_, std::vector<Gfd> gfds_) : GfdHandler(graph_, gfds_) {}
};

} // namespace algos

0 comments on commit ef78fe1

Please sign in to comment.