Files
cKit/Graph/c_BellmanFordSP.h
T

82 lines
3.1 KiB
C
Raw Normal View History

2026-09-07 18:48:16 +08:00
#ifndef INCLUDED_C_BELLMANFORDSP_H
#define INCLUDED_C_BELLMANFORDSP_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 distance from source s to w */
c_bool_t* on_queue; /* on_queue[v] = is vertex v currently sitting inside the processing FIFO queue */
c_VertexIdList_t cycle; /* Catches and stores the negative cycle path sequence if found */
c_size_t cost_counter; /* Tracks number of relax calls to gauge periodic cycle check steps */
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_BellmanFordSP_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
/**
* @brief Computes a shortest-paths tree from the source vertex 's' in the edge-weighted digraph.
* @param allocator Explicit allocator context used to configure internal tracking state memory
*/
c_err_t c_BellmanFordSP_Init(c_BellmanFordSP_t* self, c_EdgeWeightedDigraph_t* graph, c_size_t s, c_Allocator_t* allocator);
/**
* @brief Releases internal tracking buffers safely.
*/
void c_BellmanFordSP_Destroy(c_BellmanFordSP_t* self);
/**
* @brief Is there a directed path from the source vertex 's' to vertex 'v'?
*/
C_STATIC_FORCE_INLINE
c_bool_t c_BellmanFordSP_HasPathTo(c_BellmanFordSP_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'.
*/
C_STATIC_FORCE_INLINE
double c_BellmanFordSP_DistTo(c_BellmanFordSP_t* self, c_size_t v) {
if (!self || v >= self->V || !self->dist_to) return DBL_MAX;
return self->dist_to[v];
}
/**
* @brief Does the edge-weighted digraph contain a negative cycle reachable from the source vertex?
*/
C_STATIC_FORCE_INLINE
c_bool_t c_BellmanFordSP_HasNegativeCycle(c_BellmanFordSP_t* self) {
if (!self) return C_FALSE;
return !c_VertexIdList_IsEmpty(&self->cycle);
}
/**
* @brief Reconstructs the exact shortest path from the source vertex 's' to vertex 'v' and appends it to out_path.
*/
c_err_t c_BellmanFordSP_PathTo(c_BellmanFordSP_t* self, c_size_t v, c_VertexIdList_t* out_path);
/**
* @brief Extracts the vertices forming the detected negative cycle loop if present.
*/
c_err_t c_BellmanFordSP_GetNegativeCycle(c_BellmanFordSP_t* self, c_VertexIdList_t* out_cycle);
#endif /*INCLUDED_C_BELLMANFORDSP_H*/