Skip to content

trudag.dotstop.core.graph.base_graph

BaseGraph

Bases: ABC

A base abstract class that describes abstract methods that must be implemented for a class to serve as the backend for TrustableGraph.

add_edge abstractmethod

add_edge(parent_id: str, child_id: str) -> None

Add the edge parent_id -> child_id to the graph.

add_node abstractmethod

add_node(node_id: str) -> None

Add a new node with node_id to the graph.

adjacency_matrix

adjacency_matrix(node_order: Iterable[str]) -> ndarray

The unweighted adjacency matrix of the graph.

Parameters:

Name Type Description Default
node_order Iterable[str]

The order of nodes in the adjacency matrix.

required

edges abstractmethod

edges() -> list[tuple[str, str]]

Returns all edges in the graph.

Returns:

Type Description
list[tuple[str, str]]

A list of tuples representing edges where the first element is the source node and the second element is the destination node.

empty abstractmethod classmethod

empty() -> BaseGraph

Create an instance of BaseGraph with no nodes or edges.

from_file abstractmethod classmethod

from_file(path: Path) -> BaseGraph

Construct an instance of BaseGraph from a file.

Parameters:

Name Type Description Default
path Path

Path to file to build graph from

required

from_string abstractmethod classmethod

from_string(src: str) -> BaseGraph

Construct an instance of BaseGraph from a string source.

get_edge_attr abstractmethod

get_edge_attr(
    parent_id: str, child_id: str, key: str
) -> Attribute

Get the attribute associated with key from edge parent_id -> child_id.

Returns:

Type Description
Attribute

String if attribute exists, otherwise None.

get_edge_attrs abstractmethod

get_edge_attrs(
    parent_id: str, child_id: str
) -> dict[str, Attribute]

Get all attributes of edge parent_id -> child_id.

get_graph_attr abstractmethod

get_graph_attr(key: str) -> Attribute

Get the attribute associated with key from graph.

Returns:

Type Description
Attribute

String if value exists, otherwise None

get_graph_attrs abstractmethod

get_graph_attrs() -> dict[str, Attribute]

Get all attributes for graph.

get_node_attr abstractmethod

get_node_attr(node_id: str, key: str) -> Attribute

Get the attribute associated with key from node with node_id.

Returns:

Type Description
Attribute

String if value exists, otherwise None

get_node_attrs abstractmethod

get_node_attrs(node_id: str) -> dict[str, Attribute]

Get all attributes for node with node_id.

has_edge abstractmethod

has_edge(parent_id: str, child_id: str) -> bool

True if the edge parent_id -> child_id exists in the graph.

has_node abstractmethod

has_node(node_id: str) -> bool

True if node with node_id exists in graph.

is_directed abstractmethod

is_directed() -> bool

True if the graph is a directed graph.

leaf_nodes

leaf_nodes() -> list[str]

Returns nodes with no outgoing edges.

nodes abstractmethod

nodes() -> list[str]

Returns all nodes contained in the graph.

predecessors

predecessors(child_id: str) -> list[str]

Returns nodes which have an outgoing edge to the specified node.

remove_edge abstractmethod

remove_edge(parent_id: str, child_id: str) -> None

Remove the edge parent_id -> child_id from the graph.

remove_node abstractmethod

remove_node(node_id: str) -> None

Remove a node with node_id from the graph.

root_nodes

root_nodes() -> list[str]

Return nodes with no incoming edges.

set_edge_attrs abstractmethod

set_edge_attrs(
    parent_id: str, child_id: str, **attrs: Attribute
) -> None

Set the attributes for each key-value pair in attrs for edge parent_id -> child_id. Attributes should operate like a dictionary, existing attributes are updated, new attributes are appended.

set_graph_attrs abstractmethod

set_graph_attrs(**attrs: Attribute) -> None

Set the attributes for each key-value pair in attrs for graph.

set_node_attrs abstractmethod

set_node_attrs(node_id: str, **attrs: Attribute) -> None

Set the attributes for each key-value pair in attrs for node with node_id.

successors

successors(parent_id: str) -> list[str]

Returns nodes which have an incoming edge from the specified node.

to_string abstractmethod

to_string(sort: bool = True) -> str

Returns the graph as a string, this will also be used when writing the graph to a file.

Parameters:

Name Type Description Default
sort bool

Whether the string should sort nodes and edges to minimise git conflicts.

True