Graph with python: how to set up a graph correctly?

I want to use graph reduction algorithm on images in my project, I am using python 2.7 .
I found a pymaxflow implementation , but the documentation doesn't seem that clear. I will give an example, here is my 5 * 5 matrix:

>>> A
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

      

The virtual terminal nodes, S (source) and T (sink), must be associated with infinite weights across all pixels of the left and right columns of the matrix, respectively.This is what I would like to get:

Graph rapresentation to obtain

Here is my code to get this but it doesn't work

left_most = concatenate((np.zeros((1, A.shape[0])), np.arange(A.shape[0]).reshape(1, A.shape[0]))).astype(np.uint64)
left_most = np.ravel_multi_index(left_most, A.shape)
right_most = concatenate((np.ones((1, A.shape[0])) * size(A, 1) - 1, np.arange(A.shape[0]).reshape(1, A.shape[0]))).astype(np.uint64)
right_most = np.ravel_multi_index(right_most, A.shape)
g.add_grid_tedges(left_most, np.ones(left_most.shape) * np.inf, np.zeros(left_most.shape))
g.add_grid_tedges(right_most, np.zeros(right_most.shape), np.ones(right_most.shape) * np.inf)

      

g.maxflow()

makes the python console an endless loop. I'm not sure about my implementation: what is the way to generate the correct graph that can be used in the graph shortening algorithm?

Thank!

Ps If you know a solution with a different library will let me know, any suggestion would be really appreciated.

+3


source to share


1 answer


For working with graphs, I recommend the networkx

. It has many features, including the max flow feature (with example):



http://networkx.github.io/documentation/networkx-1.9.1/reference/generated/networkx.algorithms.flow.maximum_flow_value.html#networkx.algorithms.flow.maximum_flow_value

0


source







All Articles