Graph
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#ifndef INCLUDED_C_FLOYDWARSHALL_H
|
||||
#define INCLUDED_C_FLOYDWARSHALL_H
|
||||
|
||||
#ifndef INCLUDED_C_EDGEWEIGHTEDDIGRAPH_H
|
||||
#include <c_EdgeWeightedDigraph.h>
|
||||
#endif /*INCLUDED_C_EDGEWEIGHTEDDIGRAPH_H*/
|
||||
|
||||
#ifndef INCLUDED_C_EDGEIDLIST_H
|
||||
#include <c_EdgeIdList.h>
|
||||
#endif /*INCLUDED_C_EDGEIDLIST_H*/
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
typedef struct {
|
||||
double** dist_to; /* dist_to[u][v] = distance of shortest path from u to v */
|
||||
c_size_t** next_vertex; /* next_vertex[u][v] = immediate next vertex on path from u to v */
|
||||
c_size_t** edge_to; /* edge_to[u][v] = global directed edge_id for choice transition u -> next */
|
||||
c_size_t V; /* Total number of vertices in the digraph */
|
||||
c_bool_t has_neg_cycle; /* Flag indicating if a negative cycle was intercepted */
|
||||
c_Allocator_t allocator; /* Deep copy of the user-provided allocator */
|
||||
} c_FloydWarshall_t;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
/**
|
||||
* @brief Computes all-pairs shortest paths in a general edge-weighted digraph.
|
||||
* @param allocator Explicit allocator context used to configure internal tracking state memory
|
||||
*/
|
||||
c_err_t c_FloydWarshall_Init(c_FloydWarshall_t* self, c_EdgeWeightedDigraph_t* graph, c_Allocator_t* allocator);
|
||||
|
||||
/**
|
||||
* @brief Releases internal tracking buffers safely.
|
||||
*/
|
||||
void c_FloydWarshall_Destroy(c_FloydWarshall_t* self);
|
||||
|
||||
/**
|
||||
* @brief Is there a directed path from vertex 'u' to vertex 'v'?
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE c_bool_t c_FloydWarshall_HasPath(c_FloydWarshall_t* self, c_size_t u, c_size_t v) {
|
||||
if (!self || self->has_neg_cycle || u >= self->V || v >= self->V || !self->dist_to) return C_FALSE;
|
||||
return self->dist_to[u][v] < DBL_MAX;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the distance of the shortest path from vertex 'u' to vertex 'v'.
|
||||
* @return Distance value, or DBL_MAX if unreachable / parameter error
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE double c_FloydWarshall_Dist(c_FloydWarshall_t* self, c_size_t u, c_size_t v) {
|
||||
if (!self || u >= self->V || v >= self->V || !self->dist_to) return DBL_MAX;
|
||||
if (self->has_neg_cycle) return -DBL_MAX;
|
||||
return self->dist_to[u][v];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Does the edge-weighted digraph contain any negative cycles?
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
c_bool_t c_FloydWarshall_HasNegativeCycle(c_FloydWarshall_t* self) {
|
||||
return self ? self->has_neg_cycle : C_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reconstructs the exact shortest path from vertex 'u' to vertex 'v' and appends it to out_path.
|
||||
* @param out_path An initialized c_EdgeIdList_t container to collect the sequence of global directed edge_ids.
|
||||
*/
|
||||
c_err_t c_FloydWarshall_Path(c_FloydWarshall_t* self, c_size_t u, c_size_t v, c_EdgeIdList_t* out_path);
|
||||
|
||||
|
||||
#endif /*INCLUDED_C_FLOYDWARSHALL_H*/
|
||||
Reference in New Issue
Block a user