Examples

This section provides practical examples demonstrating various features of egraph.

Overview

These examples demonstrate real-world usage of egraph’s features. Each example is self-contained and can be run independently.

Layout Algorithms

Advanced Drawing Spaces

Specialized Features

Quick Example

Here’s a simple example of creating a graph and applying a layout algorithm:

import networkx as nx
import egraph as eg

# Create a graph from NetworkX
nx_graph = nx.les_miserables_graph()
graph = eg.Graph()
indices = {}
for u in nx_graph.nodes:
    indices[u] = graph.add_node(u)
for u, v in nx_graph.edges:
    graph.add_edge(indices[u], indices[v], (u, v))

# Create an initial drawing
drawing = eg.DrawingEuclidean2d.initial_placement(graph)

# Apply a layout algorithm
sm = eg.StressMajorization(graph, drawing, lambda _: 100)
sm.run(drawing)

# Extract node positions
pos = {u: (drawing.x(i), drawing.y(i)) for u, i in indices.items()}

# Visualize with NetworkX
import matplotlib.pyplot as plt
nx.draw(nx_graph, pos)