In [51]:
%matplotlib inline
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mpimg

# set up the game board
CHUTES = [
    (16, 6),
    (47, 26),
    (49, 11),
    (56, 53),
    (62, 19),
    (64, 60),
    (87, 24),
    (93, 73),
    (95, 75),
    (98, 78),
]
LADDERS = [
    (1, 38),
    (4, 14),
    (9, 31),
    (21, 42),
    (28, 84),
    (36, 44),
    (51, 67),
    (71, 91),
    (80, 100),
]
chutes_and_ladders = dict(CHUTES + LADDERS)

nodes = set(range(0, 101, 1)) - set(chutes_and_ladders)
edge_weights = {}
for node in nodes - {100}:
    for i in xrange(1, 7):
        edge = (node, min(100, chutes_and_ladders.get(node + i, node + i)))
        edge_weights.setdefault(edge, 0)
        edge_weights[edge] += 1
edges = [(k[0], k[1], v) for k, v in edge_weights.iteritems()]


G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_weighted_edges_from(edges)
pos = {}
for node in G.nodes_iter():
    pos[node] = (
        (node - 1) % 10 if ((node - 1) / 10) % 2 == 0 else 9 - ((node - 1) % 10),
        (node - 1) / 10
    )
pos[0] = (-1, 0)
pos = {k: (25 + x * 49, 470 - y * 49) for k, (x, y) in pos.items()}

def draw_graph(title='Chutes and Ladders', labels=None, show_edges=True):
    img = mpimg.imread('chutes_and_ladders.jpg')
    plt.imshow(img)
    nodes = nx.draw_networkx_nodes(G, pos, node_size=2000, node_color='y', alpha=0.8, linewidths=3)
    nodes.set_edgecolor('b')
    if show_edges:
        nx.draw_networkx_edges(G, pos, alpha=0.2, width=2, edge_color='b')
    nx.draw_networkx_labels(G, pos, labels=labels, font_size=20)
    plt.title(title, fontdict={'fontsize': 36})
    fig = plt.gcf()
    fig.set_size_inches(20, 20)
    

draw_graph()