cycle_basis#
- graph_tool.structure.cycle_basis(g, minimum=False, weights=None, edges=False)[git][source]#
Return a basis of the cycle space of the graph.
- Parameters:
- g
Graph Graph to be used. If it is directed, it is treated as undirected.
- minimum
bool(optional, default:False) If
True, a basis of minimum total weight is returned; otherwise the fundamental cycles of a spanning forest.- weights
EdgePropertyMap(optional, default:None) Non-negative edge weights, used if
minimum == True. If not supplied, every edge has unit weight.- edges
bool(optional, default:False) If
True, the cycles are given by the indices of their edges, otherwise by their vertices.
- g
- Returns:
- cycles
listofnumpy.ndarray The cycles, each given by its vertices (or edges) in the order in which they are traversed. A minimum basis is sorted by weight.
- cycles
See also
find_cycleA single cycle of the graph
all_circuitsAll cycles of a directed graph
chordless_cyclesAll chordless cycles
girthLength of the shortest cycle
Notes
The cycle space of an undirected graph is the vector space over \(\mathrm{GF}(2)\) spanned by the edge sets of its cycles, with the symmetric difference as the sum; its dimension is \(E - V + C\), where \(C\) is the number of connected components. Self-loops and parallel edges count as cycles of lengths one and two.
The fundamental cycles are formed by each edge outside a breadth-first spanning forest and the forest path between its endpoints, obtained in time \(O(V + \sum_i L_i)\), where \(L_i\) is the length of cycle \(i\).
A minimum cycle basis minimizes the sum of the weights of its cycles. It is computed with the algorithm of de Pina [pina-applications-1995] [kavitha-cycle-2009], which finds the cycles one at a time, each as the shortest cycle that has an odd number of edges in common with a “support” vector kept orthogonal to the cycles already found. Each such cycle is a shortest path in a graph with two copies of every vertex, searched from every vertex in parallel, so that the total time is \(O(KV(E + V\log V) + K^2E)\) with \(K = E - V + C\), and the memory \(O(K^2)\); hence it is suitable only for moderate graph sizes.
Parallel implementation.
If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.
References
[pina-applications-1995]José Coelho de Pina, “Applications of shortest path methods”, PhD thesis, University of Amsterdam (1995).
[kavitha-cycle-2009]Telikepalli Kavitha, Christian Liebchen, Kurt Mehlhorn, Dimitrios Michail, Romeo Rizzi, Torsten Ueckerdt and Katharina A. Zweig, “Cycle bases in graphs: characterization, algorithms, complexity, and applications”, Computer Science Review 3, 199-243 (2009), DOI: 10.1016/j.cosrev.2009.08.001 [sci-hub, @tor]
Examples
>>> g = gt.collection.data["karate"] >>> cycles = gt.cycle_basis(g, minimum=True) >>> print(len(cycles), [len(c) for c in cycles[-3:]]) 45 [4, 4, 4]
The last three cycles of the basis can be highlighted in a drawing of the network:
>>> colors = ["#e41a1c", "#377eb8", "#4daf4a"] >>> ecolor = g.new_ep("string", val="#999999") >>> ewidth = g.new_ep("double", val=1) >>> for c, color in zip(cycles[-3:], colors): ... for u, v in zip(c, np.roll(c, -1)): ... ecolor[g.edge(u, v)] = color ... ewidth[g.edge(u, v)] = 2.5 >>> gt.graph_draw(g, g.vp.pos, vertex_fill_color="#cccccc", ... edge_color=ecolor, edge_pen_width=ewidth, ... output="cycle_basis-karate.pdf") <...>
Three cycles of the minimum cycle basis of Zachary’s karate club network, shown in red, blue, and green.#