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 Jan 21, 2024
1 parent 8880e8c commit 6a7de71
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/core/algorithms/algorithm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace algos {

using AlgorithmTypes = std::tuple<Depminer, DFD, FastFDs, FDep, Fd_mine, Pyro, Tane, FUN,
hyfd::HyFD, Aid, Apriori, metric::MetricVerifier, DataStats,
fd_verifier::FDVerifier, HyUCC, PyroUCC, cfd::FDFirstAlgorithm,
ACAlgorithm, UCCVerifier, Faida, GfdValidation, EGfdValidation>;
using AlgorithmTypes =
std::tuple<Depminer, DFD, FastFDs, FDep, Fd_mine, Pyro, Tane, FUN, hyfd::HyFD, Aid, Apriori,
metric::MetricVerifier, DataStats, fd_verifier::FDVerifier, HyUCC, PyroUCC,
cfd::FDFirstAlgorithm, ACAlgorithm, UCCVerifier, Faida, GfdValidation,
EGfdValidation, NaiveGfdValidation>;

// clang-format off
/* Enumeration of all supported non-pipeline algorithms. If you implement a new
Expand Down Expand Up @@ -67,8 +68,13 @@ BETTER_ENUM(AlgorithmType, char,
>>>>>>> Implement baseline GFD validation algorithm
=======
gfdvalid,
<<<<<<< HEAD
egfdvalid
>>>>>>> Implement efficient GFD validation algorithm
=======
egfdvalid,
naivegfdvalid
>>>>>>> Implement naive GFD validation algorithm
)
// clang-format on

Expand Down
1 change: 1 addition & 0 deletions src/core/algorithms/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
/* Graph functional dependency mining algorithms */
#include "algorithms/gfd/egfd_validation.h"
#include "algorithms/gfd/gfd_validation.h"
#include "algorithms/gfd/naivegfd_validation.h"
132 changes: 132 additions & 0 deletions src/core/algorithms/gfd/naivegfd_validation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include "naivegfd_validation.h"

#include <iostream>
#include <set>

#include <boost/graph/vf2_sub_graph_iso.hpp>

#include "gfd.h"

namespace {

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

public:
CheckCallback(graph_t const& query_, graph_t const& graph_,
std::vector<Literal> const& premises_, std::vector<Literal> const& 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](std::vector<Literal> const& 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 Validate(graph_t const& graph, Gfd const& gfd) {
graph_t pattern = gfd.GetPattern();

struct VCompare {
graph_t const& pattern;
graph_t const& 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 {
graph_t const& pattern;
graph_t const& 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;
}

} // namespace

namespace algos {

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

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

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

namespace algos {

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

NaiveGfdValidation() : GfdHandler(){};

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

} // namespace algos

0 comments on commit 6a7de71

Please sign in to comment.