terror
terror

Reputation: 83

How to graph nodes on a grid in networkx

Just started learning Network Science and I'm a novice in Python so I've been having a hard time figuring this out even after reading a good bit of the networkx documentation. I need to compare the distance between all the nodes and generate an edge in the event the distance is less than d.

1) How to I compare node 1 to nodes (2...99) and then compare node 2 to nodes (3...99), etc. If there's a better way to do it than O(n^2) please show me.

2) How can I use the x,y coordinates stored in node_loc{} to graph each node to a coordinate plane?

import random, math
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
import pylab

# Calc distance given (x1,x2,y1,y2)
def distance(x1,x2,y1,y2):
    return math.sqrt(((x2-x1)**2)+((y2-y1)**2))

# Generate coordinate value
def coord_val():
    # node needs x and y coordinates (floats) from 0->100
    return random.uniform(0.0,100.0)

def main():
    # The distance that applies to link generation
    d = 20

    # Make a graph and name it
    g = nx.Graph(name = "100x100 Field Random Network")

    # Generate 100 nodes
    for num in range(0,100):

        # generate a dict with the node's location
        node_loc = {'x': coord_val(), 'y': coord_val()}

        # Add node with x,y dict
        g.add_node(num,node_loc)

    # Check node n against node n+1
    for n,d in g.nodes(data=True):
        if n == 99:
            break

        # I don't think this loop is correct
        for rl in g.nodes(data=True):
            # Don't go out of bounds on the loop
            if n == 99:
                break

            # grab coordinates from nodes
            y1=g.node[n]['y']
            x1=g.node[n]['x']
            y2=g.node[n+1]['y']
            x2=g.node[n+1]['x']

            # Check the distance, if < d, generate edge
            if distance(x1,x2,y1,y2) < d:
                # add edge
                g.add_edge(n,n+1)

    # plot
    # draw_random draws it on a plane, but randomly :(
    nx.draw_random(g,node_size=50)

    plt.show()

if __name__ == '__main__':
    main()

Upvotes: 8

Views: 13488

Answers (2)

yatu
yatu

Reputation: 88285

NetworkX has nx.grid_2d_graph, a Graph generator, that returns the 2d grid graph of mxn nodes, each being connected to its nearest neighbors. By default its labels will be the coordinates of the grid.

If we were interested in having the nodes as coordinates, we could rotate the positions so the origin is at the top left corner. Otherwise you can just leave it as is. Here's an example:

import networkx as nx
from matplotlib import pyplot as plt

G = nx.grid_2d_graph(3,3)

plt.figure(figsize=(6,6))
pos = {(x,y):(y,-x) for x,y in G.nodes()}
nx.draw(G, pos=pos, 
        node_color='lightgreen', 
        with_labels=True,
        node_size=600)

                            enter image description here

Note that nx.grid_2d_graph will generate grid graphs with up to an arbitrarily large m and n, an by positioning the labels you can also plot the grid of coordinates just as above:

                    enter image description here

Upvotes: 13

Aric
Aric

Reputation: 25299

Take a look at the networkx random geometric graph generator for an implementation of a graph like you are looking for https://github.com/networkx/networkx/blob/master/networkx/generators/geometric.py#L79 There is an example that shows the output and how to draw it here https://networkx.github.io/documentation/stable/tutorial.html#drawing-graphs

Upvotes: 1

Related Questions