graph.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* graph.cpp */
  2. /*
  3. Copyright 2001 Vladimir Kolmogorov (vnk@cs.cornell.edu), Yuri Boykov (yuri@csd.uwo.ca).
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <stdio.h>
  17. #include <iostream>
  18. #include "graph.h"
  19. using namespace OBJREC;
  20. Graph::Graph(void (*err_function)(char *))
  21. {
  22. error_function = err_function;
  23. node_block_first = NULL;
  24. arc_for_block_first = NULL;
  25. arc_rev_block_first = NULL;
  26. flow = 0;
  27. }
  28. Graph::~Graph()
  29. {
  30. while (node_block_first)
  31. {
  32. node_block *next = node_block_first -> next;
  33. delete node_block_first;
  34. node_block_first = next;
  35. }
  36. while (arc_for_block_first)
  37. {
  38. arc_for_block *next = arc_for_block_first -> next;
  39. delete [] arc_for_block_first -> start;
  40. arc_for_block_first = next;
  41. }
  42. while (arc_rev_block_first)
  43. {
  44. arc_rev_block *next = arc_rev_block_first -> next;
  45. delete [] arc_rev_block_first -> start;
  46. arc_rev_block_first = next;
  47. }
  48. }
  49. Graph::node_id Graph::add_node()
  50. {
  51. node *i;
  52. if (!node_block_first || node_block_first->current + 1 > &node_block_first->nodes[NODE_BLOCK_SIZE-1])
  53. {
  54. node_block *next = node_block_first;
  55. node_block_first = (node_block *) new node_block;
  56. if (!node_block_first) {
  57. if (error_function) (*error_function)("Not enough memory!");
  58. exit(1);
  59. }
  60. node_block_first -> current = & ( node_block_first -> nodes[0] );
  61. node_block_first -> next = next;
  62. }
  63. i = node_block_first -> current ++;
  64. i -> first_out = (arc_forward *) 0;
  65. i -> first_in = (arc_reverse *) 0;
  66. i -> tr_cap = 0;
  67. return (node_id) i;
  68. }
  69. void Graph::add_edge(node_id from, node_id to, captype cap, captype rev_cap)
  70. {
  71. arc_forward *a_for;
  72. arc_reverse *a_rev;
  73. if (!arc_for_block_first || arc_for_block_first->current + 1 > &arc_for_block_first->arcs_for[ARC_BLOCK_SIZE])
  74. {
  75. arc_for_block *next = arc_for_block_first;
  76. char *ptr = new char[sizeof(arc_for_block)+1];
  77. if (!ptr) {
  78. if (error_function) (*error_function)("Not enough memory!");
  79. exit(1);
  80. }
  81. if ((PTR_CAST)ptr & 1) arc_for_block_first = (arc_for_block *) (ptr + 1);
  82. else arc_for_block_first = (arc_for_block *) ptr;
  83. arc_for_block_first -> start = ptr;
  84. arc_for_block_first -> current = & ( arc_for_block_first -> arcs_for[0] );
  85. arc_for_block_first -> next = next;
  86. }
  87. if (!arc_rev_block_first || arc_rev_block_first->current + 1 > &arc_rev_block_first->arcs_rev[ARC_BLOCK_SIZE])
  88. {
  89. arc_rev_block *next = arc_rev_block_first;
  90. char *ptr = new char[sizeof(arc_rev_block)+1];
  91. if (!ptr) {
  92. if (error_function) (*error_function)("Not enough memory!");
  93. exit(1);
  94. }
  95. if ((PTR_CAST)ptr & 1) arc_rev_block_first = (arc_rev_block *) (ptr + 1);
  96. else arc_rev_block_first = (arc_rev_block *) ptr;
  97. arc_rev_block_first -> start = ptr;
  98. arc_rev_block_first -> current = & ( arc_rev_block_first -> arcs_rev[0] );
  99. arc_rev_block_first -> next = next;
  100. }
  101. a_for = arc_for_block_first -> current ++;
  102. a_rev = arc_rev_block_first -> current ++;
  103. a_rev -> sister = (arc_forward *) from;
  104. a_for -> shift = (PTR_CAST) to;
  105. a_for -> r_cap = cap;
  106. a_for -> r_rev_cap = rev_cap;
  107. ((node *)from) -> first_out =
  108. (arc_forward *) ((PTR_CAST)(((node *)from) -> first_out) + 1);
  109. ((node *)to) -> first_in =
  110. (arc_reverse *) ((PTR_CAST)(((node *)to) -> first_in) + 1);
  111. }
  112. void Graph::set_tweights(node_id i, captype cap_source, captype cap_sink)
  113. {
  114. flow += (cap_source < cap_sink) ? cap_source : cap_sink;
  115. ((node*)i) -> tr_cap = cap_source - cap_sink;
  116. }
  117. void Graph::add_tweights(node_id i, captype cap_source, captype cap_sink)
  118. {
  119. register captype delta = ((node*)i) -> tr_cap;
  120. if (delta > 0) cap_source += delta;
  121. else cap_sink -= delta;
  122. flow += (cap_source < cap_sink) ? cap_source : cap_sink;
  123. ((node*)i) -> tr_cap = cap_source - cap_sink;
  124. }
  125. /*
  126. Converts arcs added by 'add_edge()' calls
  127. to a forward star graph representation.
  128. Linear time algorithm.
  129. No or little additional memory is allocated
  130. during this process
  131. (it may be necessary to allocate additional
  132. arc blocks, since arcs corresponding to the
  133. same node must be contiguous, i.e. be in one
  134. arc block.)
  135. */
  136. void Graph::prepare_graph()
  137. {
  138. node *i;
  139. arc_for_block *ab_for, *ab_for_first;
  140. arc_rev_block *ab_rev, *ab_rev_first, *ab_rev_scan;
  141. arc_forward *a_for;
  142. arc_reverse *a_rev, *a_rev_scan, a_rev_tmp;
  143. node_block *nb;
  144. bool for_flag = false, rev_flag = false;
  145. PTR_CAST k;
  146. if (!arc_rev_block_first)
  147. {
  148. node_id from = add_node(), to = add_node();
  149. add_edge(from, to, 1, 0);
  150. }
  151. /* FIRST STAGE */
  152. a_rev_tmp.sister = NULL;
  153. for (a_rev = arc_rev_block_first->current; a_rev < &arc_rev_block_first->arcs_rev[ARC_BLOCK_SIZE]; a_rev++)
  154. {
  155. a_rev -> sister = NULL;
  156. }
  157. ab_for = ab_for_first = arc_for_block_first;
  158. ab_rev = ab_rev_first = ab_rev_scan = arc_rev_block_first;
  159. a_for = &ab_for->arcs_for[0];
  160. a_rev = a_rev_scan = &ab_rev->arcs_rev[0];
  161. for (nb = node_block_first; nb; nb = nb->next)
  162. {
  163. for (i = &nb->nodes[0]; i < nb->current; i++)
  164. {
  165. /* outgoing arcs */
  166. k = (PTR_CAST) i -> first_out;
  167. if (a_for + k > &ab_for->arcs_for[ARC_BLOCK_SIZE])
  168. {
  169. if (k > ARC_BLOCK_SIZE) {
  170. if (error_function) (*error_function)("# of arcs per node exceeds block size!");
  171. exit(1);
  172. }
  173. if (for_flag) ab_for = NULL;
  174. else {
  175. ab_for = ab_for -> next;
  176. ab_rev_scan = ab_rev_scan -> next;
  177. }
  178. if (ab_for == NULL)
  179. {
  180. arc_for_block *next = arc_for_block_first;
  181. char *ptr = new char[sizeof(arc_for_block)+1];
  182. if (!ptr) {
  183. if (error_function) (*error_function)("Not enough memory!");
  184. exit(1);
  185. }
  186. if ((PTR_CAST)ptr & 1) arc_for_block_first = (arc_for_block *) (ptr + 1);
  187. else arc_for_block_first = (arc_for_block *) ptr;
  188. arc_for_block_first -> start = ptr;
  189. arc_for_block_first -> current = & ( arc_for_block_first -> arcs_for[0] );
  190. arc_for_block_first -> next = next;
  191. ab_for = arc_for_block_first;
  192. for_flag = true;
  193. }
  194. else a_rev_scan = &ab_rev_scan->arcs_rev[0];
  195. a_for = &ab_for->arcs_for[0];
  196. }
  197. if (ab_rev_scan)
  198. {
  199. a_rev_scan += k;
  200. i -> parent = (arc_forward *) a_rev_scan;
  201. }
  202. else i -> parent = (arc_forward *) & a_rev_tmp;
  203. a_for += k;
  204. i -> first_out = a_for;
  205. ab_for -> last_node = i;
  206. /* incoming arcs */
  207. k = (PTR_CAST) i -> first_in;
  208. if (a_rev + k > &ab_rev->arcs_rev[ARC_BLOCK_SIZE])
  209. {
  210. if (k > ARC_BLOCK_SIZE) {
  211. if (error_function) (*error_function)("# of arcs per node exceeds block size!");
  212. exit(1);
  213. }
  214. if (rev_flag) ab_rev = NULL;
  215. else ab_rev = ab_rev -> next;
  216. if (ab_rev == NULL)
  217. {
  218. arc_rev_block *next = arc_rev_block_first;
  219. char *ptr = new char[sizeof(arc_rev_block)+1];
  220. if (!ptr) {
  221. if (error_function) (*error_function)("Not enough memory!");
  222. exit(1);
  223. }
  224. if ((PTR_CAST)ptr & 1) arc_rev_block_first = (arc_rev_block *) (ptr + 1);
  225. else arc_rev_block_first = (arc_rev_block *) ptr;
  226. arc_rev_block_first -> start = ptr;
  227. arc_rev_block_first -> current = & ( arc_rev_block_first -> arcs_rev[0] );
  228. arc_rev_block_first -> next = next;
  229. ab_rev = arc_rev_block_first;
  230. rev_flag = true;
  231. }
  232. a_rev = &ab_rev->arcs_rev[0];
  233. }
  234. a_rev += k;
  235. i -> first_in = a_rev;
  236. ab_rev -> last_node = i;
  237. }
  238. /* i is the last node in block */
  239. i -> first_out = a_for;
  240. i -> first_in = a_rev;
  241. }
  242. /* SECOND STAGE */
  243. for (ab_for = arc_for_block_first; ab_for; ab_for = ab_for->next)
  244. {
  245. ab_for -> current = ab_for -> last_node -> first_out;
  246. }
  247. for ( ab_for = ab_for_first, ab_rev = ab_rev_first;
  248. ab_for;
  249. ab_for = ab_for->next, ab_rev = ab_rev->next )
  250. for ( a_for = &ab_for->arcs_for[0], a_rev = &ab_rev->arcs_rev[0];
  251. a_for < &ab_for->arcs_for[ARC_BLOCK_SIZE];
  252. a_for++, a_rev++ )
  253. {
  254. arc_forward *af;
  255. arc_reverse *ar;
  256. node *from;
  257. PTR_CAST shift = 0, shift_new;
  258. captype r_cap, r_rev_cap, r_cap_new, r_rev_cap_new;
  259. if (!(from = (node *)(a_rev->sister))) continue;
  260. af = a_for;
  261. ar = a_rev;
  262. do
  263. {
  264. ar -> sister = NULL;
  265. shift_new = ((char *)(af->shift)) - (char *)from;
  266. r_cap_new = af -> r_cap;
  267. r_rev_cap_new = af -> r_rev_cap;
  268. if (shift)
  269. {
  270. af -> shift = shift;
  271. af -> r_cap = r_cap;
  272. af -> r_rev_cap = r_rev_cap;
  273. }
  274. shift = shift_new;
  275. r_cap = r_cap_new;
  276. r_rev_cap = r_rev_cap_new;
  277. af = -- from -> first_out;
  278. if ((arc_reverse *)(from->parent) != &a_rev_tmp)
  279. {
  280. from -> parent = (arc_forward *)(((arc_reverse *)(from -> parent)) - 1);
  281. ar = (arc_reverse *)(from -> parent);
  282. }
  283. } while (from = (node *)(ar->sister));
  284. af -> shift = shift;
  285. af -> r_cap = r_cap;
  286. af -> r_rev_cap = r_rev_cap;
  287. }
  288. for (ab_for = arc_for_block_first; ab_for; ab_for = ab_for->next)
  289. {
  290. i = ab_for -> last_node;
  291. a_for = i -> first_out;
  292. ab_for -> current -> shift = a_for -> shift;
  293. ab_for -> current -> r_cap = a_for -> r_cap;
  294. ab_for -> current -> r_rev_cap = a_for -> r_rev_cap;
  295. a_for -> shift = (PTR_CAST) (ab_for -> current + 1);
  296. i -> first_out = (arc_forward *) (((char *)a_for) - 1);
  297. }
  298. /* THIRD STAGE */
  299. for (ab_rev = arc_rev_block_first; ab_rev; ab_rev = ab_rev->next)
  300. {
  301. ab_rev -> current = ab_rev -> last_node -> first_in;
  302. }
  303. for (nb = node_block_first; nb; nb = nb->next)
  304. for (i = &nb->nodes[0]; i < nb->current; i++)
  305. {
  306. arc_forward *a_for_first, *a_for_last;
  307. a_for_first = i -> first_out;
  308. if (IS_ODD(a_for_first))
  309. {
  310. a_for_first = (arc_forward *) (((char *)a_for_first) + 1);
  311. a_for_last = (arc_forward *) ((a_for_first ++) -> shift);
  312. }
  313. else a_for_last = (i + 1) -> first_out;
  314. for (a_for = a_for_first; a_for < a_for_last; a_for++)
  315. {
  316. node *to = NEIGHBOR_NODE(i, a_for -> shift);
  317. a_rev = -- to -> first_in;
  318. a_rev -> sister = a_for;
  319. }
  320. }
  321. for (ab_rev = arc_rev_block_first; ab_rev; ab_rev = ab_rev->next)
  322. {
  323. i = ab_rev -> last_node;
  324. a_rev = i -> first_in;
  325. ab_rev -> current -> sister = a_rev -> sister;
  326. a_rev -> sister = (arc_forward *) (ab_rev -> current + 1);
  327. i -> first_in = (arc_reverse *) (((char *)a_rev) - 1);
  328. }
  329. }