Files
cKit/Graph/c_TarjanSCC.h
2026-09-07 18:48:16 +08:00

67 lines
2.6 KiB
C

#ifndef INCLUDED_C_TARJANSCC_H
#define INCLUDED_C_TARJANSCC_H
#ifndef INCLUDED_C_DIGRAPH_H
#include <c_Digraph.h>
#endif /*INCLUDED_C_DIGRAPH_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
c_bool_t* marked; /* Visited tracking array (size of graph->V) */
c_size_t* id; /* id[v] = component identifier containing v (0 to count-1) */
c_size_t* pre; /* pre[v] = preorder index of vertex v */
c_size_t* low; /* low[v] = low link of vertex v */
c_size_t* stack; /* Explicit vertex stack buffer for component mapping tracking */
c_size_t stack_size; /* Current active elements size in stack tracker */
c_bool_t* on_stack; /* Quick boolean lookup mapping for stack memberships */
c_size_t pre_counter; /* Preorder index sequencing index variable counter */
c_size_t count; /* Total number of strongly connected components discovered */
c_size_t V;
c_Allocator_t allocator; /* Deep copy of the user-provided allocator */
} c_TarjanSCC_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
/**
* @brief Computes strongly connected components for the specified digraph using Tarjan's algorithm.
* @param allocator Explicit allocator context used to allocate internal routing arrays
*/
c_err_t c_TarjanSCC_Init(c_TarjanSCC_t* self, c_Digraph_t* graph, c_Allocator_t* allocator);
/**
* @brief Releases internal tracking buffers
*/
void c_TarjanSCC_Destroy(c_TarjanSCC_t* self);
/**
* @brief Are vertices 'v' and 'w' strongly connected (belong to the same SCC)?
*/
C_STATIC_FORCE_INLINE
c_bool_t c_TarjanSCC_StronglyConnected(c_TarjanSCC_t* self, c_size_t v, c_size_t w) {
if (!self || !self->id || v >= self->V || w >= self->V) return C_FALSE;
return self->id[v] == self->id[w];
}
/**
* @brief Returns the component id containing vertex 'v'
* @return Component integer ID (0 to count-1), or C_SIZE_MAX on boundary check failures
*/
C_STATIC_FORCE_INLINE
c_size_t c_TarjanSCC_GetId(const c_TarjanSCC_t* self, c_size_t v) {
if (!self || !self->id || v >= self->V) return C_SIZE_MAX;
return self->id[v];
}
/**
* @brief Returns the total number of strongly connected components
*/
C_STATIC_FORCE_INLINE
c_size_t c_TarjanSCC_GetCount(const c_TarjanSCC_t* self) {
return self ? self->count : 0;
}
#endif /*INCLUDED_C_TARJANSCC_H*/