graph_tool.structure#

This module contains various functions that assess the graph structure.

Shortest paths#

shortest_distance

Calculate the distance from a source to a target vertex, or to all vertices from a given source, or the all pairs shortest paths, if the source is not specified.

shortest_path

Return the shortest path from source to target.

count_shortest_paths

Return the number of shortest paths from source to target.

all_shortest_paths

Return an iterator over all shortest paths from source to target.

all_predecessors

Return a property map with all possible predecessors in the search tree determined by dist_map and pred_map.

random_shortest_path

Return a random shortest path from source to target, uniformly sampled from all paths of equal length.

k_shortest_paths

Return the \(k\) shortest simple paths between two vertices.

widest_distance

Return the width of the widest paths from a source vertex to all others.

widest_path

Return a widest path from a source to a target vertex.

resource_constrained_path

Return a shortest path subject to constraints on the resources it consumes.

find_negative_cycle

Return a cycle of negative total weight, if there is one.

voronoi_cells

Assign every vertex to its closest seed vertex.

ego_graph

Return the subgraph induced by the vertices within a given distance of a vertex.

spanner

Return a spanner of a graph.

Distances, eccentricity, and centers#

pseudo_diameter

Compute the pseudo-diameter of the graph.

eccentricity

Compute the eccentricity of every vertex.

diameter

Compute the diameter of the graph.

radius

Compute the radius of the graph.

harmonic_diameter

Return the harmonic diameter of the graph.

center

Return the center of the graph, i.e. the vertices of minimum eccentricity.

periphery

Return the periphery of the graph, i.e. the vertices of maximum eccentricity.

barycenter

Return the barycenter of the graph, i.e. the vertices with the smallest sum of distances to all others.

average_shortest_path_length

Return the average shortest path length between the pairs of vertices.

Distance-based indices and small-world measures#

global_efficiency

Return the global efficiency of the graph.

local_efficiency

Return the local efficiency of every vertex.

wiener_index

Return the Wiener index of the graph.

hyper_wiener_index

Return the hyper-Wiener index of the graph.

schultz_index

Return the Schultz index of the graph.

gutman_index

Return the Gutman index of the graph.

s_metric

Return the s-metric of the graph.

small_world_sigma

Return the small-world coefficient sigma of a graph.

small_world_omega

Return the small-world coefficient omega of a graph.

Paths, walks, and cycles#

all_paths

Return an iterator over all paths from source to target.

all_circuits

Return an iterator over all the cycles in a directed graph.

random_walk

Sample random walks on the graph.

loop_erased_random_walk

Return a loop-erased random walk from a vertex to a set of targets.

eulerian_path

Return an Eulerian path or circuit, i.e. one that traverses every edge exactly once.

eulerize

Return the edges to duplicate to make a connected undirected graph Eulerian.

find_cycle

Return a cycle of the graph, if there is one.

cycle_basis

Return a basis of the cycle space of the graph.

girth

Return the girth of the graph, i.e. the length of its shortest cycle.

chordless_cycles

Return an iterator over the chordless cycles of the graph.

cycle_ratio

Return the maximum (or minimum) ratio of the total weight to the total time of the directed cycles.

Components and connectivity#

label_components

Label the components to which each vertex in the graph belongs.

label_largest_component

Label the largest component in the graph.

extract_largest_component

Extract the largest (strong) component in the graph as a GraphView (or Graph if prune==True).

label_out_component

Label the out-component (or simply the component for undirected graphs) of a root vertex.

count_reachable

Return the number of vertices reachable from every vertex.

make_connected

Add edges to a graph to make it connected.

label_biconnected_components

Label the edges of biconnected components, and the vertices which are articulation points.

find_bridges

Return the bridges of a graph.

local_bridges

Return the local bridges of a graph and their spans.

chain_decomposition

Return the chain decomposition of a graph.

is_semiconnected

Check whether a directed graph is semiconnected.

is_kl_connected

Return whether the graph is locally \((k, l)\)-connected.

kl_connected_subgraph

Return the largest locally \((k, l)\)-connected subgraph found by edge removal.

vertex_percolation

Compute the size of the largest or second-largest component as vertices are (virtually) removed from the graph.

edge_percolation

Compute the size of the largest or second-largest component as edges are (virtually) removed from the graph.

Core decompositions#

kcore_decomposition

Perform a k-core decomposition of the given graph.

kcore_subgraph

Return the \(k\)-core, \(k\)-shell, \(k\)-crust, or \(k\)-corona of the graph.

truss_decomposition

Perform a k-truss decomposition of the given graph.

onion_decomposition

Perform the onion decomposition of the given graph.

Cuts and expansion#

cut_size

Return the size of the cut between two sets of vertices.

volume

Return the volume of a set of vertices.

conductance

Return the conductance of the cut between two sets of vertices.

normalized_cut_size

Return the normalized size of the cut between two sets of vertices.

edge_expansion

Return the edge expansion of a set of vertices.

mixing_expansion

Return the mixing expansion of a set of vertices.

vertex_expansion

Return the vertex expansion of a set of vertices.

boundary_expansion

Return the boundary expansion of a set of vertices.

vertex_boundary

Return the vertex boundary of a set of vertices.

edge_boundary

Return the edge boundary of a set of vertices.

Spanning trees#

minimum_spanning_tree

Return the minimum spanning tree of a given graph.

random_spanning_tree

Return a random spanning tree of a given graph, which can be directed or undirected.

minimum_spanning_arborescence

Return the minimum spanning arborescence of a directed graph.

all_spanning_arborescences

Return an iterator over the spanning arborescences of a directed graph, in order of increasing (or decreasing) weight.

common_spanning_trees

Return an iterator over the spanning trees common to two graphs with corresponding edges.

Trees#

prufer_sequence

Return the Prüfer sequence of a tree.

unfold_tree

Unfold a graph into a forest by breadth-first search.

tree_broadcast_center

Return the broadcast time and the broadcast center of a tree.

tree_broadcast_time

Return the broadcast time of a tree, or from one of its vertices.

Directed graphs#

is_DAG

Return True if the graph is a directed acyclic graph (DAG).

edge_reciprocity

Calculate the edge reciprocity of the graph.

topological_sort

Return the topological sort of the given graph.

all_topological_sorts

Return an iterator over all topological orderings of a directed acyclic graph.

topological_generations

Return the topological generation of every vertex of a directed acyclic graph.

transitive_closure

Return the transitive closure graph of g.

transitive_reduction

Return the transitive reduction of a directed acyclic graph.

feedback_arc_set

Return a set of edges whose removal leaves the graph without cycles.

feedback_vertex_set

Return a set of vertices whose removal leaves the graph without cycles.

dag_longest_path

Return a longest path of a directed acyclic graph.

dag_to_branching

Return the branching of the paths from the sources of a directed acyclic graph.

antichains

Return an iterator over all antichains of a directed acyclic graph.

lowest_common_ancestors

Return the lowest common ancestors of pairs of vertices of a rooted tree or of a directed acyclic graph.

dominator_tree

Return a vertex property map containing the dominator vertices for each vertex.

dominance_frontiers

Return the dominance frontier of every vertex of a directed graph.

is_aperiodic

Check whether a strongly connected directed graph is aperiodic.

flow_hierarchy

Return the flow hierarchy of a directed graph.

cd_index

Return the CD index of a vertex of a citation graph.

is_tournament

Check whether a directed graph is a tournament.

tournament_hamiltonian_path

Return a Hamiltonian path of a tournament.

d-separation and graphical models#

is_d_separator

Check whether a set of vertices d-separates two others in a directed acyclic graph.

find_minimal_d_separator

Return a minimal d-separator of two sets of vertices in a directed acyclic graph.

is_minimal_d_separator

Check whether a set of vertices is a minimal d-separator of two others in a directed acyclic graph.

junction_tree

Return a junction tree of a graph.

Chordal graphs and treewidth#

is_chordal

Check whether a graph is chordal.

chordal_completion

Return the edges of a minimal chordal completion of a graph.

chordal_cliques

Return the maximal cliques of a chordal graph.

find_induced_nodes

Return the vertices on induced paths between two vertices of a chordal graph.

treewidth

Return an upper bound on the treewidth of an undirected graph.

Cliques#

maximal_cliques

Return an iterator over the maximal cliques of the graph.

all_cliques

Return an iterator over all cliques of a graph, maximal or not.

largest_cliques

Return all the cliques of maximum size of a graph.

maximum_weight_clique

Return a clique of maximum total vertex weight.

clique_number

Return the clique number, i.e. the number of vertices of a largest clique.

approx_maximum_clique

Return an approximate maximum clique.

large_clique_size

Return the size of a large clique found by a fast heuristic.

clique_removal

Return an approximate maximum independent vertex set and a clique cover.

ramsey_r2

Return a clique and an independent vertex set found by the Ramsey approximation.

graphlet_decomposition

Return the graphlet decomposition of a weighted graph.

Matchings, independent sets, and covers#

maximum_cardinality_matching

Find a maximum cardinality matching in the graph.

maximum_weight_matching

Find a matching of maximum total weight in the graph.

approx_minimum_maximal_matching

Return a maximal matching, which approximates the minimum maximal matching within a factor two.

k_factor

Return a \(k\)-factor of a graph.

maximal_extendability

Return the maximal extendability of a bipartite graph.

maximal_independent_vertex_set

Find a maximal independent vertex set in the graph.

independence_number

Return the size of the largest independent vertex set of a graph.

largest_independent_vertex_sets

Return all the independent vertex sets of maximum size of a graph.

maximal_independent_vertex_sets

Return an iterator over all maximal independent vertex sets of a graph.

approx_minimum_vertex_cover

Return an approximate minimum (weighted) vertex cover.

bipartite_vertex_cover

Return a minimum vertex cover of a bipartite graph.

minimum_edge_cover

Return an edge cover of minimum size.

approx_minimum_edge_dominating_set

Return an edge dominating set within a factor two of the minimum.

approx_minimum_dominating_set

Return an approximate minimum (weighted) dominating set.

connected_dominating_set

Return a connected dominating set of a connected graph.

Coloring#

sequential_vertex_coloring

Returns a vertex coloring of the graph.

dsatur_vertex_coloring

Return a vertex coloring obtained with the DSatur heuristic.

equitable_vertex_coloring

Return an equitable vertex coloring with k colors.

edge_coloring

Return a proper edge coloring of a simple undirected graph.

Combinatorial optimization#

tsp_tour

Return a traveling salesman tour of the graph.

steiner_tree

Return an approximate minimum Steiner tree connecting the given terminal vertices.

approx_maximum_cut

Return an approximate maximum cut.

densest_subgraph

Return an approximately densest subgraph.

vertex_ordering

Return an ordering of the vertices that reduces the bandwidth of the adjacency matrix.

bandwidth

Return the bandwidth of the adjacency matrix with the given vertex ordering.

Graph comparison and isomorphism#

similarity

Return the Jaccard adjacency similarity between two graphs.

isomorphism

Check whether two graphs are isomorphic.

subgraph_isomorphism

Obtain all subgraph isomorphisms of sub in g (or at most max_n subgraphs, if max_n > 0).

mark_subgraph

Mark a given subgraph sub on the graph g.

maximum_common_subgraphs

Return the maximum common induced subgraphs of two graphs.

graph_edit_distance

Return the edit distance between two graphs.

bliss_automorphism_group

Obtain the automorphism group of the graph with the bliss algorithm.

nauty_automorphism_group

Obtain the automorphism group of the graph with the nauty or Traces algorithms.

weisfeiler_lehman_hash

Return the Weisfeiler-Lehman hash of the graph.

Planarity#

is_planar

Test if the graph is planar.

planar_faces

Return the faces of a planar embedding of the graph.

is_straight_line_drawing

Check whether a drawing of a graph with straight edges on an integer grid has no edge crossings.

Graph classes#

is_bipartite

Test if the graph is bipartite.

is_perfect

Check whether a graph is perfect.

find_asteroidal_triple

Return an asteroidal triple of a graph, if there is one.

is_threshold_graph

Return whether the graph is a threshold graph.

find_threshold_graph

Return a threshold subgraph of the graph.

is_distance_regular

Check whether a graph is distance-regular.

is_strongly_regular

Check whether a graph is strongly regular.

is_regular_expander

Check whether a graph is a regular (near-)Ramanujan expander.

intersection_array

Return the intersection array of a distance-regular graph.

global_parameters

Return the global parameters of a distance-regular graph from its intersection array.

Graph polynomials#

tutte_polynomial

Return the Tutte polynomial of a graph.

chromatic_polynomial

Return the chromatic polynomial of a graph.