Skip to content

Commit

Permalink
Optimized the CLM-Miner algorithm + Added Max Node Requirement + Docu…
Browse files Browse the repository at this point in the history
…mentation

Took 1 hour 43 minutes
  • Loading branch information
flankedgonerogue committed Dec 30, 2023
1 parent aa245c1 commit 5fd5a38
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 134 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ If neither `-output` and `-history` are provided then the script pretty-prints f
8 : Failed to open output file
9 : Bad min support number
10 : Bad mfis output file
11 : Bad max nodes count
12 : Max nodes not specified
```

## Example
Expand Down
44 changes: 18 additions & 26 deletions include/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Graph
{
struct Node {
char label{};
uint occurrence{};
size_t occurrence{};

bool operator<(const Node & rhs) const {
return label < rhs.label;
Expand All @@ -27,14 +27,11 @@ class Graph
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Edge, from, to, extraNodes, occurrence);
};

int row_length;
int N;
std::list<std::string> transactions;
std::list<nlohmann::json> jsonHistory;
size_t maxNodes = 1;
size_t maxRowSize = maxNodes * maxNodes + maxNodes;
std::list<Node> nodes;
std::list<Edge> rawEdges;
std::map<std::string, int> itemsets;
std::map<char, std::vector<int>> CLM;
std::list<Edge> edges;
std::map<char, std::vector<size_t>> CLM;

/**
* \brief Checks if the edge exists, if so, increments the weight
Expand All @@ -46,21 +43,21 @@ class Graph
*/
bool incrementIfRawEdgeExists(char fromNode, char toNode, const std::list<char> &extraNodes) noexcept;

/**
* \return The number of nodes in the graph
*/
[[nodiscard]] int getNodesCount() const noexcept;

/**
* \brief Maps node to an integer postion to use for building CLM
* \param node The node to map
* \return The position of the node
*/
[[nodiscard]] int mapNodeToPosition(char node) const noexcept;
[[nodiscard]] char mapPostionToNode(int node) const noexcept;
[[nodiscard]] size_t mapNodeToPosition(char node) const noexcept;
[[nodiscard]] char mapPostionToNode(size_t node) const noexcept;

public:
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Graph, transactions, nodes, rawEdges, itemsets, CLM, jsonHistory);
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Graph, nodes, edges, CLM);

explicit Graph(const size_t maxNodes): maxNodes(maxNodes) {}
Graph() = default;

void setMaxNodes(size_t maxNodes) noexcept;

/**
* \brief Processes the passed string into the graph generating new nodes and
Expand All @@ -69,16 +66,11 @@ class Graph
void processTransaction(const std::string &str);

/**
* \brief Processes the graph data to fill the CLM
*/
void processCLM();

/**
* \brief Processes MFIs using the minSup
* \param minSup The minimum support count
* \return A list of MFIs that have support count above or equal to minSup
*/
std::list<std::string> processMFIs(int minSup);
* \brief Processes FIs above the minimum support using the CLM Miner algorithm
* \param minSup The minimum support count
* \return A list of FIs that have support count above or equal to minSup after using the CLM Miner
*/
std::list<std::string> useCLM_Miner(int minSup);

/**
* \brief Serializes all of the graph data excluding JSON history into a
Expand Down
47 changes: 35 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ int main(int argc, char **argv)
}
}

int maxNodes;
if (arguments.contains("-max-nodes"))
{
try
{
maxNodes = std::stoi(arguments["-max-nodes"]);

if (maxNodes < 1)
{
throw std::exception();
}
} catch (std::exception& _)
{
std::cout << "Max nodes is not a valid number!\n";
std::cout << _.what();

return 11;
}
} else
{
std::cout << "Max nodes not provided!\n";
return 12;
}

std::vector<std::string> transactions;
const std::string transactions_str = arguments["-transactions"];
for (size_t offset = 0; offset < transactions_str.length();)
Expand All @@ -84,7 +108,7 @@ int main(int argc, char **argv)
offset = pos + 1;
}

Graph graph;
Graph graph(maxNodes);

// Set up graph from image file if specified
if (arguments.contains("-image"))
Expand All @@ -94,6 +118,7 @@ int main(int argc, char **argv)
if (!fstream.is_open())
return 6;
graph = nlohmann::json::parse(fstream).get<Graph>();
graph.setMaxNodes(maxNodes);
}

// Process all transactions
Expand All @@ -102,9 +127,6 @@ int main(int argc, char **argv)
graph.processTransaction(transaction);
}

// Process the CLM after processing all transactions
graph.processCLM();

// Output history to file
if (arguments.contains("-history"))
{
Expand All @@ -130,29 +152,30 @@ int main(int argc, char **argv)

if (minSupport != -1)
{
if (arguments.contains("-mfis-output"))
if (arguments.contains("-fis-output"))
{
std::ofstream fstream(arguments["-mfis-output"]);
std::ofstream fstream(arguments["-fis-output"]);
if (!fstream.is_open())
return 10;

fstream << nlohmann::json(graph.processMFIs(minSupport));
fstream << nlohmann::json(graph.useCLM_Miner(minSupport));
fstream.flush();
fstream.close();
} else
{
std::cout << "No MFIs output file specified\n";
std::cout << "No FIs output file specified\n";
}
}

if (!(arguments.contains("-history") && arguments.contains("-output") && arguments.contains("-mfis-output")))
if (!(arguments.contains("-history") && arguments.contains("-output") && arguments.contains("-fis-output")))
{
const auto& FIs = graph.useCLM_Miner(minSupport);
std::cout << graph.toString();

std::cout << "MFIs:\n";
for (const auto& mfi : graph.processMFIs(minSupport))
std::cout << "FIs:\n";
for (const auto& FI : FIs)
{
std::cout << '\t' << mfi << '\n';
std::cout << '\t' << FI << '\n';
}
}

Expand Down
Loading

0 comments on commit 5fd5a38

Please sign in to comment.