For using the igraph C library
igraph_error_t igraph_find_cycle(const igraph_t *graph, igraph_vector_int_t *vertices, igraph_vector_int_t *edges, igraph_neimode_t mode);
This function returns a cycle of the graph, if there is one. If the graph is acyclic, it returns empty vectors.
Arguments:
|
The input graph. |
|
Pointer to an integer vector. If a cycle is found, its vertices will be stored here. Otherwise the vector will be empty. |
|
Pointer to an integer vector. If a cycle is found, its edges will be stored here. Otherwise the vector will be empty. |
|
A constant specifying how edge directions are
considered in directed graphs. Valid modes are:
|
Returns:
Error code. |
Time complexity: O(|V|+|E|), where |V| and |E| are the number of vertices and edges in the original input graph.
See also:
|
igraph_error_t igraph_simple_cycles( const igraph_t *graph, igraph_vector_int_list_t *vertices, igraph_vector_int_list_t *edges, igraph_neimode_t mode, igraph_int_t min_cycle_length, igraph_int_t max_cycle_length, igraph_int_t max_results);
This function is experimental and its signature is not considered final yet. We reserve the right to change the function signature without changing the major version of igraph. Use it at your own risk.
This function searches for all simple cycles using Johnson's cycle detection algorithm, and stores them in the provided vector lists. A simple cycle is a cycle (i.e. closed path) without repeated vertices.
Reference:
Johnson DB: Finding all the elementary circuits of a directed graph. SIAM J. Comput. 4(1):77-84. https://doi.org/10.1137/0204007
Arguments:
|
The graph to search for cycles in. |
|
The vertex IDs of each cycle will be stored here. |
|
The edge IDs of each cycle will be stored here. |
|
A constant specifying how edge directions are
considered in directed graphs. Valid modes are:
|
|
Limit the minimum length of cycles to search for. Pass a negative value to search for all cycles. |
|
Limit the maximum length of cycles to search for. Pass a negative value to search for all cycles. |
|
At most this many cycles will be recorded. If
negative, or |
Returns:
Error code. |
See also:
|
igraph_error_t igraph_simple_cycles_callback( const igraph_t *graph, igraph_neimode_t mode, igraph_int_t min_cycle_length, igraph_int_t max_cycle_length, igraph_cycle_handler_t *callback, void *arg);
This function is experimental and its signature is not considered final yet. We reserve the right to change the function signature without changing the major version of igraph. Use it at your own risk.
This function searches for all simple cycles using Johnson's cycle detection algorithm, and calls a function for each. A simple cycle is a cycle (i.e. closed path) without repeated vertices.
Reference:
Johnson DB: Finding all the elementary circuits of a directed graph. SIAM J. Comput. 4(1):77-84. https://doi.org/10.1137/0204007
Arguments:
|
The graph to search for |
|
A constant specifying how edge directions are
considered in directed graphs. Valid modes are:
|
|
Limit the minimum length of cycles to search for. Pass a negative value to search for all cycles. |
|
Limit the maximum length of cycles to search for. Pass a negative value to search for all cycles. |
|
A function to call for each cycle that is found.
See also |
|
This parameter will be passed to |
Returns:
Error code. |
See also:
|
typedef igraph_error_t igraph_cycle_handler_t( const igraph_vector_int_t *vertices, const igraph_vector_int_t *edges, void *arg);
Callback type, called by igraph_simple_cycles_callback()
when
a cycle is found.
Arguments:
|
The vertices of the current cycle. Must not be modified. |
|
The edges of the current cycle. Must not be modified. |
|
The extra parameter passed to |
Returns:
Error code; |
igraph_is_dag
— Checks whether a graph is a directed acyclic graph (DAG).igraph_is_acyclic
— Checks whether a graph is acyclic or not.igraph_topological_sorting
— Calculate a possible topological sorting of the graph.igraph_feedback_arc_set
— Feedback arc set of a graph using exact or heuristic methods.igraph_feedback_vertex_set
— Feedback vertex set of a graph.
igraph_error_t igraph_is_dag(const igraph_t* graph, igraph_bool_t *res);
A directed acyclic graph (DAG) is a directed graph with no cycles.
This function returns false
for undirected graphs.
The return value of this function is cached in the graph itself; calling the function multiple times with no modifications to the graph in between will return a cached value in O(1) time.
Arguments:
|
The input graph. |
|
Pointer to a boolean constant, the result is stored here. |
Returns:
Error code. |
Time complexity: O(|V|+|E|), where |V| and |E| are the number of vertices and edges in the original input graph.
See also:
|
igraph_error_t igraph_is_acyclic(const igraph_t *graph, igraph_bool_t *res);
This function checks whether a graph has any cycles. Edge directions are considered, i.e. in directed graphs, only directed cycles are searched for.
The result of this function is cached in the graph itself; calling the function multiple times with no modifications to the graph in between will return a cached value in O(1) time.
Arguments:
|
The input graph. |
|
Pointer to a boolean constant, the result is stored here. |
Returns:
Error code. |
See also:
|
Time complexity: O(|V|+|E|), where |V| and |E| are the number of vertices and edges in the original input graph.
igraph_error_t igraph_topological_sorting( const igraph_t* graph, igraph_vector_int_t *res, igraph_neimode_t mode);
A topological sorting of a directed acyclic graph (DAG) is a linear ordering of its vertices where each vertex comes before all nodes to which it has edges. Every DAG has at least one topological sort, and may have many. This function returns one possible topological sort among them. If the graph contains any cycles that are not self-loops, an error is raised.
Arguments:
|
The input graph. |
|
Pointer to a vector, the result will be stored here. It will be resized if needed. |
|
Specifies how to use the direction of the edges.
For |
Returns:
Error code. |
Time complexity: O(|V|+|E|), where |V| and |E| are the number of vertices and edges in the original input graph.
See also:
|
Example 18.1. File examples/simple/igraph_topological_sorting.c
#include <igraph.h> #include <stdio.h> int main(void) { igraph_t graph; igraph_vector_int_t res; /* Initialize the library. */ igraph_setup(); /* Test graph taken from http://en.wikipedia.org/wiki/Topological_sorting * @ 05.03.2006 */ igraph_small(&graph, 8, IGRAPH_DIRECTED, 0, 3, 0, 4, 1, 3, 2, 4, 2, 7, 3, 5, 3, 6, 3, 7, 4, 6, -1); igraph_vector_int_init(&res, 0); /* Sort the vertices in "increasing" order. */ igraph_topological_sorting(&graph, &res, IGRAPH_OUT); igraph_vector_int_print(&res); printf("\n"); /* Sort the vertices in "decreasing" order. */ igraph_topological_sorting(&graph, &res, IGRAPH_IN); igraph_vector_int_print(&res); /* Destroy data structures when done using them. */ igraph_destroy(&graph); igraph_vector_int_destroy(&res); return 0; }
igraph_error_t igraph_feedback_arc_set( const igraph_t *graph, igraph_vector_int_t *result, const igraph_vector_t *weights, igraph_fas_algorithm_t algo);
A feedback arc set is a set of edges whose removal makes the graph acyclic. We are usually interested in minimum feedback arc sets, i.e. sets of edges whose total weight is the smallest among all the feedback arc sets.
For undirected graphs, the solution is simple: one has to find a maximum weight
spanning tree and then remove all the edges not in the spanning tree. For directed
graphs, this is an NP-complete problem, and various heuristics are usually used to
find an approximate solution to the problem. This function implements both exact
methods and heuristics, selectable with the algo
parameter.
References:
Eades P, Lin X and Smyth WF: A fast and effective heuristic for the feedback arc set problem. Information Processing Letters 47(6), pp 319-323 (1993). https://doi.org/10.1016/0020-0190(93)90079-O
Baharev A, Hermann S, Arnold N and Tobias A: An Exact Method for the Minimum Feedback Arc Set Problem. ACM Journal of Experimental Algorithmics 26, 1–28 (2021). https://doi.org/10.1145/3446429.
Arguments:
|
The graph object. |
||||||||
|
An initialized vector, the result will be written here. |
||||||||
|
Weight vector or |
||||||||
|
The algorithm to use to solve the problem if the graph is directed. Possible values:
|
Returns:
Error code:
|
Example 18.2. File examples/simple/igraph_feedback_arc_set.c
#include <igraph.h> #include <string.h> int main(void) { igraph_t g; igraph_vector_t weights; igraph_vector_int_t result; igraph_bool_t dag; /* Initialize the library. */ igraph_setup(); igraph_vector_int_init(&result, 0); /***********************************************************************/ /* Approximation with Eades' method */ /***********************************************************************/ /* Simple unweighted graph */ igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 0, 2, 3, 2, 4, 0, 4, 4, 3, 5, 0, 6, 5, -1); igraph_feedback_arc_set(&g, &result, 0, IGRAPH_FAS_APPROX_EADES); igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 1; } igraph_destroy(&g); /* Simple weighted graph */ igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 0, 2, 3, 2, 4, 0, 4, 4, 3, 5, 0, 6, 5, -1); igraph_vector_init_int_end(&weights, -1, 1, 1, 3, 1, 1, 1, 1, 1, 1, -1); igraph_feedback_arc_set(&g, &result, &weights, IGRAPH_FAS_APPROX_EADES); igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 2; } igraph_vector_destroy(&weights); igraph_destroy(&g); /* Simple unweighted graph with loops */ igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 0, 2, 3, 2, 4, 0, 4, 4, 3, 5, 0, 6, 5, 1, 1, 4, 4, -1); igraph_feedback_arc_set(&g, &result, 0, IGRAPH_FAS_APPROX_EADES); igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 3; } igraph_destroy(&g); /* Null graph */ igraph_empty(&g, 0, IGRAPH_DIRECTED); igraph_feedback_arc_set(&g, &result, NULL, IGRAPH_FAS_APPROX_EADES); if (igraph_vector_int_size(&result) != 0) { return 4; } igraph_destroy(&g); /* Singleton graph */ igraph_empty(&g, 1, IGRAPH_DIRECTED); igraph_feedback_arc_set(&g, &result, NULL, IGRAPH_FAS_APPROX_EADES); if (igraph_vector_int_size(&result) != 0) { return 5; } igraph_destroy(&g); igraph_vector_int_destroy(&result); return 0; }
Example 18.3. File examples/simple/igraph_feedback_arc_set_ip.c
#include <igraph.h> #include <string.h> int main(void) { igraph_setup(); igraph_t g; igraph_vector_t weights; igraph_vector_int_t result; igraph_bool_t dag; igraph_error_t retval; /* Initialize the library. */ igraph_setup(); igraph_vector_int_init(&result, 0); igraph_set_error_handler(&igraph_error_handler_printignore); /***********************************************************************/ /* Exact solution with integer programming */ /***********************************************************************/ /* Simple unweighted graph */ igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 0, 2, 3, 2, 4, 0, 4, 4, 3, 5, 0, 6, 5, -1); retval = igraph_feedback_arc_set(&g, &result, 0, IGRAPH_FAS_EXACT_IP); if (retval == IGRAPH_UNIMPLEMENTED) { return 77; } igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 1; } igraph_destroy(&g); /* Simple weighted graph */ igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 0, 2, 3, 2, 4, 0, 4, 4, 3, 5, 0, 6, 5, -1); igraph_vector_init_int_end(&weights, -1, 1, 1, 3, 1, 1, 1, 1, 1, 1, -1); igraph_feedback_arc_set(&g, &result, &weights, IGRAPH_FAS_EXACT_IP); igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 2; } igraph_vector_destroy(&weights); igraph_destroy(&g); /* Simple unweighted graph with loops */ igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 0, 2, 3, 2, 4, 0, 4, 4, 3, 5, 0, 6, 5, 1, 1, 4, 4, -1); igraph_feedback_arc_set(&g, &result, 0, IGRAPH_FAS_EXACT_IP); igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 3; } igraph_destroy(&g); /* Disjoint union of two almost identical graphs */ igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 0, 2, 3, 2, 4, 0, 4, 4, 3, 5, 0, 6, 5, 1, 1, 4, 4, 7, 8, 8, 9, 9, 7, 9, 10, 9, 11, 7, 11, 11, 10, 12, 7, 13, 12, -1); igraph_feedback_arc_set(&g, &result, 0, IGRAPH_FAS_EXACT_IP); igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 4; } igraph_destroy(&g); /* Graph with lots of isolated vertices */ igraph_small(&g, 10000, IGRAPH_DIRECTED, 0, 1, -1); igraph_feedback_arc_set(&g, &result, 0, IGRAPH_FAS_EXACT_IP); igraph_vector_int_print(&result); igraph_delete_edges(&g, igraph_ess_vector(&result)); igraph_is_dag(&g, &dag); if (!dag) { return 5; } igraph_destroy(&g); /* Null graph */ igraph_empty(&g, 0, IGRAPH_DIRECTED); igraph_feedback_arc_set(&g, &result, NULL, IGRAPH_FAS_EXACT_IP); if (igraph_vector_int_size(&result) != 0) { return 6; } igraph_destroy(&g); /* Singleton graph */ igraph_empty(&g, 1, IGRAPH_DIRECTED); igraph_feedback_arc_set(&g, &result, NULL, IGRAPH_FAS_EXACT_IP); if (igraph_vector_int_size(&result) != 0) { return 7; } igraph_destroy(&g); igraph_vector_int_destroy(&result); return 0; }
Time complexity: depends on algo
, see the time complexities there.
igraph_error_t igraph_feedback_vertex_set( const igraph_t *graph, igraph_vector_int_t *result, const igraph_vector_t *vertex_weights, igraph_fvs_algorithm_t algo);
A feedback vertex set is a set of vertices whose removal makes the graph acyclic. Finding a minimum feedback vertex set is an NP-complete problem, both on directed and undirected graphs.
Arguments:
|
The graph. |
||
|
An initialized vector, the result will be written here. |
||
|
Vertex weight vector or |
||
|
Algorithm to use. Possible values:
|
Returns:
Error code. |
Time complexity: depends on algo
, see the time complexities there.
These functions calculate whether an Eulerian path or cycle exists and if so, can find them.
igraph_error_t igraph_is_eulerian(const igraph_t *graph, igraph_bool_t *has_path, igraph_bool_t *has_cycle);
An Eulerian path traverses each edge of the graph precisely once. A closed Eulerian path is referred to as an Eulerian cycle.
Arguments:
|
The graph object. |
|
Pointer to a Boolean, will be set to true if an Eulerian path exists.
Must not be |
|
Pointer to a Boolean, will be set to true if an Eulerian cycle exists.
Must not be |
Returns:
Error code:
|
Time complexity: O(|V|+|E|), the number of vertices plus the number of edges.
igraph_error_t igraph_eulerian_cycle( const igraph_t *graph, igraph_vector_int_t *edge_res, igraph_vector_int_t *vertex_res);
Finds an Eulerian cycle, if it exists. An Eulerian cycle is a closed path that traverses each edge precisely once.
If the graph has no edges, a zero-length cycle is returned.
This function uses Hierholzer's algorithm.
Arguments:
|
The graph object. |
|
Pointer to an initialised vector. The indices of edges
belonging to the cycle will be stored here. May be |
|
Pointer to an initialised vector. The indices of vertices
belonging to the cycle will be stored here. The first and
last vertex in the vector will be the same. May be |
Returns:
Error code:
|
Time complexity: O(|V|+|E|), the number of vertices plus the number of edges.
igraph_error_t igraph_eulerian_path( const igraph_t *graph, igraph_vector_int_t *edge_res, igraph_vector_int_t *vertex_res);
Finds an Eulerian path, if it exists. An Eulerian path traverses each edge precisely once.
If the graph has no edges, a zero-length path is returned.
This function uses Hierholzer's algorithm.
Arguments:
|
The graph object. |
|
Pointer to an initialised vector. The indices of edges
belonging to the path will be stored here. May be |
|
Pointer to an initialised vector. The indices of vertices
belonging to the path will be stored here. May be |
Returns:
Error code:
|
Time complexity: O(|V|+|E|), the number of vertices plus the number of edges.
igraph_error_t igraph_fundamental_cycles( const igraph_t *graph, const igraph_vector_t *weights, igraph_vector_int_list_t *result, igraph_int_t start_vid, igraph_real_t bfs_cutoff);
This function is experimental and its signature is not considered final yet. We reserve the right to change the function signature without changing the major version of igraph. Use it at your own risk.
This function computes a fundamental cycle basis associated with a breadth-first search tree of the graph.
Edge directions are ignored. Multi-edges and self-loops are supported.
Arguments:
|
The graph object. |
|
Currently unused. |
|
An initialized integer vector list. The result will be stored here, each vector containing the edge IDs of a basis element. |
|
If negative, a complete fundamental cycle basis is returned. If a vertex ID, the fundamental cycles associated with the BFS tree rooted in that vertex will be returned, only for the weakly connected component containing that vertex. |
|
If negative, a complete cycle basis is returned. Otherwise, only
cycles of length |
Returns:
Error code. |
See also:
Time complexity: O(|V| + |E|).
igraph_error_t igraph_minimum_cycle_basis( const igraph_t *graph, const igraph_vector_t *weights, igraph_vector_int_list_t *result, igraph_real_t bfs_cutoff, igraph_bool_t complete, igraph_bool_t use_cycle_order);
This function is experimental and its signature is not considered final yet. We reserve the right to change the function signature without changing the major version of igraph. Use it at your own risk.
This function computes a minimum weight cycle basis of a graph. Currently, a modified version of Horton's algorithm is used that allows for cutoffs.
Edge directions are ignored. Multi-edges and self-loops are supported.
References:
Horton, J. D. (1987) A polynomial-time algorithm to find the shortest cycle basis of a graph, SIAM Journal on Computing, 16 (2): 358–366. https://doi.org/10.1137%2F0216026
Arguments:
|
The graph object. |
|
Currently unused. |
|
An initialized integer vector list, the elements of the cycle basis will be stored here as vectors of edge IDs. |
|
If negative, an exact minimum cycle basis is returned. Otherwise
only those cycles in the result will be part of some minimum cycle basis which
are of size |
|
Boolean value. Used only when |
|
If true, each cycle is returned in natural order: the edge IDs will appear ordered along the cycle. This comes at a small performance cost. If false, no guarantees are given about the ordering of edge IDs within cycles. This parameter exists solely to control performance tradeoffs. |
Returns:
Error code. |
See also:
Time complexity: TODO.
← Chapter 17. Structural properties of graphs | Chapter 19. Cliques and independent vertex sets → |