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

Additional std:: types and functions to graph_using.hpp #131

Merged
merged 2 commits into from
Sep 4, 2024
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
51 changes: 22 additions & 29 deletions include/graph/algorithm/bellman_ford_shortest_paths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,12 @@

// Visitor Functions
public:
// vertex visitor functions
//constexpr void on_initialize_vertex(vertex_desc_type&& vdesc) {}
//constexpr void on_discover_vertex(vertex_desc_type&& vdesc) {}
//constexpr void on_examine_vertex(vertex_desc_type&& vdesc) {}
//constexpr void on_finish_vertex(vertex_desc_type&& vdesc) {}

// edge visitor functions
constexpr void on_examine_edge(sourced_edge_desc_type&& edesc) {}
constexpr void on_edge_relaxed(sourced_edge_desc_type&& edesc) {}
constexpr void on_edge_not_relaxed(sourced_edge_desc_type&& edesc) {}
constexpr void on_edge_minimized(sourced_edge_desc_type&& edesc) {}
constexpr void on_edge_not_minimized(sourced_edge_desc_type&& edesc) {}
constexpr void on_examine_edge(const sourced_edge_desc_type& edesc) {}
constexpr void on_edge_relaxed(const sourced_edge_desc_type& edesc) {}
constexpr void on_edge_not_relaxed(const sourced_edge_desc_type& edesc) {}
constexpr void on_edge_minimized(const sourced_edge_desc_type& edesc) {}
constexpr void on_edge_not_minimized(const sourced_edge_desc_type& edesc) {}

Check warning on line 45 in include/graph/algorithm/bellman_ford_shortest_paths.hpp

View check run for this annotation

Codecov / codecov/patch

include/graph/algorithm/bellman_ford_shortest_paths.hpp#L45

Added line #L45 was not covered by tests
};

template <class G, class Visitor>
Expand Down Expand Up @@ -86,10 +80,10 @@
*/
template <index_adjacency_list G, forward_range Predecessors, class OutputIterator>
requires output_iterator<OutputIterator, vertex_id_t<G>>
void find_negative_cycle(G& g,
const Predecessors& predecessor,
const std::optional<vertex_id_t<G>>& cycle_vertex_id,
OutputIterator out_cycle) {
void find_negative_cycle(G& g,
const Predecessors& predecessor,
const optional<vertex_id_t<G>>& cycle_vertex_id,
OutputIterator out_cycle) {
// Does a negative weight cycle exist?
if (cycle_vertex_id.has_value()) {
vertex_id_t<G> uid = cycle_vertex_id.value();
Expand Down Expand Up @@ -137,7 +131,7 @@
input_range Sources,
random_access_range Distances,
random_access_range Predecessors,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = bellman_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
Expand All @@ -148,7 +142,7 @@
sized_range<Predecessors> && //
basic_edge_weight_function<G, WF, range_value_t<Distances>, Compare, Combine>
// && bellman_visitor<G, Visitor>
[[nodiscard]] std::optional<vertex_id_t<G>> bellman_ford_shortest_paths(
[[nodiscard]] optional<vertex_id_t<G>> bellman_ford_shortest_paths(
G& g,
const Sources& sources,
Distances& distances,
Expand All @@ -160,7 +154,7 @@
using id_type = vertex_id_t<G>;
using DistanceValue = range_value_t<Distances>;
using weight_type = invoke_result_t<WF, edge_reference_t<G>>;
using return_type = std::optional<vertex_id_t<G>>;
using return_type = optional<vertex_id_t<G>>;

// relxing the target is the function of reducing the distance from the source to the target
auto relax_target = [&g, &predecessor, &distances, &compare, &combine] //
Expand Down Expand Up @@ -240,7 +234,7 @@
template <index_adjacency_list G,
random_access_range Distances,
random_access_range Predecessors,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = bellman_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
Expand All @@ -250,7 +244,7 @@
sized_range<Predecessors> && //
basic_edge_weight_function<G, WF, range_value_t<Distances>, Compare, Combine>
// && bellman_visitor<G, Visitor>
[[nodiscard]] std::optional<vertex_id_t<G>> bellman_ford_shortest_paths(
[[nodiscard]] optional<vertex_id_t<G>> bellman_ford_shortest_paths(
G& g,
const vertex_id_t<G> source,
Distances& distances,
Expand All @@ -265,7 +259,7 @@


/**
* @brief Shortest distnaces from a single source using Bellman-Ford's single-source shortest paths
* @brief Shortest distances from a single source using Bellman-Ford's single-source shortest paths
* algorithm with a visitor.
*
* This is identical to bellman_ford_shortest_paths() except that it does not require a predecessors range.
Expand Down Expand Up @@ -297,7 +291,7 @@
template <index_adjacency_list G,
input_range Sources,
random_access_range Distances,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = bellman_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
Expand All @@ -306,7 +300,7 @@
sized_range<Distances> && //
basic_edge_weight_function<G, WF, range_value_t<Distances>, Compare, Combine>
//&& bellman_visitor<G, Visitor>
[[nodiscard]] std::optional<vertex_id_t<G>> bellman_ford_shortest_distances(
[[nodiscard]] optional<vertex_id_t<G>> bellman_ford_shortest_distances(
G& g,
const Sources& sources,
Distances& distances,
Expand All @@ -315,21 +309,20 @@
Compare&& compare = less<range_value_t<Distances>>(),
Combine&& combine = plus<range_value_t<Distances>>()) {
return bellman_ford_shortest_paths(g, sources, distances, _null_predecessors, forward<WF>(weight),
std::forward<Visitor>(visitor), std::forward<Compare>(compare),
std::forward<Combine>(combine));
forward<Visitor>(visitor), forward<Compare>(compare), forward<Combine>(combine));
}

template <index_adjacency_list G,
random_access_range Distances,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = bellman_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
requires is_arithmetic_v<range_value_t<Distances>> && //
sized_range<Distances> && //
basic_edge_weight_function<G, WF, range_value_t<Distances>, Compare, Combine>
//&& bellman_visitor<G, Visitor>
[[nodiscard]] std::optional<vertex_id_t<G>> bellman_ford_shortest_distances(
[[nodiscard]] optional<vertex_id_t<G>> bellman_ford_shortest_distances(
G& g,
const vertex_id_t<G> source,
Distances& distances,
Expand All @@ -338,8 +331,8 @@
Compare&& compare = less<range_value_t<Distances>>(),
Combine&& combine = plus<range_value_t<Distances>>()) {
return bellman_ford_shortest_paths(g, subrange(&source, (&source + 1)), distances, _null_predecessors,
forward<WF>(weight), std::forward<Visitor>(visitor),
std::forward<Compare>(compare), std::forward<Combine>(combine));
forward<WF>(weight), forward<Visitor>(visitor), forward<Compare>(compare),
forward<Combine>(combine));
}

} // namespace graph
Expand Down
2 changes: 1 addition & 1 deletion include/graph/algorithm/dijkstra_clrs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ constexpr auto print_types(Ts...) {
template <adjacency_list G,
random_access_range Distance,
random_access_range Predecessor,
class WF = std::function<range_value_t<Distance>(edge_reference_t<G>)>>
class WF = function<range_value_t<Distance>(edge_reference_t<G>)>>
requires random_access_range<vertex_range_t<G>> && //
integral<vertex_id_t<G>> && //
is_arithmetic_v<range_value_t<Distance>> && //
Expand Down
32 changes: 15 additions & 17 deletions include/graph/algorithm/dijkstra_shortest_paths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class dijkstra_visitor_base {
// Visitor Functions
public:
// vertex visitor functions
constexpr void on_initialize_vertex(vertex_desc_type&& vdesc) {}
constexpr void on_discover_vertex(vertex_desc_type&& vdesc) {}
constexpr void on_examine_vertex(vertex_desc_type&& vdesc) {}
constexpr void on_finish_vertex(vertex_desc_type&& vdesc) {}
constexpr void on_initialize_vertex(const vertex_desc_type& vdesc) {}
constexpr void on_discover_vertex(const vertex_desc_type& vdesc) {}
constexpr void on_examine_vertex(const vertex_desc_type& vdesc) {}
constexpr void on_finish_vertex(const vertex_desc_type& vdesc) {}

// edge visitor functions
constexpr void on_examine_edge(sourced_edge_desc_type&& edesc) {}
constexpr void on_edge_relaxed(sourced_edge_desc_type&& edesc) {}
constexpr void on_edge_not_relaxed(sourced_edge_desc_type&& edesc) {}
constexpr void on_examine_edge(const sourced_edge_desc_type& edesc) {}
constexpr void on_edge_relaxed(const sourced_edge_desc_type& edesc) {}
constexpr void on_edge_not_relaxed(const sourced_edge_desc_type& edesc) {}
};

template <class G, class Visitor>
Expand Down Expand Up @@ -97,7 +97,7 @@ template <index_adjacency_list G,
input_range Sources,
random_access_range Distances,
random_access_range Predecessors,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = dijkstra_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
Expand Down Expand Up @@ -188,7 +188,7 @@ void dijkstra_shortest_paths(
visitor.on_examine_edge({uid, vid, uv});

// Negative weights are not allowed for Dijkstra's algorithm
if constexpr (std::is_signed_v<weight_type>) {
if constexpr (is_signed_v<weight_type>) {
if (w < zero) {
throw std::out_of_range(
fmt::format("dijkstra_shortest_paths: invalid negative edge weight of '{}' encountered", w));
Expand Down Expand Up @@ -230,7 +230,7 @@ void dijkstra_shortest_paths(
template <index_adjacency_list G,
random_access_range Distances,
random_access_range Predecessors,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = dijkstra_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
Expand Down Expand Up @@ -282,7 +282,7 @@ void dijkstra_shortest_paths(
template <index_adjacency_list G,
input_range Sources,
random_access_range Distances,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = dijkstra_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
Expand All @@ -299,14 +299,13 @@ void dijkstra_shortest_distances(
Visitor&& visitor = dijkstra_visitor_base<G>(),
Compare&& compare = less<range_value_t<Distances>>(),
Combine&& combine = plus<range_value_t<Distances>>()) {
dijkstra_shortest_paths(g, sources, distances, _null_predecessors, forward<WF>(weight),
std::forward<Visitor>(visitor), std::forward<Compare>(compare),
std::forward<Combine>(combine));
dijkstra_shortest_paths(g, sources, distances, _null_predecessors, forward<WF>(weight), forward<Visitor>(visitor),
forward<Compare>(compare), forward<Combine>(combine));
}

template <index_adjacency_list G,
random_access_range Distances,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Visitor = dijkstra_visitor_base<G>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>>
Expand All @@ -323,8 +322,7 @@ void dijkstra_shortest_distances(
Compare&& compare = less<range_value_t<Distances>>(),
Combine&& combine = plus<range_value_t<Distances>>()) {
dijkstra_shortest_paths(g, subrange(&source, (&source + 1)), distances, _null_predecessors, forward<WF>(weight),
std::forward<Visitor>(visitor), std::forward<Compare>(compare),
std::forward<Combine>(combine));
forward<Visitor>(visitor), forward<Compare>(compare), forward<Combine>(combine));
}

} // namespace graph
Expand Down
8 changes: 4 additions & 4 deletions include/graph/algorithm/experimental/co_dijkstra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ template <index_adjacency_list G,
random_access_range Predecessors,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>>
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>>
requires convertible_to<range_value_t<Seeds>, vertex_id_t<G>> && //
is_arithmetic_v<range_value_t<Distances>> && //
convertible_to<vertex_id_t<G>, range_value_t<Predecessors>> && //
Expand Down Expand Up @@ -168,7 +168,7 @@ Generator<bfs_value_t<dijkstra_events, G>> co_dijkstra(
dijkstra_yield_edge(dijkstra_events::examine_edge, uid, vid, uv);

// Negative weights are not allowed for Dijkstra's algorithm
if constexpr (std::is_signed_v<weight_type>) {
if constexpr (is_signed_v<weight_type>) {
if (w < zero) {
throw graph_error("co_dijkstra: negative edge weight");
}
Expand Down Expand Up @@ -233,10 +233,10 @@ template <index_adjacency_list G,
random_access_range Predecessors,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
_queueable Queue = std::priority_queue<vertex_id_t<G>,
std::vector<vertex_id_t<G>>,
std::greater<vertex_id_t<G>>>>
greater<vertex_id_t<G>>>>
requires is_arithmetic_v<range_value_t<Distances>> && //
convertible_to<vertex_id_t<G>, range_value_t<Predecessors>> && //
basic_edge_weight_function<G, WF, range_value_t<Distances>, Compare, Combine>
Expand Down
6 changes: 3 additions & 3 deletions include/graph/algorithm/experimental/visitor_dijkstra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ template <index_adjacency_list G,
input_range Sources,
random_access_range Distances,
random_access_range Predecessors,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>
//queueable Que = _dijkstra_queue<G, Distances> // not used
Expand Down Expand Up @@ -241,7 +241,7 @@ void dijkstra_with_visitor(
visitor.on_examine_edge({uid, vid, uv});

// Negative weights are not allowed for Dijkstra's algorithm
if constexpr (std::is_signed_v<weight_type>) {
if constexpr (is_signed_v<weight_type>) {
if (w < zero) {
throw graph_error("dijkstra_with_visitor: negative edge weight");
}
Expand Down Expand Up @@ -303,7 +303,7 @@ template <index_adjacency_list G,
class Visitor,
random_access_range Distances,
random_access_range Predecessors,
class WF = std::function<range_value_t<Distances>(edge_reference_t<G>)>,
class WF = function<range_value_t<Distances>(edge_reference_t<G>)>,
class Compare = less<range_value_t<Distances>>,
class Combine = plus<range_value_t<Distances>>,
queueable Que = _dijkstra_queue<G, Distances>>
Expand Down
Loading
Loading