NetworkX Examples

Let’s begin by creating a directed graph with random edge weights.

import algorithmx
import networkx as nx
from random import randint

canvas = algorithmx.jupyter_canvas()

# Create a directed graph
G = nx.circular_ladder_graph(5).to_directed()
# Randomize edge weights
nx.set_edge_attributes(G, {e: {'weight': randint(1, 9)} for e in G.edges})

# Add nodes
canvas.nodes(G.nodes).add()

# Add directed edges with weight labels
canvas.edges(G.edges).add().directed(True) \
    .label().text(lambda e: G.edges[e]['weight'])

canvas

Next, we can use NetworkX run a breadth-first search, and AlgorithmX to animate it.

canvas = algorithmx.jupyter_canvas(buttons=True)
canvas.size((500, 400))

# Generate a 'caveman' graph with 3 cliques of size 4
G = nx.connected_caveman_graph(3, 4)

# Add nodes and edges
canvas.nodes(G.nodes).add()
canvas.edges(G.edges).add()
canvas.pause(1)

# Traverse the graph using breadth-first search
bfs = nx.edge_bfs(G, 0)

# Animate traversal
source = None
for e in bfs:
    if e[0] != source:
        # Make the new source large
        canvas.node(e[0]).size('1.25x').color('purple')
        # Make the previous source small again
        if source is not None:
            canvas.node(source).size('0.8x')
        # Update source node
        source = e[0]
        canvas.pause(0.5)

    # Traverse edges
    canvas.edge(e).traverse().color('pink')
    canvas.pause(0.5)

# Make the remaining source small again
canvas.node(source).size('0.8x')

canvas

For our final visualization, let’s find the shortest path on a random graph using Dijkstra’s algorithm.

import random
random.seed(436)

canvas = algorithmx.jupyter_canvas(buttons=True)
canvas.size((500, 400))

# Generate random graph with random edge weights
G = nx.newman_watts_strogatz_graph(16, 2, 0.4, seed=537)
nx.set_edge_attributes(G, {e: randint(1, 20) for e in G.edges}, 'weight')

# Add nodes and edges with weight labels
canvas.nodes(G.nodes).add()
canvas.edges(G.edges).add().label().text(lambda e: G.edges[e]['weight'])
canvas.pause(1)

# Select source and target
source = 0
target = 8
canvas.node(source).color('green').highlight().size('1.25x')
canvas.node(target).color('red').highlight().size('1.25x')
canvas.pause(1.5)

# Run Dijkstra's shortest path algorithm
path = nx.dijkstra_path(G, source, target)

# Animate the algorithm
path_length = 0
for i in range(len(path) - 1):
    u, v = path[i], path[i + 1]

    # Update path length
    path_length += G[u][v]['weight']

    # Traverse edge
    canvas.edge((u, v)).traverse().color('blue')
    canvas.pause(0.4)

    # Make the next node blue, unless it's the target
    if v != target:
        canvas.node(v).color('blue')

    # Add a label to indicate current path length
    canvas.node(v).label('path').add().color('blue').text(path_length)
    canvas.pause(0.4)

canvas