Skip to content

Commit

Permalink
Added tests and examples (#376)
Browse files Browse the repository at this point in the history
* added a new DFStest

* added a test_8 and test_9 in DFSTest

* added Prim example

* added Floyd Warshall example

* added source of my example

* changed to a more comprehensible floyd warshall example

* added a floyd warshall test
  • Loading branch information
ErikDervishi03 authored Dec 19, 2023
1 parent da9c725 commit 20d818c
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 25 deletions.
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ add_subdirectory(DialExample)
add_subdirectory(DijkstraExample)
add_subdirectory(NetworkDynamicsExample)
add_subdirectory(PartitionExample)
add_subdirectory(PrimExample)
add_subdirectory(FloydWarshallExample)

endif(EXAMPLES)
17 changes: 17 additions & 0 deletions examples/FloydWarshallExample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.9)
project(floydWarshallExample)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES /usr/local/lib ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})

add_executable(floyd_warshall floyd_warshall.cpp)
target_include_directories(floyd_warshall PUBLIC "${CMAKE_SOURCE_DIR}/include")

target_link_libraries(floyd_warshall
pthread
ssl
crypto
z)
45 changes: 45 additions & 0 deletions examples/FloydWarshallExample/floyd_warshall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <CXXGraph/CXXGraph.hpp>
#include <memory>

using std::make_shared;
// example taken from
// https://www.youtube.com/watch?v=B06q2yjr-Cc
int main() {
CXXGraph::Node<int> node0("0", 0);
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
CXXGraph::Node<int> node4("4", 4);

CXXGraph::UndirectedWeightedEdge<int> edge1(1, node0, node1, 1);
CXXGraph::UndirectedWeightedEdge<int> edge2(2, node0, node2, 2);
CXXGraph::UndirectedWeightedEdge<int> edge3(3, node1, node2, 6);
CXXGraph::UndirectedWeightedEdge<int> edge4(4, node1, node3, 4);
CXXGraph::UndirectedWeightedEdge<int> edge5(5, node2, node3, 5);
CXXGraph::UndirectedWeightedEdge<int> edge6(6, node3, node4, 3);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge4));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge5));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge6));
// Can print out the edges for debugging
std::cout << edge1 << "\n";
std::cout << edge2 << "\n";
std::cout << edge3 << "\n";
std::cout << edge4 << "\n";
std::cout << edge5 << "\n";
std::cout << edge6 << "\n";

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::FWResult res = graph.floydWarshall();
std::cout << "floyd Warshall Result: "
<< "\n";
for (auto i : res.result) {
std::cout << "distance of: " << i.first.first << " " << i.first.second
<< " = " << i.second << "\n";
}
return 0;
}
17 changes: 17 additions & 0 deletions examples/PrimExample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.9)
project(PrimExample)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES /usr/local/lib ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})

add_executable(prim_example prim_example.cpp)
target_include_directories(prim_example PUBLIC "${CMAKE_SOURCE_DIR}/include")

target_link_libraries(prim_example
pthread
ssl
crypto
z)
80 changes: 80 additions & 0 deletions examples/PrimExample/prim_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <CXXGraph/CXXGraph.hpp>
#include <memory>

using std::make_shared;

// example taken from
// https://www.geeksforgeeks.org/prims-mst-for-adjacency-list-representation-greedy-algo-6/TEST(FWTest,
// test_1)
int main() {
CXXGraph::Node<int> node0("0", 0);
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
CXXGraph::Node<int> node4("4", 4);
CXXGraph::Node<int> node5("5", 5);
CXXGraph::Node<int> node6("6", 6);
CXXGraph::Node<int> node7("7", 7);
CXXGraph::Node<int> node8("8", 8);

CXXGraph::UndirectedWeightedEdge<int> edge1(1, node0, node1, 4);
CXXGraph::UndirectedWeightedEdge<int> edge2(2, node0, node7, 8);
CXXGraph::UndirectedWeightedEdge<int> edge3(3, node1, node7, 11);
CXXGraph::UndirectedWeightedEdge<int> edge4(4, node1, node2, 8);
CXXGraph::UndirectedWeightedEdge<int> edge5(5, node2, node8, 2);
CXXGraph::UndirectedWeightedEdge<int> edge6(6, node2, node5, 4);
CXXGraph::UndirectedWeightedEdge<int> edge7(7, node2, node3, 7);
CXXGraph::UndirectedWeightedEdge<int> edge8(8, node3, node3, 1);
CXXGraph::UndirectedWeightedEdge<int> edge9(9, node3, node4, 9);
CXXGraph::UndirectedWeightedEdge<int> edge10(10, node3, node5, 14);
CXXGraph::UndirectedWeightedEdge<int> edge11(11, node4, node5, 10);
CXXGraph::UndirectedWeightedEdge<int> edge12(12, node5, node6, 2);
CXXGraph::UndirectedWeightedEdge<int> edge13(13, node6, node8, 6);
CXXGraph::UndirectedWeightedEdge<int> edge14(14, node6, node7, 1);
CXXGraph::UndirectedWeightedEdge<int> edge15(15, node7, node8, 7);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge4));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge5));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge6));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge7));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge8));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge9));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge10));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge11));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge12));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge13));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge14));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge15));

// Can print out the edges for debugging
std::cout << edge1 << "\n";
std::cout << edge2 << "\n";
std::cout << edge3 << "\n";
std::cout << edge4 << "\n";
std::cout << edge5 << "\n";
std::cout << edge6 << "\n";
std::cout << edge7 << "\n";
std::cout << edge8 << "\n";
std::cout << edge9 << "\n";
std::cout << edge10 << "\n";
std::cout << edge11 << "\n";
std::cout << edge12 << "\n";
std::cout << edge13 << "\n";
std::cout << edge14 << "\n";
std::cout << edge15 << "\n";

CXXGraph::Graph<int> graph(edgeSet);
auto res = graph.prim();
std::cout << "Prim Result: "
<< "\n";
for (auto edge : res.mst) {
std::cout << edge.first << " " << edge.second << "\n";
}
std::cout << "mstCost: " << res.mstCost << "\n";

return 0;
}
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ if(TEST)
add_test(test_topological_sort test_exe --gtest_filter=TopologicalSort*)
add_test(test_transitive_reductiono test_exe --gtest_filter=TransitiveReduction*)
add_test(test_union_find test_exe --gtest_filter=UnionFind*)
add_test(test_floyd_warshall test_exe --gtest_filter=FloydWarshall*)
endif(TEST)
25 changes: 0 additions & 25 deletions test/DFSTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,3 @@ TEST(DFSTest, test_6) {
ASSERT_FALSE(std::find(res.begin(), res.end(), node3) != res.end());
ASSERT_FALSE(std::find(res.begin(), res.end(), node4) != res.end());
}

TEST(DFSTest, test_7) {
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
CXXGraph::Node<int> node4("4", 4);
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);
CXXGraph::DirectedWeightedEdge<int> edge3(3, node3, node4, 1);
CXXGraph::DirectedWeightedEdge<int> edge4(4, node4, node1, 1);
CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::DirectedWeightedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::DirectedWeightedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::DirectedWeightedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::DirectedWeightedEdge<int>>(edge4));
CXXGraph::Graph<int> graph(edgeSet);
std::vector<CXXGraph::Node<int>> res = graph.depth_first_search(node1);
ASSERT_EQ(res.size(), 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());
ASSERT_TRUE(std::find(res.begin(), res.end(), node4) != res.end());
}
91 changes: 91 additions & 0 deletions test/FloydWarshallTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <memory>

#include "CXXGraph/CXXGraph.hpp"
#include "gtest/gtest.h"

// Smart pointers alias
template <typename T>
using unique = std::unique_ptr<T>;
template <typename T>
using shared = std::shared_ptr<T>;

using std::make_shared;
using std::make_unique;

// minimum spanning tree can differ so instead of checking
// the exact order of elements, we can check some properties
// like the length & cost of mst which must remain the same

// example taken from
// https://www.geeksforgeeks.org/prims-mst-for-adjacency-list-representation-greedy-algo-6/TEST(FWTest,
// test_1)
TEST(FloydWarshallTest, test_1) {
CXXGraph::Node<int> node0("0", 0);
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
CXXGraph::Node<int> node4("4", 4);
CXXGraph::Node<int> node5("5", 5);
CXXGraph::Node<int> node6("6", 6);
CXXGraph::Node<int> node7("7", 7);
CXXGraph::Node<int> node8("8", 8);

CXXGraph::UndirectedWeightedEdge<int> edge1(1, node0, node1, 4);
CXXGraph::UndirectedWeightedEdge<int> edge2(2, node0, node7, 8);
CXXGraph::UndirectedWeightedEdge<int> edge3(3, node1, node7, 11);
CXXGraph::UndirectedWeightedEdge<int> edge4(3, node1, node2, 8);
CXXGraph::UndirectedWeightedEdge<int> edge5(4, node7, node8, 7);
CXXGraph::UndirectedWeightedEdge<int> edge6(3, node7, node6, 1);
CXXGraph::UndirectedWeightedEdge<int> edge7(3, node8, node2, 2);
CXXGraph::UndirectedWeightedEdge<int> edge8(3, node8, node6, 6);
CXXGraph::UndirectedWeightedEdge<int> edge9(3, node2, node5, 4);
CXXGraph::UndirectedWeightedEdge<int> edge10(3, node2, node3, 7);
CXXGraph::UndirectedWeightedEdge<int> edge11(3, node6, node5, 2);
CXXGraph::UndirectedWeightedEdge<int> edge12(3, node3, node4, 9);
CXXGraph::UndirectedWeightedEdge<int> edge13(3, node3, node5, 14);
CXXGraph::UndirectedWeightedEdge<int> edge14(3, node5, node4, 10);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge5));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge3));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge4));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge5));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge6));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge7));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge8));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge9));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge10));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge11));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge12));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge13));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge14));

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::FWResult res = graph.floydWarshall();

ASSERT_TRUE(res.success);
ASSERT_FALSE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, "");
}

TEST(FloydWarshallTest, test_2) {
CXXGraph::Node<int> node0("0", 0);
CXXGraph::Node<int> node1("1", 1);

CXXGraph::UndirectedWeightedEdge<int> edge1(1, node0, node1, 1);

CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge1));

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::FWResult res = graph.floydWarshall();

auto distance_0_1 = res.result.find(std::make_pair("0", "1"));
auto distance_1_0 = res.result.find(std::make_pair("1", "0"));

ASSERT_TRUE(res.success);
ASSERT_FALSE(res.negativeCycle);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(distance_0_1->second, distance_1_0->second);
}

0 comments on commit 20d818c

Please sign in to comment.