Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for Graph Connectivity #194

Merged
merged 3 commits into from
Mar 17, 2022
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
37 changes: 7 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,10 @@ endif(CODE_COVERAGE)

# add the library

add_executable(test_exe test/main.cpp
test/NodeTest.cpp
test/EdgeTest.cpp
test/DirectedEdgeTest.cpp
test/UndirectedEdgeTest.cpp
test/DirectedWeightedEdgeTest.cpp
test/UndirectedWeightedEdgeTest.cpp
test/GraphTest.cpp
test/DijkstraTest.cpp
test/BellmanFordTest.cpp
test/FWTest.cpp
test/PrimTest.cpp
test/BoruvkaTest.cpp
test/KruskalTest.cpp
test/BFSTest.cpp
test/DFSTest.cpp
test/CycleCheckTest.cpp
test/RWOutputTest.cpp
test/PartitionTest.cpp
test/DialTest.cpp
test/GraphSlicingTest.cpp
test/UnionFindTest.cpp
test/EulerPathTest.cpp
)
file (GLOB TEST_FILES "test/*.cpp" "test/*.hpp")

add_executable(test_exe ${TEST_FILES})

target_include_directories(test_exe PUBLIC
"${PROJECT_SOURCE_DIR}/include"
)
Expand Down Expand Up @@ -96,12 +76,9 @@ add_test(test_dial test_exe --gtest_filter=DialTest*)

option(BENCHMARK "Enable Benchmark" OFF)
if(BENCHMARK)
add_executable(benchmark benchmark/Utilities.hpp
benchmark/Node_BM.cpp
benchmark/Edge_BM.cpp
benchmark/Graph_BM.cpp
benchmark/FloydWarshall_BM.cpp
)

file (GLOB BENCHMARK_FILES "benchmark/*.cpp" "benchmark/*.hpp")
add_executable(benchmark ${BENCHMARK_FILES})
target_include_directories(benchmark PUBLIC
"${PROJECT_SOURCE_DIR}/include"
)
Expand Down
59 changes: 31 additions & 28 deletions include/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ namespace CXXGRAPH
result.errorMessage = ERR_DIR_GRAPH;
return result;
}
if(!isConnectedGraph())
if (!isConnectedGraph())
{
result.errorMessage = ERR_NOT_STRONG_CONNECTED;
return result;
Expand Down Expand Up @@ -1822,7 +1822,7 @@ namespace CXXGRAPH
template <typename T>
bool Graph<T>::isConnectedGraph() const
{
if (isDirectedGraph())
if (!isUndirectedGraph())
{
return false;
}
Expand Down Expand Up @@ -1870,45 +1870,48 @@ namespace CXXGRAPH
template <typename T>
bool Graph<T>::isStronglyConnectedGraph() const
{
if (isUndirectedGraph())
if (!isDirectedGraph())
{
return false;
}
else
{
auto nodeSet = getNodeSet();
auto adjMatrix = getAdjMatrix();
// created visited map
std::unordered_map<unsigned long, bool> visited;
for (const auto &node : nodeSet)
{
visited[node->getId()] = false;
}
std::function<void(const Node<T> *)> dfs_helper = [this, &adjMatrix, &visited, &dfs_helper](const Node<T> *source)
for (const auto &start_node : nodeSet)
{
// mark the vertex visited
visited[source->getId()] = true;

// travel the neighbors
for (int i = 0; i < adjMatrix[source].size(); i++)
// created visited map
std::unordered_map<unsigned long, bool> visited;
for (const auto &node : nodeSet)
{
const Node<T> *neighbor = adjMatrix[source].at(i).first;
if (visited[neighbor->getId()] == false)
visited[node->getId()] = false;
}
std::function<void(const Node<T> *)> dfs_helper = [this, &adjMatrix, &visited, &dfs_helper](const Node<T> *source)
{
// mark the vertex visited
visited[source->getId()] = true;

// travel the neighbors
for (int i = 0; i < adjMatrix[source].size(); i++)
{
// make recursive call from neighbor
dfs_helper(neighbor);
const Node<T> *neighbor = adjMatrix[source].at(i).first;
if (visited[neighbor->getId()] == false)
{
// make recursive call from neighbor
dfs_helper(neighbor);
}
}
}
};
// call dfs_helper for the first node
dfs_helper(nodeSet.front());
};
// call dfs_helper for the first node
dfs_helper(start_node);

// check if all the nodes are visited
for (const auto &node : nodeSet)
{
if (visited[node->getId()] == false)
// check if all the nodes are visited
for (const auto &node : nodeSet)
{
return false;
if (visited[node->getId()] == false)
{
return false;
}
}
}
return true;
Expand Down
Loading