-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: utils header containing "get_transposed_graph" function (#136)
- Loading branch information
1 parent
ffaa4d1
commit ae3e232
Showing
6 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include <graaflib/graph.h> | ||
|
||
namespace graaf { | ||
|
||
/** | ||
* Get transposed version of a given directed graph | ||
* | ||
* @param graph The directed graph that is to be transposed | ||
* @return directed_graph<VERTEX_T, EDGE_T> The transposed graph | ||
*/ | ||
template <typename VERTEX_T, typename EDGE_T> | ||
directed_graph<VERTEX_T, EDGE_T> get_transposed_graph( | ||
const directed_graph<VERTEX_T, EDGE_T>& graph); | ||
|
||
} // namespace graaf | ||
|
||
#include "utils.tpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
namespace graaf { | ||
|
||
template <typename VERTEX_T, typename EDGE_T> | ||
directed_graph<VERTEX_T, EDGE_T> get_transposed_graph( | ||
const directed_graph<VERTEX_T, EDGE_T>& graph) { | ||
directed_graph<VERTEX_T, EDGE_T> transposed_graph{}; | ||
for (auto [edge_vertices, edge_type] : graph.get_edges()) { | ||
const auto [vertex_id_lhs, vertex_id_rhs] = edge_vertices; | ||
|
||
auto vertex_value_lhs = graph.get_vertex(vertex_id_lhs); | ||
auto vertex_value_rhs = graph.get_vertex(vertex_id_rhs); | ||
|
||
if (!transposed_graph.has_vertex(vertex_id_lhs)) { | ||
auto vertex_id = transposed_graph.add_vertex(std::move(vertex_value_lhs), | ||
vertex_id_lhs); | ||
} | ||
|
||
if (!transposed_graph.has_vertex(vertex_id_rhs)) { | ||
auto vertex_id = transposed_graph.add_vertex(std::move(vertex_value_rhs), | ||
vertex_id_rhs); | ||
} | ||
|
||
transposed_graph.add_edge(vertex_id_rhs, vertex_id_lhs, | ||
std::move(edge_type)); | ||
} | ||
|
||
return transposed_graph; | ||
} | ||
|
||
} // namespace graaf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <graaflib/algorithm/utils.h> | ||
#include <gtest/gtest.h> | ||
|
||
/** | ||
* Tests which miscellaneous utility functions contained | ||
* in utils.h should go here. Any test relating specifically | ||
* to the public graph interface should instead be located in | ||
* graph_test.cpp. | ||
*/ | ||
|
||
namespace graaf { | ||
|
||
TEST(UtilsTest, Transpose) { | ||
// GIVEN | ||
using graph_t = directed_graph<int, int>; | ||
graph_t graph{}; | ||
const auto vertex_id_1 = graph.add_vertex(1); | ||
const auto vertex_id_2 = graph.add_vertex(2); | ||
const auto vertex_id_3 = graph.add_vertex(3); | ||
graph.add_edge(vertex_id_1, vertex_id_2, 100); | ||
graph.add_edge(vertex_id_2, vertex_id_3, 200); | ||
graph.add_edge(vertex_id_3, vertex_id_1, 300); | ||
|
||
// WHEN | ||
graph_t transposed_graph = get_transposed_graph(graph); | ||
|
||
// THEN | ||
EXPECT_EQ(get_weight(transposed_graph.get_edge(vertex_id_2, vertex_id_1)), | ||
100); | ||
EXPECT_EQ(get_weight(transposed_graph.get_edge(vertex_id_3, vertex_id_2)), | ||
200); | ||
EXPECT_EQ(get_weight(transposed_graph.get_edge(vertex_id_1, vertex_id_3)), | ||
300); | ||
} | ||
|
||
} // namespace graaf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters