NetworkXToolkit

class NetworkXToolkit(BaseToolkit):

_get_nx

def _get_nx(cls):

Lazily import networkx module when needed.

init

def __init__(
    self,
    timeout: Optional[float] = None,
    graph_type: Literal['graph', 'digraph', 'multigraph', 'multidigraph'] = 'graph'
):

Initializes the NetworkX graph client.

Parameters:

  • timeout (Optional[float]): The timeout value for API requests in seconds. If None, no timeout is applied. (default: :obj:None)

add_node

def add_node(self, node_id: str, **attributes: Any):

Adds a node to the graph.

Parameters:

  • node_id (str): The ID of the node.
  • attributes (dict): Additional node attributes.

add_edge

def add_edge(
    self,
    source: str,
    target: str,
    **attributes: Any
):

Adds an edge to the graph.

Parameters:

  • source (str): Source node ID.
  • target (str): Target node ID.
  • attributes (dict): Additional edge attributes.

get_nodes

def get_nodes(self):

Returns:

List[str]: A list of node IDs.

get_edges

def get_edges(self):

Returns:

List[Tuple[str, str]]: A list of edges as (source, target).

get_shortest_path

def get_shortest_path(
    self,
    source: str,
    target: str,
    weight: Optional[Union[str, Callable]] = None,
    method: Literal['dijkstra', 'bellman-ford'] = 'dijkstra'
):

Finds the shortest path between two nodes.

Parameters:

  • method (Literal['dijkstra', 'bellman-ford'], optional): Algorithm to compute the path. Ignored if weight is None. (default: :obj:'dijkstra')

Returns:

List[str]: A list of nodes in the shortest path.

compute_centrality

def compute_centrality(self):

Returns:

Dict[str, float]: Centrality values for each node.

serialize_graph

def serialize_graph(self):

Returns:

str: The serialized graph in JSON format.

deserialize_graph

def deserialize_graph(self, data: str):

Loads a graph from a serialized JSON string.

Parameters:

  • data (str): The JSON string representing the graph.

export_to_file

def export_to_file(self, file_path: str):

Exports the graph to a file in JSON format.

Parameters:

  • file_path (str): The file path to save the graph.

import_from_file

def import_from_file(self, file_path: str):

Imports a graph from a JSON file.

Parameters:

  • file_path (str): The file path to load the graph from.

clear_graph

def clear_graph(self):

Clears the current graph.

get_tools

def get_tools(self):

Returns:

List[FunctionTool]: A list of FunctionTool objects for the toolkit methods.