OR-Tools  8.1
hungarian.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //
15 // IMPORTANT NOTE: we advise using the code in
16 // graph/linear_assignment.h whose complexity is
17 // usually much smaller.
18 // TODO(user): base this code on LinearSumAssignment.
19 //
20 // For each of the four functions declared in this file, in case the input
21 // parameter 'cost' contains NaN, the function will return without invoking the
22 // Hungarian algorithm, and the output parameters 'direct_assignment' and
23 // 'reverse_assignment' will be left unchanged.
24 //
25 
26 // An O(n^4) implementation of the Kuhn-Munkres algorithm (a.k.a. the
27 // Hungarian algorithm) for solving the assignment problem.
28 // The assignment problem takes a set of agents, a set of tasks and a
29 // cost associated with assigning each agent to each task and produces
30 // an optimal (i.e., least cost) assignment of agents to tasks.
31 // The code also enables computing a maximum assignment by changing the
32 // input matrix.
33 //
34 // This code is based on (read: translated from) the Java version
35 // (read: translated from) the Python version at
36 // http://www.clapper.org/software/python/munkres/.
37 
38 #ifndef OR_TOOLS_ALGORITHMS_HUNGARIAN_H_
39 #define OR_TOOLS_ALGORITHMS_HUNGARIAN_H_
40 
41 #include <vector>
42 
43 #include "absl/container/flat_hash_map.h"
44 
45 namespace operations_research {
46 
47 // See IMPORTANT NOTE at the top of the file.
49  const std::vector<std::vector<double> >& cost,
50  absl::flat_hash_map<int, int>* direct_assignment,
51  absl::flat_hash_map<int, int>* reverse_assignment);
52 
53 // See IMPORTANT NOTE at the top of the file.
55  const std::vector<std::vector<double> >& cost,
56  absl::flat_hash_map<int, int>* direct_assignment,
57  absl::flat_hash_map<int, int>* reverse_assignment);
58 
59 } // namespace operations_research
60 
61 #endif // OR_TOOLS_ALGORITHMS_HUNGARIAN_H_
operations_research::MaximizeLinearAssignment
void MaximizeLinearAssignment(const std::vector< std::vector< double >> &cost, absl::flat_hash_map< int, int > *direct_assignment, absl::flat_hash_map< int, int > *reverse_assignment)
Definition: hungarian.cc:675
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
cost
int64 cost
Definition: routing_flow.cc:130
operations_research::MinimizeLinearAssignment
void MinimizeLinearAssignment(const std::vector< std::vector< double >> &cost, absl::flat_hash_map< int, int > *direct_assignment, absl::flat_hash_map< int, int > *reverse_assignment)
Definition: hungarian.cc:657