graph.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* graph.h */
  2. /*
  3. This software library implements the maxflow algorithm
  4. described in
  5. An Experimental Comparison of Min-Cut/Max-Flow Algorithms
  6. for Energy Minimization in Vision.
  7. Yuri Boykov and Vladimir Kolmogorov.
  8. In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
  9. September 2004
  10. This algorithm was developed by Yuri Boykov and Vladimir Kolmogorov
  11. at Siemens Corporate Research. To make it available for public use,
  12. it was later reimplemented by Vladimir Kolmogorov based on open publications.
  13. If you use this software for research purposes, you should cite
  14. the aforementioned paper in any resulting publication.
  15. */
  16. /*
  17. Copyright 2001 Vladimir Kolmogorov (vnk@cs.cornell.edu), Yuri Boykov (yuri@csd.uwo.ca).
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2 of the License, or
  21. (at your option) any later version.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. /*
  31. For description, example usage, discussion of graph representation
  32. and memory usage see README.TXT.
  33. */
  34. #ifndef __GRAPH_H__
  35. #define __GRAPH_H__
  36. #ifndef USE_64_BIT_PTR_CAST
  37. #define PTR_CAST int
  38. #else
  39. #define PTR_CAST long int
  40. #endif
  41. #include "block.h"
  42. /*
  43. Nodes, arcs and pointers to nodes are
  44. added in blocks for memory and time efficiency.
  45. Below are numbers of items in blocks
  46. */
  47. #define NODE_BLOCK_SIZE 512
  48. #define ARC_BLOCK_SIZE 1024
  49. #define NODEPTR_BLOCK_SIZE 128
  50. namespace OBJREC {
  51. class Graph
  52. {
  53. public:
  54. typedef enum
  55. {
  56. SOURCE = 0,
  57. SINK = 1
  58. } termtype; /* terminals */
  59. /* Type of edge weights.
  60. Can be changed to char, int, float, double, ... */
  61. typedef double captype;
  62. /* Type of total flow */
  63. typedef int flowtype;
  64. typedef void * node_id;
  65. /* interface functions */
  66. /* Constructor. Optional argument is the pointer to the
  67. function which will be called if an error occurs;
  68. an error message is passed to this function. If this
  69. argument is omitted, exit(1) will be called. */
  70. Graph(void (*err_function)(char *) = NULL);
  71. /* Destructor */
  72. ~Graph();
  73. /* Adds a node to the graph */
  74. node_id add_node();
  75. /* Adds a bidirectional edge between 'from' and 'to'
  76. with the weights 'cap' and 'rev_cap' */
  77. void add_edge(node_id from, node_id to, captype cap, captype rev_cap);
  78. /* Sets the weights of the edges 'SOURCE->i' and 'i->SINK'
  79. Can be called at most once for each node before any call to 'add_tweights'.
  80. Weights can be negative */
  81. void set_tweights(node_id i, captype cap_source, captype cap_sink);
  82. /* Adds new edges 'SOURCE->i' and 'i->SINK' with corresponding weights
  83. Can be called multiple times for each node.
  84. Weights can be negative */
  85. void add_tweights(node_id i, captype cap_source, captype cap_sink);
  86. /* After the maxflow is computed, this function returns to which
  87. segment the node 'i' belongs (Graph::SOURCE or Graph::SINK) */
  88. termtype what_segment(node_id i);
  89. /* Computes the maxflow. Can be called only once. */
  90. flowtype maxflow();
  91. /***********************************************************************/
  92. /***********************************************************************/
  93. /***********************************************************************/
  94. private:
  95. /* internal variables and functions */
  96. struct arc_forward_st;
  97. struct arc_reverse_st;
  98. #define IS_ODD(a) ((PTR_CAST)(a) & 1)
  99. #define MAKE_ODD(a) ((arc_forward *) ((PTR_CAST)(a) | 1))
  100. #define MAKE_EVEN(a) ((arc_forward *) ((PTR_CAST)(a) & (~1)))
  101. #define MAKE_ODD_REV(a) ((arc_reverse *) ((PTR_CAST)(a) | 1))
  102. #define MAKE_EVEN_REV(a) ((arc_reverse *) ((PTR_CAST)(a) & (~1)))
  103. /* node structure */
  104. typedef struct node_st
  105. {
  106. /*
  107. Usually i->first_out is the first outgoing
  108. arc, and (i+1)->first_out-1 is the last outgoing arc.
  109. However, it is not always possible, since
  110. arcs are allocated in blocks, so arcs corresponding
  111. to two consecutive nodes may be in different blocks.
  112. If outgoing arcs for i are last in the arc block,
  113. then a different mechanism is used. i->first_out
  114. is odd in this case; the first outgoing arc
  115. is (a+1), and the last outgoing arc is
  116. ((arc_forward *)(a->shift))-1, where
  117. a = (arc_forward *) (((char *)(i->first_out)) + 1);
  118. Similar mechanism is used for incoming arcs.
  119. */
  120. arc_forward_st *first_out; /* first outcoming arc */
  121. arc_reverse_st *first_in; /* first incoming arc */
  122. arc_forward_st *parent; /* describes node's parent
  123. if IS_ODD(parent) then MAKE_EVEN(parent) points to 'arc_reverse',
  124. otherwise parent points to 'arc_forward' */
  125. node_st *next; /* pointer to the next active node
  126. (or to itself if it is the last node in the list) */
  127. int TS; /* timestamp showing when DIST was computed */
  128. int DIST; /* distance to the terminal */
  129. short is_sink; /* flag showing whether the node is in the source or in the sink tree */
  130. captype tr_cap; /* if tr_cap > 0 then tr_cap is residual capacity of the arc SOURCE->node
  131. otherwise -tr_cap is residual capacity of the arc node->SINK */
  132. } node;
  133. /* arc structures */
  134. #define NEIGHBOR_NODE(i, shift) ((node *) ((char *)(i) + (shift)))
  135. #define NEIGHBOR_NODE_REV(i, shift) ((node *) ((char *)(i) - (shift)))
  136. typedef struct arc_forward_st
  137. {
  138. PTR_CAST shift; /* node_to = NEIGHBOR_NODE(node_from, shift) */
  139. captype r_cap; /* residual capacity */
  140. captype r_rev_cap; /* residual capacity of the reverse arc*/
  141. } arc_forward;
  142. typedef struct arc_reverse_st
  143. {
  144. arc_forward *sister; /* reverse arc */
  145. } arc_reverse;
  146. /* 'pointer to node' structure */
  147. typedef struct nodeptr_st
  148. {
  149. node_st *ptr;
  150. nodeptr_st *next;
  151. } nodeptr;
  152. typedef struct node_block_st
  153. {
  154. node *current;
  155. struct node_block_st *next;
  156. node nodes[NODE_BLOCK_SIZE];
  157. } node_block;
  158. #define last_node LAST_NODE.LAST_NODE
  159. typedef struct arc_for_block_st
  160. {
  161. char *start; /* the actual start address of this block.
  162. May be different from 'this' since 'this'
  163. must be at an even address. */
  164. arc_forward *current;
  165. struct arc_for_block_st *next;
  166. arc_forward arcs_for[ARC_BLOCK_SIZE]; /* all arcs must be at even addresses */
  167. union
  168. {
  169. arc_forward dummy;
  170. node *LAST_NODE; /* used in graph consruction */
  171. } LAST_NODE;
  172. } arc_for_block;
  173. typedef struct arc_rev_block_st
  174. {
  175. char *start; /* the actual start address of this block.
  176. May be different from 'this' since 'this'
  177. must be at an even address. */
  178. arc_reverse *current;
  179. struct arc_rev_block_st *next;
  180. arc_reverse arcs_rev[ARC_BLOCK_SIZE]; /* all arcs must be at even addresses */
  181. union
  182. {
  183. arc_reverse dummy;
  184. node *LAST_NODE; /* used in graph consruction */
  185. } LAST_NODE;
  186. } arc_rev_block;
  187. node_block *node_block_first;
  188. arc_for_block *arc_for_block_first;
  189. arc_rev_block *arc_rev_block_first;
  190. DBlock<nodeptr> *nodeptr_block;
  191. void (*error_function)(char *); /* this function is called if a error occurs,
  192. with a corresponding error message
  193. (or exit(1) is called if it's NULL) */
  194. flowtype flow; /* total flow */
  195. /***********************************************************************/
  196. node *queue_first[2], *queue_last[2]; /* list of active nodes */
  197. nodeptr *orphan_first, *orphan_last; /* list of pointers to orphans */
  198. int TIME; /* monotonically increasing global counter */
  199. /***********************************************************************/
  200. /* functions for processing active list */
  201. void set_active(node *i);
  202. node *next_active();
  203. void prepare_graph();
  204. void maxflow_init();
  205. void augment(node *s_start, node *t_start, captype *cap_middle, captype *rev_cap_middle);
  206. void process_source_orphan(node *i);
  207. void process_sink_orphan(node *i);
  208. };
  209. } //namespace
  210. #endif