A fast algorithm for detecting whether a directed graph is a Directed Acyclic Graph (DAG).
This project implements an improved algorithm based on topological sorting for efficiently detecting cycles in directed graphs. The implementation uses a bidirectional adjacency list data structure for optimal performance.
Originally designed for use in streaming computation engines, this repository provides a C language demonstration implementation.
The algorithm works by iteratively removing edge nodes (nodes with zero in-degree or zero out-degree) from the graph:
- Find Terminal Nodes: Identify all nodes where either in-degree or out-degree is zero
- Remove Terminal Nodes: Delete these nodes and their associated edges from the graph
- Update Degrees: When removing edges, check if connected nodes become terminal nodes
- Repeat: Continue until no more terminal nodes can be found
- Determine Result:
- If all nodes are removed → The graph is a DAG (Directed Acyclic Graph)
- If nodes remain → The graph is a DCG (Directed Cyclic Graph)
This approach is based on the principle that a DAG must have at least one node with zero in-degree and one node with zero out-degree. Cycles prevent nodes from ever becoming terminal nodes.
.
├── dag_check.c # Main program with DAG detection logic
├── graph_point.h # Graph node data structure definitions
├── graph_point.c # Graph node operations implementation
├── linked_deque.h # Double-ended queue definitions
├── linked_deque.c # Double-ended queue implementation
├── Makefile # Build configuration
└── README.md # This file
typedef struct point_s {
int num; // Node identifier
deque_t *from; // Incoming edges (source nodes)
deque_t *to; // Outgoing edges (destination nodes)
int queued; // Queue status flag
} point_t;A linked list-based double-ended queue used for:
- Storing graph edges (both incoming and outgoing)
- Managing the queue of terminal nodes to process
- GCC compiler
- Make build tool
makemake cleanRun the compiled program:
./dag_checkThe program includes two test cases:
- A DAG (Directed Acyclic Graph) - passes the check
- A DCG (Directed Cyclic Graph) - fails the check
find_term_point done
point address : 0x...
clean point 1
point 1 delete relation ok
...
This graph is a Directed Acyclic Graph (DAG).
find_term_point done
point address : 0x...
clean point 1
point 1 delete relation fail
This graph is a Directed Cyclic Graph (DCG).
| Function | Description |
|---|---|
point_init(int num) |
Initialize a node with the given identifier |
point_free(point_t *p) |
Free a node and its memory |
point_add_edge(point_t *pa[], int from, int to) |
Add a directed edge between two nodes |
point_in_degree(point_t *p) |
Get the in-degree of a node |
point_out_degree(point_t *p) |
Get the out-degree of a node |
point_find_term(point_t *pa[], int len, deque_t *q) |
Find all terminal nodes |
point_del_rel(point_t *p, deque_t *q) |
Delete a node's relationships |
| Function | Description |
|---|---|
deque_init(deque_t *) |
Initialize an empty deque |
deque_free(deque_t *) |
Free deque memory |
deque_empty(deque_t *) |
Check if deque is empty |
deque_enqueue(deque_t *, void *) |
Add element to rear |
deque_dequeue(deque_t *) |
Remove and return front element |
deque_first(deque_t *) |
Get front element without removing |
deque_last(deque_t *) |
Get rear element without removing |
MIT License
Copyright (C) 2024 张懿曦(Zhang Yixi)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.