-
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.
refactor: algorithm directory structure (#105)
* put each algorithm in separate file * consistently use underscores in algorithm directory names
- Loading branch information
Showing
47 changed files
with
2,414 additions
and
1,895 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
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
include/graaflib/algorithm/graph_traversal/breadth_first_search.h
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,38 @@ | ||
#pragma once | ||
|
||
#include <graaflib/algorithm/graph_traversal/common.h> | ||
#include <graaflib/graph.h> | ||
#include <graaflib/types.h> | ||
|
||
#include <concepts> | ||
|
||
namespace graaf::algorithm { | ||
|
||
/** | ||
* @brief Traverses the graph, starting at start_vertex, and visits all | ||
* reachable vertices in a BFS manner. | ||
* | ||
* @param graph The graph to traverse. | ||
* @param start_vertex Vertex id where the traversal should be started. | ||
* @param edge_callback A callback which is called for each traversed edge. | ||
* Should be invocable with an edge_id_t. | ||
* @param search_termination_strategy A unary predicate to indicate whether we | ||
* should continue the traversal or not. Traversal continues while this | ||
* predicate returns false. | ||
*/ | ||
template < | ||
typename V, typename E, graph_type T, | ||
typename EDGE_CALLBACK_T = detail::noop_callback, | ||
typename SEARCH_TERMINATION_STRATEGY_T = detail::exhaustive_search_strategy> | ||
requires std::invocable<EDGE_CALLBACK_T &, edge_id_t &> && | ||
std::is_invocable_r_v<bool, SEARCH_TERMINATION_STRATEGY_T &, | ||
vertex_id_t> | ||
void breadth_first_traverse( | ||
const graph<V, E, T> &graph, vertex_id_t start_vertex, | ||
const EDGE_CALLBACK_T &edge_callback, | ||
const SEARCH_TERMINATION_STRATEGY_T &search_termination_strategy = | ||
SEARCH_TERMINATION_STRATEGY_T{}); | ||
|
||
} // namespace graaf::algorithm | ||
|
||
#include "breadth_first_search.tpp" |
41 changes: 41 additions & 0 deletions
41
include/graaflib/algorithm/graph_traversal/breadth_first_search.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,41 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <queue> | ||
#include <unordered_set> | ||
|
||
namespace graaf::algorithm { | ||
|
||
template <typename V, typename E, graph_type T, typename EDGE_CALLBACK_T, | ||
typename SEARCH_TERMINATION_STRATEGY_T> | ||
requires std::invocable<EDGE_CALLBACK_T&, edge_id_t&> && | ||
std::is_invocable_r_v<bool, SEARCH_TERMINATION_STRATEGY_T&, | ||
vertex_id_t> | ||
void breadth_first_traverse( | ||
const graph<V, E, T>& graph, vertex_id_t start_vertex, | ||
const EDGE_CALLBACK_T& edge_callback, | ||
const SEARCH_TERMINATION_STRATEGY_T& search_termination_strategy) { | ||
std::unordered_set<vertex_id_t> seen_vertices{}; | ||
std::queue<vertex_id_t> to_explore{}; | ||
|
||
to_explore.push(start_vertex); | ||
|
||
while (!to_explore.empty()) { | ||
const auto current{to_explore.front()}; | ||
to_explore.pop(); | ||
|
||
if (search_termination_strategy(current)) { | ||
return; | ||
} | ||
|
||
seen_vertices.insert(current); | ||
for (const auto neighbor_vertex : graph.get_neighbors(current)) { | ||
if (!seen_vertices.contains(neighbor_vertex)) { | ||
edge_callback(edge_id_t{current, neighbor_vertex}); | ||
to_explore.push(neighbor_vertex); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} // namespace graaf::algorithm |
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,28 @@ | ||
#pragma once | ||
|
||
#include <graaflib/types.h> | ||
|
||
namespace graaf::algorithm { | ||
|
||
namespace detail { | ||
|
||
/** | ||
* An edge callback which does nothing. | ||
*/ | ||
struct noop_callback { | ||
void operator()(const edge_id_t& /*edge*/) const {} | ||
}; | ||
|
||
/* | ||
* A unary predicate which always returns false, effectively resulting in an | ||
* exhaustive search. | ||
*/ | ||
struct exhaustive_search_strategy { | ||
[[nodiscard]] bool operator()(const vertex_id_t /*vertex*/) const { | ||
return false; | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
|
||
} // namespace graaf::algorithm |
38 changes: 38 additions & 0 deletions
38
include/graaflib/algorithm/graph_traversal/depth_first_search.h
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,38 @@ | ||
#pragma once | ||
|
||
#include <graaflib/algorithm/graph_traversal/common.h> | ||
#include <graaflib/graph.h> | ||
#include <graaflib/types.h> | ||
|
||
#include <concepts> | ||
|
||
namespace graaf::algorithm { | ||
|
||
/** | ||
* @brief Traverses the graph, starting at start_vertex, and visits all | ||
* reachable vertices in a DFS manner. | ||
* | ||
* @param graph The graph to traverse. | ||
* @param start_vertex Vertex id where the traversal should be started. | ||
* @param edge_callback A callback which is called for each traversed edge. | ||
* Should be invocable with an edge_id_t. | ||
* @param search_termination_strategy A unary predicate to indicate whether we | ||
* should continue the traversal or not. Traversal continues while this | ||
* predicate returns false. | ||
*/ | ||
template < | ||
typename V, typename E, graph_type T, | ||
typename EDGE_CALLBACK_T = detail::noop_callback, | ||
typename SEARCH_TERMINATION_STRATEGY_T = detail::exhaustive_search_strategy> | ||
requires std::invocable<EDGE_CALLBACK_T &, edge_id_t &> && | ||
std::is_invocable_r_v<bool, SEARCH_TERMINATION_STRATEGY_T &, | ||
vertex_id_t> | ||
void depth_first_traverse( | ||
const graph<V, E, T> &graph, vertex_id_t start_vertex, | ||
const EDGE_CALLBACK_T &edge_callback, | ||
const SEARCH_TERMINATION_STRATEGY_T &search_termination_strategy = | ||
SEARCH_TERMINATION_STRATEGY_T{}); | ||
|
||
} // namespace graaf::algorithm | ||
|
||
#include "depth_first_search.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
24 changes: 24 additions & 0 deletions
24
include/graaflib/algorithm/minimum_spanning_tree/kruskal.h
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,24 @@ | ||
#pragma once | ||
|
||
#include <graaflib/graph.h> | ||
#include <graaflib/types.h> | ||
|
||
#include <vector> | ||
|
||
namespace graaf::algorithm { | ||
/** | ||
* Computes the minimum spanning tree (MST) or minimum spanning forest of a | ||
* graph using Kruskal's algorithm. | ||
* | ||
* @tparam V The vertex type of the graph. | ||
* @tparam E The edge type of the graph. | ||
* @param graph The input graph. | ||
* @return A vector of edges forming the MST or minimum spanning forest. | ||
*/ | ||
template <typename V, typename E> | ||
[[nodiscard]] std::vector<edge_id_t> kruskal_minimum_spanning_tree( | ||
const graph<V, E, graph_type::UNDIRECTED>& graph); | ||
|
||
} // namespace graaf::algorithm | ||
|
||
#include "kruskal.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
Oops, something went wrong.