Contents Menu Expand Light mode Dark mode Auto light/dark mode
rustworkx 0.17.1
rustworkx 0.17.1
  • About Rustworkx
  • Installation and Getting Started
  • Rustworkx Tutorials and Guides
    • Introduction to rustworkx
    • Directed Acyclic Graphs
    • Working with Betweenness Centrality
  • Rustworkx API
    • Graph Classes
      • PyGraph
        • rustworkx.PyGraph.add_edge
        • rustworkx.PyGraph.add_edges_from
        • rustworkx.PyGraph.add_edges_from_no_data
        • rustworkx.PyGraph.add_node
        • rustworkx.PyGraph.add_nodes_from
        • rustworkx.PyGraph.adj
        • rustworkx.PyGraph.clear
        • rustworkx.PyGraph.clear_edges
        • rustworkx.PyGraph.compose
        • rustworkx.PyGraph.contract_nodes
        • rustworkx.PyGraph.copy
        • rustworkx.PyGraph.degree
        • rustworkx.PyGraph.edge_index_map
        • rustworkx.PyGraph.edge_indices
        • rustworkx.PyGraph.edge_indices_from_endpoints
        • rustworkx.PyGraph.edge_list
        • rustworkx.PyGraph.edge_subgraph
        • rustworkx.PyGraph.edges
        • rustworkx.PyGraph.extend_from_edge_list
        • rustworkx.PyGraph.extend_from_weighted_edge_list
        • rustworkx.PyGraph.filter_edges
        • rustworkx.PyGraph.filter_nodes
        • rustworkx.PyGraph.find_node_by_weight
        • rustworkx.PyGraph.from_adjacency_matrix
        • rustworkx.PyGraph.from_complex_adjacency_matrix
        • rustworkx.PyGraph.get_all_edge_data
        • rustworkx.PyGraph.get_edge_data
        • rustworkx.PyGraph.get_edge_data_by_index
        • rustworkx.PyGraph.get_edge_endpoints_by_index
        • rustworkx.PyGraph.get_node_data
        • rustworkx.PyGraph.has_edge
        • rustworkx.PyGraph.has_node
        • rustworkx.PyGraph.has_parallel_edges
        • rustworkx.PyGraph.in_edge_indices
        • rustworkx.PyGraph.in_edges
        • rustworkx.PyGraph.incident_edge_index_map
        • rustworkx.PyGraph.incident_edges
        • rustworkx.PyGraph.neighbors
        • rustworkx.PyGraph.node_indexes
        • rustworkx.PyGraph.node_indices
        • rustworkx.PyGraph.nodes
        • rustworkx.PyGraph.num_edges
        • rustworkx.PyGraph.num_nodes
        • rustworkx.PyGraph.out_edge_indices
        • rustworkx.PyGraph.out_edges
        • rustworkx.PyGraph.read_edge_list
        • rustworkx.PyGraph.remove_edge
        • rustworkx.PyGraph.remove_edge_from_index
        • rustworkx.PyGraph.remove_edges_from
        • rustworkx.PyGraph.remove_node
        • rustworkx.PyGraph.remove_nodes_from
        • rustworkx.PyGraph.subgraph
        • rustworkx.PyGraph.subgraph_with_nodemap
        • rustworkx.PyGraph.substitute_node_with_subgraph
        • rustworkx.PyGraph.to_directed
        • rustworkx.PyGraph.to_dot
        • rustworkx.PyGraph.update_edge
        • rustworkx.PyGraph.update_edge_by_index
        • rustworkx.PyGraph.weighted_edge_list
        • rustworkx.PyGraph.write_edge_list
      • PyDiGraph
        • rustworkx.PyDiGraph.add_child
        • rustworkx.PyDiGraph.add_edge
        • rustworkx.PyDiGraph.add_edges_from
        • rustworkx.PyDiGraph.add_edges_from_no_data
        • rustworkx.PyDiGraph.add_node
        • rustworkx.PyDiGraph.add_nodes_from
        • rustworkx.PyDiGraph.add_parent
        • rustworkx.PyDiGraph.adj
        • rustworkx.PyDiGraph.adj_direction
        • rustworkx.PyDiGraph.can_contract_without_cycle
        • rustworkx.PyDiGraph.clear
        • rustworkx.PyDiGraph.clear_edges
        • rustworkx.PyDiGraph.compose
        • rustworkx.PyDiGraph.contract_nodes
        • rustworkx.PyDiGraph.copy
        • rustworkx.PyDiGraph.edge_index_map
        • rustworkx.PyDiGraph.edge_indices
        • rustworkx.PyDiGraph.edge_indices_from_endpoints
        • rustworkx.PyDiGraph.edge_list
        • rustworkx.PyDiGraph.edge_subgraph
        • rustworkx.PyDiGraph.edges
        • rustworkx.PyDiGraph.extend_from_edge_list
        • rustworkx.PyDiGraph.extend_from_weighted_edge_list
        • rustworkx.PyDiGraph.filter_edges
        • rustworkx.PyDiGraph.filter_nodes
        • rustworkx.PyDiGraph.find_adjacent_node_by_edge
        • rustworkx.PyDiGraph.find_node_by_weight
        • rustworkx.PyDiGraph.find_predecessor_node_by_edge
        • rustworkx.PyDiGraph.find_predecessors_by_edge
        • rustworkx.PyDiGraph.find_successor_node_by_edge
        • rustworkx.PyDiGraph.find_successors_by_edge
        • rustworkx.PyDiGraph.from_adjacency_matrix
        • rustworkx.PyDiGraph.from_complex_adjacency_matrix
        • rustworkx.PyDiGraph.get_all_edge_data
        • rustworkx.PyDiGraph.get_edge_data
        • rustworkx.PyDiGraph.get_edge_data_by_index
        • rustworkx.PyDiGraph.get_edge_endpoints_by_index
        • rustworkx.PyDiGraph.get_node_data
        • rustworkx.PyDiGraph.has_edge
        • rustworkx.PyDiGraph.has_node
        • rustworkx.PyDiGraph.has_parallel_edges
        • rustworkx.PyDiGraph.in_degree
        • rustworkx.PyDiGraph.in_edge_indices
        • rustworkx.PyDiGraph.in_edges
        • rustworkx.PyDiGraph.incident_edge_index_map
        • rustworkx.PyDiGraph.incident_edges
        • rustworkx.PyDiGraph.insert_node_on_in_edges
        • rustworkx.PyDiGraph.insert_node_on_in_edges_multiple
        • rustworkx.PyDiGraph.insert_node_on_out_edges
        • rustworkx.PyDiGraph.insert_node_on_out_edges_multiple
        • rustworkx.PyDiGraph.is_symmetric
        • rustworkx.PyDiGraph.make_symmetric
        • rustworkx.PyDiGraph.merge_nodes
        • rustworkx.PyDiGraph.neighbors
        • rustworkx.PyDiGraph.neighbors_undirected
        • rustworkx.PyDiGraph.node_indexes
        • rustworkx.PyDiGraph.node_indices
        • rustworkx.PyDiGraph.nodes
        • rustworkx.PyDiGraph.num_edges
        • rustworkx.PyDiGraph.num_nodes
        • rustworkx.PyDiGraph.out_degree
        • rustworkx.PyDiGraph.out_edge_indices
        • rustworkx.PyDiGraph.out_edges
        • rustworkx.PyDiGraph.predecessor_indices
        • rustworkx.PyDiGraph.predecessors
        • rustworkx.PyDiGraph.read_edge_list
        • rustworkx.PyDiGraph.remove_edge
        • rustworkx.PyDiGraph.remove_edge_from_index
        • rustworkx.PyDiGraph.remove_edges_from
        • rustworkx.PyDiGraph.remove_node
        • rustworkx.PyDiGraph.remove_node_retain_edges
        • rustworkx.PyDiGraph.remove_node_retain_edges_by_id
        • rustworkx.PyDiGraph.remove_node_retain_edges_by_key
        • rustworkx.PyDiGraph.remove_nodes_from
        • rustworkx.PyDiGraph.reverse
        • rustworkx.PyDiGraph.subgraph
        • rustworkx.PyDiGraph.subgraph_with_nodemap
        • rustworkx.PyDiGraph.substitute_node_with_subgraph
        • rustworkx.PyDiGraph.successor_indices
        • rustworkx.PyDiGraph.successors
        • rustworkx.PyDiGraph.to_dot
        • rustworkx.PyDiGraph.to_undirected
        • rustworkx.PyDiGraph.update_edge
        • rustworkx.PyDiGraph.update_edge_by_index
        • rustworkx.PyDiGraph.weighted_edge_list
        • rustworkx.PyDiGraph.write_edge_list
      • PyDAG
        • rustworkx.PyDAG.add_child
        • rustworkx.PyDAG.add_edge
        • rustworkx.PyDAG.add_edges_from
        • rustworkx.PyDAG.add_edges_from_no_data
        • rustworkx.PyDAG.add_node
        • rustworkx.PyDAG.add_nodes_from
        • rustworkx.PyDAG.add_parent
        • rustworkx.PyDAG.adj
        • rustworkx.PyDAG.adj_direction
        • rustworkx.PyDAG.can_contract_without_cycle
        • rustworkx.PyDAG.clear
        • rustworkx.PyDAG.clear_edges
        • rustworkx.PyDAG.compose
        • rustworkx.PyDAG.contract_nodes
        • rustworkx.PyDAG.copy
        • rustworkx.PyDAG.edge_index_map
        • rustworkx.PyDAG.edge_indices
        • rustworkx.PyDAG.edge_indices_from_endpoints
        • rustworkx.PyDAG.edge_list
        • rustworkx.PyDAG.edge_subgraph
        • rustworkx.PyDAG.edges
        • rustworkx.PyDAG.extend_from_edge_list
        • rustworkx.PyDAG.extend_from_weighted_edge_list
        • rustworkx.PyDAG.filter_edges
        • rustworkx.PyDAG.filter_nodes
        • rustworkx.PyDAG.find_adjacent_node_by_edge
        • rustworkx.PyDAG.find_node_by_weight
        • rustworkx.PyDAG.find_predecessor_node_by_edge
        • rustworkx.PyDAG.find_predecessors_by_edge
        • rustworkx.PyDAG.find_successor_node_by_edge
        • rustworkx.PyDAG.find_successors_by_edge
        • rustworkx.PyDAG.from_adjacency_matrix
        • rustworkx.PyDAG.from_complex_adjacency_matrix
        • rustworkx.PyDAG.get_all_edge_data
        • rustworkx.PyDAG.get_edge_data
        • rustworkx.PyDAG.get_edge_data_by_index
        • rustworkx.PyDAG.get_edge_endpoints_by_index
        • rustworkx.PyDAG.get_node_data
        • rustworkx.PyDAG.has_edge
        • rustworkx.PyDAG.has_node
        • rustworkx.PyDAG.has_parallel_edges
        • rustworkx.PyDAG.in_degree
        • rustworkx.PyDAG.in_edge_indices
        • rustworkx.PyDAG.in_edges
        • rustworkx.PyDAG.incident_edge_index_map
        • rustworkx.PyDAG.incident_edges
        • rustworkx.PyDAG.insert_node_on_in_edges
        • rustworkx.PyDAG.insert_node_on_in_edges_multiple
        • rustworkx.PyDAG.insert_node_on_out_edges
        • rustworkx.PyDAG.insert_node_on_out_edges_multiple
        • rustworkx.PyDAG.is_symmetric
        • rustworkx.PyDAG.make_symmetric
        • rustworkx.PyDAG.merge_nodes
        • rustworkx.PyDAG.neighbors
        • rustworkx.PyDAG.neighbors_undirected
        • rustworkx.PyDAG.node_indexes
        • rustworkx.PyDAG.node_indices
        • rustworkx.PyDAG.nodes
        • rustworkx.PyDAG.num_edges
        • rustworkx.PyDAG.num_nodes
        • rustworkx.PyDAG.out_degree
        • rustworkx.PyDAG.out_edge_indices
        • rustworkx.PyDAG.out_edges
        • rustworkx.PyDAG.predecessor_indices
        • rustworkx.PyDAG.predecessors
        • rustworkx.PyDAG.read_edge_list
        • rustworkx.PyDAG.remove_edge
        • rustworkx.PyDAG.remove_edge_from_index
        • rustworkx.PyDAG.remove_edges_from
        • rustworkx.PyDAG.remove_node
        • rustworkx.PyDAG.remove_node_retain_edges
        • rustworkx.PyDAG.remove_node_retain_edges_by_id
        • rustworkx.PyDAG.remove_node_retain_edges_by_key
        • rustworkx.PyDAG.remove_nodes_from
        • rustworkx.PyDAG.reverse
        • rustworkx.PyDAG.subgraph
        • rustworkx.PyDAG.subgraph_with_nodemap
        • rustworkx.PyDAG.substitute_node_with_subgraph
        • rustworkx.PyDAG.successor_indices
        • rustworkx.PyDAG.successors
        • rustworkx.PyDAG.to_dot
        • rustworkx.PyDAG.to_undirected
        • rustworkx.PyDAG.update_edge
        • rustworkx.PyDAG.update_edge_by_index
        • rustworkx.PyDAG.weighted_edge_list
        • rustworkx.PyDAG.write_edge_list
    • Algorithm Functions
      • Centrality
        • rustworkx.betweenness_centrality
        • rustworkx.degree_centrality
        • rustworkx.edge_betweenness_centrality
        • rustworkx.eigenvector_centrality
        • rustworkx.katz_centrality
        • rustworkx.closeness_centrality
        • rustworkx.newman_weighted_closeness_centrality
        • rustworkx.in_degree_centrality
        • rustworkx.out_degree_centrality
      • Coloring
        • ColoringStrategy
        • rustworkx.graph_greedy_color
        • rustworkx.graph_bipartite_edge_color
        • rustworkx.graph_greedy_edge_color
        • rustworkx.graph_misra_gries_edge_color
        • rustworkx.two_color
      • Connectivity and Cycles
        • rustworkx.number_connected_components
        • rustworkx.connected_components
        • rustworkx.node_connected_component
        • rustworkx.is_connected
        • rustworkx.number_strongly_connected_components
        • rustworkx.strongly_connected_components
        • rustworkx.is_strongly_connected
        • rustworkx.number_weakly_connected_components
        • rustworkx.weakly_connected_components
        • rustworkx.is_weakly_connected
        • rustworkx.cycle_basis
        • rustworkx.simple_cycles
        • rustworkx.digraph_find_cycle
        • rustworkx.articulation_points
        • rustworkx.bridges
        • rustworkx.biconnected_components
        • rustworkx.chain_decomposition
        • rustworkx.all_simple_paths
        • rustworkx.all_pairs_all_simple_paths
        • rustworkx.stoer_wagner_min_cut
        • rustworkx.longest_simple_path
        • rustworkx.is_bipartite
        • rustworkx.isolates
        • rustworkx.has_path
        • rustworkx.connected_subgraphs
      • DAG Algorithms
        • rustworkx.dag_longest_path
        • rustworkx.dag_longest_path_length
        • rustworkx.dag_weighted_longest_path
        • rustworkx.dag_weighted_longest_path_length
        • rustworkx.is_directed_acyclic_graph
        • rustworkx.layers
        • rustworkx.transitive_reduction
        • rustworkx.topological_generations
      • Dominance
        • rustworkx.immediate_dominators
        • rustworkx.dominance_frontiers
      • Graph Operations
        • rustworkx.complement
        • rustworkx.union
        • rustworkx.cartesian_product
      • Isomorphism
        • rustworkx.is_isomorphic
        • rustworkx.is_subgraph_isomorphic
        • rustworkx.is_isomorphic_node_match
        • rustworkx.vf2_mapping
      • Link Analysis
        • rustworkx.pagerank
        • rustworkx.hits
      • Matching
        • rustworkx.max_weight_matching
        • rustworkx.is_matching
        • rustworkx.is_maximal_matching
      • Other Algorithm Functions
        • rustworkx.adjacency_matrix
        • rustworkx.transitivity
        • rustworkx.core_number
        • rustworkx.graph_line_graph
        • rustworkx.metric_closure
        • rustworkx.is_planar
        • rustworkx.digraph_maximum_bisimulation
      • Shortest Paths
        • rustworkx.dijkstra_shortest_paths
        • rustworkx.dijkstra_shortest_path_lengths
        • rustworkx.all_pairs_dijkstra_shortest_paths
        • rustworkx.all_pairs_dijkstra_path_lengths
        • rustworkx.bellman_ford_shortest_paths
        • rustworkx.bellman_ford_shortest_path_lengths
        • rustworkx.all_pairs_bellman_ford_shortest_paths
        • rustworkx.all_pairs_bellman_ford_path_lengths
        • rustworkx.negative_edge_cycle
        • rustworkx.find_negative_cycle
        • rustworkx.distance_matrix
        • rustworkx.floyd_warshall
        • rustworkx.floyd_warshall_numpy
        • rustworkx.floyd_warshall_successor_and_distance
        • rustworkx.astar_shortest_path
        • rustworkx.k_shortest_path_lengths
        • rustworkx.num_shortest_paths_unweighted
        • rustworkx.unweighted_average_shortest_path_length
        • rustworkx.all_shortest_paths
        • rustworkx.digraph_all_shortest_paths
        • rustworkx.single_source_all_shortest_paths
      • Traversal
        • rustworkx.dfs_edges
        • rustworkx.dfs_search
        • rustworkx.bfs_successors
        • rustworkx.bfs_predecessors
        • rustworkx.bfs_search
        • rustworkx.dijkstra_search
        • rustworkx.topological_sort
        • rustworkx.lexicographical_topological_sort
        • rustworkx.descendants
        • rustworkx.ancestors
        • rustworkx.collect_runs
        • rustworkx.collect_bicolor_runs
        • DFSVisitor
          • rustworkx.visit.DFSVisitor.back_edge
          • rustworkx.visit.DFSVisitor.discover_vertex
          • rustworkx.visit.DFSVisitor.finish_vertex
          • rustworkx.visit.DFSVisitor.forward_or_cross_edge
          • rustworkx.visit.DFSVisitor.tree_edge
        • BFSVisitor
          • rustworkx.visit.BFSVisitor.black_target_edge
          • rustworkx.visit.BFSVisitor.discover_vertex
          • rustworkx.visit.BFSVisitor.finish_vertex
          • rustworkx.visit.BFSVisitor.gray_target_edge
          • rustworkx.visit.BFSVisitor.non_tree_edge
          • rustworkx.visit.BFSVisitor.tree_edge
        • DijkstraVisitor
          • rustworkx.visit.DijkstraVisitor.discover_vertex
          • rustworkx.visit.DijkstraVisitor.edge_not_relaxed
          • rustworkx.visit.DijkstraVisitor.edge_relaxed
          • rustworkx.visit.DijkstraVisitor.examine_edge
          • rustworkx.visit.DijkstraVisitor.finish_vertex
        • TopologicalSorter
          • rustworkx.TopologicalSorter.done
          • rustworkx.TopologicalSorter.get_ready
          • rustworkx.TopologicalSorter.is_active
      • Tree
        • rustworkx.minimum_spanning_edges
        • rustworkx.minimum_spanning_tree
        • rustworkx.steiner_tree
    • Generators
      • rustworkx.generators.cycle_graph
      • rustworkx.generators.directed_cycle_graph
      • rustworkx.generators.path_graph
      • rustworkx.generators.directed_path_graph
      • rustworkx.generators.star_graph
      • rustworkx.generators.directed_star_graph
      • rustworkx.generators.mesh_graph
      • rustworkx.generators.directed_mesh_graph
      • rustworkx.generators.grid_graph
      • rustworkx.generators.directed_grid_graph
      • rustworkx.generators.binomial_tree_graph
      • rustworkx.generators.directed_binomial_tree_graph
      • rustworkx.generators.hexagonal_lattice_graph
      • rustworkx.generators.directed_hexagonal_lattice_graph
      • rustworkx.generators.heavy_square_graph
      • rustworkx.generators.directed_heavy_square_graph
      • rustworkx.generators.heavy_hex_graph
      • rustworkx.generators.directed_heavy_hex_graph
      • rustworkx.generators.lollipop_graph
      • rustworkx.generators.generalized_petersen_graph
      • rustworkx.generators.barbell_graph
      • rustworkx.generators.full_rary_tree
      • rustworkx.generators.empty_graph
      • rustworkx.generators.directed_empty_graph
      • rustworkx.generators.complete_graph
      • rustworkx.generators.directed_complete_graph
      • rustworkx.generators.dorogovtsev_goltsev_mendes_graph
      • rustworkx.generators.karate_club_graph
    • Random Graph Generator Functions
      • rustworkx.directed_gnp_random_graph
      • rustworkx.undirected_gnp_random_graph
      • rustworkx.directed_gnm_random_graph
      • rustworkx.undirected_gnm_random_graph
      • rustworkx.directed_sbm_random_graph
      • rustworkx.undirected_sbm_random_graph
      • rustworkx.random_geometric_graph
      • rustworkx.hyperbolic_random_graph
      • rustworkx.barabasi_albert_graph
      • rustworkx.directed_barabasi_albert_graph
      • rustworkx.directed_random_bipartite_graph
      • rustworkx.undirected_random_bipartite_graph
    • Layout Functions
      • rustworkx.random_layout
      • rustworkx.spring_layout
      • rustworkx.bipartite_layout
      • rustworkx.circular_layout
      • rustworkx.shell_layout
      • rustworkx.spiral_layout
    • Serialization
      • rustworkx.node_link_json
      • rustworkx.read_graphml
      • rustworkx.write_graphml
      • rustworkx.from_node_link_json_file
      • rustworkx.parse_node_link_json
    • Converters
      • rustworkx.networkx_converter
    • API functions for PyDigraph
      • rustworkx.digraph_is_isomorphic
      • rustworkx.digraph_is_subgraph_isomorphic
      • rustworkx.digraph_vf2_mapping
      • rustworkx.digraph_distance_matrix
      • rustworkx.digraph_floyd_warshall
      • rustworkx.digraph_floyd_warshall_numpy
      • rustworkx.digraph_floyd_warshall_successor_and_distance
      • rustworkx.digraph_adjacency_matrix
      • rustworkx.digraph_all_simple_paths
      • rustworkx.digraph_all_pairs_all_simple_paths
      • rustworkx.digraph_astar_shortest_path
      • rustworkx.digraph_dijkstra_shortest_paths
      • rustworkx.digraph_all_pairs_dijkstra_shortest_paths
      • rustworkx.digraph_dijkstra_shortest_path_lengths
      • rustworkx.digraph_all_pairs_dijkstra_path_lengths
      • rustworkx.digraph_bellman_ford_shortest_path_lengths
      • rustworkx.digraph_bellman_ford_shortest_path_lengths
      • rustworkx.digraph_all_pairs_bellman_ford_shortest_paths
      • rustworkx.digraph_all_pairs_bellman_ford_path_lengths
      • rustworkx.digraph_k_shortest_path_lengths
      • rustworkx.digraph_all_shortest_paths
      • rustworkx.digraph_single_source_all_shortest_paths
      • rustworkx.digraph_dfs_edges
      • rustworkx.digraph_dfs_search
      • rustworkx.digraph_find_cycle
      • rustworkx.digraph_transitivity
      • rustworkx.digraph_core_number
      • rustworkx.digraph_complement
      • rustworkx.digraph_union
      • rustworkx.digraph_tensor_product
      • rustworkx.digraph_cartesian_product
      • rustworkx.digraph_random_layout
      • rustworkx.digraph_bipartite_layout
      • rustworkx.digraph_circular_layout
      • rustworkx.digraph_shell_layout
      • rustworkx.digraph_spiral_layout
      • rustworkx.digraph_spring_layout
      • rustworkx.digraph_num_shortest_paths_unweighted
      • rustworkx.digraph_betweenness_centrality
      • rustworkx.digraph_edge_betweenness_centrality
      • rustworkx.digraph_closeness_centrality
      • rustworkx.digraph_newman_weighted_closeness_centrality
      • rustworkx.digraph_eigenvector_centrality
      • rustworkx.digraph_katz_centrality
      • rustworkx.digraph_unweighted_average_shortest_path_length
      • rustworkx.digraph_bfs_search
      • rustworkx.digraph_dijkstra_search
      • rustworkx.digraph_node_link_json
      • rustworkx.digraph_longest_simple_path
    • API functions for PyGraph
      • rustworkx.graph_is_isomorphic
      • rustworkx.graph_is_subgraph_isomorphic
      • rustworkx.graph_vf2_mapping
      • rustworkx.graph_distance_matrix
      • rustworkx.graph_floyd_warshall
      • rustworkx.graph_floyd_warshall_numpy
      • rustworkx.graph_floyd_warshall_successor_and_distance
      • rustworkx.graph_adjacency_matrix
      • rustworkx.graph_all_simple_paths
      • rustworkx.graph_all_pairs_all_simple_paths
      • rustworkx.graph_astar_shortest_path
      • rustworkx.graph_dijkstra_shortest_paths
      • rustworkx.graph_dijkstra_shortest_path_lengths
      • rustworkx.graph_all_pairs_dijkstra_shortest_paths
      • rustworkx.graph_k_shortest_path_lengths
      • rustworkx.graph_all_pairs_dijkstra_path_lengths
      • rustworkx.graph_bellman_ford_shortest_path_lengths
      • rustworkx.graph_bellman_ford_shortest_path_lengths
      • rustworkx.graph_all_pairs_bellman_ford_shortest_paths
      • rustworkx.graph_all_pairs_bellman_ford_path_lengths
      • rustworkx.graph_all_shortest_paths
      • rustworkx.graph_single_source_all_shortest_paths
      • rustworkx.graph_dfs_edges
      • rustworkx.graph_dfs_search
      • rustworkx.graph_transitivity
      • rustworkx.graph_core_number
      • rustworkx.graph_complement
      • rustworkx.local_complement
      • rustworkx.graph_union
      • rustworkx.graph_tensor_product
      • rustworkx.graph_token_swapper
      • rustworkx.graph_cartesian_product
      • rustworkx.graph_random_layout
      • rustworkx.graph_bipartite_layout
      • rustworkx.graph_circular_layout
      • rustworkx.graph_shell_layout
      • rustworkx.graph_spiral_layout
      • rustworkx.graph_spring_layout
      • rustworkx.graph_num_shortest_paths_unweighted
      • rustworkx.graph_betweenness_centrality
      • rustworkx.graph_edge_betweenness_centrality
      • rustworkx.graph_closeness_centrality
      • rustworkx.graph_newman_weighted_closeness_centrality
      • rustworkx.graph_eigenvector_centrality
      • rustworkx.graph_katz_centrality
      • rustworkx.graph_unweighted_average_shortest_path_length
      • rustworkx.graph_bfs_search
      • rustworkx.graph_dijkstra_search
      • rustworkx.graph_node_link_json
      • rustworkx.graph_longest_simple_path
    • Exceptions
      • rustworkx.DAGHasCycle
      • rustworkx.DAGWouldCycle
      • rustworkx.FailedToConverge
      • rustworkx.GraphNotBipartite
      • rustworkx.InvalidMapping
      • rustworkx.InvalidNode
      • rustworkx.JSONDeserializationError
      • rustworkx.JSONSerializationError
      • rustworkx.NegativeCycle
      • rustworkx.NoEdgeBetweenNodes
      • rustworkx.NoPathFound
      • rustworkx.NoSuitableNeighbors
      • rustworkx.NullGraph
      • rustworkx.visit.PruneSearch
      • rustworkx.visit.StopSearch
    • Custom Return Types
      • BFSSuccessors
      • BFSPredecessors
      • NodeIndices
      • EdgeIndices
      • EdgeList
      • WeightedEdgeList
      • EdgeIndexMap
        • rustworkx.EdgeIndexMap.items
        • rustworkx.EdgeIndexMap.keys
        • rustworkx.EdgeIndexMap.values
      • PathMapping
        • rustworkx.PathMapping.items
        • rustworkx.PathMapping.keys
        • rustworkx.PathMapping.values
      • PathLengthMapping
        • rustworkx.PathLengthMapping.items
        • rustworkx.PathLengthMapping.keys
        • rustworkx.PathLengthMapping.values
      • Pos2DMapping
        • rustworkx.Pos2DMapping.items
        • rustworkx.Pos2DMapping.keys
        • rustworkx.Pos2DMapping.values
      • AllPairsPathMapping
        • rustworkx.AllPairsPathMapping.items
        • rustworkx.AllPairsPathMapping.keys
        • rustworkx.AllPairsPathMapping.values
      • AllPairsPathLengthMapping
        • rustworkx.AllPairsPathLengthMapping.items
        • rustworkx.AllPairsPathLengthMapping.keys
        • rustworkx.AllPairsPathLengthMapping.values
      • CentralityMapping
        • rustworkx.CentralityMapping.items
        • rustworkx.CentralityMapping.keys
        • rustworkx.CentralityMapping.values
      • EdgeCentralityMapping
        • rustworkx.EdgeCentralityMapping.items
        • rustworkx.EdgeCentralityMapping.keys
        • rustworkx.EdgeCentralityMapping.values
      • Chains
      • NodeMap
        • rustworkx.NodeMap.items
        • rustworkx.NodeMap.keys
        • rustworkx.NodeMap.values
      • ProductNodeMap
        • rustworkx.ProductNodeMap.items
        • rustworkx.ProductNodeMap.keys
        • rustworkx.ProductNodeMap.values
      • BiconnectedComponents
        • rustworkx.BiconnectedComponents.items
        • rustworkx.BiconnectedComponents.keys
        • rustworkx.BiconnectedComponents.values
      • RelationalCoarsestPartition
      • IndexPartitionBlock
  • Visualization
    • rustworkx.visualization.mpl_draw
    • rustworkx.visualization.graphviz_draw
  • Release Notes
  • Contributing Guide
  • rustworkx for NetworkX users
  • Benchmarks
  • 0.15
  • 0.14
  • 0.13
  • 0.12
Back to top



rustworkx.graph_bfs_search#

graph_bfs_search(graph, source=None, visitor=None)#

Breadth-first traversal of a undirected graph with several source vertices.

Pseudo-code for the breadth-first search algorithm with a single source vertex is listed below with annotated event points at which a method of the given BFSVisitor is called.

# G - graph, s - single source node
BFS(G, s)
  let color be a mapping             # color[u] - vertex u color WHITE/GRAY/BLACK
  for each u in G                    # u - vertex in G
    color[u] := WHITE                # color all vertices as undiscovered
  end for
  let Q be a queue
  ENQUEUE(Q, s)
  color[s] := GRAY                   # event: discover_vertex(s)
  while (Q is not empty)
    u := DEQUEUE(Q)
    for each v, w in OutEdges(G, u)  # v - target vertex, w - edge weight
      if (WHITE = color[v])          # event: tree_edge((u, v, w))
        color[v] := GRAY             # event: discover_vertex(v)
        ENQUEUE(Q, v)
      else                           # event: non_tree_edge((u, v, w))
        if (GRAY = color[v])         # event: gray_target_edge((u, v, w))
          ...                       
        elif (BLACK = color[v])      # event: black_target_edge((u, v, w))
          ...                       
    end for
    color[u] := BLACK                # event: finish_vertex(u)
  end while

For several source nodes, the BFS algorithm is applied on source nodes by the given order.

If an exception is raised inside the callback method of the BFSVisitor instance, the graph traversal will be stopped immediately. You can exploit this to exit early by raising a StopSearch exception, in which case the search function will return but without raising back the exception. You can also prune part of the search tree by raising PruneSearch.

In the following example we keep track of the tree edges:

import rustworkx as rx
from rustworkx.visit import BFSVisitor

class TreeEdgesRecorder(BFSVisitor):

    def __init__(self):
        self.edges = []

    def tree_edge(self, edge):
        self.edges.append(edge)

graph = rx.PyGraph()
graph.extend_from_edge_list([(1, 3), (0, 1), (2, 1), (0, 2)])
vis = TreeEdgesRecorder()
rx.graph_bfs_search(graph, [0], vis)
print('Tree edges:', vis.edges)
Tree edges: [(0, 2, None), (0, 1, None), (1, 3, None)]

Here is another example, using the PruneSearch exception, to find the shortest path between two vertices with some restrictions on the edges (for a more efficient ways to find the shortest path, see Shortest Paths):

import rustworkx as rx
from rustworkx.visit import BFSVisitor, PruneSearch


graph = rx.PyGraph()
home, market, school = graph.add_nodes_from(['home', 'market', 'school'])
graph.add_edges_from_no_data(
    [(school, home), (school, market), (market, home)]
)

class DistanceHomeFinder(BFSVisitor):

    def __init__(self):
        self.distance = {}

    def discover_vertex(self, vertex):
        self.distance.setdefault(vertex, 0)

    def tree_edge(self, edge):
        source, target, _ = edge
        # the road directly from home to school is closed
        if {source, target} == {home, school}:
            raise PruneSearch
        self.distance[target] = self.distance[source] + 1

vis = DistanceHomeFinder()
rx.graph_bfs_search(graph, [school], vis)
print('Distance from school to home:', vis.distance[home])
Distance from school to home: 2

Note

Graph can not be mutated while traversing. Trying to do so raises an exception.

Note

An exception is raised if the PruneSearch is raised in the finish_vertex event.

Parameters:
  • graph (PyGraph) – The graph to be used.

  • source – An optional list of node indices to use as the starting nodes for the breadth-first search. If None or not specified then a source will be chosen arbitrarily and repeated until all components of the graph are searched. This can be a Sequence[int] or None.

  • visitor – A visitor object that is invoked at the event points inside the algorithm. This should be a subclass of BFSVisitor. This has a default value of None as a backwards compatibility artifact (to preserve argument ordering from an earlier version) but it is a required argument and will raise a TypeError if not specified.

Next
rustworkx.graph_dijkstra_search
Previous
rustworkx.graph_unweighted_average_shortest_path_length
Copyright © 2021, rustworkx Contributors
Made with Sphinx and @pradyunsg's Furo
On this page
  • rustworkx.graph_bfs_search
    • graph_bfs_search()