|
ADS
Algorithms and Data Structures
|
The branch-&-bound algorithms of section 19.3 and of the exercises 19.9 to 19.12. More...
Files | |
| file | ADS/intractable/BranchAndBound.h |
| The branch-&-bound algorithms of section 19.3 and of the exercises 19.9 to 19.12: the enumeration of chapter 16 that skips the subtree of a sequence of choices \( S \) whose lower bound \( \mathit{lb}( S ) \) is not below the cost \( \mathit{minCost} \) of the best solution found so far. | |
Typedefs | |
| template<class T> | |
| using | ADS::IncumbentChanged = mod::Assigned< int , T > |
| the best value found so far by a search changed, the handle 0 | |
Enumerations | |
| enum class | ADS::TspBound { ADS::TspBound::BOOK , ADS::TspBound::ONE_TREE } |
| the lower bound of a circuit that starts with \( S \) More... | |
Functions | |
| template<class T> | |
| T | ADS::spanningTreeWeight (const std::vector< std::vector< T > > &D, const Nodes &V) |
| the weight of a minimum spanning tree of the nodes in \( V \), by the symmetric distances \( D \) | |
| template<class T> | |
| T | ADS::oneTree (const std::vector< std::vector< T > > &D, int r) |
| the weight of a minimum 1-tree of \( r \): a minimum spanning tree of the other nodes and the two least edges of \( r \); each circuit is a 1-tree, so that the weight bounds the optimum from below | |
| template<class T> | |
| T | ADS::tspLowerBound (const std::vector< std::vector< T > > &D, const Nodes &S, TspBound how=TspBound::BOOK) |
| a lower bound \( \mathit{lb}( S ) \) of the circuits that start with the path \( S \), the cost of the circuit if \( S \) has all the cities or if \( n = 2 \), where the circuit through both cities is the only one | |
| template<class T> | |
| std::pair< T, Nodes > | ADS::bbTsp (const std::vector< std::vector< T > > &D, const Nodes &start={}, TspBound how=TspBound::BOOK, long *generated=nullptr) |
| the cost and a minimum circuit from 0, starting from the circuit start if not empty; the number of nodes of the tree of the choices generated, the root included, goes to *generated, if given | |
| template<class T, class O> | |
| std::pair< T, Nodes > | ADS::bbTsp (const std::vector< std::vector< T > > &D, const Nodes &start, TspBound how, long *generated, O &o) |
| bbTsp( D , start , how , generated ), each improvement of the best cost told to \( o \) as IncumbentChanged< T > | |
| template<class T> | |
| std::pair< T, Nodes > | ADS::littleTsp (const std::vector< std::vector< T > > &D, long *generated=nullptr) |
| the cost and a minimum circuit from 0, by the reduction of the matrix; the number of nodes of the tree generated goes to *generated, if given | |
| std::pair< long, Nodes > | ADS::maxWeightClique (const Adjacency &G, const std::vector< long > &w, long *generated=nullptr) |
| the weight and the nodes of a clique of \( G \) of largest weight (MAXIMUM WEIGHT CLIQUE), the weights \( w \) not negative; a set is extended by the candidates adjacent to all of it, and pruned when its weight and that of all the candidates is not above the best; the nodes generated go to *generated, if given | |
| template<class O> | |
| std::pair< long, Nodes > | ADS::maxWeightClique (const Adjacency &G, const std::vector< long > &w, long *generated, O &o) |
| maxWeightClique( G , w , generated ), each improvement of the best weight told to \( o \) as IncumbentChanged< long > | |
| std::pair< long, Nodes > | ADS::knapsackBB (const std::vector< long > &p, const std::vector< long > &v, long C, long *generated=nullptr) |
| the best profit and the objects of KNAPSACK by branch-&-bound, by places; the objects by decreasing profit per volume, each in and then out, pruned when the fractional knapsack of the rest does not beat the best; the nodes generated go to *generated, if given | |
| template<class O> | |
| std::pair< long, Nodes > | ADS::knapsackBB (const std::vector< long > &p, const std::vector< long > &v, long C, long *generated, O &o) |
| knapsackBB( p , v , C , generated ), each improvement of the best profit told to \( o \) as IncumbentChanged< long > | |
| std::pair< long, Nodes > | ADS::knapsackBacktrack (const std::vector< long > &p, const std::vector< long > &v, long C, long *generated=nullptr) |
| KNAPSACK by backtracking with the simple bound: the objects in their order, each in and then out, an object that does not fit skipped, and a node pruned when its profit plus the profits of all the objects still to decide does not beat the best; the nodes generated go to *generated. | |
| std::pair< long, Nodes > | ADS::knapsackGreedy (const std::vector< long > &p, const std::vector< long > &v, long C) |
| the greedy heuristic of KNAPSACK: the objects by decreasing profit per volume, each taken if it still fits; not optimal in general | |
The branch-&-bound algorithms of section 19.3 and of the exercises 19.9 to 19.12.
The definitions, the invariants and the costs of the area are in Theory of intractable problems.
| using ADS::IncumbentChanged = mod::Assigned< int , T > |
the best value found so far by a search changed, the handle 0
|
strong |
| std::pair< T, Nodes > ADS::bbTsp | ( | const std::vector< std::vector< T > > & | D, |
| const Nodes & | start, | ||
| TspBound | how, | ||
| long * | generated, | ||
| O & | o ) |
bbTsp( D , start , how , generated ), each improvement of the best cost told to \( o \) as IncumbentChanged< T >
| std::pair< T, Nodes > ADS::bbTsp | ( | const std::vector< std::vector< T > > & | D, |
| const Nodes & | start = {}, | ||
| TspBound | how = TspBound::BOOK, | ||
| long * | generated = nullptr ) |
the cost and a minimum circuit from 0, starting from the circuit start if not empty; the number of nodes of the tree of the choices generated, the root included, goes to *generated, if given
In pseudocode (section 19.3.1), depth first, a child generated for each city not in \( S \) and visited only if its bound is below the best:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bb}( S )\quad \triangleright\ S \text{ a path from the city } 0 \cr 2 & \quad \textbf{foreach}\ c \notin S \text{, by increasing name} \ \textbf{do} \cr 3 & \qquad \mathit{lb} \gets \mathrm{tspLowerBound}( D , S \cdot ( c ) , \mathit{how} );\ \textbf{if}\ \mathit{minCost} \le \mathit{lb} \ \textbf{then continue}\quad \triangleright\ \text{pruned} \cr 4 & \qquad \textbf{if}\ |S| + 1 = n\ \textbf{then}\ ( \mathit{minSol} , \mathit{minCost} ) \gets ( S \cdot ( c ) , \mathit{lb} )\quad \triangleright\ \text{a circuit: the bound is its cost} \cr 5 & \qquad \textbf{else}\ \mathrm{bb}( S \cdot ( c ) ) \cr 6 & \textbf{function}\ \mathrm{bbTsp}( D , \mathit{start} ) \cr 7 & \quad \mathit{minSol} \gets \mathit{start};\ \mathit{minCost} \gets \text{the cost of } \mathit{start} \text{, or } \infty \text{ if it is empty} \cr 8 & \quad \mathrm{bb}( ( 0 ) );\ \textbf{return}\ ( \mathit{minCost} , \mathit{minSol} ) \end{array}} \]
|
inline |
KNAPSACK by backtracking with the simple bound: the objects in their order, each in and then out, an object that does not fit skipped, and a node pruned when its profit plus the profits of all the objects still to decide does not beat the best; the nodes generated go to *generated.
In pseudocode (exercise 19.12), the procedure of knapsackBB() with the objects in their order and \( \sum_{j \ge i} p_j \) in place of \( \mathit{frac}( i , c ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bt}( S , i , c , \mathit{profit} )\quad \triangleright\ \text{as } \mathrm{bb} \text{, with a weaker bound} \cr 2 & \quad \textbf{if}\ \mathit{best} < \mathit{profit}\ \textbf{then}\ ( \mathit{best} , \mathit{bestSet} ) \gets ( \mathit{profit} , S ) \cr 3 & \quad \textbf{if}\ i = n\ \textbf{or}\ \mathit{profit} + \textstyle\sum_{j \ge i} p_j \le \mathit{best}\ \textbf{then return} \cr 4 & \quad \textbf{if}\ v_i \le c\ \textbf{then}\ \mathrm{bt}( S \cup \{ i \} , i + 1 , c - v_i , \mathit{profit} + p_i ) \cr 5 & \quad \mathrm{bt}( S , i + 1 , c , \mathit{profit} ) \end{array}} \]
| std::pair< long, Nodes > ADS::knapsackBB | ( | const std::vector< long > & | p, |
| const std::vector< long > & | v, | ||
| long | C, | ||
| long * | generated, | ||
| O & | o ) |
knapsackBB( p , v , C , generated ), each improvement of the best profit told to \( o \) as IncumbentChanged< long >
|
inline |
the best profit and the objects of KNAPSACK by branch-&-bound, by places; the objects by decreasing profit per volume, each in and then out, pruned when the fractional knapsack of the rest does not beat the best; the nodes generated go to *generated, if given
In pseudocode (exercise 19.12), the objects renamed by decreasing \( p_i / v_i \), \( \mathit{frac}( i , c ) \) the greedy of the fractional knapsack of chapter 14 on the objects from \( i \) within \( c \), an upper bound of their best profit:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bb}( S , i , c , \mathit{profit} )\quad \triangleright\ \text{the objects } i , \ldots , n - 1 \text{ still to decide} \cr 2 & \quad \textbf{if}\ \mathit{best} < \mathit{profit}\ \textbf{then}\ ( \mathit{best} , \mathit{bestSet} ) \gets ( \mathit{profit} , S ) \cr 3 & \quad \textbf{if}\ i = n\ \textbf{or}\ \mathit{profit} + \mathit{frac}( i , c ) \le \mathit{best}\ \textbf{then return}\quad \triangleright\ \text{the fractional bound} \cr 4 & \quad \textbf{if}\ v_i \le c\ \textbf{then}\ \mathrm{bb}( S \cup \{ i \} , i + 1 , c - v_i , \mathit{profit} + p_i )\quad \triangleright \ \text{with } i \text{ first} \cr 5 & \quad \mathrm{bb}( S , i + 1 , c , \mathit{profit} ) \end{array}} \]
|
inline |
the greedy heuristic of KNAPSACK: the objects by decreasing profit per volume, each taken if it still fits; not optimal in general
In pseudocode (exercise 19.12):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{knapsackGreedy}( p , v , C )\quad \triangleright\ \text{no bound on its ratio to the optimum} \cr 2 & \quad S \gets \emptyset \cr 3 & \quad \textbf{foreach}\ i \text{, by decreasing } p_i / v_i \text{, that fits in the room left}\ \textbf{do}\ S \gets S \cup \{ i \};\ C \gets C - v_i \cr 4 & \quad \textbf{return}\ ( \textstyle\sum_{i \in S} p_i , S ) \end{array}} \]
| std::pair< T, Nodes > ADS::littleTsp | ( | const std::vector< std::vector< T > > & | D, |
| long * | generated = nullptr ) |
the cost and a minimum circuit from 0, by the reduction of the matrix; the number of nodes of the tree generated goes to *generated, if given
In pseudocode (exercise 19.11, the algorithm of Little, Murty, Sweeney and Karel), a node \( x \) with its reduced matrix \( M_x \), its edges included and its bound \( \mathit{lb}_x \), the sum of the minima subtracted, which no circuit of the subtree can avoid:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{littleTsp}( D )\quad \triangleright \ \text{a node } x \text{: the edges chosen, a reduced matrix } M_x \text{, a bound } \mathit{lb}_x \cr 2 & \quad x \gets \text{the root, with } M_x = D \text{ and } \infty \text{ on the diagonal};\ \mathrm{reduce}( x ) \cr 3 & \quad Q \gets \text{a priority queue holding } x \text{ with priority } \mathit{lb}_x \cr 4 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 5 & \qquad x \gets \mathrm{deleteMin}( Q );\ \textbf{if}\ x \text{ has } n \text{ edges}\ \textbf{then return} \ \text{its circuit and its cost} \cr 6 & \qquad ( i , j ) \gets \text{the zero of } M_x \text{ whose exclusion costs most, } \min_{h \ne j} M_x[ i ][ h ] + \min_{h \ne i} M_x[ h ][ j ] \cr 7 & \qquad y \gets x \text{ with the edge } ( i , j ) \text{: its row and column done, } \infty \text{ on the edge that would close a subcircuit} \cr 8 & \qquad z \gets x \text{ without the edge } ( i , j ) \text{: } M_z[ i ][ j ] = \infty \cr 9 & \qquad \textbf{foreach}\ c \in \{ y , z \} \text{ that has } n \text{ edges or for which } \mathrm{reduce}( c ) \text{ succeeds} \ \textbf{do}\ \mathrm{insert}( Q , c , \mathit{lb}_c ) \cr 10 & \textbf{function}\ \mathrm{reduce}( x )\quad \triangleright \ \text{false if a row or a column not done has no finite entry} \cr 11 & \quad \text{subtract from each row not done, then from each column not } \text{done, its least entry } \mu \text{, adding } \mu \text{ to } \mathit{lb}_x \end{array}} \]
| std::pair< long, Nodes > ADS::maxWeightClique | ( | const Adjacency & | G, |
| const std::vector< long > & | w, | ||
| long * | generated, | ||
| O & | o ) |
maxWeightClique( G , w , generated ), each improvement of the best weight told to \( o \) as IncumbentChanged< long >
|
inline |
the weight and the nodes of a clique of \( G \) of largest weight (MAXIMUM WEIGHT CLIQUE), the weights \( w \) not negative; a set is extended by the candidates adjacent to all of it, and pruned when its weight and that of all the candidates is not above the best; the nodes generated go to *generated, if given
In pseudocode (exercise 19.9), \( K \) the candidates, started with \( S = \emptyset \) and \( K = V \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{extend}( S , K , \mathit{weight} )\quad \triangleright\ K \text{ the candidates adjacent to all of } S \cr 2 & \quad \textbf{if}\ \mathit{best} < \mathit{weight}\ \textbf{then}\ ( \mathit{best} , \mathit{bestSet} ) \gets ( \mathit{weight} , S ) \cr 3 & \quad \mathit{rest} \gets \text{the weight of the nodes of } K \cr 4 & \quad \textbf{foreach}\ v = K[ a ] \text{, in order}\ \textbf{do} \cr 5 & \qquad \textbf{if}\ \mathit{weight} + \mathit{rest} \le \mathit{best}\ \textbf{then return}\quad \triangleright \ \text{even all of them would not do better} \cr 6 & \qquad \mathit{rest} \gets \mathit{rest} - w( v ) \cr 7 & \qquad \mathrm{extend}( S \cup \{ v \} , \text{the } K[ b ] \text{, } b > a \text{, adjacent to } v , \mathit{weight} + w( v ) ) \end{array}} \]
| T ADS::oneTree | ( | const std::vector< std::vector< T > > & | D, |
| int | r ) |
the weight of a minimum 1-tree of \( r \): a minimum spanning tree of the other nodes and the two least edges of \( r \); each circuit is a 1-tree, so that the weight bounds the optimum from below
In pseudocode (exercise 19.10, after Held and Karp):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{oneTree}( D , r )\quad \triangleright \ \text{a tree on the rest, and two edges of } r \cr 2 & \quad \textbf{return}\ \mathrm{spanningTreeWeight}( D , V \setminus \{ r \} ) + \text{the two least } D[ r ][ h ] \text{, } h \ne r \end{array}} \]
| T ADS::spanningTreeWeight | ( | const std::vector< std::vector< T > > & | D, |
| const Nodes & | V ) |
the weight of a minimum spanning tree of the nodes in \( V \), by the symmetric distances \( D \)
In pseudocode (exercise 19.10):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{spanningTreeWeight}( D , V ) \cr 2 & \quad \textbf{return}\ \text{the weight of the tree that } \mathrm{kruskal} \text{ finds on the complete graph on } V \text{ weighted by } D \end{array}} \]
| T ADS::tspLowerBound | ( | const std::vector< std::vector< T > > & | D, |
| const Nodes & | S, | ||
| TspBound | how = TspBound::BOOK ) |
a lower bound \( \mathit{lb}( S ) \) of the circuits that start with the path \( S \), the cost of the circuit if \( S \) has all the cities or if \( n = 2 \), where the circuit through both cities is the only one
In pseudocode (section 19.3.1), \( R \) the cities out of \( S \) and \( i = |S| - 1 \); each city of a circuit is entered once and left once, so that half the least cost of entering and leaving each city of \( R \) bounds what is left:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tspLowerBound}( D , S , \mathit{how} )\quad \triangleright\ S = ( S[ 0 ] , \ldots , S[ i ] ) \text{, } R \text{ the cities left} \cr 2 & \quad C \gets \text{the cost of the path } S \cr 3 & \quad \textbf{if}\ R = \emptyset\ \textbf{then return}\ C + D[ S[ i ] ][ S[ 0 ] ]\quad \triangleright\ \text{the circuit closed} \cr 4 & \quad A \gets \text{the least } D[ h ][ S[ 0 ] ] \text{, } B \gets \text{the least } D[ S[ i ] ][ h ] \text{, over } h \in R \cr 5 & \quad \textbf{if}\ \mathit{how} = \text{ONE\_TREE} \ \textbf{then return}\ C + A + B + \mathrm{spanningTreeWeight}( D , R ) \cr 6 & \quad D_h \gets \text{the least } D[ p ][ h ] + D[ h ][ q ] \text{ over } p \ne q \text{, both } \ne h \text{, for each } h \in R\quad \triangleright\ \text{the two edges of } h \cr 7 & \quad \textbf{return}\ C + \lceil ( A + B + \textstyle\sum_{h \in R} D_h ) / 2 \rceil \end{array}} \]