Generating a random planar graph in python

Question:

I am looking to generate a random planar graph in python with around 20 vertices. I checked out this planar graph generator but two problems emerged:

  1. The algorithm on the aforementioned GitHub project seems a bit too overkill to generate a random planar graph that doesn’t have those many edges
  2. Because it’s meant to generate massive graph, that algorithm is very complex, and therefore also a bit clunky and difficult to use

With that said, is there a simpler way to randomly generate a relatively small planar graph in python?

Asked By: Emilio

||

Answers:

Create required number of nodes
Assign random x,y locations to the nodes.
WHILE nodes with no connected edges
    Select N a random node with no edge
    LOOP select M a different node at random
        IF edge N-M does NOT intersect previous edges
             Add N-M edge to graph
             BREAK out of LOOP
Answered By: ravenspoint

The algorithm consists of converting a Voronoi diagram, which is a partitioning of a plane into regions based on the distance to points in a specific subset of the plane, into a weighted graph represented by a networkx object in Python.

The input to the algorithm is a list of points in two dimensions. The Voronoi diagram is computed using the scipy.spatial library, which returns a set of vertices and edges representing the diagram.

The algorithm then iterates through the edges of the Voronoi diagram and checks if each edge connects two points whose coordinates lie in the range [0,1]. If so, the edge is added as an edge in the networkx graph, with a weight equal to the Euclidean distance between the two endpoints of the edge.

Finally, the resulting networkx graph is returned as output. This graph represents the Voronoi diagram as a weighted graph, where the vertices correspond to the points in the input list and the edges represent the borders between the Voronoi regions. The weights on the edges represent the lengths of these borders.

The Voronoi diagram itself can be thought of as a planar graph, where the vertices represent the input points and the edges represent the borders between the Voronoi regions. This graph can be used for various applications, such as geographic information systems, computer vision, and computational geometry.

import numpy as np
import networkx as nx
from scipy.spatial import Voronoi,voronoi_plot_2d
import matplotlib.pyplot as plt

#we create a function to generate a planar graph from a voronoi diagram

def voronoi_to_networkx(points):
# we get the voronoi diagram
  vor = Voronoi(points)

  G = nx.Graph()

# Add an edge for each ridge in the Voronoi diagram that connects two points in the range [0,1] 
  for simplex in vor.ridge_vertices:
      if -1 not in simplex:
          i, j = simplex
          p = vor.vertices[i]
          q = vor.vertices[j]
          if 0 <= p[0] <= 1 and 0 <= p[1] <= 1 and 0 <= q[0] <= 1 and 0 <= q[1] <= 1:
              distance = np.linalg.norm(p - q) # Calculate the Euclidean distance between p and q
              G.add_edge(tuple(p), tuple(q),weight=distance)

  return G

#We create 20 points from which the voronoi diagram will be generated
points=np.random.rand(20,2)

#we plot the diagram
vor=Voronoi(points)
voronoi_plot_2d(vor)
plt.show()

#We convert diagram to networkx
graph=voronoi_to_networkx(points)
#We assign each node to its actual position in the plane
pos = dict(zip(graph.nodes(), graph.nodes()))
nx.draw(graph, pos,node_size=5)
#We check that the graph is planar.
print(nx.is_planar(graph))
plt.show()
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.