pmneila / PyMaxflow

Python library for creating flow networks and computing the maxflow/mincut (aka graph-cuts for Python)
http://pmneila.github.io/PyMaxflow/
246 stars 59 forks source link

3D graph grid construction #10

Closed Rowandish closed 10 years ago

Rowandish commented 10 years ago

Hi Pmneila, sorry for this further question, I'd like to build a 3D graph like the the following one: screen shot 2014-10-19 at 12 11 32 copia

Is it possible using your library?

pmneila commented 10 years ago

Hi!

It should be possible.

Note that every node is connected to the nodes (N)orth, (E)ast, (S)outh and (W)est in its own layer, and to the nodes N, E, S, W and (C)enter in the upper and lower layers. You just have to encode that neighborhood structure in the structure array. Assuming all edges have the same weight, you could get the graph with:

import maxflow

g = maxflow.Graph[float]()
nodeids = g.add_grid_nodes((2, 2, 3))
structure = np.array([ [[0, 1, 0],
                        [1, 1, 1],
                        [0, 1, 0]],
                       [[0, 1, 0],
                        [1, 0, 1],
                        [0, 1, 0]],
                       [[0, 1, 0],
                        [1, 1, 1],
                        [0, 1, 0]]])
g.add_grid_edges(nodeids, structure=structure)

You can then plot the graph:

import networkx as nx
nxg = g.get_nx_graph()
nx.draw(nxg)

and you should get: screen shot 2014-10-23 at 02 51 48 (terminal nodes omitted)

Is that what you want?

pmneila commented 10 years ago

Hi.

Did my answer solve your problem? Do you need more details?

Rowandish commented 10 years ago

Hi! Sorry, I forgot to answer you, it solved my problem. Thank you!