FeatureGraph#
- class FeatureGraph(adjacency_list=None, node_features=None)#
Bases:
Graph
,Generic
[F
]- __init__(adjacency_list=None, node_features=None)#
Constructor of the FeatureGraph class.
- Parameters:
adjacency_list (adjacency_list: Optional[dict[Node, dict[Node, SingleEdgeData]]] (default None)) – Optional adjacency list from which to build the feature graph.
node_features (Optional[dict[Node, F]] (default None)) – Node features mapping to start the graph from. If any nodes present here are not in the adjacency list, they are silently added.
Methods
__init__
([adjacency_list, node_features])Constructor of the FeatureGraph class.
add_edge
(edge)Add a single edge to the graph.
add_edges_from
(edges)Add multiple edges from an iterable.
add_node
(node)Add a single node to the graph.
add_node_with_features
(node, features)Add a single node to the graph and assign its features.
add_nodes_from
(nodes)Add multiple nodes from an iterable.
Add nodes along with their features from a bunch.
degree
(node)Return degree for a given node.
get_edge_data
(source, target)- rtype:
Union
[TypeVar
(EdgeData
),NoEdge
]
get_node_features
(node)Return node features of the given node.
increment_n_ops
([increment])Convenience method to increment n_ops count of the given complexity object.
indegree
(node)Return indegree for a given node.
neighbors
(node)Return a set of adjacent nodes for a given node.
outdegree
(node)Return outdegree for a given node.
remove_edge
(source, target, *data)Remove an edge from the graph.
remove_edges_from
(edges_to_remove)Remove a given bunch of edges from the graph.
remove_node
(node)Remove a node from the graph.
remove_nodes_from
(nodes_to_remove)Remove a given bunch of nodes from the graph.
Convenience method to reset n_ops count of the given complexity object.
Attributes
Getter for the adjacency list representation of the graph.
Getter for transposed adjacency list representation of the graph.
Getter for the adjacency matrix of the graph.
Retrieve the edges of this graph.
- rtype:
bool
- rtype:
bool
- rtype:
int
- rtype:
str
Getter for the node features dictionary of the graph.
Retrieve the nodes of this graph.
Retrieve the number of edges of this graph.
Retrieve the number of nodes of this graph.
- rtype:
str
- add_edge(edge)#
Add a single edge to the graph. An edge is a tuple of two nodes and edge data. If an edge is already present in the graph between two nodes, edge’s data is rewritten by the input of this method. Both nodes are silently added to the graph in case they are not present in the graph.
- Parameters:
edge (Edge) – The edge to add represented as a tuple of (nodeA, nodeB, edge data).
- Return type:
None
- add_edges_from(edges)#
Add multiple edges from an iterable.
- Parameters:
edges (Iterable[Edge]) – Iterable from which to add the edges. Edges are represented as tuples of (source node, destination node, edge data).
- Return type:
None
- add_node(node)#
Add a single node to the graph. Nothing changes if the node already exists.
- Parameters:
node (Node) – Node to add.
- Return type:
None
- add_node_with_features(node, features)#
Add a single node to the graph and assign its features. If the node already exists, its features are rewritten.
- Parameters:
node (Node) – Node to add.
features (F) – Features to be assigned to the node
- Return type:
None
- add_nodes_from(nodes)#
Add multiple nodes from an iterable.
- Parameters:
nodes (Iterable[Node]) – Iterable from which to add the nodes.
- Return type:
None
- add_nodes_with_features_from(node_features_mapping)#
Add nodes along with their features from a bunch.
- Parameters:
node_features_mapping (dict[Node, F]) – Dictionary of node : node features pairs to add as nodes.
- Return type:
None
- property adjacency_list: dict[Node, dict[Node, EdgeData]]#
Getter for the adjacency list representation of the graph.
- Returns:
adjacency_list – Adjacency list representation of the graph, represented as a dict of node : neighbours pairs with neighbours being a dict of neighbour : edge data or set of edge data in case of a multigraph.
- Return type:
dict[Node, dict[Node, EdgeData]]
- property adjacency_list_transposed: dict[Node, dict[Node, EdgeData]]#
Getter for transposed adjacency list representation of the graph.
- Returns:
adjacency_list_transposed – If the graph is directed, return an adjacency list with all edges having opposite direction. Otherwise, simply return the actual adjacency list.
- Return type:
dict[Node, dict[Node, EdgeData]]
- property adjacency_matrix: list[list[Union[EdgeData, algpy_src.data_structures.graphs.graph_utils.no_edge_object.NoEdge]]]#
Getter for the adjacency matrix of the graph. Note that the graph is internally represented as an adjacency list, thus the adjacency matrix is built in O(n^2) time with O(n^2) space complexity for each call of this method. It is then cached as the graph’s attribute until further change in the graph.
- Returns:
adjacency_matrix – Adjacency matrix representation of the graph object represented as a list of lists with edges represented either by the data itself or set of data in case of a multigraph. Symmetrical for undirected graph.
- Return type:
list[list[EdgeData | NoEdge]]
- degree(node)#
Return degree for a given node.
- Parameters:
node (Node) – Node for which to find the degree. If not present in the graph, 0 is returned.
- Returns:
degree – Degree of the given node.
- Return type:
int
- property edges: set[tuple[Node, Node, EdgeData]]#
Retrieve the edges of this graph. Edges are represented as tuples of (source node, destination node, edge data). The order retrieved here corresponds to the order of being added to the graph and there can be multiple edges between two nodes in case of a multigraph. This method retrieves only one direction of each edge even for undirected graph, which is internally represented as a directed graph with edges going both ways.
- Returns:
edges – Edges of this graph.
- Return type:
set[Edge]
- get_node_features(node)#
Return node features of the given node.
- increment_n_ops(increment=1)#
Convenience method to increment n_ops count of the given complexity object.
- Return type:
None
- indegree(node)#
Return indegree for a given node.
- Parameters:
node (Node) – Node for which to find the indegree. If not present in the graph, 0 is returned.
- Returns:
indegree – Indegree of the given node.
- Return type:
int
- property is_directed: bool#
- Return type:
bool
- property is_multigraph: bool#
- Return type:
bool
- property n_ops: int#
- Return type:
int
- property name: str#
- Return type:
str
- neighbors(node)#
Return a set of adjacent nodes for a given node.
- Parameters:
node (Node) – Node for which to find the neighbours. If not present in the graph, empty set is returned.
- Returns:
neighbours – A set of adjacent nodes.
- Return type:
set[Node]
- property node_features: dict[Node, F]#
Getter for the node features dictionary of the graph.
- Returns:
node_features – Dictionary of node features assigned to each node.
- Return type:
dict[Node, F]
- property nodes: list[Node]#
Retrieve the nodes of this graph. Nodes can be represented by any Python object and the order retrieved here corresponds to the order of being added to the graph.
- Returns:
nodes – Nodes of this graph.
- Return type:
list[Node]
- property number_of_edges: int#
Retrieve the number of edges of this graph. In case of an undirected graph, each edge is counted only once despite being internally represented as a directed graph.
- Returns:
number_of_edges – Number of edges of this graph.
- Return type:
int
- property number_of_nodes: int#
Retrieve the number of nodes of this graph.
- Returns:
number_of_nodes – Number of nodes of this graph.
- Return type:
int
- outdegree(node)#
Return outdegree for a given node.
- Parameters:
node (Node) – Node for which to find the outdegree. If not present in the graph, 0 is returned.
- Returns:
outdegree – Outdegree of the given node.
- Return type:
int
- remove_edge(source, target, *data)#
Remove an edge from the graph. If an edge is not present in the graph, it is silently ignored.
- Parameters:
source (Node) – Source node of the edge to remove.
target (Node) – Target node of the edge to remove.
*data (SingleEdgeData) – Data of the edge to be removed. Only one entry has to be given for a simple graph.
- Return type:
None
- remove_edges_from(edges_to_remove)#
Remove a given bunch of edges from the graph.
- Parameters:
edges_to_remove (Iterable[Edge | tuple[Node, Node]]) – Edges to be removed from the graph, given either as tuple(s) (source node, destination node, edge data) or just (source node, destination node).
- Return type:
None
- remove_node(node)#
Remove a node from the graph. If a node does not exist in the graph, it is silently ignored.
- Parameters:
node (Node) – Node to be removed.
- Return type:
None
- remove_nodes_from(nodes_to_remove)#
Remove a given bunch of nodes from the graph.
- Parameters:
nodes_to_remove (Iterable[Node]) – Nodes to be removed from the graph.
- Return type:
None
- reset_n_ops()#
Convenience method to reset n_ops count of the given complexity object.
- Return type:
None
- property space_complexity: str#
- Return type:
str