# 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:

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.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

For working with graphs, I recommend the `networkx`

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

0

source

All Articles