## 1. Introduction

Many current publications dealing with the energetics of ocean circulation use the concept of available potential energy (Winters et al. 1995; Huang 2005; Tailleux 2009, 2013b). The concept of available potential energy goes back to Margules (1905) and became a popular tool for the study of atmospheric energetics after Lorenz (1955). The available potential energy (APE) is an integral quantity defined as the potential energy of a system minus the background potential energy of the same system, where the background potential energy is defined as the minimum energy state the system can achieve by adiabatic rearrangements of its water parcels.

*n*th parcel (counting from the top or bottom) is placed will vary between different arrangements of water parcels even though the pressure does not. We will therefore consider models where the APE can be defined according to

*h*is the specific enthalpy in the current state,

*h*

_{ref}is the enthalpy in the reference state, and the integration is done over the total mass. This is an appropriate form of APE for a hydrostatic and compressible atmosphere or ocean (Reid et al. 1981; Tailleux 2013a). However, it is a somewhat approximate form to use when bottom topography is present (Huang 2005). This is also an exact form of APE to use with the Boussinesq model when a nonlinear equation of state is used. Furthermore, it is shown in appendix A that we do not need to consider the full enthalpy in either the compressible hydrostatic model or the Boussinesq model with a nonlinear equation of state, since the same result can be reached by minimizing the so-called dynamic enthalpy

^{1}(Young 2010). The focus in this paper is primarily on the Boussinesq model with a nonlinear equation of state, partly because we can find the exact minimum energy state without introducing any additional approximations, such as, for example, a flat lower boundary, to the model and also because it is a very commonly used model.

Some key properties with the separation of potential energy into an available and a background reservoir are that only diabatic processes such as ocean mixing and surface fluxes are able to change the background potential energy and that the effects of surface buoyancy fluxes, which are typically masked in potential energy budgets, become apparent in the APE framework.

The APE framework, however, has a major difficulty when a realistic nonlinear and pressure-dependent equation of state is used. The difficulty is in finding the rearrangement of water parcels that minimizes the potential energy. This difficulty is the result of the binary nature of the fluid and thermobaricity. By binary, we mean that the density depends on two components (temperature and salinity). By thermobaric, we mean the fact that the thermal expansion coefficient and, to a lesser degree, the haline contraction coefficient are pressure dependent (depth dependent in the Boussinesq equations). Finding the background state for a nonthermobaric equation of state, or for a unary fluid, on the other hand, is a trivial sorting task.

Let’s consider a discrete ocean consisting of a finite number of water parcels. The existence of a minimum energy state for such an ocean is almost self-evident. Here, we offer a simple proof by construction that reads as follows: Given *n* distinct water parcels, there are *n*! possible rearrangements to consider. We can evaluate the energy of all these possible states, and the minimum is the smallest. This minimum may or may not be unique in terms of parcel rearrangements; that is, more than one rearrangement may result in the same energy minimum.

Even though it is easy to prove the existence of a minimum energy state, devising an efficient algorithm that finds this minimum has proven to be far from trivial. There are, however, algorithms designed to find approximate minimum potential energy stratifications, one such example is the algorithm suggested by Huang (2005). During the revision of this work we have also become aware of an approximate method presented by Saenz et al. (2015), which uses similar ideas to those of Huang (2005), but the algorithm is faster. Both of these algorithms produce stably stratified reference states that can be thought of as the local minima of potential energy, since they are stable with regard to small adiabatic displacements. In contrast, what we are looking for in this article is the global minimum of potential energy. Computationally speaking, finding a global minimum is a much more difficult task. Other authors have instead chosen to redefine the reference state into something that can be computed easily. The dynamical potential energy suggested by Roquet (2013) is such an approach. It separates the potential energy into a background part based on the horizontally average density and an available part based on the deviation from that average. Such a framework is much more easily implemented than an APE framework, but it also lacks the ability of the APE framework to single out the diabatic effects. Another and more general approach is presented by Tailleux (2013b), who defines APE density relative to arbitrary reference states for Boussinesq multicomponent fluids.

It has been suggested that the problem of minimizing enthalpy by parcel rearrangements is an asymmetric traveling salesman problem (ATSP) (Tailleux and Grandpeix 2004). The traveling salesman problem is a classical problem in optimization. In its most classical formulation, a salesman must find the shortest route between *n* cities, visiting each city only once and return to the starting city. An ATSP problem is a TSP problem where the distance from city *j* to city *i* is different from the distance from city *i* to city *j*. In this paper, we will show that, although our problem is very similar to the ATSP problem, the problem is in fact a linear assignment problem. This is indeed very fortunate since the two problems belong to different problem classes. The ATSP problem belongs to a problem class known as NP-hard in computational complexity theory, while the linear assignment problem belongs to a class known as P.

The problem class known as P, to which our linear assignment problem belongs, consists of decision problems that can be solved in polynomial time.^{2} The problem class NP-hard is defined using a problem class called NP. The problem class NP contains all decision problems where a solution can be verified in polynomial time. A problem belongs to NP-hard if all problems in NP can be reduced to it in polynomial time. Thus, finding a polynomial time solution algorithm to any problem in NP-hard would give polynomial time solution algorithms to all problems in NP. NP-hard is thus often said to consist of problems that are at least as hard as any problem in NP. An Euler diagram that illustrates the different classes is shown in Fig. 1.

A great unsolved question in computer science and mathematics is whether or not all problems in NP are also in P. The Clay Mathematics Institute^{3} offers a $1 000 000 (U.S. dollars) prize for a solution to the problem. If P ≠ NP, then NP-hard problems cannot be solved in polynomial time, while P = NP does not resolve whether the NP-hard problems can be solved in polynomial time. It is commonly believed, among experts, that P ≠ NP, and thus NP-hard problems cannot be solved in polynomial time. A reason for this belief is that there are plenty of known NP-complete problems (problems that belong to both NP and NP-hard), of which many are important and have been studied extensively, yet no polynomial time solution algorithm to any of these problems have ever been found. The fact that our problem belongs to P, and not NP-hard, is thus extremely fortunate. Otherwise, usage of the classical APE framework, that is, one with the global minimum potential energy state as the background state, would be practically impossible to implement.

*n*tasks must be distributed between

*n*workers and each worker–task assignment has a predefined cost. Formally, this can be written as the following minimization problem:

*c*

_{ij}is the cost associated with assigning worker

*i*to task

*j*, and

*x*

_{ij}= 1 is if worker

*i*is assigned to task

*j*and is zero otherwise. The problem is then to determine the assignments

*x*

_{ij}that minimize Eq. (2), while obeying

*x*

_{ij}is a permutation matrix with exactly one entry (a one) in each row and column. In our problem, the workers are our different water parcels and the tasks are to be placed at a specific pressure (depth when the Boussinesq approximation is used), and each such assignment is associated with a cost, the value of the specific enthalpy of parcel

*i*at pressure

*j*. The constraints defined in Eq. (3) can then be interpreted as saying that each parcel can only be placed at one depth and at each depth there can only be one parcel.

*c*

_{ij}that holds the distances between city

*i*and

*j*and an

*x*

_{ij}function that equals one if the path goes from city

*i*to city

*j*and is zero otherwise. However, the TSP needs additional constraints as well as a slight modification done to those defined in Eq. (3). One common set of extra constraints used for the TSP are

*n*cities. However, this comes at the cost. Not only do we need extra constraints but also the extra variables

*u*

_{i}must be solved for. The modification that must be done to the constraints in Eq. (3) to be used for the TSP is that

*x*

_{ii}must be forced to equal zero. This is because in the TSP

*x*

_{ii}= 1 means that the route goes between a city and itself. In the linear assignment problem,

*x*

_{ii}could well be equal to 1, since it only means that worker

*i*is assigned job

*i*. More details on these and other TSP constraints are given in, for example, Laporte (1992).

The value of the specific enthalpy of parcel *i* at pressure *j* is typically different from that of parcel *j* at pressure *i*. Our problem is thus similar to the ATSP; although, it is clear from the different constraints presented that our problem is a linear assignment problem and thus different from the ATSP. It is also elucidating to think of the two problems in terms of a route. In the ATSP case, the route is naturally the order in which you visit the different cities, and in our problem the route is the order in which you visit the different parcels, starting, for example, from the ocean surface. The important difference between the problems in this respect is that there is a fixed cost of placing a parcel as number *n* on a route in our problem, namely, the value of the specific enthalpy of the parcel that ends up at that pressure. However, the cost of placing some city as number *n* on a route does not depend on the number *n* but on the distances from the city before and after on the route. In section 4, we show by example that the matrix operations used to solve the linear assignment problem cannot be used to solve the ATSP.

A few polynomial time solution algorithms exist for the linear assignment problem. We will focus on an algorithm called the Hungarian algorithm or the Munkres algorithm (Kuhn 1955). The time complexity of the original algorithm is *O*(*n*^{4}) (Munkres 1957), which can be improved to *O*(*n*^{3}) (Lawler 1976).

The remainder of this article is organized as follows: Section 2 introduces an exact method for finding the minimum energy state of the so-called Hungarian algorithm and two different approximate methods. In section 3, we apply the different methods to some idealized stratifications where the minimum energy state can be found exactly in reasonable time as well as to a more realistic case using an approximate method. Section 4 contains a discussion and the conclusions.

## 2. The Hungarian algorithm, Huang’s algorithm, and the 2-opt algorithm

*c*

_{11}is thus the dynamic enthalpy of parcel 1 at depth 1,

*c*

_{22}is the dynamic enthalpy of parcel 2 at depth 2, and so on. This example is meant to illustrate a simple case where the water parcel in the third column is warmer and less saline than the two others, the parcel in the second column is warmer and less saline than that in the first column, and a higher row number indicates a greater depth. If one uses a reference depth that is smaller than the smallest depth in the calculation (e.g., the surface) in the definition of dynamic enthalpy for this set of water parcels, then the dynamic enthalpy of the parcel in the third column will always be the greatest, and the difference in dynamic enthalpy between the parcels will increase with increasing depth, just as in our cost matrix. This example is so simple that its solution is intuitively evident; however, this is definitely not true of the problem type in general. How to solve the problem using the Hungarian is described in the steps below and illustrated in Fig. 2:

Step one is to subtract the minimum value in each row from that row.

Step two is to subtract the minimum value in each column from that column.

Step three is to cover all the zero elements with a minimum amount of horizontal and vertical lines. An assignment is possible if the number of covering lines equal the number of rows in the matrix. In this case, we have two lines and three rows, so we continue to step four.

Step four is to add the minimum value of the uncovered elements to the covered ones; we add two times the minimum value to any element that is covered twice.

Step five is to subtract the minimum element from all the elements of the matrix.

Step six is to again cover all the zero elements with a minimum amount of horizontal and vertical lines. Now the number of lines equals the number of rows in the matrix, which means that an assignment is possible. If this is not the case, then one simply restarts from step four. A minimum cost assignment is found by choosing three zeros such that only one is selected from each row and column. Our assignment is thus [

*c*_{13},*c*_{22},*c*_{31}], meaning that parcel 3 should be at depth 1, parcel 2 should be at depth 2, and parcel 1 should be at depth 3. Thus, the parcels are sorted from the top to bottom according to decreasing temperature and increasing salinity. The total cost, or the integrated dynamic enthalpy, of this configuration is [*c*_{13}+*c*_{22}+*c*_{31}= 22], assuming that the parcels have unit volume.

To understand how the algorithm works on our physical problem we must consider where the zeros in the cost matrix end up. After step 1, we have a zero in every row and that zero corresponds to the water parcel with the smallest value of the dynamic enthalpy at that depth. If the zeros in all rows were to end up in different columns, then the problem would be solved. However, this is typically not the case. In step 2, we add a zero to every column. This zero corresponds to the depth where the dynamic enthalpy of each water parcel is the smallest. Each zero we have introduced into the matrix so far thus corresponds to an optimal parcel placement. In step 3, we check whether we have found enough optimal placements to solve the problem. In a realistic case, the answer is almost certainly no. We therefore have to create more zeros from the uncovered elements. This is done by subtracting the smallest value of the dynamic enthalpy due to any possible parcel placement from all the others. The new zero will thus correspond to the possible parcel placement that contributes the least to the integrated dynamic enthalpy. Continuing in this way until an assignment is possible will therefore result in an optimal placement. If we instead were to do the same operations on the cost matrix from the ATSP problem, then step 1 would introduce a zero in every row *i* at the column *j*, which corresponds to the smallest distance between city *i* and any other. Even if we set *c*_{ii} = ∞ and we find ourselves in a situation where each row has its zero in a different column, these zeros do not give an acceptable route. This is most readily seen if we consider the symmetric TSP because if the zero in row 1 is at column 3 then the zero at row 3 is at column 1, so one would go from city 1 to city 3 and then back again in clear violation of the criteria of an acceptable route.

In practice, we calculate the optimal solution using a freely available MATLAB code written by Yi Cao^{4} and calculate the dynamic enthalpy using the International Thermodynamic Equation Of Seawater—2010 (TEOS-10) (IOC et al. 2010).

Apart from the exact solution, we also try some approximate methods. The first one is the algorithm of Huang (2005). Our implementation of Huang’s algorithm works as follows: Start from the greatest depth and place the water parcel that maximizes the in situ density at that depth and then continue upward and maximize the density at the second greatest depth by placing the densest of the remaining parcels there. At the shallowest depth, there is just one parcel remaining, so the placing is automatic. We have also tried to apply Huang’s algorithm to the dynamic enthalpy by minimizing *ρ*. However, with our test case the result of that approach was worse than that of the original.

We have also tried the so-called 2-opt algorithm, an algorithm that was proposed for solving the TSP by Croes (1958). The 2-opt algorithm works by trying all possible two-parcel interchanges until an optimal stratification has been found. We implement this algorithm as a nested loop that evaluates the interchange of the parcels currently situated at depths *i* and *j*. The outer loop goes from *i* = 1 to *i* = *n* − 1 and the inner loop goes from *j* = *i* + 1 to *j* = *n*, and an optimal stratification is found when the nested loop can be run from start to finish without finding any favorable parcel interchanges. The 2-opt algorithm is part of a family of algorithms called *k*-opt that tries all possible *k* parcel interchanges until an optimum is reached. A *k*-opt optimal stratification is also (*k* − 1)-opt optimal because all possible *k* − 1 parcel interchanges are contained within all possible *k* parcel interchanges. The reverse, however, is not true. More information on the *k*-opt algorithm can be found in Helsgaun (2000). Another obvious but important fact about these different optimums is that they are not unique, meaning that the 2-opt optimum one finds in general depends on the initial guess, as several typically exist for a given set of water parcels.

## 3. Results

### a. An idealized case that can be solved exactly

The idealized case discussed here is one where the ocean stratification is made up of relatively few water parcels, say *n*, to be placed at equally many depths. To obtain the water properties of our *n* parcels from hydrographic data,^{5} we first produce a discrete probability density function (PDF), which gives the volume of water as a function of Absolute Salinity and Conservative Temperature. This PDF is called *π*(*S*_{A}, Θ) and has the property *S*_{A}–Θ space, that is, the domain of our PDF where the *S*_{A}–Θ properties of the grid box best match those in the *S*_{A}–Θ space. Such PDFs have been used by, for example, Worthington (1981), Döös et al. (2012), and Hieronymus et al. (2014). To get *n* water parcels with distinct *S*_{A}–Θ properties, we choose the *k* elements from our PDF that represent the largest volumes. The next step is to create volumes of equal size from our *k* elements. We create water parcels with a volume equal to the volume of the smallest of the *k* elements (Δ*V*). If one of the *k* elements is *a* times larger than the smallest, we then create *a* water parcels of this water type. We thus have just one water parcel with the least common water type and several water parcels of the more common water types.

*A*(

*z*), which gives the ocean area as a function of depth. Starting either from the surface or the bottom, we can construct a vector of depths where we can place our volumes by summing Δ

*z*defined as

Table 1 shows the difference in volume-integrated dynamic enthalpy between the different approximate methods discussed in section 2 and the real minimum energy state for a test case where *n* = 2000. Positive values indicate the excess energy of an optimized stratification compared to the minimum energy state. The fact that the 2-opt algorithm finds the true minimum state in this case is encouraging. In fact, it finds it either if one starts with an initial guess based on Huang’s algorithm or the *σ*_{2.9} state. Starting with an initial guess based on Huang’s algorithm, running the 2-opt algorithm takes less than 1 s for the *n* = 2000 case, while running the Hungarian algorithm takes several hours. However, we have also found cases where the 2-opt algorithm does not converge to the state of minimum potential energy. Another issue is that the run time of the 2-opt algorithm is quite sensitive to the quality of the initial guess. If we were to start from the minimum energy state, the algorithm would test *n*^{2}/2 interchanges and none would be favorable. However, for every favorable interchange, the algorithm finds the runtime increases since the optimization is not finished until all possible two-parcel interchanges are tested and none of them are favorable.

Differences in volume-integrated dynamic enthalpy between the various optimized stratifications and the minimum energy state. The volume of the ocean is here taken to be 1.37 × 10^{18} m^{3} (Thorpe 2007). The term *σ*_{2.9} is a stratification derived from sorting the water parcels according to middepth-referenced potential density.

Figure 4 shows the different stratifications and the difference between the optimal stratification and that obtained with Huang’s algorithm, both in terms of *S*_{A} and Θ and dynamic enthalpy. The stratifications found from our *S*_{A}–Θ PDF are generally rather spiky and have too little warm water. The lack of warm water stems from the fact that we choose the *k* elements from the PDF that represents the largest volume, which gives preference to the large deep-water masses. To compensate for this effect, we use rather large *S*_{A} and Θ steps of 0.36 g kg^{−1} and 0.32°C, respectively. The large steps are partly responsible for the spikiness. However, the spikiness is also in part because when the entire ocean is considered, there is plenty of water with similar values of dynamic enthalpy but rather different Conservative Temperature and Absolute Salinity properties. However, what water parcels one uses is unimportant for the arguments made in this subsection; a more realistic hydrography is considered in the following subsection.

Figure 5 shows a case where the 2-opt method fails at finding the optimal stratification. Figure 5a shows our best guess of the optimal stratification, which is derived using a mixture of 2-opt and 3-opt. This case has *n* = 9807, so running the Hungarian algorithm would be too time consuming since the code is not parallelized. Figure 5b shows the stratification derived using Huang’s algorithm, Fig. 5c shows the difference in *S*_{A} and Θ between our best guess of the optimal stratification and that found using Huang’s algorithm, and Fig. 5d shows the difference in dynamic enthalpy of the two cases.

Figures 4d and 5c both show that Huang’s algorithm works rather well on our test cases, even though it does not converge to the optimal stratification in any of them. Caution is, however, still advised. In appendix B, we show a simple case where the algorithm fails at finding the minimum energy configuration, and we also derived a condition for when it fails at finding the minimum gravitational potential energy. The sorting according to middepth-referenced potential density (Fig. 4c) does, in contrast to Huang’s algorithm, perform rather miserably. For instance, it places the warm and highly saline parcels that resemble Mediterranean water (the parcels with *S*_{A} ≈ 38.8) more than 1000 m deeper than the optimal placing.

### b. An approximate case with realistic hydrography

*S*

_{A}–Θ coordinates that was used in the preceding section but with higher and variable resolution. We also need knowledge of the bathymetry to calculate the ocean volume between different depths and a concept called salinity curves. A salinity curve is found by fixing density at a given value and then calculating the salinity needed to achieve that density at a given depth for all values of Θ in the domain. A salinity curve is thus a solution on the form

*S*

_{A}(Θ,

*z*

_{0}) to an equation of the type

*ρ*

_{0}is a given density, and

*z*

_{0}is a given depth. The idea is then to equate the volume between two given depths in the ocean and the part of volume of the

*S*

_{A}–Θ PDF that is caught between two salinity curves. An example of two salinity curves is shown in Fig. 6. A first salinity curve is calculated either by fixing the density at the maximum value any parcel has at the largest depth in the domain or by fixing it at the smallest possible density any parcel has at the surface. We thus have a first salinity curve by assumption and wish to find a second one such that

*V*

_{T}is the total ocean volume, and

*σ*is given by

*π*is the volumetric PDF in

*S*

_{A}–Θ coordinates, and

*z*

_{r}is the position of the parcel in the reference state, and

*ρ*

_{r}is the density stratification in the reference state. A problem with the LNBE is that it may have more than one solution, meaning that there are water parcels whose density equal that in the reference state at more than one depth. Fortunately, this is only a minor problem since very few parcels were found to have multiple reference states by Saenz et al. (2015).

Our usage of salinity curves differ somewhat from that presented by Saenz et al. (2015) because the prime focus of this paper is on the mathematical nature of the energy minimization problem rather than on producing a smart approximate method. Therefore, we have not taken the LNBE approach. Instead, we have used the salinity curves approach to split the problem of finding the minimum energy stratification into parts. This is done by taking the water parcels (i.e., the elements of the discrete PDF) that exist between two salinity curves and placing them between *z*_{1} and *z*_{2} using the Hungarian algorithm. We start from the surface, that is, having *z*_{1} = 0 and continue downward choosing *z*_{2} such that the number of water parcels to place between *z*_{1} and *z*_{2} never exceeds 1900.

We have also used the additional simplifications of assuming that all water parcels between the two salinity curves have equal volume and that the ocean is rectangular between *z*_{1} and *z*_{2}, so that the distance between *z*_{1} and *z*_{2} can be split into equal parts where the parcels can be placed. Every discrete *S*_{A} and Θ value in the PDF can then be assigned a value of *z*_{r}, which is then used to find the reference depths of the water parcels in our gridded hydrography by interpolation in *S*_{A} and Θ.

Figure 7 shows two transects of *z*_{r} calculated using the method described above, one through longitude 23°W and the other through 178°W. Figure 8 offers a close up of the reference positions at high latitudes. The similarities between the distribution of *z*_{r} in our calculation and in that of Saenz et al. (2015) are striking. They also find the same two regions of deep reference positions in the Arctic Ocean in the Greenland Sea and in the Barents Sea. The results are very similar around Antarctica as well; in both cases, the deepest reference positions are found in the Weddell and Ross Seas. The constant longitude transects in our calculation is also very similar to those of Saenz et al. (2015).

The results of our calculation of the reference positions using a more realistic hydrography are thus very similar to those of Saenz et al. (2015). In fact, when the problem of finding the reference state is split into parts, it is of very little use to solve each part exactly using the Hungarian algorithm. In our calculation, we split the World Ocean into 80 depth regions and in each region a maximum of 1900 parcels were placed. We did this placing both using the Hungarian and Huang’s algorithm. The maximum amount of misplaced parcels in any depth region when Huang’s algorithm was used was 60. This may seem a lot, however, the difference in energy between a profile sorted according to Huang’s approach and the real minimum energy stratification is very small in each region. Given then that running this optimization using the Hungarian algorithm took almost 5 days and also considering the fact that when we split the problem into parts using the salinity curve approach we are not guaranteed to find the global minimum energy stratification anyway, it appears that using the Hungarian algorithm this way cannot be advised. Nevertheless, it is reassuring to see that the approximate method does a good job in all subregions.

## 4. Discussion and conclusions

The most important result in this article is the recognition that the problem of finding the minimum energy stratification by parcel rearrangements belongs to the problem class P rather than NP-hard. This result ensures that there exists, in some sense, an efficient algorithm that can solve the problem. Our specific cost matrix, however, is problematic for the Hungarian algorithm. The code we run solves a problem of size *n* = 2000 with random numbers between 0 and 1 in 39 s, while our problem matrix takes almost 7 h to solve. The reason for this slow convergence is that the zeros created by extracting the minimum value from each row and column will typically be aligned along a single row and a single column with our problem matrix. The alignment along a single row happens because the dynamic enthalpy grows with the distance |*z* − *z*_{ref}|, while the alignment along a single column happens because sorting water parcels according to dynamic enthalpy at different depths typically give rather similar results. This creates very few lines (step 3 and 6 in the Hungarian algorithm), and many iterations are needed for the method to converge.

However, the algorithm has its merits in the understandability of how the operations lead to a minimum. The author of the code we use for the Hungarian algorithm also has a faster code that runs an algorithm derived by Jonker and Volgenant (1987). This code is up to 10 times faster but still much too slow for solving a problem of significant size. A 1° by 1° climatology with 30 vertical levels is, as an example, a problem of size *n* = 1 944 000.

For a really large case, such as the aforementioned 1° by 1° climatology, Huang’s algorithm and the algorithm presented by Saenz et al. (2015) are really the only algorithms that are fast enough to be useful at the moment. However, appendix B shows that some caution is still advised when using these approximate methods. The relatively quick 2-opt method may therefore be valuable as a check for these algorithms.

The approximate methods, however, perform well in both our test cases as only a handful of water parcels were placed wrong using Huang’s algorithm and even fewer using the 2-opt algorithm. The exact errors for the case with the realistic stratification, where we split the World Ocean into depth ranges that are sorted individually, are not known. This is simply because the salinity curves approach does not ensure that the minimum potential energy state is found. In fact, this approach is probably best thought of as a generalization of Huang’s algorithm, which will ensure that a stable stratification is found if the distance |*z*_{1} − *z*_{2}| is sufficiently small. Minimizing the energy in all subregions will therefore not ensure that a global energy minimum is reached.

To conclude, we have identified the problem of finding the minimum energy stratification by adiabatic rearrangements of water parcels as the linear assignment problem by examining the constraints needed to solve the minimization problem. The problem can be solved in polynomial time, although the speed of the algorithm is still an issue. At present this approach is typically fast enough to be useful for finding the minimum energy configuration from, for example, a CTD cast or an atmospheric sounding but not fast enough to be useful to do energy diagnostics in a GCM. However, if one could find a good way of representing the oceanic distribution of *S*_{A} and Θ using relatively few water parcels, then the Hungarian algorithm could be used together with the LNBE to find the minimum energy stratification efficiently also in a large case. Furthermore, the problem of finding the minimum energy stratification is not hostage to the linear assignment problem formulation. In fact, the problem formulation of the linear assignment problem is more general than our problem formulation needs to be. In the linear assignment problem, the costs can be given by any arbitrary function, whereas in our case the cost function is the dynamic enthalpy, whose functional dependencies on *S*_{A}, Θ, and *p* are known. This difference between the two problems is what ensures that the minimum energy stratification can be found for a linear equation of state by sorting all water parcels according to potential density. Perhaps there are some, yet to be found, empirical properties of the nonlinear equation of state for seawater and the water masses in the global ocean that can be used to simplify the problem and still maintain absolute accuracy.

## Acknowledgments

We thank Remi Tailleux and two anonymous reviewers for valuable discussions on the manuscript. Figure 1 is created by Behnam Esfahbod and released under the GNU Free Documentation Licenses CC-BY-SA-3.0, CC-BY-SA-2.5, CC-BY-SA-2.0, and CC-BY-SA-1.0.

## APPENDIX A

### Boussinesq and Compressible Energetics

**u**is the velocity vector,

**f**is the Coriolis parameter,

*p*is pressure,

**z**is a vertical unit vector, Θ is Conservative Temperature,

*S*

_{A}is Absolute Salinity, and

*b*= −

*gρ*

^{−1}(

*ρ*−

*ρ*

_{0}) which differs from the usual Boussinesq buoyancy by having

*ρ*rather than

*ρ*

_{0}in the denominator.

*z*

_{ref}is an arbitrarily chosen reference depth. The integral can be interpreted as the energy required to move a water parcel adiabatically from its vertical position

*z*to the reference depth. Combining the scalar product of

**u**and Eq. (A1) with the material derivative of Eq. (A6) gives the energy conservation law

*V*is the entire ocean volume. From Eqs. (A7) and (A8) we can see that the Boussinesq dynamic enthalpy plays the same role in the energy budget of the Boussinesq model with a nonlinear equation of state as the gravitational potential energy does in the Boussinesq model with a linear one. In a recent publication, Eden (2015) derives a more complete set of conservation equations for energy in the Boussinesq approximation, using a slight modification to the first law of thermodynamics. Among other things, he shows that the energetics of the Boussinesq equations can be expressed either in terms of the sum of internal and gravitational potential energy or in terms of enthalpy. The conclusions drawn in this appendix are, however, unchanged by these new findings so we have opted to keep the conservation equations derived by Young (2010).

*η*is specific entropy, and

*υ*=

*ρ*

^{−1}is the specific volume. The first term on the right-hand side in Eq. (A9) is used by McDougall (2003) to define the Conservative Temperature through

*c*

_{p}is the average heat capacity at

*p*=

*p*

_{ref}, and the pressure integral is the so-called dynamic enthalpy. Because the first term in Eq. (A9) is invariant under parcel rearrangement, it is clear that a rearrangement that minimizes the dynamic enthalpy also minimizes the full enthalpy. To further elucidate the connection to the Boussinesq dynamic enthalpy, we follow the steps taken by Young (2010) and replace

*η*with Θ and use Young’s definition of buoyancy to write Eq. (A9) as

^{u}(Θ

^{l}) and Absolute Salinity

*z*, and the lower is situated at

*z*−

*δz*, interchanging the results in

*δz*, the expression reduces to

*z*. The same conclusion can be drawn about the compressible model from Eq. (A9).

## APPENDIX B

### Cases where Huang’s Algorithm Fails

*γ*is the thermobaric coefficient, and

*g*is the acceleration due to gravity. The dynamic enthalpy

*z*= 0 at the ocean middepth. The surface is then at

*z*=

*H*, and the bottom is at

*z*= −

*H*. It is trivial to see that our two water mass have equal density at

*z*= 0 if

*n*water parcels of each water mass and use Huang’s algorithm to sort the water parcels, the obvious result is that the cold freshwater mass will occupy the deep half of the domain (the cold parcels are denser than the warm parcels below

*z*= 0) and consequently that the warm salty water mass will occupy the upper half of the domain. This configuration is stable (

*N*

^{2}= 0 everywhere), but it does not minimize the integrated dynamic enthalpy. It is trivial to see from Eqs. (B2) and (B3) that the integrated dynamic enthalpy is minimized if the warm water parcels are placed in the middle.

*z*= −

*H*/2 and

*z*=

*H*/2 and the cold water is on either side, is given by

*n*water parcels of equal mass (

*m*) with different Conservative Temperatures and Absolute Salinities. The gravitational potential energy of the system (GPE) is given by

*ρ*

_{k}is the density of parcel number

*k*,

*V*

_{k}is the volume of parcel number

*k*, and

*k*. Now we assume a rectangular flat bottom basin with horizontal area

*A*. We also put

*z*= 0 at the bottom and use the convention that

*k*= 1 at the uppermost parcel,

*k*= 2 on the second from the top, and so forth. The term

*l*with that at position

*m*gives

*S*

_{A}(

*l*) and Θ(

*l*) [

*S*

_{A}(

*m*) and Θ(

*m*)] are the Absolute Salinity and Conservative Temperature of the parcel at position

*l*(

*m*) before the interchange, and

*p*is pressure. Equation (B11) can also be written as

*p*(

*m*) >

*p*(

*l*), it follows that

*m*and

*l*. Letting

*δ*tend to zero, we get a decrease in GPE for any switch where

*δ*= 0 we see from Eqs. (B15) and (B16) that if Θ(

*m*) > Θ(

*l*) then ΔGPE is positive for this two-parcel interchange. One might think then that any such switch that lowers the GPE would also make the water column unstable. However, this is not the case since there may be parcels in between parcel

*l*and

*m*. Consider, for example, a case where we have three parcels sorted using Huang’s algorithm and the result is that a relatively warm and salty parcel, with properties

*S*

_{A}(3) and Θ(3), stays at the bottom; a very warm and very salty parcel, with properties

*S*

_{A}(2) and Θ(2), is in the middle; and a cold and fresh parcel, with properties

*S*

_{A}(1) and Θ(1), rests on top. We also assume that the density of the top and bottom parcel are such that switching them lowers the GPE. Now if we switch the top and bottom parcels, maintained stability requires that

*p*(1) is the pressure at the intersection between the top and the middle parcel, and

*p*(2) is the pressure at the intersection between the middle and the bottom parcel. Assume, for example, that the densities in Eq. (B17) are equal, then Eq. (B16) shows that it is simply a matter of making

*S*

_{A}(2) and Θ(2) big enough to ensure that

*k*-opt approach with

*k*> 2, but the algebra becomes lengthy so we have settled for a demonstration of failure in the 2-opt case.

## REFERENCES

Adkins, J. F., A. P. Ingersoll, and C. Pasquero, 2005: Rapid climate change and conditional instability of the glacial deep ocean from the thermobaric effect and geothermal heating.

,*Quat. Sci. Rev.***24**, 581–594, doi:10.1016/j.quascirev.2004.11.005.Croes, G. A., 1958: A method for solving traveling salesman problems.

,*Oper. Res.***6**, 791–812, doi:10.1287/opre.6.6.791.Döös, K., J. Nilsson, J. Nycander, L. Brodeau, and M. Ballarotta, 2012: The World Ocean thermohaline circulation.

,*J. Phys. Oceanogr.***42**, 1445–1460, doi:10.1175/JPO-D-11-0163.1.Eden, K., 2015: Revisiting the energetics of the ocean in Boussinesq approximation.

,*J. Phys. Oceanogr.***45**, 630–637, doi:10.1175/JPO-D-14-0072.1.Helsgaun, K., 2000: An effective implementation of the Lin–Kernighan traveling salesman heuristic.

,*Eur. J. Oper. Res.***126**, 106–130, doi:10.1016/S0377-2217(99)00284-2.Hieronymus, M., J. Nilsson, and J. Nycander, 2014: Water mass transformation in salinity–temperature space.

,*J. Phys. Oceanogr.***44**, 2547–2568, doi:10.1175/JPO-D-13-0257.1.Huang, R. X., 2005: Available potential energy in the world’s oceans.

,*J. Mar. Res.***63**, 141–158, doi:10.1357/0022240053693770.IOC, SCOR, and IAPSO, 2010: The international thermodynamic equation of seawater—2010: Calculation and use of thermodynamic properties. Intergovernmental Oceanographic Commission, Manuals and Guides 56, 220 pp. [Available online at http://www.teos-10.org/pubs/TEOS-10_Manual.pdf.]

Jonker, R., and A. Volgenant, 1987: A shortest augmenting path algorithm for dense and spare linear assignment problems.

,*Computing***38**, 325–340, doi:10.1007/BF02278710.Kuhn, H. W., 1955: The Hungarian method for the assignment problem.

,*Nav. Res. Logist. Quart.***2**, 83–97, doi:10.1002/nav.3800020109.Laporte, G., 1992: The travelling salesman problem: An overview of exact and approximate algorithms.

,*Eur. J. Oper. Res.***59**, 231–247, doi:10.1016/0377-2217(92)90138-Y.Lawler, E. L., 1976:

*Combinatorial Optimization: Networks and Matroids.*Holt, Rinehart, and Winston, 374 pp.Lorenz, E. N., 1955: Available potential energy and the maintenance of the general circulation.

,*Tellus***7A**, 157–167, doi:10.1111/j.2153-3490.1955.tb01148.x.Margules, M., 1905: Über die Energie der Stürme. Wein: K. K. Hot-und Stattsdruckerei, 26 pp.

McDougall, T. J., 2003: Potential enthalpy: A conservative ocean variable for evaluating heat content and heat fluxes.

,*J. Phys. Oceanogr.***33**, 945–963, doi:10.1175/1520-0485(2003)033<0945:PEACOV>2.0.CO;2.Miller, C. E., A. W. Tucker, and R. A. Zemlin, 1960: Integer programming formulation of traveling salesman problems.

*J. Assoc. Comp. Mach.,***7,**326–329.Munkres, J., 1957: Algorithms for the assignment and transportation problems.

,*J. Soc. Ind. Appl. Math.***5**, 32–38, doi:10.1137/0105003.Nycander, J., 2011: Energy conversion, mixing energy, and neutral surfaces with a nonlinear equation of state.

,*J. Phys. Oceanogr.***41**, 28–41, doi:10.1175/2010JPO4250.1.Reid, R. O., B. A. Elliott, and D. B. Olsen, 1981: Available potential energy: A clarification.

,*J. Phys. Oceanogr.***11**, 15–29, doi:10.1175/1520-0485(1981)011<0015:APEAC>2.0.CO;2.Roquet, F., 2013: Dynamical potential energy: A new approach to ocean energetics.

,*J. Phys. Oceanogr.***43**, 457–476, doi:10.1175/JPO-D-12-098.1.Saenz, J. A., R. Tailleux, E. D. Butler, G. O. Hughes, and K. Oliver, 2015: Estimating Lorenz’s reference state in an ocean with a nonlinear equation of state.

,*J. Phys. Oceanogr.***45,**1242–1257, doi:10.1175/JPO-D-14-0105.1.Tailleux, R., 2009: Understanding mixing efficiency in the oceans: Do the nonlinearities of the equation of state matter.

,*Ocean Sci.***5**, 271–283, doi:10.5194/os-5-271-2009.Tailleux, R., 2013a: Available potential energy and exergy in stratified fluids.

,*Annu. Rev. Fluid Mech.***45**, 35–58, doi:10.1146/annurev-fluid-011212-140620.Tailleux, R., 2013b: Available potential energy density for a multicomponent Boussinesq fluid with arbitrary nonlinear equation of state.

,*J. Fluid Mech.***735**, 499–518, doi:10.1017/jfm.2013.509.Tailleux, R., and J. Y. Grandpeix, 2004: On the seemingly incompatible parcel and globally integrated views of the energetics of triggered atmospheric deep convection over land.

,*Quart. J. Roy. Meteor. Soc.***130**, 3223–3243, doi:10.1256/qj.03.140.Thorpe, S. A., 2007:

*An Introduction to Ocean Turbulence.*Cambridge University Press, 240 pp.Vallis, G. K., 2006:

*Atmospheric and Oceanic Fluid Dynamics.*Cambridge University Press, 36 pp.Winters, K. B., P. N. Lombard, J. J. Riley, and E. A. D’Asaro, 1995: Available potential energy and mixing in density-stratified fluids.

,*J. Fluid Mech.***289**, 115–128, doi:10.1017/S002211209500125X.Worthington, L. V., 1981: The water masses of the World Ocean: Some results of a fine-scale census.

*Evolution of Physical Oceanography,*MIT Press, 42–69.Young, W. R., 2010: Dynamic enthalpy, Conservative Temperature, and the seawater Boussinesq approximation.

,*J. Phys. Oceanogr.***40**, 394–400, doi:10.1175/2009JPO4294.1.

^{1}

Also called effective potential energy (Nycander 2011).

^{2}

A problems of size *n* can be solved in polynomial time if the runtime *T*(*n*) = *O*(*n*^{k}) for some fixed *k*.

^{4}

The code can be downloaded online from http://www.mathworks.com/matlabcentral/fileexchange/20652-hungarian-algorithm-for-linear-assignment-problems--v2-3-.

^{5}

The hydrographic data are from the *World Ocean Atlas 2009* interpolated onto a NEMO ORCA1 grid.