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

60 lines
2.2 KiB
C

#ifndef INCLUDED_C_EDGEWEIGHTEDDIRECTEDCYCLE_H
#define INCLUDED_C_EDGEWEIGHTEDDIRECTEDCYCLE_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_bool_t* marked; /* Visited tracking array (size of graph->V) */
c_size_t* edge_to; /* edge_to[w] = global entering directed edge_id on path to w */
c_bool_t* on_stack; /* Keeps track of vertices currently active on the emulated DFS stack */
c_VertexIdList_t cycle; /* Stores the cycle edge_id sequence if a cycle is discovered */
c_Allocator_t allocator; /* Deep copy of the user-provided allocator */
} c_EdgeWeightedDirectedCycle_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
/**
* @brief Non-recursively determines if an edge-weighted digraph contains a directed cycle.
* @param allocator Explicit allocator context used to configure internal tracking state memory
*/
c_err_t c_EdgeWeightedDirectedCycle_Init(c_EdgeWeightedDirectedCycle_t* self, c_EdgeWeightedDigraph_t* graph, c_Allocator_t* allocator);
/**
* @brief Releases internal tracking buffers safely.
*/
void c_EdgeWeightedDirectedCycle_Destroy(c_EdgeWeightedDirectedCycle_t* self);
/**
* @brief Does the edge-weighted digraph contain a directed cycle?
*/
C_STATIC_FORCE_INLINE
c_bool_t c_EdgeWeightedDirectedCycle_HasCycle(c_EdgeWeightedDirectedCycle_t* self) {
if (!self) return C_FALSE;
return !c_VertexIdList_IsEmpty(&self->cycle);
}
/**
* @brief Gets the collection of global directed edge IDs that form the detected cycle loop.
* @param out_cycle An initialized c_VertexIdList_t container to collect the edge sequence.
*/
c_err_t c_EdgeWeightedDirectedCycle_GetCycle(c_EdgeWeightedDirectedCycle_t* self, c_VertexIdList_t* out_cycle);
#endif /*INCLUDED_C_EDGEWEIGHTEDDIRECTEDCYCLE_H*/