aryagraph.core.graph.Graph#

class aryagraph.core.graph.Graph(data: Any = None, *, nodes: Iterable[Any] | None = None, name: str | None = None, **attrs: Any)[source]#

Bases: object

Undirected simple graph (self-loops allowed, no parallel edges).

Parameters:
  • data – Optional initial content: another graph (copied), an adjacency mapping {u: [v, ...]} / {u: {v: attrs}}, or an iterable of edges where each edge is (u, v), (u, v, attrs_dict) or (u, v, weight).

  • nodes – Extra nodes to add, each n or (n, attrs_dict). They are added before the content of data, so they lead the graph order; pass them to fix the order nodes are iterated, laid out and listed in.

  • name – Optional display name (stored as g.attrs['name']).

  • **attrs – Graph-level attributes.

Examples

>>> g = Graph([("a", "b"), ("b", "c", 2.5)], name="demo")
>>> g.add_node("d", color="red")
>>> g.edges["b", "c"]
{'weight': 2.5}
classmethod from_edges(edges: Iterable[Any], **kwargs: Any)[source]#

Build a graph from an edge iterable (see the class docstring for edge forms).

property nodes: NodeView#

Live view of the nodes; g.nodes[n] is n’s attribute dict.

property edges: EdgeView#

Live view of the edges; g.edges[u, v] is the edge’s attribute dict.

property adj: AdjacencyView#

Read-only {node: {neighbor: attrs}} (successors for directed graphs).

property succ: AdjacencyView#

Read-only successor adjacency (same as adj for undirected graphs).

property pred: AdjacencyView#

Read-only predecessor adjacency (same as adj for undirected graphs).

add_node(node: Hashable, **attrs: Any) → None[source]#

Add node (no-op if present) and update its attributes with attrs.

add_nodes(nodes: Iterable[Any], **common: Any) → None[source]#

Add many nodes; each item is n or (n, attrs_dict). common attrs apply to all.

remove_node(node: Hashable) → None[source]#

Remove node and all its incident edges.

remove_nodes(nodes: Iterable[Hashable]) → None[source]#

Remove every node in nodes (missing ones raise NodeNotFound).

add_edge(u: Hashable, v: Hashable, **attrs: Any) → None[source]#

Add edge (u, v) (creating missing endpoints) and update its attributes.

add_edges(edges: Iterable[Any], **common: Any) → None[source]#

Add many edges: (u, v), (u, v, attrs_dict) or (u, v, weight).

common attributes are applied to every edge (per-edge values win).

remove_edge(u: Hashable, v: Hashable) → None[source]#

Remove edge (u, v).

edge_attrs(u: Hashable, v: Hashable) → dict[source]#

The (mutable) attribute dict of edge (u, v).

get_edge_data(u: Hashable, v: Hashable, default: Any = None) → Any[source]#

Edge attribute dict, or default when the edge does not exist.

selfloops() → list[tuple[Hashable, Hashable]][source]#

Edges whose two endpoints coincide.

neighbors(node: Hashable) → Iterator[Hashable][source]#

Iterate over the neighbors of node (successors for directed graphs).

degree(node: Hashable | None = None, weight: str | None = None) → Any[source]#

Degree of node, or a NodeMap of all degrees.

With weight, sums that edge attribute (missing values count as 1). A self-loop contributes 2 to its node’s degree.

copy()[source]#

Deep-enough copy: new structure and new attribute dicts (values are shared).

subgraph(nodes: Iterable[Hashable])[source]#

Induced subgraph on nodes (a new graph; attributes are copied).

The subgraph lists its nodes in the order nodes gives them. A set has no order of its own, so a set (or frozenset) keeps the graph’s order.

edge_subgraph(edges: Iterable[tuple[Hashable, Hashable]])[source]#

Subgraph made of edges and their endpoints.

to_undirected() → Graph[source]#

Undirected copy (for directed graphs, u→v and v→u merge; later attrs win).

to_directed() → DiGraph[source]#

Directed copy; every undirected edge becomes a pair of opposite arcs.

relabel(mapping: Mapping[Hashable, Hashable] | Callable[[Hashable], Hashable])[source]#

Copy with nodes renamed by mapping (dict or function); unmapped nodes keep their name.

compose(other: Graph)[source]#

Union of nodes and edges of both graphs (other’s attributes win on overlap).

clear() → None[source]#

Remove every node and edge (graph attributes are kept).

node_index() → dict[Hashable, int][source]#

{node: position} in iteration order, which is the row order of every matrix AryaGraph builds.

draw(**kwargs: Any)[source]#

Render this graph; see aryagraph.draw() for every option.

layout(method: str = 'auto', **kwargs: Any)[source]#

Compute a layout; see aryagraph.layout.compute().

analyze(**kwargs: Any)[source]#

Full analytical report; see aryagraph.analyze().