RTS2D
|
00001 /* 00002 Esse arquivo foi escrito pelo autor descrito abaixo. 00003 O arquivo possui algumas modificacoes do nome da classe Graph para MaxflowGraph apenas para compatibilidade. 00004 */ 00005 /* 00006 This software library implements the maxflow algorithm 00007 described in 00008 00009 An Experimental Comparison of Min-Cut/Max-Flow Algorithms 00010 for Energy Minimization in Vision. 00011 Yuri Boykov and Vladimir Kolmogorov. 00012 In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI), 00013 September 2004 00014 00015 This algorithm was developed by Yuri Boykov and Vladimir Kolmogorov 00016 at Siemens Corporate Research. To make it available for public use, 00017 it was later reimplemented by Vladimir Kolmogorov based on open publications. 00018 00019 If you use this software for research purposes, you should cite 00020 the aforementioned paper in any resulting publication. 00021 */ 00022 00023 /* 00024 Copyright 2001 Vladimir Kolmogorov (vnk@cs.cornell.edu), Yuri Boykov (yuri@csd.uwo.ca). 00025 00026 This program is free software; you can redistribute it and/or modify 00027 it under the terms of the GNU General Public License as published by 00028 the Free Software Foundation; either version 2 of the License, or 00029 (at your option) any later version. 00030 00031 This program is distributed in the hope that it will be useful, 00032 but WITHOUT ANY WARRANTY; without even the implied warranty of 00033 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00034 GNU General Public License for more details. 00035 00036 You should have received a copy of the GNU General Public License 00037 along with this program; if not, write to the Free Software 00038 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00039 */ 00040 00041 00042 /* 00043 For description, example usage, discussion of graph representation 00044 and memory usage see README.TXT. 00045 */ 00046 00047 #ifndef __GRAPH_H__ 00048 #define __GRAPH_H__ 00049 00050 #include "block.h" 00051 #include <limits.h> 00052 00053 /* 00054 Nodes, arcs and pointers to nodes are 00055 added in blocks for memory and time efficiency. 00056 Below are numbers of items in blocks 00057 */ 00058 #define NODE_BLOCK_SIZE 512 00059 #define ARC_BLOCK_SIZE 1024 00060 #define NODEPTR_BLOCK_SIZE 128 00061 00062 00063 class MaxflowGraph 00064 { 00065 public: 00066 typedef enum 00067 { 00068 SOURCE = 0, 00069 SINK = 1 00070 } termtype; /* terminals */ 00071 00072 /* Type of edge weights. 00073 Can be changed to char, int, float, double, ... */ 00074 typedef int captype; 00075 static const long CAP_MAX=INT_MAX; //=SHRT_MAX; 00076 /* Type of total flow */ 00077 typedef int flowtype; 00078 00079 typedef void * node_id; 00080 00081 /* interface functions */ 00082 00083 /* Constructor. Optional argument is the pointer to the 00084 function which will be called if an error occurs; 00085 an error message is passed to this function. If this 00086 argument is omitted, exit(1) will be called. */ 00087 MaxflowGraph(void (*err_function)(char *) = NULL); 00088 00089 /* Destructor */ 00090 ~MaxflowGraph(); 00091 00092 /* Adds a node to the graph */ 00093 node_id add_node(); 00094 00095 /* Adds a bidirectional edge between 'from' and 'to' 00096 with the weights 'cap' and 'rev_cap' */ 00097 void add_edge(node_id from, node_id to, captype cap, captype rev_cap); 00098 00099 /* Sets the weights of the edges 'SOURCE->i' and 'i->SINK' 00100 Can be called at most once for each node before any call to 'add_tweights'. 00101 Weights can be negative */ 00102 void set_tweights(node_id i, captype cap_source, captype cap_sink); 00103 00104 /* Adds new edges 'SOURCE->i' and 'i->SINK' with corresponding weights 00105 Can be called multiple times for each node. 00106 Weights can be negative */ 00107 void add_tweights(node_id i, captype cap_source, captype cap_sink); 00108 00109 /* After the maxflow is computed, this function returns to which 00110 segment the node 'i' belongs (MaxflowGraph::SOURCE or MaxflowGraph::SINK) */ 00111 termtype what_segment(node_id i); 00112 00113 /* Computes the maxflow. Can be called only once. */ 00114 flowtype maxflow(); 00115 00116 /***********************************************************************/ 00117 /***********************************************************************/ 00118 /***********************************************************************/ 00119 00120 private: 00121 /* internal variables and functions */ 00122 00123 struct arc_st; 00124 00125 /* node structure */ 00126 typedef struct node_st 00127 { 00128 arc_st *first; /* first outcoming arc */ 00129 00130 arc_st *parent; /* node's parent */ 00131 node_st *next; /* pointer to the next active node 00132 (or to itself if it is the last node in the list) */ 00133 int TS; /* timestamp showing when DIST was computed */ 00134 int DIST; /* distance to the terminal */ 00135 short is_sink; /* flag showing whether the node is in the source or in the sink tree */ 00136 00137 captype tr_cap; /* if tr_cap > 0 then tr_cap is residual capacity of the arc SOURCE->node 00138 otherwise -tr_cap is residual capacity of the arc node->SINK */ 00139 } node; 00140 00141 /* arc structure */ 00142 typedef struct arc_st 00143 { 00144 node_st *head; /* node the arc points to */ 00145 arc_st *next; /* next arc with the same originating node */ 00146 arc_st *sister; /* reverse arc */ 00147 00148 captype r_cap; /* residual capacity */ 00149 } arc; 00150 00151 /* 'pointer to node' structure */ 00152 typedef struct nodeptr_st 00153 { 00154 node_st *ptr; 00155 nodeptr_st *next; 00156 } nodeptr; 00157 00158 Block<node> *node_block; 00159 Block<arc> *arc_block; 00160 DBlock<nodeptr> *nodeptr_block; 00161 00162 void (*error_function)(char *); /* this function is called if a error occurs, 00163 with a corresponding error message 00164 (or exit(1) is called if it's NULL) */ 00165 00166 flowtype flow; /* total flow */ 00167 00168 /***********************************************************************/ 00169 00170 node *queue_first[2], *queue_last[2]; /* list of active nodes */ 00171 nodeptr *orphan_first, *orphan_last; /* list of pointers to orphans */ 00172 int TIME; /* monotonically increasing global counter */ 00173 00174 /***********************************************************************/ 00175 00176 /* functions for processing active list */ 00177 void set_active(node *i); 00178 node *next_active(); 00179 00180 void maxflow_init(); 00181 void augment(arc *middle_arc); 00182 void process_source_orphan(node *i); 00183 void process_sink_orphan(node *i); 00184 }; 00185 00186 #endif