This commit is contained in:
2026-09-07 18:48:16 +08:00
parent 1564716731
commit c945aa211f
138 changed files with 10337 additions and 150 deletions
+66
View File
@@ -0,0 +1,66 @@
#ifndef INCLUDED_C_ACYCLICSP_H
#define INCLUDED_C_ACYCLICSP_H
#ifndef INCLUDED_C_EDGEWEIGHTEDDIGRAPH_H
#include <c_EdgeWeightedDigraph.h>
#endif /*INCLUDED_C_EDGEWEIGHTEDDIGRAPH_H*/
#ifndef INCLUDED_C_VERTEXIDLIST_H
#include <c_VertexIdList.h>
#endif /*INCLUDED_C_VERTEXIDLIST_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
c_size_t* edge_to; /* edge_to[w] = entering directed edge_id on shortest path to w */
c_size_t* from_vertex; /* from_vertex[w] = source vertex parent link on path to w */
double* dist_to; /* dist_to[w] = cumulative shortest path distance from source s to w */
c_size_t s; /* The source vertex index */
c_size_t V; /* Total number of vertices in the digraph */
c_Allocator_t allocator; /* Deep copy of the user-provided allocator */
} c_AcyclicSP_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
/**
* @brief Computes a shortest-paths tree from the source vertex 's' in a directed acyclic graph (DAG).
* @param allocator Explicit allocator context used to configure internal tracking state memory
*/
c_err_t c_AcyclicSP_Init(c_AcyclicSP_t* self, c_EdgeWeightedDigraph_t* graph, c_size_t s, c_Allocator_t* allocator);
/**
* @brief Releases internal tracking buffers safely.
*/
void c_AcyclicSP_Destroy(c_AcyclicSP_t* self);
/**
* @brief Is there a directed path from the source vertex 's' to vertex 'v'?
*/
C_STATIC_FORCE_INLINE
c_bool_t c_AcyclicSP_HasPathTo(c_AcyclicSP_t* self, c_size_t v) {
if (!self || v >= self->V || !self->dist_to) return C_FALSE;
return self->dist_to[v] < DBL_MAX;
}
/**
* @brief Returns the distance of the shortest path from the source vertex 's' to vertex 'v'.
* @return Distance value, or DBL_MAX if unreachable / parameter error
*/
C_STATIC_FORCE_INLINE
double c_AcyclicSP_DistTo(c_AcyclicSP_t* self, c_size_t v) {
if (!self || v >= self->V || !self->dist_to) return DBL_MAX;
return self->dist_to[v];
}
/**
* @brief Reconstructs the exact shortest path from the source vertex 's' to vertex 'v' and appends it to out_path.
* @param out_path An initialized c_VertexIdList_t container to collect the sequence of global directed edge_ids.
*/
c_err_t c_AcyclicSP_PathTo(c_AcyclicSP_t* self, c_size_t v, c_VertexIdList_t* out_path);
#endif /*INCLUDED_C_ACYCLICSP_H*/