Planar Graphs · Planar Graphs

Lesson 5

Nikolai Chukhin · Alexander S. Kulikov

Let's demonstrate how differently the same graph can be drawn. The code below builds six different layouts of the dodecahedron graph. The first layout chooses vertex positions randomly. As you can see, the resulting layout is hard to interpret. The second layout arranges the vertices in a circle. This is already a nicer picture: for example, it's immediately clear that each vertex has degree three. The third, fourth, and fifth layouts are spring-based: the graph's edges are modeled as springs, and a stable configuration is found. As you can see, the results are nice symmetric pictures. The sixth layout is planar: it has no edge crossings, but it’s probably not the most effective layout for this particular graph.

import networkx as nx
import matplotlib.pyplot as plt

g = nx.dodecahedral_graph()

positions = [
    nx.random_layout(g, seed=4),
    nx.circular_layout(g),
    nx.spring_layout(g, seed=5),
    nx.spring_layout(g, seed=19),
    nx.spring_layout(g, seed=58),
    nx.planar_layout(g, scale=2),
]

for idx, pos in enumerate(positions):
    plt.clf()
    plt.gca().set_aspect('equal')
    nx.draw(g, pos=pos, node_size=40)
    plt.savefig(f'dod{idx}.png')