67 lines
2.5 KiB
C
67 lines
2.5 KiB
C
#ifndef INCLUDED_C_DIJKSTRASP_H
|
|
#define INCLUDED_C_DIJKSTRASP_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 tracking matrix */
|
|
c_size_t s; /* The starting 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_DijkstraSP_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_DijkstraSP_Init(c_DijkstraSP_t* self, c_EdgeWeightedDigraph_t* graph, c_size_t s, c_Allocator_t* allocator);
|
|
|
|
/**
|
|
* @brief Releases internal tracking buffers safely.
|
|
*/
|
|
void c_DijkstraSP_Destroy(c_DijkstraSP_t* self);
|
|
|
|
/**
|
|
* @brief Is there a directed path from the source vertex 's' to vertex 'v'?
|
|
*/
|
|
C_STATIC_FORCE_INLINE
|
|
c_bool_t c_DijkstraSP_HasPathTo(c_DijkstraSP_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_DijkstraSP_DistTo(c_DijkstraSP_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_DijkstraSP_PathTo(c_DijkstraSP_t* self, c_size_t v, c_VertexIdList_t* out_path);
|
|
|
|
|
|
|
|
#endif /*INCLUDED_C_DIJKSTRASP_H*/
|