CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-igraph

High performance graph data structures and algorithms for Python

Pending
Overview
Eval results
Files

graph-creation.mddocs/

Graph Creation

Methods for creating graphs from various sources including edge lists, adjacency matrices, classic graph families, and random graph models. igraph provides extensive capabilities for generating both synthetic graphs for testing and real-world graph structures.

Capabilities

Basic Graph Construction

Create graphs from fundamental data structures like edge lists, adjacency matrices, and vertex/edge specifications.

class Graph:
    def __init__(self, n=0, edges=None, directed=False, graph_attrs=None, vertex_attrs=None, edge_attrs=None):
        """
        Create a graph from basic components.
        
        Parameters:
        - n: int, number of vertices (default 0)
        - edges: list of tuples, edge list [(source, target), ...]
        - directed: bool, whether graph is directed (default False)
        - graph_attrs: dict, graph-level attributes
        - vertex_attrs: dict, vertex attribute dictionaries  
        - edge_attrs: dict, edge attribute dictionaries
        
        Returns:
        Graph object
        """

    @classmethod
    def Adjacency(cls, matrix, mode=ADJ_DIRECTED, weighted=None, loops=True):
        """
        Create graph from adjacency matrix.
        
        Parameters:
        - matrix: 2D array-like, adjacency matrix
        - mode: adjacency mode (ADJ_DIRECTED, ADJ_UNDIRECTED, ADJ_MAX, etc.)
        - weighted: bool/str, whether edges are weighted or weight attribute name
        - loops: bool, whether self-loops are allowed
        
        Returns:
        Graph object
        """

    @classmethod 
    def Incidence(cls, matrix, directed=False, mode=OUT, multiple=False, weighted=None):
        """
        Create graph from incidence matrix.
        
        Parameters:
        - matrix: 2D array-like, incidence matrix (vertices x edges)
        - directed: bool, whether graph is directed
        - mode: edge direction mode for directed graphs (IN, OUT, ALL)
        - multiple: bool, whether multiple edges are allowed
        - weighted: bool/str, edge weights
        
        Returns:
        Graph object
        """

Usage Examples:

import igraph as ig
import numpy as np

# From edge list
edges = [(0, 1), (1, 2), (2, 0), (2, 3)]
g1 = ig.Graph(edges=edges, directed=False)

# With attributes
g2 = ig.Graph(
    n=4, 
    edges=edges,
    vertex_attrs={"name": ["A", "B", "C", "D"]},
    edge_attrs={"weight": [1, 2, 3, 1]}
)

# From adjacency matrix
adj_matrix = [[0, 1, 1, 0],
              [1, 0, 1, 1],
              [1, 1, 0, 1], 
              [0, 1, 1, 0]]
g3 = ig.Graph.Adjacency(adj_matrix, mode=ig.ADJ_UNDIRECTED)

# Weighted from numpy array
weights = np.random.rand(4, 4)
g4 = ig.Graph.Adjacency(weights, weighted=True)

Classic Graph Families

Generate well-known graph structures including complete graphs, cycles, trees, and lattices.

class Graph:
    @classmethod
    def Full(cls, n, directed=False, loops=False):
        """
        Create complete graph.
        
        Parameters:
        - n: int, number of vertices
        - directed: bool, whether graph is directed  
        - loops: bool, whether to include self-loops
        
        Returns:
        Complete graph with n vertices
        """

    @classmethod
    def Ring(cls, n, directed=False, mutual=False, circular=True):
        """
        Create ring/cycle graph.
        
        Parameters:
        - n: int, number of vertices
        - directed: bool, whether graph is directed
        - mutual: bool, add reverse edges in directed case
        - circular: bool, connect last vertex to first
        
        Returns:
        Ring graph
        """

    @classmethod
    def Tree(cls, n, children=2, mode=TREE_UNDIRECTED):
        """
        Create regular tree.
        
        Parameters:
        - n: int, number of vertices
        - children: int, children per internal node
        - mode: tree direction (TREE_UNDIRECTED, TREE_IN, TREE_OUT)
        
        Returns:
        Tree graph
        """

    @classmethod
    def Lattice(cls, dim, nei=1, directed=False, mutual=False, circular=False):
        """
        Create lattice graph.
        
        Parameters:
        - dim: list of ints, dimensions of lattice
        - nei: int, neighborhood distance
        - directed: bool, whether graph is directed
        - mutual: bool, add reverse edges
        - circular: bool, periodic boundary conditions
        
        Returns:
        Lattice graph
        """

    @classmethod
    def Star(cls, n, mode=STAR_UNDIRECTED, center=0):
        """
        Create star graph.
        
        Parameters:
        - n: int, number of vertices
        - mode: star type (STAR_UNDIRECTED, STAR_IN, STAR_OUT, STAR_MUTUAL)
        - center: int, index of center vertex
        
        Returns:
        Star graph with center vertex
        """

Usage Examples:

# Complete graphs
complete5 = ig.Graph.Full(5)  # K5 complete graph
directed_complete = ig.Graph.Full(4, directed=True)

# Cycles and paths  
cycle10 = ig.Graph.Ring(10)  # 10-vertex cycle
path10 = ig.Graph.Ring(10, circular=False)  # 10-vertex path

# Trees
binary_tree = ig.Graph.Tree(15, children=2)  # Binary tree
ternary_tree = ig.Graph.Tree(40, children=3)  # Ternary tree

# Lattices
grid_3x3 = ig.Graph.Lattice([3, 3], circular=False)  # 3x3 grid
torus_4x4 = ig.Graph.Lattice([4, 4], circular=True)  # 4x4 torus

# Star graphs
star6 = ig.Graph.Star(7)  # 6 leaves + 1 center

Random Graph Models

Generate random graphs using various probabilistic models for network simulation and testing.

class Graph:
    @classmethod
    def Erdos_Renyi(cls, n, m=None, p=None, directed=False, loops=False):
        """
        Create Erdős-Rényi random graph.
        
        Parameters:
        - n: int, number of vertices
        - m: int, number of edges (use m OR p, not both)
        - p: float, edge probability (use m OR p, not both) 
        - directed: bool, whether graph is directed
        - loops: bool, allow self-loops
        
        Returns:
        Random graph
        """

    @classmethod
    def Barabasi(cls, n, m=1, power=1, zero_appeal=1, directed=False):
        """
        Create Barabási-Albert preferential attachment graph.
        
        Parameters:
        - n: int, number of vertices
        - m: int/list, edges per step or per-vertex out-degrees
        - power: float, power of preferential attachment
        - zero_appeal: float, attractiveness of vertices with degree 0
        - directed: bool, whether graph is directed
        
        Returns:
        Scale-free graph
        """

    @classmethod
    def Watts_Strogatz(cls, dim, size, nei, p):
        """
        Create Watts-Strogatz small-world graph.
        
        Parameters:
        - dim: int, dimension of lattice
        - size: int, size in each dimension  
        - nei: int, neighborhood size
        - p: float, rewiring probability
        
        Returns:
        Small-world graph
        """

    @classmethod
    def Random_Geometric(cls, n, radius, torus=False):
        """
        Create random geometric graph.
        
        Parameters:
        - n: int, number of vertices
        - radius: float, connection radius
        - torus: bool, use torus topology
        
        Returns:
        Geometric graph with embedded coordinates
        """

Usage Examples:

# Erdős-Rényi graphs
er_fixed_edges = ig.Graph.Erdos_Renyi(100, m=200)  # 100 vertices, 200 edges
er_prob = ig.Graph.Erdos_Renyi(100, p=0.05)  # 100 vertices, 5% edge probability

# Scale-free graphs
scale_free = ig.Graph.Barabasi(1000, m=3)  # 1000 vertices, 3 edges per step
directed_sf = ig.Graph.Barabasi(500, m=2, directed=True)

# Small-world graphs  
small_world = ig.Graph.Watts_Strogatz(1, 100, 4, 0.1)  # Ring lattice rewiring

# Geometric graphs
geometric = ig.Graph.Random_Geometric(200, 0.1)  # 200 points, radius 0.1

Bipartite Graphs

Create and work with bipartite (two-mode) graphs where vertices are divided into two disjoint sets.

class Graph:
    @classmethod
    def Bipartite(cls, types, edges, directed=False):
        """
        Create bipartite graph from vertex types and edges.
        
        Parameters:
        - types: list of bool, vertex types (True/False for two sets)
        - edges: list of tuples, edge list
        - directed: bool, whether graph is directed
        
        Returns:
        Bipartite graph with type attribute
        """

    @classmethod
    def Random_Bipartite(cls, n1, n2, p=None, m=None, directed=False):
        """
        Create random bipartite graph.
        
        Parameters:
        - n1, n2: int, sizes of vertex sets
        - p: float, edge probability between sets
        - m: int, number of edges (use p OR m)
        - directed: bool, whether graph is directed
        
        Returns:
        Random bipartite graph
        """

    def is_bipartite(self):
        """
        Check if graph is bipartite.
        
        Returns:
        bool, True if graph is bipartite
        """

    def bipartite_projection(self, types=None, multiplicity=True, probe1=-1):
        """
        Create one-mode projections of bipartite graph.
        
        Parameters:
        - types: list, vertex types (if not stored as attribute)
        - multiplicity: bool, whether to store edge multiplicities
        - probe1: int, which projection to return (-1 for both)
        
        Returns:
        Projected graph(s)
        """

Usage Examples:

# Create bipartite graph
types = [False, False, False, True, True, True]  # 3+3 partition
edges = [(0, 3), (0, 4), (1, 3), (1, 5), (2, 4), (2, 5)]
bip = ig.Graph.Bipartite(types, edges)

# Random bipartite
random_bip = ig.Graph.Random_Bipartite(10, 15, p=0.3)  # 10+15 vertices

# Check and project
is_bip = bip.is_bipartite()
proj1, proj2 = bip.bipartite_projection()  # Both projections

Named Graphs

Generate specific named graphs and graph families commonly used in graph theory and network analysis.

class Graph:
    @classmethod 
    def Famous(cls, name):
        """
        Create famous named graphs.
        
        Parameters:
        - name: str, name of famous graph
          ("Petersen", "Karate", "Zachary", "Tutte", etc.)
        
        Returns:
        Named graph
        """

    @classmethod
    def Atlas(cls, n):
        """
        Create graph from Graph Atlas.
        
        Parameters:
        - n: int, index in atlas (0-1252)
        
        Returns:
        Graph from atlas
        """

    @classmethod
    def Kautz(cls, m, n):
        """
        Create Kautz graph.
        
        Parameters:
        - m: int, base (alphabet size minus 1)
        - n: int, dimension
        
        Returns:
        Kautz graph
        """

    @classmethod
    def de_Bruijn(cls, m, n):
        """
        Create de Bruijn graph.
        
        Parameters:
        - m: int, alphabet size
        - n: int, string length
        
        Returns:
        de Bruijn graph
        """

Usage Examples:

# Famous graphs
petersen = ig.Graph.Famous("Petersen")
karate = ig.Graph.Famous("Zachary")  # Karate club network
tutte = ig.Graph.Famous("Tutte")

# Graph atlas
small_graph = ig.Graph.Atlas(123)  # Graph #123 from atlas

# Kautz and de Bruijn
kautz = ig.Graph.Kautz(3, 4)  # Kautz graph K(3,4)
debruijn = ig.Graph.de_Bruijn(2, 5)  # Binary strings of length 5

Install with Tessl CLI

npx tessl i tessl/pypi-igraph

docs

analysis.md

clustering.md

community.md

data-types.md

graph-creation.md

graph-structure.md

index.md

io-formats.md

layout.md

sequences.md

visualization.md

tile.json