CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-networkx

Python package for creating and manipulating graphs and networks

Pending
Overview
Eval results
Files

graph-classes.mddocs/

Graph Classes

Core graph data structures that form the foundation of NetworkX. These classes support different types of networks with flexible node and edge attribute storage.

Capabilities

Undirected Graph

Base class for undirected graphs. Supports self-loops but not multiple edges between the same nodes.

class Graph:
    def __init__(self, incoming_graph_data=None, **attr):
        """
        Initialize a graph with optional data and attributes.
        
        Parameters:
        - incoming_graph_data: Input graph data (edge list, dict of dicts, NetworkX graph, etc.)
        - **attr: Graph attributes as keyword arguments
        """
    
    # Node operations
    def add_node(self, node_for_adding, **attr):
        """Add a single node with optional attributes."""
    
    def add_nodes_from(self, nodes_for_adding, **attr):
        """Add multiple nodes with optional attributes."""
    
    def remove_node(self, n):
        """Remove node n and all adjacent edges."""
    
    def remove_nodes_from(self, nodes):
        """Remove multiple nodes and all adjacent edges."""
    
    def has_node(self, n):
        """Return True if node n is in the graph."""
    
    def number_of_nodes(self):
        """Return the number of nodes in the graph."""
    
    # Edge operations  
    def add_edge(self, u_of_edge, v_of_edge, **attr):
        """Add an edge between u and v with optional attributes."""
    
    def add_edges_from(self, ebunch_to_add, **attr):
        """Add edges from an iterable of edge tuples."""
    
    def remove_edge(self, u, v):
        """Remove the edge between u and v."""
    
    def remove_edges_from(self, ebunch):
        """Remove edges from an iterable of edge tuples."""
    
    def has_edge(self, u, v):
        """Return True if the edge (u,v) is in the graph."""
    
    def get_edge_data(self, u, v, default=None):
        """Return edge attribute dictionary for edge (u,v)."""
    
    def number_of_edges(self):
        """Return the number of edges in the graph."""
    
    # Graph properties
    def nodes(self, data=False, default=None):
        """Return a view of the graph nodes."""
    
    def edges(self, nbunch=None, data=False, default=None):
        """Return a view of the graph edges."""
    
    def neighbors(self, n):
        """Return an iterator over neighbors of node n."""
    
    def degree(self, nbunch=None, weight=None):
        """Return degree of nodes."""
    
    def adjacency(self):
        """Return an iterator over (node, adjacency dict) tuples."""
    
    # Graph operations
    def copy(self, as_view=False):
        """Return a copy of the graph."""
    
    def clear(self):
        """Remove all nodes and edges from the graph."""
    
    def clear_edges(self):
        """Remove all edges from the graph without altering nodes."""
    
    def update(self, edges=None, nodes=None):
        """Update graph using nodes/edges/graphs as input."""
    
    def size(self, weight=None):
        """Return number of edges or sum of edge weights."""
    
    def subgraph(self, nodes):
        """Return a subgraph view containing only specified nodes."""
    
    def edge_subgraph(self, edges):
        """Return edge-induced subgraph view."""
    
    def to_directed(self, as_view=False):
        """Return directed representation of the graph."""
    
    def to_undirected(self, as_view=False):
        """Return undirected representation of the graph."""
    
    def is_multigraph(self):
        """Return True if graph is a multigraph."""
    
    def is_directed(self):
        """Return True if graph is directed."""
    
    def order(self):
        """Return number of nodes (same as number_of_nodes)."""

Directed Graph

Directed graph class that extends Graph with support for directed edges and in/out degree operations.

class DiGraph(Graph):
    """Directed graph class supporting directed edges."""
    
    # Directed-specific degree methods
    def in_degree(self, nbunch=None, weight=None):
        """Return in-degree of nodes."""
    
    def out_degree(self, nbunch=None, weight=None):
        """Return out-degree of nodes."""
    
    # Directed neighbors
    def predecessors(self, n):
        """Return an iterator over predecessor nodes of n."""
    
    def successors(self, n):
        """Return an iterator over successor nodes of n."""
    
    # Directed graph operations
    def reverse(self, copy=True):
        """Return the reverse of the graph."""
    
    def to_undirected(self, reciprocal=False, as_view=False):
        """Return an undirected representation of the digraph."""

Undirected MultiGraph

Undirected graph that allows multiple edges between any pair of nodes.

class MultiGraph(Graph):
    """Undirected multigraph supporting multiple edges."""
    
    # Multi-edge operations
    def add_edge(self, u_of_edge, v_of_edge, key=None, **attr):
        """Add an edge with optional key and attributes."""
    
    def remove_edge(self, u, v, key=None):
        """Remove an edge by key, or arbitrary edge if key not specified."""
    
    def edges(self, nbunch=None, data=False, keys=False, default=None):
        """Return edges with optional keys."""
    
    def get_edge_data(self, u, v, key=None, default=None):
        """Return the attribute dictionary associated with edge (u,v)."""
    
    def number_of_edges(self, u=None, v=None):
        """Return number of edges between u and v."""

Directed MultiGraph

Directed graph that allows multiple edges between any pair of nodes.

class MultiDiGraph(DiGraph, MultiGraph):
    """Directed multigraph supporting multiple directed edges."""
    
    # Inherits methods from both DiGraph and MultiGraph
    def to_undirected(self, reciprocal=False, as_view=False):
        """Return an undirected MultiGraph representation."""
    
    def reverse(self, copy=True):
        """Return the reverse of the directed multigraph."""

Graph Views and Filters

Classes for creating filtered and transformed views of graphs without copying data.

class SubGraph:
    """A view of a subgraph of another graph."""
    
class GraphView:
    """A read-only view of a graph."""

class NodeView:
    """A view of graph nodes with attribute access."""

class EdgeView:
    """A view of graph edges with attribute access."""

class DegreeView:
    """A view of node degrees."""

Usage Examples

Creating Different Graph Types

import networkx as nx

# Undirected graph
G = nx.Graph()
G.add_edges_from([(1, 2), (2, 3), (3, 1)])

# Directed graph  
DG = nx.DiGraph()
DG.add_edges_from([(1, 2), (2, 3), (3, 1)])

# Multigraph (allows multiple edges)
MG = nx.MultiGraph()
MG.add_edge(1, 2, key='first')
MG.add_edge(1, 2, key='second')  # Second edge between same nodes

# Directed multigraph
MDG = nx.MultiDiGraph()
MDG.add_edge(1, 2, key='a', weight=1.5)
MDG.add_edge(1, 2, key='b', weight=2.0)

Node and Edge Attributes

G = nx.Graph()

# Add nodes with attributes
G.add_node(1, name='Alice', age=25)
G.add_node(2, name='Bob', age=30)

# Add edges with attributes
G.add_edge(1, 2, weight=0.8, relationship='friend')

# Access attributes
print(G.nodes[1]['name'])  # 'Alice'
print(G.edges[1, 2]['weight'])  # 0.8

Graph Operations

G = nx.complete_graph(5)

# Basic properties
print(f"Nodes: {G.number_of_nodes()}")
print(f"Edges: {G.number_of_edges()}")
print(f"Density: {nx.density(G)}")

# Node operations
neighbors = list(G.neighbors(0))
degree = G.degree(0)

# Subgraph
subgraph = G.subgraph([0, 1, 2])

# Copy
G_copy = G.copy()

Install with Tessl CLI

npx tessl i tessl/pypi-networkx

docs

algorithms.md

conversion.md

drawing.md

generators.md

graph-classes.md

graph-io.md

index.md

linear-algebra.md

tile.json