Search code examples
pythonnetworkx

How to connect nodes in a networkx graph?


Left is input, right is desired output: enter image description here

Input: I am given some n. I generate n points uniformly at random from [0, 1]. So, the points are tuples (x, y).

I then add this list of nodes into NetworkX graph object. Now, I'd like to connect the edges as shown in the right. That is, the graph is connected (you can get from anywhere to anywhere using some number of edges) but not necessarily an Erdos Renyi graph.

I'm not sure what the term is for this kind of graph - no overlapping edges graph? but is it possible to generate edges for such a graph using Networkx?


Solution

  • Networks derived from points in Euclidean space are typically called geometric graphs. Graphs with no overlapping edges are called planar graphs. As you have drawn all your edges as straight lines, I assume that you are particularly interested in planar, straight-line graphs (PSLGs).

    There are several generators for geometric graphs in networkx, however, I am unsure if any of them would necessarily honor the planarity constraint (it feels that you could coerce the geographical_treshold_graph to do that if you picked the threshold parameter in an intelligent way but I don't have a solution off the top of my head).

    Personally, I would start with my random points, and then get the edges by computing a Delaunay triangulation, implemented in scipy.spatial. I would then subsample the edges (depending on the task) and create my graph objects in networkx/igraph/graph-tool.