Abstract
In a recent paper, a solution approach to the Maximum Happy Vertices Problem has been proposed. The approach is based on a constructive heuristic improved by a matheuristic local search phase. We propose a new procedure able to outperform the previous solution algorithm both in terms of solution quality and computational time. Our approach is based on simple ingredients implying as starting solution generator an approximation algorithm and as an improving phase a new matheuristic local search. The procedure is then extended to a multistart configuration, able to further improve the solution quality at the cost of an acceptable increase in computational time.
Introduction
Vertex coloring problems are one of the most popular and extensively studied subjects in the field of graph theory. They have received wide attention in the literature, not only for their realworld applications but also for their theoretical aspects and for the computational hardness (Malaguti and Toth 2010). Traditional vertex coloring problems consist of coloring all vertices of a graph G with different colors in such a way that any pair of adjacent vertices are labeled with different colors. Recently, interest has been also devoted to vertex coloring problems where the coloring of adjacent vertices is desired to be the same. This is the case of the problem called Maximum Happy Vertices Problem (MHV) considered in this paper. Given a set of precolored vertices, the problem asks to extend the coloring to the remaining vertices with the objective to maximize the number of nodes colored with the same color of their adjacent vertices.
The MHV problem and the concept of “happiness” related to vertices have been proposed in Zhang and Li (2015). A vertex is considered happy if all its neighbors are of the same color. The problem objective is the maximization of the number of happy vertices.
More formally, the MHV problem considers an undirected graph \(G = (V, E)\) with n vertices and m edges (with \(\varGamma (i)\) defined as the set of neighbors of vertex i), a color set \(K=\{ 1,\ldots ,k\}\), a subset of vertices \(A \subseteq V\) where \(A \ge k\) and a partial coloring \(c : A \rightarrow \{1,\dots ,k\}\) such that \(\forall \ i \in \{1,\dots ,k\}, \exists \ v \in A : c(v) = i\). The problem asks to extend the coloring c to the remaining nonprecolored vertices to a complete graph coloring \(\bar{c} : V \rightarrow \{1,\dots ,k\}\) such that the total number of happy vertices is maximized.
In a recent paper Lewis et al. (2019), the MHV problem has been addressed and a solution approach based on the Construct, Merge, Solve & Adapt (CMSA) framework of Blum et al. (2016) has been applied to deal with 380 computationally hard instances.
The problem has also been tackled from a theoretical point of view, see the proof of NPhardness in Zhang and Li (2015), approximation algorithms in Zhang et al. (2018) and complexity results in Agrawal (2017) and Aravind et al. (2016), where polynomial algorithms for simple special cases have been proposed.
From a computational perspective, to the best of our knowledge, the work of Lewis et al. (2019) is the first attempt to propose solution procedures dealing with largesize instances. Moreover, the authors of Lewis et al. (2019) made freely available both the instance generator Lewis et al. (2018a, b) and the source code (except the part related to the mixed integer linear programming solver GUROBI) Lewis et al. (2019). The work of Lewis et al. (2019) proposes a hybrid heuristic approach, based on a constructive heuristic improved by a matheuristic local search phase.
Matheuristics are solution methods that have been successfully applied to several combinatorial optimization problems [see for instance Ball (2011), Della Croce et al. (2013)], giving rise to an impressive amount of research in recent years. Matheuristics have been applied to routing Macrina et al. (2019) Shahmanzari et al. (2020), packing Billaut et al. (2015), MartinezSykora et al. (2017), rostering Della Croce and Salassa (2014), Doi et al. (2018), lot sizing Ghirardi and Amerio (2019) and machine scheduling Della Croce et al. (2014), Croce et al. (2019), FanjulPeyro et al. (2017) just to cite a few of them. Matheuristics rely on the general idea of exploiting the strength of both metaheuristic algorithms and exact methods.
In the present work, we developed a simple but effective matheuristic algorithm, along the same line of CMSA in Lewis et al. (2019), to deal with the Maximum Happy Vertices Problem. The proposed matheuristic algorithm is based on an overarching neighborhood search approach with an intensification search phase realized by a MILP solver. The main advantages of our approach, with respect to the one of Lewis et al. (2019), are:

Better performances in terms of solution quality,

Much better performances in terms of computational times (few seconds against 1 h),

Simple design of the solution procedure,

Simple integration in a multistart version able to further improve the solutions quality.
The paper is organized as follows. In Sect. 2 , the integer linear programming formulations of the problem are provided. Section 3 is devoted to the description of the proposed solution algorithms. In Sect. 4, computational results and benchmarks are presented. Sect. 5 concludes the paper with final remarks.
MIP models
Two mixed integer linear programming formulations are provided in Lewis et al. (2019).
In the first model (M1), integer variables \(x_i \in \{1,\ldots , k\}\) define the color assigned to each vertex, while variables \(y_i\) are forced to be one only if vertex i is unhappy. The set A represents the set of precolored vertices while c(i) is the color assigned to each vertex in A. Recall that \(\varGamma (i)\) is defined as the set of neighbors of vertex i. The overall model is:
where (1) maximizes the number of happy vertices, (2) assigns colors to all the precolored vertices, (3) sets \(y_i=1\) for unappy vertices. (4) and (5) define the optimization variables.
Note that constraints (3) are not linear, and hence they require a linearization [not explicitated in Lewis et al. (2019)], which results in their substitution with:
The second model (M2) uses binary variables \(x_{ij}\) where \(x_{ij}=1\) if and only if color j is assigned to vertex i. Variables \(y_i\) have the same meaning as in the first model.
Here, (8) maximizes the number of happy vertices, constraints (9) specifies the precolorings, constraints (10) ensures that one color is assigned to any vertex, and constraints (11) forces \(y_i = 1\) if vertex i is unhappy. (12) and (13) define the optimization variables.
As before, constraints (11) are not linear, and we propose the following linearization:
We also point out that \(y_i\) variables, for this second model, not necessarily need to be defined as binary. It is, in fact, sufficient to define them as \(0 \le y_i \le 1\) given that they are constrained by (14) and (15) which enforce \(y_i\) to be 0 or 1.
Despite the fact that in Lewis et al. (2019), it is reported that model M2 is far less reliable w.r.t. solution quality, we tested it against all instances using CPLEX 12.7 as MIP solver and found out that 80 instances out of the whole dataset made of 380 instances were solved to optimality. All instances with 250, 500, 750 and 1000 nodes and \(k = 10\) had been solved to optimality within the time limit of 3600 s, the same time limit used in Lewis et al. (2019). Thus, in our experiments, model M2 outperforms model M1. From now on, in our algorithms, we use model M2 for benchmarks since, overall, it gives better solutions within the same time limit with respect to the model M1.
A simple solution approach
We propose here a simple but effective matheuristic improvement approach. Starting from a given solution, the algorithm iteratively improves it with a scheme based on the neighborhood search approach. Each iteration explores the neighborhood by constructing a problem where the variables to be optimized refer to a subset of the variables of the original problem, while other ones are fixed to the value they have in the current solution. The detailed procedure is described in Algorithm 1. The algorithm starts with a given feasible solution \(\bar{c}\) (step 1). A counter \(no\_improvement\) of iterations passed without finding an improving solution is set to 0 (step 2). At each iteration of the main loop (cycle 3–16), a subset of candidate colors for each node is selected and an exact method is employed to build a possibly improved solution. In the current solution (steps \(48\)), candidate colors for each node i are the current color and all the colors assigned to nodes adjacent to i with a path of length L, i.e. nodes colors that can be recognized following an \(Llength\) edge path. The resulting problem is then optimally solved through model (8)–(13), obtaining solution \(\bar{c}'\) (step 9). If the new solution is better than the previous one, the counter of nonimproving iterations is reset to 0 (step 11). Otherwise, it is increased by one (step 13). Note that solution \(\bar{c}'\) cannot be worse than the current solution \(\bar{c}\) because the latter is a feasible solution of the ILP model. Hence, it is always accepted as the new current solution (step 15). The improvement phase is repeated if less than S iterations have been performed without improving the current solution.
CMSA solution approach proposed in Lewis et al. (2019) is based on a similar improvement scheme as the one we propose, with a different neighborhood definition. We highlight here the main differences:

In CMSA, candidate colors for each node i to be chosen for reoptimization are only the color of i plus, with a given probability, a subset of the colors of the neighbor nodes of i, while our matheuristic procedure considers, as possible candidates for each node i, all colors of the nodes that could be reached from node i with a path of a given length. Hence, the neighborhood dimension of the proposed algorithm is larger than the one of CMSA.

CMSA uses model M1, while in our case, model M2 has been selected. This choice does not affect the algorithm results in terms of solution quality (all ILPs are solved to optimality) but influences the running time.
In Lewis et al. (2019), two constructive methods are proposed for the initial solution generation, namely \(GreedyMHV\) and \(GrowthMHV\). We point out that \(GreedyMHV\) is the same procedure as the approximation algorithm \(\mathcal {G}\) proposed in Zhang et al. (2018). The approximation algorithm \(\mathcal {G}\) has been used in our approach as starting solution. During preliminary testings, we tried the best among algorithm \(\mathcal {G}\) (a.k.a. \(GreedyMHV\)) and \(GrowthMHV\), but we experienced no improvements in the solutions quality.
The rationale of algorithm \(\mathcal {G}\) of Zhang et al. (2018) is to label all the uncolored vertices with the same color and testing all possible k colors obtaining, in such way, k different vertices colorings. The starting solution is then chosen among all k colorings, i.e. the one exhibiting the largest number of happy vertices (Algorithm 2).
To further test the matheuristic improvement algorithm in order to assess the quality of the proposed approach, we tested it in a multistart setting. In this new configuration, the improvement procedure depicted in Algorithm 1 is applied not only on the solution obtained by the best coloring of algorithm \(\mathcal {G}\), but on all possible k different vertices colorings. The best final result is then returned. Algorithm 3 resumes the main steps of the multistart procedure.
Computational experiments
We decided to test different configurations of algorithms over a dataset generated thanks to the instance generator in Lewis et al. (2018a, b). According to Lewis et al. (2019), instances were generated as random graphs using values of \(p = 5 / (n  1)\), where n is the total number of nodes and p the probability of two vertices being adjacent. This value of p induces an average vertex degree of 5. In all instances, \(10\%\) of the vertices were precolored. Authors of Lewis et al. (2019) state that these configurations lead to the creation of the most difficulttosolve instances. As in Lewis et al. (2019), we considered classes of instances with a number of colors k equal to 10 or 50 on graphs having a number of nodes n of 500, 750, 1000, 2000, 3000, 4000, 5000, 7500 and 10000. Since the solver is able to solve to optimality four classes (namely all the ones with \(k=10\) and with n equal to 250, 500, 750 and 1000), these were not considered in our dataset. For each of the remaining 15 classes, 20 instances were generated. For tuning the algorithm parameters, we generated an additional smaller dataset, composed of 10 instances for each of the classes with k equal to 10 or 50 and n equal to 3000, 5000 and 10000.
The algorithms have been implemented in C++ and the source code is available upon request to the authors. All tests have been performed on an i58500 3 GHz CPU system with 16 GB of RAM and CPLEX 12.7 as MIP solver. CPLEX solver has been applied with no parameters tuning and in multithreaded mode.
The following two subsections present the results of the experiments aiming to tune the algorithm parameters, and the comparison between the results of the proposed algorithms and CMSA, proposed in Lewis et al. (2019).
Parameter tuning
In order to tune the values of parameters L and S of Algorithm 1, a set of computational experiments has been performed.
Table 1 summarizes the results. For each class of instances (k colors and n nodes), we present the average percentage of happy nodes \(H\%\) and computational time T over the 10 tuning instances, with different parameters values L and S. The best entries for each line are highlighted in bold.
Parameter L defines the neighborhood size, and has been considered equal to 1 or 2. Setting a value of 3 or more will result in the creation of ILP models with too many free variables, sometimes exceeding a time limit of 3600 s without finding the optimal solution. It is clear from the table that the best choice is \(L=2\), having better results at cost of an acceptable increase of computational time.
Paramenter S configures the algorithm stopping criterion and ranges from 1 to 3. While an improvement is clear in results obtained increasing S from 1 to 2, the results are, for most instances, the same when \(S=3\). Hence, we decided to set \(S=2\).
Algorithms results comparison
As previously pointed out, authors of Lewis et al. (2019) made freely available the source code of their algorithms except the part related to the mixed integer linear programming model and solver. Then, in order to benchmark our procedure with the reference algorithm CMSA, we reimplemented it, integrating their source code with a mixed integer linear programming model. In the description of CMSA, it is not clear how singlecolor labels could be efficiently excluded from the list of possible colors since the variables used are of integer type (model M1 is used) and no constraints sets (i.e. disjunctive constraints) have been explicitated to deal with values exclusion. Hence, we contacted the authors of Lewis et al. (2019) asking for details on CMSA implementation which is slightly different with respect to the published paper. Thanks to their help we reconstructed CMSA as originally implemented. Each time the LP model M1 is run, the following rules are used:

If a node \(\bar{i}\) has only one candidate color \(\bar{c}\), the corresponding variable is set to that color (\(x_{\bar{i}} = \bar{c}\)).

If a node \(\bar{i}\) has more candidate colors, the corresponding variable is left free to get any value (\(x_{\bar{i}} \in \{1,...,k\}\)).
For other details about CMSA refer to Lewis et al. (2019). On the other side, excluding values implying model M2 as in our matheuristic is rather simple: it is, in fact, sufficient to add constraints like \(x_{\bar{i}\bar{j}} = 0\) if we want to prevent node \(\bar{i}\) to be labeled with color \(\bar{j}\).
We tested the following approaches:

CPLEX: Lower Bound and Upper Bound after 3600 s calculated by CPLEX solver with model M1.

CMSA: original CMSA using as starting solution the best among \(GreedyMHV\) and \(GrowthMHV\) with a time limit of 3600 s [as in Lewis et al. (2019)]. Considering that CMSA is not a deterministic algorithm, we present here the best result obtained with 10 different executions.

MHG: matheuristic algorithm 1, configured with \(L=2\) and \(S=2\), using as starting solution the approximation Algorithm \(\mathcal {G}\).

MS: multistart version of the procedure, depicted in algorithm 3.
Table 2 summarizes the results. The meaning of the columns of Table 2 is the following:

Column 1: number of different colors k.

Column 2: number of nodes n of the specific class of instances.

Column 3: percentage of “happy” vertices w.r.t the total number of nodes of the upper bound provided by CPLEX after 3600 s of run.

Column 4: percentage of “happy” vertices w.r.t the total number of nodes of the lower bound provided by CPLEX after 3600 s of run.

Column 5: average values of the percentage of “happy” vertices given by the CMSA approach after 3600 s—best of 10 executions.

Column 6: average values of the percentage of “happy” vertices given by the proposed \(MHG\) algorithm (bold characters if \(MHG\) is better than CMSA).

Column 7: average maximum CPU time needed to compute the result of \(MHG\) algorithm, in seconds.

Column 8: average values of the percentage of “happy” vertices given by the MS configuration of the proposed algorithm (bold characters if MS is better than \(MHG\)).

Column 9: average maximum CPU time needed to compute the result of MS procedure, in seconds.
As can be seen, the simple proposed approach \(MHG\) outperforms CMSA both in terms of solution quality and CPU effort. We recall that the stopping criterion used in Lewis et al. (2019) is the time limit of 3600 s. Our approach gives better results in about two order of magnitude less CPU time. Moreover with algorithm MS, we gain even more solution quality, largely within the 3600 s limit. These results illustrate the effectiveness of our approach which shows up to improve with respect to the current literature.
To further assess the effectiveness of our approaches, Table 3 is reported. Even if the averages improvements of objectives function values may seem limited, the number of improvements is definitely clear. Here, Columns 1 and 2 are the same as in Table 3, while column 3 explicits the number of instances per class. Columns 4 and 5 are dedicated to enlight the number of instances improved with respect to the CMSA procedure of \(MHG\) and MS algorithms, respectively. As can be seen, apart from one case where the number of improved instances is very limited, \(MHG\) (and consequently MS) approaches consistently improve over CMSA. It is important to note that there are no instances where CMSA is better than any of our approaches. Globally, we could improve 180 out of 300 instances and we point out that our approach is consistently better on the largersize instances. This again confirms the effectiveness of the proposed approach.
Concluding remarks
A simple procedure has been developed to deal with the Maximum Happy Vertices Problem. A starting solution generation obtained thanks to an approximation algorithm is improved via a largescale neighborhood exploration made with an MILP formulation of the problem. The procedure is then extended in a multistart configuration. Both approaches have been tested over 300 instances from the literature and compared with a reference algorithm, namely CMSA from Lewis et al. (2019). Solution quality and very limited running times confirm the effectiveness of our approach which is based on simple elements and shows up to improve with respect to the current literature.
References
Agrawal A (2017) On the parameterized complexity of happy vertex coloring. In IWOCA
Aravind N, Kalyanasundaram S, Anjeneya SK (2016) Linear time algorithms for happy vertex coloring problems for trees. In IWOCA
Ball M (2011) Heuristics based on mathematical programming. Surv Oper Res Manag Sci 16:21–38
Billaut J, Croce F, Grosso A (2015) A single machine scheduling problem with twodimensional vector packing constraints. Eur J Oper Res 243:75–81
Blum C, Pinacho P, LópezIbáñez P, Lozano JA (2016) Construct, merge, solve and adapt a new general algorithm for combinatorial optimization. Comput Oper Res 68:75–88
Croce F, Grosso A, Salassa F (2019) Minimizing total completion time in the twomachine noidle nowait flow shop problem. J Heuristics 1:1–15
Della Croce F, Salassa F (2014) A variable neighborhood search based matheuristic for nurse rostering problems. Ann Oper Res 218:185–199
Della Croce F, Grosso A, Salassa F (2013) Matheuristics: embedding milp solvers into heuristic algorithms for combinatorial optimization problems. Heuristics: theory and applications. Nova Science Publishers, New York, pp 31–52
Della Croce F, Grosso A, Salassa F (2014) A matheuristic approach for the twomachine total completion time flow shop problem. Ann Oper Res 213:67–78
Doi Tsubasa, Nishi Tatsushi, Voß Stefan (2018) Twolevel decompositionbased matheuristic for airline crew rostering problems with fair working time. Eur J Oper Res 267:428–438
Ghirardi M, Amerio A (2019) Matheuristics for the lot sizing problem with backordering, setup carryovers, and nonidentical machines. Comput Ind Eng 127:822–831
Giusy Macrina G, Laporte F. Guerriero, Pugliese L (2019) An energyefficient greenvehicle routing problem with mixed vehicle fleet, partial battery recharging and time windows. Eur J Oper Res 276:971–982
Lewis R, Thiruvady D, Morgan K (2018a) Algorithm source code. http://www.rhydlewis.eu/resources/happyalgs.zip. Accessed date 7 Jun 2021
Lewis R, Thiruvady D, Morgan K (2018b) Problem instance generator. http://www.rhydlewis.eu/resources/happygen.zip. Accessed date 7 Jun 2021
Lewis R, Thiruvady D, Morgan K (2019) Finding happiness: an analysis of the maximum happy vertices problem. Comput Oper Res 103:265–276
Luis FanjulPeyro F, Perea R. Ruiz (2017) Models and matheuristics for the unrelated parallel machine scheduling problem with additional resources. Eur J Oper Res 260:482–493
Malaguti E, Toth P (2010) A survey on vertex coloring problems. Int Trans Oper Res 17:1–34
MartinezSykora A, AlvarezValdés R, Bennell J, Ruiz R, Tamarit JM (2017) Matheuristics for the irregular bin packing problem with free rotations. Eur J Oper Res 258:440–455
Shahmanzari Masoud, Aksen D, Salhi S (2020) Formulation and a twophase matheuristic for the roaming salesman problem: application to election logistics. Eur J Oper Res 280:656–670
Zhang P, Li A (2015) Algorithmic aspects of homophyly of networks. Theor Comput Sci 593:117–131
Zhang P, Xu Y, Li A, Lin G (2018) Improved approximation algorithms for the maximum happy vertices and edges problems. Algorithmica 80:1412–1438
Funding
Open access funding provided by Politecnico di Torino within the CRUICARE Agreement.
Author information
Authors and Affiliations
Corresponding author
Additional information
Publisher's Note
Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Rights and permissions
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecommons.org/licenses/by/4.0/.
About this article
Cite this article
Ghirardi, M., Salassa, F. A simple and effective algorithm for the maximum happy vertices problem. TOP 30, 181–193 (2022). https://doi.org/10.1007/s11750021006104
Received:
Accepted:
Published:
Issue Date:
DOI: https://doi.org/10.1007/s11750021006104
Keywords
 Happy coloring
 Matheuristics
 Local search
Mathematics Subject Classification
 90C27 Combinatorial Optimization
 90C11 Mixed Integer Programming
 90C59 Approximation methods and heuristics in mathematical programming