Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
hungarian.h
Go to the documentation of this file.
1// Copyright 2010-2024 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#include "absl/types/span.h"
45
46namespace operations_research {
47
48// See IMPORTANT NOTE at the top of the file.
50 absl::Span<const std::vector<double>> cost,
51 absl::flat_hash_map<int, int>* direct_assignment,
52 absl::flat_hash_map<int, int>* reverse_assignment);
53
54// See IMPORTANT NOTE at the top of the file.
56 absl::Span<const std::vector<double>> cost,
57 absl::flat_hash_map<int, int>* direct_assignment,
58 absl::flat_hash_map<int, int>* reverse_assignment);
59
60} // namespace operations_research
61
62#endif // OR_TOOLS_ALGORITHMS_HUNGARIAN_H_
In SWIG mode, we don't want anything besides these top-level includes.
void MinimizeLinearAssignment(absl::Span< const std::vector< double > > cost, absl::flat_hash_map< int, int > *direct_assignment, absl::flat_hash_map< int, int > *reverse_assignment)
See IMPORTANT NOTE at the top of the file.
Definition hungarian.cc:655
void MaximizeLinearAssignment(absl::Span< const std::vector< double > > cost, absl::flat_hash_map< int, int > *direct_assignment, absl::flat_hash_map< int, int > *reverse_assignment)
See IMPORTANT NOTE at the top of the file.
Definition hungarian.cc:673