Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 25 additions & 25 deletions include/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ namespace CXXGRAPH
result.success = false;
result.errorMessage = "";
result.result = INF_DOUBLE;
auto nodeSet = getNodeSet();
auto nodeSet = Graph<T>::getNodeSet();
if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end())
{
// check if source node exist in the graph
Expand All @@ -911,7 +911,7 @@ namespace CXXGRAPH
result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH;
return result;
}
const AdjacencyMatrix<T> adj = getAdjMatrix();
const AdjacencyMatrix<T> adj = Graph<T>::getAdjMatrix();
// n denotes the number of vertices in graph
int n = adj.size();

Expand Down Expand Up @@ -1008,7 +1008,7 @@ namespace CXXGRAPH
result.success = false;
result.errorMessage = "";
result.result = INF_DOUBLE;
auto nodeSet = getNodeSet();
auto nodeSet = Graph<T>::getNodeSet();
if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end())
{
// check if source node exist in the graph
Expand Down Expand Up @@ -1039,7 +1039,7 @@ namespace CXXGRAPH
// outer loop for vertex relaxation
for (int i=0; i<n-1; i++)
{
auto edgeSet = this->getEdgeSet();
auto edgeSet = Graph<T>::getEdgeSet();
// inner loop for distance updates of
// each relaxation
for (auto edge : edgeSet)
Expand Down Expand Up @@ -1079,7 +1079,7 @@ namespace CXXGRAPH
// check if there exists a negative cycle
if (!earlyStopping)
{
auto edgeSet = this->getEdgeSet();
auto edgeSet = Graph<T>::getEdgeSet();
for (auto edge : edgeSet)
{
auto elem = edge->getNodePair();
Expand Down Expand Up @@ -1114,7 +1114,7 @@ namespace CXXGRAPH
result.success = false;
result.errorMessage = "";
std::map<std::pair<unsigned long, unsigned long>, double> pairwise_dist;
auto nodeSet = getNodeSet();
auto nodeSet = Graph<T>::getNodeSet();
// create a pairwise distance matrix with distance node distances
// set to inf. Distance of node to itself is set as 0.
for (auto elem1 : nodeSet)
Expand All @@ -1129,7 +1129,7 @@ namespace CXXGRAPH
}
}

auto edgeSet = this->getEdgeSet();
auto edgeSet = Graph<T>::getEdgeSet();
// update the weights of nodes
// connected by edges
for (auto edge : edgeSet)
Expand Down Expand Up @@ -1199,9 +1199,9 @@ namespace CXXGRAPH
result.errorMessage = ERR_DIR_GRAPH;
return result;
}
auto nodeSet = getNodeSet();
auto nodeSet = Graph<T>::getNodeSet();
auto n = nodeSet.size();
const AdjacencyMatrix<T> adj = getAdjMatrix();
const AdjacencyMatrix<T> adj = Graph<T>::getAdjMatrix();

// setting all the distances initially to INF_DOUBLE
std::map<const Node<T> *, double> dist;
Expand Down Expand Up @@ -1273,13 +1273,13 @@ namespace CXXGRAPH
{
// vector to keep track of visited nodes
std::vector<Node<T>> visited;
auto nodeSet = getNodeSet();
auto nodeSet = Graph<T>::getNodeSet();
//check is exist node in the graph
if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end())
{
return visited;
}
const AdjacencyMatrix<T> adj = getAdjMatrix();
const AdjacencyMatrix<T> adj = Graph<T>::getAdjMatrix();
// queue that stores vertices that need to be further explored
std::queue<const Node<T> *> tracker;

Expand Down Expand Up @@ -1313,13 +1313,13 @@ namespace CXXGRAPH
{
// vector to keep track of visited nodes
std::vector<Node<T>> visited;
auto nodeSet = getNodeSet();
auto nodeSet = Graph<T>::getNodeSet();
//check is exist node in the graph
if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end())
{
return visited;
}
const AdjacencyMatrix<T> adj = getAdjMatrix();
const AdjacencyMatrix<T> adj = Graph<T>::getAdjMatrix();
std::function<void(const AdjacencyMatrix<T> &, const Node<T> &, std::vector<Node<T>> &)> explore;
explore = [&explore](const AdjacencyMatrix<T> &adj, const Node<T> &node, std::vector<Node<T>> &visited) -> void
{
Expand Down Expand Up @@ -1353,8 +1353,8 @@ namespace CXXGRAPH
in_stack,
visited
};
auto nodeSet = this->getNodeSet();
auto adjMatrix = this->getAdjMatrix();
auto nodeSet = Graph<T>::getNodeSet();
auto adjMatrix = Graph<T>::getAdjMatrix();

/* State of the node.
*
Expand Down Expand Up @@ -1437,8 +1437,8 @@ namespace CXXGRAPH
{
return false;
}
auto adjMatrix = this->getAdjMatrix();
auto nodeSet = this->getNodeSet();
auto adjMatrix = Graph<T>::getAdjMatrix();
auto nodeSet = Graph<T>::getNodeSet();

std::map<unsigned long, unsigned int> indegree;
for (auto node : nodeSet)
Expand Down Expand Up @@ -1467,7 +1467,7 @@ namespace CXXGRAPH
}

// Vertices that need to be traversed.
auto remain = this->getNodeSet().size();
auto remain = Graph<T>::getNodeSet().size();
// While there are safe nodes that we can visit.
while (!can_be_solved.empty())
{
Expand Down Expand Up @@ -1502,7 +1502,7 @@ namespace CXXGRAPH
template <typename T>
bool Graph<T>::isDirectedGraph() const
{
auto edgeSet = this->getEdgeSet();
auto edgeSet = Graph<T>::getEdgeSet();
for (auto edge : edgeSet)
{
if (!(edge->isDirected().has_value() && edge->isDirected().value()))
Expand All @@ -1518,7 +1518,7 @@ namespace CXXGRAPH
template <typename T>
bool Graph<T>::isUndirectedGraph() const
{
auto edgeSet = this->getEdgeSet();
auto edgeSet = Graph<T>::getEdgeSet();
for (auto edge : edgeSet)
{
if ((edge->isDirected().has_value() && edge->isDirected().value()))
Expand All @@ -1537,8 +1537,8 @@ namespace CXXGRAPH
DialResult result;
result.success = false;

auto adj = getAdjMatrix();
auto nodeSet = getNodeSet();
auto adj = Graph<T>::getAdjMatrix();
auto nodeSet = Graph<T>::getNodeSet();

if (std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end())
{
Expand Down Expand Up @@ -1664,13 +1664,13 @@ namespace CXXGRAPH
{
std::vector<Node<T>> result;

std::list<const Node<T> *> nodeSet = getNodeSet();
std::list<const Node<T> *> nodeSet = Graph<T>::getNodeSet();
//check if start node in the graph
if (std::find(nodeSet.begin(), nodeSet.end(), &start) == nodeSet.end())
{
return result;
}
std::vector<Node<T>> C = depth_first_search(start);
std::vector<Node<T>> C = Graph<T>::depth_first_search(start);
std::list<const Node<T> *> C1; //complement of C i.e. nodeSet - C
for (auto const node : nodeSet)
{
Expand All @@ -1685,7 +1685,7 @@ namespace CXXGRAPH
std::vector<Node<T>> M;
for (auto const node : C1)
{
std::vector<Node<T>> reachableNodes = depth_first_search(*node);
std::vector<Node<T>> reachableNodes = Graph<T>::depth_first_search(*node);
M.insert(M.end(),reachableNodes.begin(),reachableNodes.end());
}
// removes nodes from C that are reachable from M.
Expand Down
43 changes: 43 additions & 0 deletions test/BFSTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ TEST(BFSTest, test_1)
ASSERT_TRUE(std::find(res.begin(), res.end(), node1) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node2) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node3) != res.end());

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
std::vector<CXXGRAPH::Node<int>> res_ts = graph_ts.breadth_first_search(node1);
ASSERT_EQ(res_ts.size(), 3);
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node1) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node2) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node3) != res_ts.end());
}

TEST(BFSTest, test_2)
Expand All @@ -41,6 +48,13 @@ TEST(BFSTest, test_2)
ASSERT_FALSE(std::find(res.begin(), res.end(), node1) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node2) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node3) != res.end());

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
std::vector<CXXGRAPH::Node<int>> res_ts = graph_ts.breadth_first_search(node2);
ASSERT_EQ(res_ts.size(), 2);
ASSERT_FALSE(std::find(res_ts.begin(), res_ts.end(), node1) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node2) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node3) != res_ts.end());
}

TEST(BFSTest, test_3)
Expand All @@ -62,6 +76,13 @@ TEST(BFSTest, test_3)
ASSERT_TRUE(std::find(res.begin(), res.end(), node1) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node2) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node3) != res.end());

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
std::vector<CXXGRAPH::Node<int>> res_ts = graph_ts.breadth_first_search(node2);
ASSERT_EQ(res_ts.size(), 3);
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node1) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node2) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node3) != res_ts.end());
}

TEST(BFSTest, test_4)
Expand All @@ -83,6 +104,13 @@ TEST(BFSTest, test_4)
ASSERT_TRUE(std::find(res.begin(), res.end(), node1) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node2) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node3) != res.end());

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
std::vector<CXXGRAPH::Node<int>> res_ts = graph.breadth_first_search(node3);
ASSERT_EQ(res_ts.size(), 3);
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node1) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node2) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node3) != res_ts.end());
}

TEST(BFSTest, test_5)
Expand All @@ -104,6 +132,13 @@ TEST(BFSTest, test_5)
ASSERT_FALSE(std::find(res.begin(), res.end(), node1) != res.end());
ASSERT_FALSE(std::find(res.begin(), res.end(), node2) != res.end());
ASSERT_TRUE(std::find(res.begin(), res.end(), node3) != res.end());

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
std::vector<CXXGRAPH::Node<int>> res_ts = graph_ts.breadth_first_search(node3);
ASSERT_EQ(res_ts.size(), 1);
ASSERT_FALSE(std::find(res_ts.begin(), res_ts.end(), node1) != res_ts.end());
ASSERT_FALSE(std::find(res_ts.begin(), res_ts.end(), node2) != res_ts.end());
ASSERT_TRUE(std::find(res_ts.begin(), res_ts.end(), node3) != res_ts.end());
}

TEST(BFSTest, test_6)
Expand All @@ -127,4 +162,12 @@ TEST(BFSTest, test_6)
ASSERT_FALSE(std::find(res.begin(), res.end(), node2) != res.end());
ASSERT_FALSE(std::find(res.begin(), res.end(), node3) != res.end());
ASSERT_FALSE(std::find(res.begin(), res.end(), node4) != res.end());

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
std::vector<CXXGRAPH::Node<int>> res_ts = graph_ts.breadth_first_search(node4);
ASSERT_EQ(res_ts.size(), 0);
ASSERT_FALSE(std::find(res_ts.begin(), res_ts.end(), node1) != res_ts.end());
ASSERT_FALSE(std::find(res_ts.begin(), res_ts.end(), node2) != res_ts.end());
ASSERT_FALSE(std::find(res_ts.begin(), res_ts.end(), node3) != res_ts.end());
ASSERT_FALSE(std::find(res_ts.begin(), res_ts.end(), node4) != res_ts.end());
}
53 changes: 51 additions & 2 deletions test/BellmanFordTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ TEST(BellmanFordTest, test_1)
ASSERT_FALSE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, -2);

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
CXXGRAPH::BellmanFordResult res_ts = graph_ts.bellmanford(node0, node3);
ASSERT_TRUE(res_ts.success);
ASSERT_FALSE(res_ts.negativeCycle);
ASSERT_EQ(res_ts.errorMessage, "");
ASSERT_EQ(res_ts.result, -2);
}

// a graph with negative cycle
Expand All @@ -64,6 +71,13 @@ TEST(BellmanFordTest, test_2)
ASSERT_TRUE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, CXXGRAPH::INF_DOUBLE);

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
CXXGRAPH::BellmanFordResult res_ts = graph_ts.bellmanford(node0, node2);
ASSERT_TRUE(res_ts.success);
ASSERT_TRUE(res_ts.negativeCycle);
ASSERT_EQ(res_ts.errorMessage, "");
ASSERT_EQ(res_ts.result, CXXGRAPH::INF_DOUBLE);
}

// UndirectedWeightedEdge
Expand All @@ -86,6 +100,14 @@ TEST(BellmanFordTest, test_3)
ASSERT_FALSE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 2);

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
CXXGRAPH::BellmanFordResult res_ts = graph_ts.bellmanford(node1, node3);
ASSERT_TRUE(res_ts.success);
ASSERT_FALSE(res_ts.negativeCycle);
ASSERT_EQ(res_ts.errorMessage, "");
ASSERT_EQ(res_ts.result, 2);

}

// No weighted edge
Expand All @@ -108,6 +130,13 @@ TEST(BellmanFordTest, test_4)
ASSERT_FALSE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, CXXGRAPH::ERR_NO_WEIGHTED_EDGE);
ASSERT_EQ(res.result, CXXGRAPH::INF_DOUBLE);

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
CXXGRAPH::BellmanFordResult res_ts = graph_ts.bellmanford(node1, node3);
ASSERT_FALSE(res_ts.success);
ASSERT_FALSE(res_ts.negativeCycle);
ASSERT_EQ(res_ts.errorMessage, CXXGRAPH::ERR_NO_WEIGHTED_EDGE);
ASSERT_EQ(res_ts.result, CXXGRAPH::INF_DOUBLE);
}


Expand All @@ -129,21 +158,41 @@ TEST(BellmanFordTest, test_5)
ASSERT_EQ(res.errorMessage, CXXGRAPH::ERR_SOURCE_NODE_NOT_IN_GRAPH);
ASSERT_EQ(res.result, CXXGRAPH::INF_DOUBLE);

CXXGRAPH::Graph_TS<int> graph_ts(edgeSet);
CXXGRAPH::BellmanFordResult res_ts = graph.bellmanford(node3, node1);
ASSERT_FALSE(res_ts.success);
ASSERT_FALSE(res_ts.negativeCycle);
ASSERT_EQ(res_ts.errorMessage, CXXGRAPH::ERR_SOURCE_NODE_NOT_IN_GRAPH);
ASSERT_EQ(res_ts.result, CXXGRAPH::INF_DOUBLE);

res = graph.bellmanford(node1, node3);
ASSERT_FALSE(res.success);
ASSERT_FALSE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, CXXGRAPH::ERR_TARGET_NODE_NOT_IN_GRAPH);
ASSERT_EQ(res.result, CXXGRAPH::INF_DOUBLE);

res_ts = graph_ts.bellmanford(node1, node3);
ASSERT_FALSE(res_ts.success);
ASSERT_FALSE(res_ts.negativeCycle);
ASSERT_EQ(res_ts.errorMessage, CXXGRAPH::ERR_TARGET_NODE_NOT_IN_GRAPH);
ASSERT_EQ(res_ts.result, CXXGRAPH::INF_DOUBLE);

CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node3, node2, 1);
edgeSet.push_back(&edge2);
CXXGRAPH::Graph<int> graph1(edgeSet);


CXXGRAPH::Graph<int> graph1(edgeSet);
res = graph1.bellmanford(node1, node3);
ASSERT_FALSE(res.success);
ASSERT_FALSE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, CXXGRAPH::ERR_TARGET_NODE_NOT_REACHABLE);
ASSERT_EQ(res.result, -1);

CXXGRAPH::Graph<int> graph1_ts(edgeSet);
res_ts = graph1_ts.bellmanford(node1, node3);
ASSERT_FALSE(res_ts.success);
ASSERT_FALSE(res_ts.negativeCycle);
ASSERT_EQ(res_ts.errorMessage, CXXGRAPH::ERR_TARGET_NODE_NOT_REACHABLE);
ASSERT_EQ(res_ts.result, -1);

}

Loading