igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] grid graphs


From: Tamás Nepusz
Subject: Re: [igraph] grid graphs
Date: Mon, 11 Nov 2013 22:36:14 +0100

Hi,

Suppose that you have an N x M grid (N rows, M columns). In this case, you can simply convert from an (x,y) index pair to an integer node ID with the following function:

def grid_coords_to_index(x, y):
    return (y-1)*M + (x-1)

and vice versa:

def index_to_grid_coords(index):
    y, x = divmod(index, M)
    return x+1, y+1

Note that I’m assuming 1-based grid coords since your example seems to imply that. Then you can use the first function above to convert your edge list given in grid coords to an edge list given in vertex indices.

-- 
T.

On Monday, 11 November 2013 at 22:28, Raphael C wrote:

Hi,

I have a graph laid out on a 2d graph. Some edges are included in the
graph and some not. The edges are represented as lists of
((x1,y1),(x2,y2)) as in the following example of a complete graph
description,

edges = [((1, 1), (2, 1)), ((2, 1), (3, 1)), ((3, 1), (4, 1)),
((3, 2), (4, 2)), ((3, 5), (4, 5)), ((4, 1), (5, 1)), ((4, 2), (5,
2)), ((4, 5), (5, 5)), ((1, 1), (1, 2)), ((3, 1), (3, 2)), ((4, 1),
(4, 2)), ((5, 1), (5, 2)), ((5, 2), (5, 3)), ((5, 3), (5, 4)), ((5,
4), (5, 5))]

What is a simple way to make an undirected igraph graph from this
list? I need to be able to have the nodes labelled by their `(x,y)`
positions.

I would like to be able to plot this graph on a grid too so ideally it
would maintain the positions of the nodes.

I am working in python.

Thanks for any help.
Raphael

_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help


reply via email to

[Prev in Thread] Current Thread [Next in Thread]