corneto.graph.Graph#

class corneto.graph.Graph(default_edge_type=EdgeType.DIRECTED, **kwargs)#

Bases: BaseGraph

Concrete graph implementation supporting directed/undirected edges and hyperedges.

Allows parallel edges (multiple edges between same vertices) and hyperedges (edges connecting multiple vertices). Edges and vertices can have attributes.

Examples

>>> graph = corneto.Graph()
>>> graph.add_edge(1, 2)
>>> graph.plot()
Parameters:

default_edge_type (EdgeType)

__init__(default_edge_type=EdgeType.DIRECTED, **kwargs)#

Initialize Graph.

Parameters:
  • default_edge_type (EdgeType) – Default type for new edges

  • **kwargs – Additional graph attributes

Return type:

None

Methods

__init__([default_edge_type])

Initialize Graph.

add_edge(source, target[, type, ...])

Add edge to the graph.

add_edges(edges[, type])

Add multiple edges to the graph.

add_vertex(v, **kwargs)

Add vertex to the graph.

add_vertices(vertices, **kwargs)

Add multiple vertices to the graph.

bfs(starting_vertices[, reverse, undirected])

Perform breadth-first search (BFS) traversal.

copy()

Create deep copy of graph.

edge_subgraph(edges)

Create subgraph induced by a set of edges.

edges([vertices])

Get edges in the graph.

extract_subgraph([vertices, edges])

Extract subgraph induced by vertices and/or edges.

filter_graph([filter_vertex, filter_edge])

Create filtered graph based on vertex and edge predicates.

from_dict(data)

Create a graph from a dictionary representation.

from_json(json_str)

Create a graph from a JSON string.

from_tuples(tuples)

Alias for corneto.io.load_graph_from_sif_tuples for backwards compatibility.

get_attr_edge(index)

Get attributes of an edge.

get_attr_edges([indexes])

Get attributes of multiple edges.

get_attr_from_edges(attr[, default])

Get specific attribute from all edges.

get_attr_vertex(v)

Get attributes of a vertex.

get_attr_vertices([vertices])

Get attributes of multiple vertices.

get_common_incident_edges(vertices)

Get common incident edges for multiple vertices.

get_edge(index)

Get edge at specified index.

get_edges(indexes)

Get multiple edges by their indices.

get_edges_by_attr(key, value)

Get edges by specific attribute value.

get_graph_attributes()

Get global graph attributes.

get_incident_edges(vertices)

Get incident edges for multiple vertices.

get_vertex(index)

Get vertex by its index.

get_vertex_incidence_matrix_as_lists([values])

Get vertex incidence matrix as lists.

hash()

Compute hash of the graph based on its content.

in_edges(vertices)

Get incoming edges for vertices.

is_hypergraph()

Check if the graph is a hypergraph.

load(filename[, compression])

Load graph from a saved file.

load_json(filepath[, compression])

Load graph from a JSON file.

neighbors(vertex)

Get neighbors of a vertex (ignoring edge direction).

out_edges(vertices)

Get outgoing edges for vertices.

plot(**kwargs)

Plot the graph using Graphviz.

plot_values([vertex_values, edge_values, ...])

predecessors(vertices)

Get predecessor vertices for multiple vertices.

prune([source, target])

Prune the graph to include only reachable vertices.

reachability_analysis(input_nodes, output_nodes)

Perform reachability analysis from input nodes to output nodes.

reverse()

Create new graph with all edge directions reversed.

save(filename[, compression])

Save graph to file using pickle serialization.

save_json(filepath[, indent, compression])

Save graph to a JSON file.

subgraph(vertices)

Create subgraph induced by a set of vertices.

successors(vertices)

Get successor vertices for multiple vertices.

to_dict()

Convert graph to a JSON-serializable dictionary.

to_dot(**kwargs)

Convert graph to DOT format.

to_graphviz(**kwargs)

Convert graph to Graphviz representation.

to_json(**kwargs)

Serialize graph to JSON string.

toposort()

Perform topological sort on the graph using Kahn's algorithm.

vertex_incidence_matrix([values, sparse])

Get vertex incidence matrix.

Attributes

E

Edges in the graph.

V

Vertices in the graph.

ne

Number of edges in the graph.

num_edges

Number of edges in the graph.

num_vertices

Number of vertices in the graph.

nv

Number of vertices in the graph.

shape

Shape of the graph (number of vertices, number of edges).

vertices

Vertices in the graph.

get_edge(index)#

Get edge at specified index.

Parameters:

index (int) – Edge index to retrieve

Returns:

Edge tuple (source vertices, target vertices)

Return type:

Tuple[FrozenSet[Any], FrozenSet[Any]]

get_graph_attributes()#

Get global graph attributes.

Returns:

Attributes object containing graph-level properties

Return type:

Attributes

extract_subgraph(vertices=None, edges=None)#

Extract subgraph induced by vertices and/or edges.

Parameters:
  • vertices (Iterable | None) – Optional vertices to include

  • edges (Iterable[int] | None) – Optional edge indices to include

Returns:

New Graph containing only specified vertices/edges and their incident edges

reverse()#

Create new graph with all edge directions reversed.

Returns:

New Graph with reversed edges

Return type:

Graph

filter_graph(filter_vertex=None, filter_edge=None)#

Create filtered graph based on vertex and edge predicates.

Parameters:
  • filter_vertex (Callable[[Any], bool] | None) – Optional function that returns True for vertices to keep

  • filter_edge (Callable[[int], bool] | None) – Optional function that returns True for edges to keep

Returns:

New Graph containing only elements that pass filters

copy()#

Create deep copy of graph.

Returns:

New Graph with copied structure and attributes

Return type:

Graph

static from_tuples(tuples)#

Alias for corneto.io.load_graph_from_sif_tuples for backwards compatibility.

Parameters:

tuples (Iterable[Tuple]) – Iterable of (source, interaction, target) tuples

Returns:

New Graph created from tuple data

to_dict()#

Convert graph to a JSON-serializable dictionary.

Returns:

Dictionary representation of the graph

Return type:

Dict

to_json(**kwargs)#

Serialize graph to JSON string.

Parameters:

**kwargs – Additional arguments passed to json.dumps()

Returns:

JSON string representation of the graph

Return type:

str

classmethod from_dict(data)#

Create a graph from a dictionary representation.

Parameters:

data (Dict) – Dictionary containing graph data

Returns:

New Graph instance created from the dictionary

Return type:

Graph

classmethod from_json(json_str)#

Create a graph from a JSON string.

Parameters:

json_str (str) – JSON string containing graph data

Returns:

New Graph instance created from the JSON string

Return type:

Graph

save_json(filepath, indent=None, compression=None)#

Save graph to a JSON file.

Parameters:
  • filepath (str) – Path where to save the JSON file

  • indent (int | None) – Optional number of spaces for indentation (for pretty printing)

  • compression (str | None) – Optional compression format (‘auto’, ‘gzip’, ‘bz2’, ‘xz’, or None)

Raises:

ValueError – If filepath is empty

Return type:

None

classmethod load_json(filepath, compression='auto')#

Load graph from a JSON file.

Parameters:
  • filepath (str) – Path to the JSON file

  • compression (str | None) – Optional compression format (‘auto’, ‘gzip’, ‘bz2’, ‘xz’, or None)

Returns:

New Graph instance loaded from the file

Raises:

FileNotFoundError – If the file doesn’t exist

Return type:

Graph