An exercise in detecting cycles in digraphs
- Clone the repository:
git clone https://github.com/amstokely/dag.git
- Navigate into the project's root directory:
cd dag
- Install CMake if it is not already installed:
If you have conda installed, you can install cmake via:
sudo apt-get install cmake
conda install -c conda-forge cmake
- Create a build directory and navigate into it:
mkdir build && cd build
- If building the Python interface, install SWIG, pytest, networkx, and NumPy with either pip or conda:
and set an environment variable: FLAGS, equal to the following:
pip install numpy swig pytest networkx
FLAGS="-DBUILD_PYTHON_BINDINGS=ON"
- Run CMake to configure the build:
cmake ${FLAGS} ..
- Build the project:
cmake --build . -j
- Build the Python interface:
make PythonInstall
- Run the C++ tests:
ctest
- Run the Python tests
cd .. pytest
To run all the test graph examples, navigate into the project's root directory and set an environment variable equal to the current working directory:
export DAG_ROOT_DIR=$(pwd)
Now navigate into the bin directory
cd $DAG_ROOT_DIR/bin
and execute the following command:
for graph in $DAG_ROOT_DIR/test_graphs/*; do ./dag $graph; done
To check if a NumPy adjacency matrix is a DAG using the Python interface, simply import the dag module:
import dag
and call the isDAGAdjacencyMatrix of isDAGBitArray function depending on if the numpy array is a standard adjacency matrix or a bitwise adjacency matrix, respectively.
print(dag.isDAGAdjacencyMatrix(adjacency_matrix))
Here, adjacency_matrix is a int32 NumPy array that represents a directed graph.
Let
Fig. 1: A simple directed graph.
A directed path is a sequence of vertices
A digraph
If we assume that the vertices are numbered
Fig. 3: A bit-array representation of an adjacency list.
In this exercise, our objective is to write a function in C/C++ that will
determine whether a digraph is a dag. The main driver program, which is
provided in the file main.c
, takes as a command-line argument the name of a file containing a
representation of a digraph and reads the graph into an array of uint32_t
values. The size of the array is equal to the number of vertices in the digraph,
and each element
The function to determine whether a digraph is a dag has the following prototype:
int is_dag(uint32_t digraph[], int n_vertices);
This function takes as input an adjacency list representation of a digraph
and the number of vertices in the graph. The size of the digraph
array is
n_vertices
, with n_vertices
at most 32. The function returns 1 if the given
graph is a dag and 0 otherwise.
After implementing the is_dag
function, the program should be run for each of
the digraphs in the test_graphs/
sub-directory of this repository.