structure = np.zeros((3,3)) structure[1,2] = 1 g.add_grid_edges(nodeids, structure=structure, weights=weights, symmetric=False) # Edges pointing up structure = np.zeros((3,3)) structure[0,1] = 1 g.add_grid_edges(nodeids, structure=structure, weights=weights+100, symmetric=False) # Edges pointing down structure = np.zeros((3,3)) structure[2,1] = 1 g.add_grid_edges(nodeids, structure=structure, weights=weights+200, symmetric=False) # Source node connected to leftmost non-terminal nodes. left = nodeids[:, 0] g.add_grid_tedges(left, np.inf, 0) # Sink node connected to rightmost non-terminal nodes. right = nodeids[:, -1] g.add_grid_tedges(right, 0, np.inf) return nodeids, g if __name__ == '__main__': nodeids, g = create_graph() plot_graph_2d(g, nodeids.shape) g.maxflow() print(g.get_grid_segments(nodeids))
import numpy as np import maxflow from examples_utils import plot_graph_2d # Standard 4-connected grid g = maxflow.Graph[int]() nodeids = g.add_grid_nodes((5, 5)) g.add_grid_edges(nodeids, 1) # Equivalent to # structure = maxflow.vonNeumann_structure(ndim=2, directed=False) # g.add_grid_edges(nodeids, 1, # structure=structure, # symmetric=False) plot_graph_2d(g, nodeids.shape, plot_terminals=False) # 8-connected grid, ignore two nodes g = maxflow.Graph[int]() nodeids = g.add_grid_nodes((5, 5)) structure = np.array([[0, 0, 0], [0, 0, 1], [1, 1, 1]]) # Also structure = maxflow.moore_structure(ndim=2, directed=True) # Nodeid -1 is ignored when adding edges nodeids[1, 1] = -1 nodeids[3, 2] = -1 g.add_grid_edges(nodeids, 1, structure=structure, symmetric=True) plot_graph_2d(g, nodeids.shape, plot_terminals=False) # 24-connected 5x5 neighborhood g = maxflow.Graph[int]() nodeids = g.add_grid_nodes((5, 5))
structure[1, 2] = 1 g.add_grid_edges(nodeids, structure=structure, weights=weights, symmetric=False) # Edges pointing up structure = np.zeros((3, 3)) structure[0, 1] = 1 g.add_grid_edges(nodeids, structure=structure, weights=weights+100, symmetric=False) # Edges pointing down structure = np.zeros((3, 3)) structure[2, 1] = 1 g.add_grid_edges(nodeids, structure=structure, weights=weights+200, symmetric=False) # Source node connected to leftmost non-terminal nodes. left = nodeids[:, 0] g.add_grid_tedges(left, np.inf, 0) # Sink node connected to rightmost non-terminal nodes. right = nodeids[:, -1] g.add_grid_tedges(right, 0, np.inf) return nodeids, g if __name__ == '__main__': nodeids, g = create_graph() plot_graph_2d(g, nodeids.shape) g.maxflow() print(g.get_grid_segments(nodeids))