00001 /* 00002 * munkres.h 00003 * Hungaraian Assignment Algorithm 00004 * 00005 * Authored by Ryan Rigdon on [May15]. 00006 * Copyright 2008 M. Ryan Rigdon 00007 * mr.rigdon@gmail.com 00008 * 00009 */ 00010 00011 //This file is part of The Kuhn-Munkres Assignment Algorithm. 00012 // 00013 //The Kuhn-Munkres Assignment Algorithm is free software: you can redistribute it and/or modify 00014 //it under the terms of the GNU General Public License as published by 00015 //the Free Software Foundation, either version 3 of the License, or 00016 //(at your option) any later version. 00017 // 00018 //The Kuhn-Munkres Assignment Algorithm is distributed in the hope that it will be useful, 00019 //but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 //GNU General Public License for more details. 00022 // 00023 //You should have received a copy of the GNU General Public License 00024 //along with Foobar. If not, see <http://www.gnu.org/licenses/>. 00025 00026 00027 #ifndef MUNKRES_H 00028 #define MUNKRES_H 00029 00030 #include <iostream> 00031 #include <vector> 00032 00033 //struct for the cells of the matrix 00034 struct cell 00035 { 00036 //The weight value in the cell 00037 int weight; 00038 //Values for whether the cell is primed or starred 00039 bool starred, primed; 00040 00041 //initialize starred and primed values to false 00042 cell() 00043 { 00044 starred = false; 00045 primed = false; 00046 weight = -1; 00047 } 00048 }; 00049 00050 //It's an ordered_pair, not much else to say 00051 struct ordered_pair 00052 { 00053 int row, col; 00054 }; 00055 00056 class munkres 00057 { 00058 public: 00059 munkres(void); 00060 ~munkres(void); 00061 00062 //turn diagnostics on or off 00063 void set_diag(bool a) 00064 { 00065 diag_on = a; 00066 } 00067 00068 //assign a matching 00069 //returns the total weight of the matching 00070 //takes a pointer to integer as a parameter and this 00071 //will be the matching in the form of an array 00072 int assign(ordered_pair *matching); 00073 00074 //Load weight array from a vector of vectors of integers 00075 //Accepts an object of type vector< vector<int> >, which is 00076 //a matrix of any dimensions with integer values > -1 00077 void load_weights(std::vector<std::vector<int> > x); 00078 00079 private: 00080 00081 //Delimiters to show number of operable rows and columns 00082 //after the cell array is populated 00083 int num_rows; 00084 int num_columns; 00085 00086 //arrays to keep track of which columns and rows have 00087 //starred zeroes 00088 std::vector<bool> row_starred, column_starred; 00089 00090 //array to show which rows and columns are covered 00091 std::vector<bool> row_cov, column_cov; 00092 00093 //A boolean value to turn daignostics on or off 00094 bool diag_on; 00095 00096 //Initialize all variables for operations 00097 void init(void); 00098 00099 //The matrix operated on by Munkres' algorithm 00100 //(could be better than an array in the future) 00101 std::vector<std::vector<cell> > cell_array; 00102 00103 //array to store the weights for calculating total weight 00104 std::vector<std::vector<int> > weight_array; 00105 00106 //functions to check if there is a starred zero in the current row or column 00107 int find_star_column(int c); 00108 int find_star_row(int r); 00109 00110 //functions to check if there is a primed zero in the current row or column 00111 int find_prime_column(int c); 00112 int find_prime_row(int r); 00113 00114 //These are the declarations for Munkres' algorithm steps 00115 void step1(void); 00116 void step2(void); 00117 bool step3(void); 00118 bool step4(void); 00119 bool step5(int, int); 00120 bool step6(); 00121 00122 //The function that will call each of step of Munkres' algorithm in turn 00123 //We're using this since multiple functions use the algorithm 00124 bool find_a_matching(void); 00125 00126 //A function simply for diagnostic purposes 00127 //Useful for testing new code and to help both myself and anyone who 00128 //wants to modify this in the future 00129 void diagnostic(int a) const; 00130 }; 00131 00132 #endif // MUNKRES_H