3 This software library implements the maxflow algorithm
\r
6 An Experimental Comparison of Min-Cut/Max-Flow Algorithms
\r
7 for Energy Minimization in Vision.
\r
8 Yuri Boykov and Vladimir Kolmogorov.
\r
9 In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
\r
12 This algorithm was developed by Yuri Boykov and Vladimir Kolmogorov
\r
13 at Siemens Corporate Research. To make it available for public use,
\r
14 it was later reimplemented by Vladimir Kolmogorov based on open publications.
\r
16 If you use this software for research purposes, you should cite
\r
17 the aforementioned paper in any resulting publication.
\r
21 Copyright 2001 Vladimir Kolmogorov (vnk@cs.cornell.edu), Yuri Boykov (yuri@csd.uwo.ca).
\r
23 This program is free software; you can redistribute it and/or modify
\r
24 it under the terms of the GNU General Public License as published by
\r
25 the Free Software Foundation; either version 2 of the License, or
\r
26 (at your option) any later version.
\r
28 This program is distributed in the hope that it will be useful,
\r
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
31 GNU General Public License for more details.
\r
33 You should have received a copy of the GNU General Public License
\r
34 along with this program; if not, write to the Free Software
\r
35 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
40 For description, example usage, discussion of graph representation
\r
41 and memory usage see README.TXT.
\r
47 #include <tmwtypes.h>
\r
51 Nodes, arcs and pointers to nodes are
\r
52 added in blocks for memory and time efficiency.
\r
53 Below are numbers of items in blocks
\r
55 #define NODE_BLOCK_SIZE 512
\r
56 #define ARC_BLOCK_SIZE 1024
\r
57 #define NODEPTR_BLOCK_SIZE 128
\r
67 } termtype; /* terminals */
\r
69 typedef void * node_id;
\r
71 /* Type of edge weights.
\r
72 Can be changed to char, int, float, double, ... */
\r
73 typedef float captype; /* bagon changed from int to float */
\r
75 /* Type of total flow */
\r
76 typedef float flowtype; /* bagon changed from int to float */
\r
79 /* interface functions */
\r
81 /* Constructor. Optional argument is the pointer to the
\r
82 function which will be called if an error occurs;
\r
83 an error message is passed to this function. If this
\r
84 argument is omitted, exit(1) will be called. */
\r
85 Graph(void (*err_function)(const char *) = NULL);
\r
90 /* Adds a node to the graph */
\r
93 /* Adds a bidirectional edge between 'from' and 'to'
\r
94 with the weights 'cap' and 'rev_cap' */
\r
95 void add_edge(node_id from, node_id to, captype cap, captype rev_cap);
\r
97 /* Sets the weights of the edges 'SOURCE->i' and 'i->SINK'
\r
98 Can be called at most once for each node before any call to 'add_tweights'.
\r
99 Weights can be negative */
\r
100 void set_tweights(node_id i, captype cap_source, captype cap_sink);
\r
102 /* Adds new edges 'SOURCE->i' and 'i->SINK' with corresponding weights
\r
103 Can be called multiple times for each node.
\r
104 Weights can be negative */
\r
105 void add_tweights(node_id i, captype cap_source, captype cap_sink);
\r
107 /* After the maxflow is computed, this function returns to which
\r
108 segment the node 'i' belongs (Graph::SOURCE or Graph::SINK) */
\r
109 termtype what_segment(node_id i);
\r
111 /* Computes the maxflow. Can be called only once. */
\r
112 flowtype maxflow();
\r
114 /***********************************************************************/
\r
115 /***********************************************************************/
\r
116 /***********************************************************************/
\r
119 /* internal variables and functions */
\r
121 struct arc_forward_st;
\r
122 struct arc_reverse_st;
\r
125 #define POINTER_CAST int64_T
\r
127 #define POINTER_CAST int
\r
130 #define IS_ODD(a) ((POINTER_CAST)(a) & 1)
\r
131 #define MAKE_ODD(a) ((arc_forward *) ((POINTER_CAST)(a) | 1))
\r
132 #define MAKE_EVEN(a) ((arc_forward *) ((POINTER_CAST)(a) & (~1)))
\r
133 #define MAKE_ODD_REV(a) ((arc_reverse *) ((POINTER_CAST)(a) | 1))
\r
134 #define MAKE_EVEN_REV(a) ((arc_reverse *) ((POINTER_CAST)(a) & (~1)))
\r
136 /* node structure */
\r
137 typedef struct node_st
\r
140 Usually i->first_out is the first outgoing
\r
141 arc, and (i+1)->first_out-1 is the last outgoing arc.
\r
142 However, it is not always possible, since
\r
143 arcs are allocated in blocks, so arcs corresponding
\r
144 to two consecutive nodes may be in different blocks.
\r
146 If outgoing arcs for i are last in the arc block,
\r
147 then a different mechanism is used. i->first_out
\r
148 is odd in this case; the first outgoing arc
\r
149 is (a+1), and the last outgoing arc is
\r
150 ((arc_forward *)(a->shift))-1, where
\r
151 a = (arc_forward *) (((char *)(i->first_out)) + 1);
\r
153 Similar mechanism is used for incoming arcs.
\r
155 arc_forward_st *first_out; /* first outcoming arc */
\r
156 arc_reverse_st *first_in; /* first incoming arc */
\r
158 arc_forward_st *parent; /* describes node's parent
\r
159 if IS_ODD(parent) then MAKE_EVEN(parent) points to 'arc_reverse',
\r
160 otherwise parent points to 'arc_forward' */
\r
162 node_st *next; /* pointer to the next active node
\r
163 (or to itself if it is the last node in the list) */
\r
165 int TS; /* timestamp showing when DIST was computed */
\r
166 int DIST; /* distance to the terminal */
\r
167 short is_sink; /* flag showing whether the node is in the source or in the sink tree */
\r
169 captype tr_cap; /* if tr_cap > 0 then tr_cap is residual capacity of the arc SOURCE->node
\r
170 otherwise -tr_cap is residual capacity of the arc node->SINK */
\r
173 /* arc structures */
\r
174 #define NEIGHBOR_NODE(i, shift) ((node *) ((char *)(i) + (shift)))
\r
175 #define NEIGHBOR_NODE_REV(i, shift) ((node *) ((char *)(i) - (shift)))
\r
176 typedef struct arc_forward_st
\r
178 POINTER_CAST shift; /* node_to = NEIGHBOR_NODE(node_from, shift) */
\r
179 captype r_cap; /* residual capacity */
\r
180 captype r_rev_cap; /* residual capacity of the reverse arc*/
\r
183 typedef struct arc_reverse_st
\r
185 arc_forward *sister; /* reverse arc */
\r
188 /* 'pointer to node' structure */
\r
189 typedef struct nodeptr_st
\r
195 typedef struct node_block_st
\r
198 struct node_block_st *next;
\r
199 node nodes[NODE_BLOCK_SIZE];
\r
202 #define last_node LAST_NODE.LAST_NODE
\r
204 typedef struct arc_for_block_st
\r
206 char *start; /* the actual start address of this block.
\r
207 May be different from 'this' since 'this'
\r
208 must be at an even address. */
\r
209 arc_forward *current;
\r
210 struct arc_for_block_st *next;
\r
211 arc_forward arcs_for[ARC_BLOCK_SIZE]; /* all arcs must be at even addresses */
\r
215 node *LAST_NODE; /* used in graph consruction */
\r
219 typedef struct arc_rev_block_st
\r
221 char *start; /* the actual start address of this block.
\r
222 May be different from 'this' since 'this'
\r
223 must be at an even address. */
\r
224 arc_reverse *current;
\r
225 struct arc_rev_block_st *next;
\r
226 arc_reverse arcs_rev[ARC_BLOCK_SIZE]; /* all arcs must be at even addresses */
\r
230 node *LAST_NODE; /* used in graph consruction */
\r
234 node_block *node_block_first;
\r
235 arc_for_block *arc_for_block_first;
\r
236 arc_rev_block *arc_rev_block_first;
\r
237 DBlock<nodeptr> *nodeptr_block;
\r
239 void (*error_function)(const char *); /* this function is called if a error occurs,
\r
240 with a corresponding error message
\r
241 (or exit(1) is called if it's NULL) */
\r
243 flowtype flow; /* total flow */
\r
245 /***********************************************************************/
\r
247 node *queue_first[2], *queue_last[2]; /* list of active nodes */
\r
248 nodeptr *orphan_first, *orphan_last; /* list of pointers to orphans */
\r
249 int TIME; /* monotonically increasing global counter */
\r
251 /***********************************************************************/
\r
253 /* functions for processing active list */
\r
254 void set_active(node *i);
\r
255 node *next_active();
\r
257 void prepare_graph();
\r
258 void maxflow_init();
\r
259 void augment(node *s_start, node *t_start, captype *cap_middle, captype *rev_cap_middle);
\r
260 void process_source_orphan(node *i);
\r
261 void process_sink_orphan(node *i);
\r