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:
objectUndirected 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
nor(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).
- 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
nor(n, attrs_dict). common attrs apply to all.
- 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).
- get_edge_data(u: Hashable, v: Hashable, default: Any = None) Any[source]#
Edge attribute dict, or default when the edge does not exist.
- 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
NodeMapof all degrees.With weight, sums that edge attribute (missing values count as 1). A self-loop contributes 2 to its node’s degree.
- 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→vandv→umerge; 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).
- node_index() dict[Hashable, int][source]#
{node: position}in iteration order, which is the row order of every matrix AryaGraph builds.
- layout(method: str = 'auto', **kwargs: Any)[source]#
Compute a layout; see
aryagraph.layout.compute().