Skip to content

Commit c1b0bdc

Browse files
committed
gl alg topology docs
1 parent 9176f26 commit c1b0bdc

4 files changed

Lines changed: 124 additions & 3 deletions

File tree

include/gl/algorithm/topology/coloring.hpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,65 @@
22
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
33
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
44

5+
/// @file gl/algorithm/topology/coloring.hpp
6+
/// @brief Algorithms for detecting and computing graph colorings.
7+
58
#pragma once
69

710
#include "gl/algorithm/core.hpp"
811
#include "gl/algorithm/templates/bfs.hpp"
912

1013
namespace gl::algorithm {
1114

15+
/// @ingroup GL GL-Algorithm
16+
/// @brief Alias for a container mapping vertex indices to their calculated binary (bipartite) colors.
1217
using bicoloring_type = std::vector<binary_color>;
1318

19+
/// @ingroup GL GL-Algorithm
20+
/// @brief Attempts to compute a valid bipartite (2-color) coloring for the given graph.
21+
///
22+
/// This algorithm utilizes the generic @ref gl::algorithm::bfs "bfs" template to traverse the graph and
23+
/// alternate colors between adjacent vertices. If an edge connects two vertices of the same color
24+
/// (indicating an odd-length cycle), the graph is not bipartite, and the search immediately aborts.
25+
///
26+
/// ### Example Usage
27+
/// ```cpp
28+
/// if (auto coloring = gl::algorithm::bipartite_coloring(graph)) // (1)!
29+
/// gl::algorithm::apply_coloring(graph, *coloring); // (2)!
30+
/// else
31+
/// std::cout << "Graph contains an odd cycle and is not bipartite.\n";
32+
/// ```
33+
///
34+
/// 1\. Attempts to find a valid 2-coloring for the graph. Returns `std::nullopt` if impossible.
35+
///
36+
/// 2\. If successful, directly modifies the graph's internal vertex properties to store the colors.
37+
/// **NOTE:** This operation is only available if the property type of the graph's vertices satisfies [**c_binary_color_properties_type**](gl_concepts.md#gl-traits-c-binary-color-properties-type).
38+
///
39+
/// > [!INFO] Algorithmic Complexity
40+
/// >
41+
/// > The time complexity depends entirely on the underlying representation of `GraphType`:
42+
/// >
43+
/// > - **Adjacency List Representations**: \f$O(|V| + |E|)\f$
44+
/// > - *Includes:* @ref gl::impl::list_t "list_t" and @ref gl::impl::flat_list_t "flat_list_t".
45+
/// >
46+
/// > - **Dense Adjacency Matrix Representations**: \f$O(|V|^2)\f$
47+
/// > - *Includes:* @ref gl::impl::matrix_t "matrix_t" and @ref gl::impl::flat_matrix_t "flat_matrix_t".
48+
/// > - *Note:* Iterating over adjacent vertices requires scanning the entire \f$|V|\f$-length matrix row.
49+
///
50+
/// ### Template Parameters
51+
/// | Parameter | Description |
52+
/// | :-------- | :--- |
53+
/// | G | The type of the graph being traversed. |
54+
/// | PreVisitCallback | Type of the callable executed immediately before a vertex is officially visited. |
55+
/// | PostVisitCallback | Type of the callable executed after all adjacent edges of a vertex are evaluated. |
56+
///
57+
/// @param graph The graph to evaluate.
58+
/// @param pre_visit Hook executed immediately before the internal visit logic.
59+
/// @param post_visit Hook executed after all adjacent edges of the current vertex have been enqueued.
60+
/// @return An `std::optional` containing the @ref gl::algorithm::bicoloring_type "bicoloring_type" map if the graph is bipartite. Returns `std::nullopt` otherwise.
61+
/// ### See Also
62+
/// - @ref gl::algorithm::is_bipartite "is_bipartite"
63+
/// - @ref gl::algorithm::apply_coloring "apply_coloring"
1464
template <
1565
traits::c_graph G,
1666
traits::c_optional_callback<void, typename G::id_type> PreVisitCallback = empty_callback,
@@ -64,10 +114,30 @@ template <
64114
return coloring;
65115
}
66116

117+
/// @ingroup GL GL-Algorithm
118+
/// @brief Convenience wrapper for the @ref gl::algorithm::bipartite_coloring "bipartite_coloring" algorithm to check if a graph is bipartite without extracting the exact coloring map.
119+
/// @param graph The graph to evaluate.
120+
/// @return `true` if the graph is bipartite (2-colorable), `false` otherwise.
121+
/// @see @ref gl::algorithm::apply_coloring "apply_coloring"
67122
[[nodiscard]] gl_attr_force_inline bool is_bipartite(const traits::c_graph auto& graph) {
68123
return bipartite_coloring(graph).has_value();
69124
}
70125

126+
/// @ingroup GL GL-Algorithm
127+
/// @brief Applies a computed range of binary colors to the property payload of each vertex in the graph.
128+
///
129+
/// ### Template Parameters
130+
/// | Parameter | Description |
131+
/// | :-------- | :--- |
132+
/// | `G` | The type of the graph to modify. Must have compatible color properties. |
133+
/// | `ColorRange` | The type of the range containing the computed colors. |
134+
///
135+
/// @param graph The mutable graph instance whose properties will be updated.
136+
/// @param color_range A sized range (e.g., @ref gl::algorithm::bicoloring_type "bicoloring_type") matching the vertex count.
137+
/// @return `true` if the coloring was successfully applied, `false` if the size of the range does not match the graph's vertex count.
138+
/// ### See Also
139+
/// - @ref gl::algorithm::bipartite_coloring "bipartite_coloring"
140+
/// - @ref gl::algorithm::is_bipartite "is_bipartite"
71141
template <traits::c_graph G, traits::c_sized_range_of<binary_color> ColorRange>
72142
requires(traits::c_binary_color_properties_type<typename G::vertex_properties_type>)
73143
bool apply_coloring(G& graph, const ColorRange& color_range) {

include/gl/algorithm/topology/topological_sort.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,64 @@
22
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
33
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
44

5+
/// @file gl/algorithm/topology/topological_sort.hpp
6+
/// @brief Algorithms for computing the topological ordering of directed acyclic graphs.
7+
58
#pragma once
69

710
#include "gl/algorithm/core.hpp"
811
#include "gl/algorithm/templates/bfs.hpp"
912

1013
namespace gl::algorithm {
1114

15+
/// @ingroup GL GL-Algorithm
16+
/// @brief Computes a topological ordering of the vertices in a Directed Acyclic Graph (DAG).
17+
///
18+
/// This implementation relies on Kahn's Algorithm. It utilizes the generic @ref gl::algorithm::bfs "bfs"
19+
/// template, seeding the queue with all vertices that have an in-degree of 0. As vertices are processed,
20+
/// the in-degrees of adjacent vertices are iteratively decremented.
21+
///
22+
/// If the final sorted order does not contain all vertices in the graph, it indicates the presence
23+
/// of a cycle, meaning the graph is not a DAG.
24+
///
25+
/// ### Example Usage
26+
/// ```cpp
27+
/// if (auto top_order = gl::algorithm::topological_sort(graph)) { // (1)!
28+
/// std::cout << "Topological Order: "
29+
/// << gl::io::range_formatter(top_order.value()) // (2)!
30+
/// << '\n';
31+
/// }
32+
/// else {
33+
/// std::cout << "Graph contains a cycle!\n";
34+
/// }
35+
/// ```
36+
///
37+
/// 1\. Attempts to compute the ordering. Fails and returns `std::nullopt` if a cycle is detected.
38+
///
39+
/// 2\. Prints the topologically sorted vector of vertex IDs using the @ref gl::io::range_formatter "range_formatter" helper.
40+
///
41+
/// > [!INFO] Algorithmic Complexity
42+
/// >
43+
/// > The time complexity depends entirely on the underlying representation of `GraphType`:
44+
/// >
45+
/// > - **Adjacency List Representations**: \f$O(|V| + |E|)\f$
46+
/// > - *Includes:* @ref gl::impl::list_t "list_t" and @ref gl::impl::flat_list_t "flat_list_t".
47+
/// >
48+
/// > - **Dense Adjacency Matrix Representations**: \f$O(|V|^2)\f$
49+
/// > - *Includes:* @ref gl::impl::matrix_t "matrix_t" and @ref gl::impl::flat_matrix_t "flat_matrix_t".
50+
/// > - *Note:* Iterating over adjacent vertices requires scanning the entire \f$|V|\f$-length matrix row.
51+
///
52+
/// ### Template Parameters
53+
/// | Parameter | Description |
54+
/// | :-------- | :--- |
55+
/// | `G` | The type of the directed graph being traversed. Must satisfy [**c_directed_graph**](gl_concepts.md#gl-traits-c-directed-graph) |
56+
/// | `PreVisitCallback` | Type of the callable executed immediately before a vertex is pushed into the sort order. |
57+
/// | `PostVisitCallback` | Type of the callable executed after all adjacent edges of a vertex are evaluated. |
58+
///
59+
/// @param graph The directed graph to evaluate.
60+
/// @param pre_visit Hook executed immediately before the internal sort logic processes a vertex.
61+
/// @param post_visit Hook executed after all adjacent edges of the current vertex have been evaluated and their in-degrees decremented.
62+
/// @return An `std::optional` containing a vector of vertex IDs in topological order or `std::nullopt` if the graph is not a DAG.
1263
template <
1364
traits::c_directed_graph G,
1465
traits::c_optional_callback<void, typename G::id_type> PreVisitCallback = empty_callback,

include/gl/algorithm/traversal/breadth_first_search.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace gl::algorithm {
1717
/// @ingroup GL GL-Algorithm
1818
/// @brief Executes a concrete Breadth-First Search (BFS) traversal over the graph.
1919
///
20-
/// This function utilizes the generic @ref gl::algorithm::bfs "bfs" engine to perform a standard queue-based traversal.
20+
/// This function utilizes the generic @ref gl::algorithm::bfs "bfs" template to perform a standard queue-based traversal.
2121
/// It automatically manages the visited states, predecessor tracking, and queue initialization.
2222
///
2323
/// If a specific `root_vertex_id` is provided, the algorithm explores only the connected component

include/gl/algorithm/traversal/depth_first_search.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace gl::algorithm {
1717
/// @ingroup GL GL-Algorithm
1818
/// @brief Executes a concrete iterative Depth-First Search (DFS) traversal over the graph.
1919
///
20-
/// This function utilizes the generic @ref gl::algorithm::dfs "dfs" engine to perform a standard, stack-based traversal.
20+
/// This function utilizes the generic @ref gl::algorithm::dfs "dfs" template to perform a standard, stack-based traversal.
2121
/// It automatically manages the visited states and predecessor tracking.
2222
///
2323
/// If a specific `root_vertex_id` is provided, the algorithm explores only the connected component
@@ -124,7 +124,7 @@ result_type<Result, predecessors_map<G>> depth_first_search(
124124
/// @ingroup GL GL-Algorithm
125125
/// @brief Executes a concrete recursive Depth-First Search (DFS) traversal over the graph.
126126
///
127-
/// This function relies on the generic @ref gl::algorithm::r_dfs "r_dfs" engine. Instead of a
127+
/// This function relies on the generic @ref gl::algorithm::r_dfs "r_dfs" template. Instead of a
128128
/// heap-allocated stack, it utilizes the C++ call stack to navigate the graph.
129129
///
130130
/// > [!WARNING] Call Stack Depth

0 commit comments

Comments
 (0)