Algorithms Module API Reference¶
graphite.algorithms
¶
Implemented standard graph algorithms for Graphite graph database.
Functions:
-
all_shortest_paths–All shortest paths from
from_nodetoto_end. -
bfs–Highly Customizable BFS
-
connected_components–Splits given nodes to connected components.
-
neighborhood–Returns neighbors of
startin givenmax_distance. -
shortest_path–Finds shortest path from
from_nodetoto_end.
Classes¶
Functions¶
all_shortest_paths
¶
all_shortest_paths(engine: GraphiteEngine, from_node: Node | str, to_end: Node | str | Callable[[list[tuple[Relation, Node]]], bool] | None = None, direction: Direction = OUTGOING, relation_type: str | None = None, max_depth: int | None = None, allow_direction_switch: bool = False, ignore_nodes: set[str] | None = None, weight: str | None = None, max_results: int | None = None) -> list[tuple[str, int, list[tuple[Relation, Node]]]]
All shortest paths from from_node to to_end.
Non-weighted mode uses bfs() internally.
Not Implemented Completely
Weighted mode is not implemented yet.
Parameters:
-
from_node(Node | str) –Starting node ID or object.
-
to_end(Node | str | Callable[[list[tuple[Relation, Node]]], bool] | None, default:None) –One of below:
None: Match on all nodes in the result. It means all (ormax_results) nearest neighbors.- Node ID or object: Match on all paths to given node.
- Callable (
(path) -> bool): Match on a path when given callable returnsTrue.
-
direction(Direction, default:OUTGOING) –Direction to traverse.
-
relation_type(str | None, default:None) –Type of relations to limit traverse.
-
max_depth(int | None, default:None) –Maximum depth to traverse.
-
allow_direction_switch(bool, default:False) –If
Trueallow direction switch in a path whendirection=Direction.BOTH. -
ignore_nodes(set[str] | None, default:None) –Visited set of node IDs to ignore.
-
weight(str | None, default:None) –Optional field name to weighted pathfinding.
-
max_results(int | None, default:None) –Maximum number of results to return.
Returns:
bfs
¶
bfs(engine: GraphiteEngine, start: Node | str, end: Node | str | Callable[[list[tuple[Relation, Node]]], bool] | None = None, stop_at_first: bool = True, direction: Direction = OUTGOING, relation_type: str | None = None, max_depth: int | None = None, include_start: bool = False, allow_direction_switch: bool = False, visited: set[str] | None = None, max_results: int | None = None) -> list[tuple[str, int, list[tuple[Relation, Node]]]]
Highly Customizable BFS (Breadth-First Search) in the graph.
Note
Steps are sorted by distance in returned result.
Parameters:
-
start(Node | str) –Starting node ID or object.
-
end(Node | str | Callable[[list[tuple[Relation, Node]]], bool] | None, default:None) –One of below:
None: Match on all nodes in the result.- Node ID or object: Match on all paths to given node.
- Callable (
(path) -> bool): Match on a path when given callable returnsTrue.
-
stop_at_first(bool, default:True) –If
Trueandendis provided, stops at first match. -
direction(Direction, default:OUTGOING) –Direction to traverse.
-
relation_type(str | None, default:None) –Type of relations to limit traverse.
-
max_depth(int | None, default:None) –Maximum depth to traverse.
-
include_start(bool, default:False) –If
Truecheck starting node on result. -
allow_direction_switch(bool, default:False) –If
Trueallow direction switch in a path whendirection=Direction.BOTH. -
visited(set[str] | None, default:None) –Visited set of node IDs to ignore.
-
max_results(int | None, default:None) –Maximum number of results to return.
Returns:
connected_components
¶
connected_components(engine: GraphiteEngine, nodes: Node | str | set[Node | str] | None = None, return_all_nodes: bool = False, direction: Direction = OUTGOING, relation_type: str | None = None, allow_direction_switch: bool = False, ignore_nodes: set[str] | None = None) -> list[set[str]]
Splits given nodes to connected components.
Note
Uses bfs() internally.
Parameters:
-
nodes(Node | str | set[Node | str] | None, default:None) –One or a set of node (objects or IDs) to group, or
Noneto get all nodes from engine. -
return_all_nodes(bool, default:False) –If
True, includes all nodes in each component, not just intersection of them with inputnodes. Has no effect whennodes = None.Falseis unusual when using a single node. -
direction(Direction, default:OUTGOING) –Direction to traverse.
-
relation_type(str | None, default:None) –Type of relations to allow traverse.
-
allow_direction_switch(bool, default:False) –If
True, allow direction switch in a path whendirection = Direction.BOTH. -
ignore_nodes(set[str] | None, default:None) –Node IDs to ignore.
Returns:
neighborhood
¶
neighborhood(engine: GraphiteEngine, start: Node | str, max_distance: int | None = None, filter_method: Callable[[list[tuple[Relation, Node]]], bool] | None = None, max_results: int | None = None, direction: Direction = OUTGOING, relation_type: str | None = None, allow_direction_switch: bool = False, ignore_nodes: set[str] | None = None) -> tuple[set[tuple[Node, int]], set[Relation]]
Returns neighbors of start in given max_distance.
Note
Uses bfs() internally.
Parameters:
-
start(Node | str) –Starting node object or ID.
-
max_distance(int | None, default:None) –Maximum distance to traverse.
-
filter_method(Callable[[list[tuple[Relation, Node]]], bool] | None, default:None) –Optional callable to filter neighbors (
(path) -> bool). -
max_results(int | None, default:None) –Maximum number of results to return.
-
direction(Direction, default:OUTGOING) –Direction to traverse.
-
relation_type(str | None, default:None) –Type of relations to allow traverse.
-
allow_direction_switch(bool, default:False) –If
True, allow direction switch in a path whendirection = Direction.BOTH. -
ignore_nodes(set[str] | None, default:None) –Node IDs to ignore.
Returns:
shortest_path
¶
shortest_path(engine: GraphiteEngine, from_node: Node | str, to_end: Node | str | Callable[[list[tuple[Relation, Node]]], bool] | None = None, direction: Direction = OUTGOING, relation_type: str | None = None, max_depth: int | None = None, allow_direction_switch: bool = False, ignore_nodes: set[str] | None = None, weight: str | None = None) -> tuple[str, int, list[tuple[Relation, Node]]] | None
Finds shortest path from from_node to to_end.
Non-weighted mode uses bfs() internally.
Not Implemented Completely
Weighted mode is not implemented yet.
Parameters:
-
from_node(Node | str) –Starting node Id or object.
-
to_end(Node | str | Callable[[list[tuple[Relation, Node]]], bool] | None, default:None) –One of below:
None: Match on all nodes in the result. It means nearest neighbor.- Node ID or object: Match on all paths to given node.
- Callable (
(path) -> bool): Match on a path when given callable returnsTrue.
-
direction(Direction, default:OUTGOING) –Direction to traverse.
-
relation_type(str | None, default:None) –Type of relations to allow traverse.
-
max_depth(int | None, default:None) –Maximum depth to traverse.
-
allow_direction_switch(bool, default:False) –If
Trueallow direction switch in a path whendirection = Direction.BOTH. -
ignore_nodes(set[str] | None, default:None) –Node IDs to ignore.
-
weight(str | None, default:None) –Optional field name to weighted pathfinding. (Not implemented yet)
Returns: