Skip to content

Commit

Permalink
Corrected Major bug using std::set with pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigRazor authored Jul 6, 2021
1 parent 19c1ef1 commit 5c9025c
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 178 deletions.
61 changes: 42 additions & 19 deletions include/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <optional>
#include <iostream>
#include <limits>
#include <list>
#include <queue>
#include <string>
#include <functional>
Expand Down Expand Up @@ -464,7 +465,7 @@ namespace CXXGRAPH
class Graph
{
private:
std::set<const Edge<T> *> edgeSet;
std::list<const Edge<T> *> edgeSet;
void addElementToAdjMatrix(AdjacencyMatrix<T> &adjMatrix, const Node<T> *nodeFrom, const Node<T> *nodeTo, const Edge<T> *edge) const;
int writeToStandardFile_csv(const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const;

Expand All @@ -478,13 +479,13 @@ namespace CXXGRAPH
} InputOutputFormat;

Graph() = default;
Graph(const std::set<const Edge<T> *> &edgeSet);
Graph(const std::list<const Edge<T> *> &edgeSet);
~Graph() = default;
const std::set<const Edge<T> *> &getEdgeSet() const;
void setEdgeSet(std::set<const Edge<T> *> &edgeSet);
const std::list<const Edge<T> *> &getEdgeSet() const;
void setEdgeSet(std::list<const Edge<T> *> &edgeSet);
void addEdge(const Edge<T> &edge);
void removeEdge(unsigned long edgeId);
const std::set<const Node<T> *> getNodeSet() const;
const std::list<const Node<T> *> getNodeSet() const;
const std::optional<const Edge<T> *> getEdge(unsigned long edgeId) const;
/*This function generate a list of adjacency matrix with every element of the matrix
* contain the node where is directed the link and the Edge corrispondent to the link
Expand Down Expand Up @@ -568,27 +569,43 @@ namespace CXXGRAPH
};

template <typename T>
Graph<T>::Graph(const std::set<const Edge<T> *> &edgeSet)
Graph<T>::Graph(const std::list<const Edge<T> *> &edgeSet)
{
this->edgeSet = edgeSet;
for (auto edgeSetIt = edgeSet.begin(); edgeSetIt != edgeSet.end(); ++edgeSetIt)
{
if (std::find(this->edgeSet.begin(), this->edgeSet.end(), *edgeSetIt) == this->edgeSet.end())
{
this->edgeSet.push_back(*edgeSetIt);
}
}
}

template <typename T>
const std::set<const Edge<T> *> &Graph<T>::getEdgeSet() const
const std::list<const Edge<T> *> &Graph<T>::getEdgeSet() const
{
return edgeSet;
}

template <typename T>
void Graph<T>::setEdgeSet(std::set<const Edge<T> *> &edgeSet)
void Graph<T>::setEdgeSet(std::list<const Edge<T> *> &edgeSet)
{
this->edgeSet = edgeSet;
this->edgeSet.clear();
for (auto edgeSetIt = edgeSet.begin(); edgeSetIt != edgeSet.end(); ++edgeSetIt)
{
if (std::find(this->edgeSet.begin(), this->edgeSet.end(), *edgeSetIt) == this->edgeSet.end())
{
this->edgeSet.push_back(*edgeSetIt);
}
}
}

template <typename T>
void Graph<T>::addEdge(const Edge<T> &edge)
{
edgeSet.insert(&edge);
if (std::find(edgeSet.begin(), edgeSet.end(), &edge) == edgeSet.end())
{
edgeSet.push_back(&edge);
}
}

template <typename T>
Expand All @@ -602,13 +619,19 @@ namespace CXXGRAPH
}

template <typename T>
const std::set<const Node<T> *> Graph<T>::getNodeSet() const
const std::list<const Node<T> *> Graph<T>::getNodeSet() const
{
std::set<const Node<T> *> nodeSet;
std::list<const Node<T> *> nodeSet;
for (auto edge : edgeSet)
{
nodeSet.insert(edge->getNodePair().first);
nodeSet.insert(edge->getNodePair().second);
if (std::find(nodeSet.begin(), nodeSet.end(), edge->getNodePair().first) == nodeSet.end())
{
nodeSet.push_back(edge->getNodePair().first);
}
if (std::find(nodeSet.begin(), nodeSet.end(), edge->getNodePair().second) == nodeSet.end())
{
nodeSet.push_back(edge->getNodePair().second);
}
}
return nodeSet;
}
Expand Down Expand Up @@ -712,13 +735,13 @@ namespace CXXGRAPH
result.errorMessage = "";
result.result = INF_DOUBLE;
auto nodeSet = getNodeSet();
if (nodeSet.find(&source) == nodeSet.end())
if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end())
{
// check if source node exist in the graph
result.errorMessage = ERR_DIJ_SOURCE_NODE_NOT_IN_GRAPH;
return result;
}
if (nodeSet.find(&target) == nodeSet.end())
if (std::find(nodeSet.begin(), nodeSet.end(), &target) == nodeSet.end())
{
// check if target node exist in the graph
result.errorMessage = ERR_DIJ_TARGET_NODE_NOT_IN_GRAPH;
Expand Down Expand Up @@ -821,7 +844,7 @@ namespace CXXGRAPH
std::vector<Node<T>> visited;
auto nodeSet = getNodeSet();
//check is exist node in the graph
if (nodeSet.find(&start) == nodeSet.end())
if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end())
{
return visited;
}
Expand Down Expand Up @@ -861,7 +884,7 @@ namespace CXXGRAPH
std::vector<Node<T>> visited;
auto nodeSet = getNodeSet();
//check is exist node in the graph
if (nodeSet.find(&start) == nodeSet.end())
if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end())
{
return visited;
}
Expand Down
48 changes: 24 additions & 24 deletions test/BFSTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ TEST(BFSTest, test_1)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::UndirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
std::vector<CXXGRAPH::Node<int>> res = graph.breadth_first_search(node1);
ASSERT_EQ(res.size(), 3);
Expand All @@ -31,10 +31,10 @@ TEST(BFSTest, test_2)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
std::vector<CXXGRAPH::Node<int>> res = graph.breadth_first_search(node2);
ASSERT_EQ(res.size(), 2);
Expand All @@ -52,10 +52,10 @@ TEST(BFSTest, test_3)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::UndirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
std::vector<CXXGRAPH::Node<int>> res = graph.breadth_first_search(node2);
ASSERT_EQ(res.size(), 3);
Expand All @@ -73,10 +73,10 @@ TEST(BFSTest, test_4)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::UndirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
std::vector<CXXGRAPH::Node<int>> res = graph.breadth_first_search(node3);
ASSERT_EQ(res.size(), 3);
Expand All @@ -94,10 +94,10 @@ TEST(BFSTest, test_5)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
std::vector<CXXGRAPH::Node<int>> res = graph.breadth_first_search(node3);
ASSERT_EQ(res.size(), 1);
Expand All @@ -116,10 +116,10 @@ TEST(BFSTest, test_6)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
std::vector<CXXGRAPH::Node<int>> res = graph.breadth_first_search(node4);
ASSERT_EQ(res.size(), 0);
Expand Down
60 changes: 30 additions & 30 deletions test/CycleCheckTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ TEST(CycleCheckTest, test_1)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphDFS();
ASSERT_EQ(res, false);
Expand All @@ -27,9 +27,9 @@ TEST(CycleCheckTest, test_2)
std::pair<const CXXGRAPH::Node<int> *, const CXXGRAPH::Node<int> *> pairNode(&node1, &node2);
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphDFS();
ASSERT_EQ(res, false);
Expand All @@ -44,10 +44,10 @@ TEST(CycleCheckTest, test_3)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::UndirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphDFS();
ASSERT_EQ(res, false);
Expand All @@ -62,10 +62,10 @@ TEST(CycleCheckTest, test_4)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node3, node1, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphDFS();
ASSERT_EQ(res, true);
Expand All @@ -80,10 +80,10 @@ TEST(CycleCheckTest, test_5)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphBFS();
ASSERT_EQ(res, false);
Expand All @@ -97,9 +97,9 @@ TEST(CycleCheckTest, test_6)
std::pair<const CXXGRAPH::Node<int> *, const CXXGRAPH::Node<int> *> pairNode(&node1, &node2);
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphBFS();
ASSERT_EQ(res, false);
Expand All @@ -114,10 +114,10 @@ TEST(CycleCheckTest, test_7)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::UndirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphBFS();
ASSERT_EQ(res, false);
Expand All @@ -132,10 +132,10 @@ TEST(CycleCheckTest, test_8)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node3, node1, 6);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
bool res = graph.isCyclicDirectedGraphBFS();
ASSERT_EQ(res, true);
Expand Down
Loading

0 comments on commit 5c9025c

Please sign in to comment.