Files
cKit/Graph/c_BreadthFirstDirectedPaths.h
T

64 lines
2.4 KiB
C
Raw Normal View History

2026-09-07 18:48:16 +08:00
#ifndef INCLUDED_C_BREADTHFIRSTDIRECTEDPATHS_H
#define INCLUDED_C_BREADTHFIRSTDIRECTEDPATHS_H
#ifndef INCLUDED_C_DIGRAPH_H
#include <c_Digraph.h>
#endif /*INCLUDED_C_DIGRAPH_H*/
#ifndef INCLUDED_C_VERTEXIDLIST_H
#include <c_VertexIdList.h>
#endif /*INCLUDED_C_VERTEXIDLIST_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
c_bool_t* marked; /* Visited tracking array (size of graph->V) */
c_size_t* edge_to; /* edge_to[w] = last edge on shortest path from s to w */
c_size_t* dist_to; /* dist_to[w] = number of edges on shortest path from s to w */
c_size_t s; /* Source vertex */
c_Allocator_t allocator; /* Deep copy of the user-provided allocator */
} c_BreadthFirstDirectedPaths_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
/**
* @brief Computes shortest directed paths from a single source vertex 's' using BFS
* @param allocator Explicit allocator context used to allocate internal routing arrays
*/
c_err_t c_BreadthFirstDirectedPaths_Init(c_BreadthFirstDirectedPaths_t* self, const c_Digraph_t* graph, c_size_t s, c_Allocator_t* allocator);
/**
* @brief Releases internal tracking buffers
*/
void c_BreadthFirstDirectedPaths_Destroy(c_BreadthFirstDirectedPaths_t* self);
/**
* @brief Is there a directed path from the source 's' to vertex 'v'?
*/
C_STATIC_FORCE_INLINE
c_bool_t c_BreadthFirstDirectedPaths_HasPathTo(c_BreadthFirstDirectedPaths_t* self, c_size_t v, c_size_t total_V) {
if (!self || !self->marked || v >= total_V) return C_FALSE;
return self->marked[v];
}
/**
* @brief Returns the number of edges in a shortest path from the source 's' to vertex 'v'
* @return Distance value, or C_SIZE_MAX if unreachable / parameter error
*/
C_STATIC_FORCE_INLINE
c_size_t c_BreadthFirstDirectedPaths_DistTo(c_BreadthFirstDirectedPaths_t* self, c_size_t v, c_size_t total_V) {
if (!self || v >= total_V || !self->marked[v]) return C_SIZE_MAX;
return self->dist_to[v];
}
/**
* @brief Reconstructs the exact shortest path from source 's' to vertex 'v' and appends it to out_path
*/
c_err_t c_BreadthFirstDirectedPaths_PathTo(c_BreadthFirstDirectedPaths_t* self, c_size_t v, c_size_t total_V, c_VertexIdList_t* out_path);
#endif /*INCLUDED_C_BREADTHFIRSTDIRECTEDPATHS_H*/