Zoltan2
Zoltan2_GreedyMWM.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #ifndef ZOLTAN2_GREEDYMWM_HPP__
00051 #define ZOLTAN2_GREEDYMWM_HPP__
00052 namespace Zoltan2 {
00053 
00055 //
00056 // Greedy algorithm for maximum-weight matching.
00057 // This is an 1/2-approximation, but requires a sort.
00058 // We could also use the Path Growing Algorithm by
00059 // Drake & Hogardy, which runs in linear time.
00060 // The algorithm runs in serial; the graph must be gathered to 
00061 // the process.
00063 
00064 #include <vector>
00065 #include <algorithm>
00066 
00067 // This struct should be local to GreedyMWM(), but compiler complains.
00068 template <typename vtx_t, typename wgt_t> 
00069 struct GMWM_triplet{
00070   vtx_t i;
00071   vtx_t j;
00072   wgt_t val;
00073 };
00074 
00075 template <typename vtx_t, typename wgt_t>
00076 static bool compare_triplets(GMWM_triplet<vtx_t,wgt_t> a,
00077                              GMWM_triplet<vtx_t,wgt_t> b)
00078 {
00079   return (a.val > b.val); // descending order
00080 }
00081 
00082 
00083 template <typename vtx_t, typename wgt_t>
00084 vtx_t GreedyMWM(
00085   int *idx,         // Index into compressed sparse edge list; 
00086                     // idx[i] is index into adj of first edge for vertex i
00087   vtx_t *adj,       // Edge list in compressed sparse format
00088   wgt_t *wgt,       // weights for each edge
00089   vtx_t tnVtx,      // number of vertices
00090   vtx_t *match      // output result:  vtx i matches with vtx match[i]
00091 )
00092 {
00093   typedef GMWM_triplet<vtx_t, wgt_t> triplet_t;
00094   vtx_t nmatch=0;
00095   std::vector<triplet_t> edges(idx[tnVtx]);
00096 
00097   // Make vector of triplets (edges)
00098   size_t k=0;
00099   for (int i=0; i<tnVtx; i++){
00100     for (int jj=idx[i]; jj<idx[i+1]; jj++){
00101       int j = adj[jj];
00102       if (i<=j){ // We need each edge only once.
00103         edges[k].i = i;
00104         edges[k].j = j;
00105         edges[k].val = wgt[k];
00106       }
00107       k++;
00108     }
00109   }
00110 
00111   // Sort edges by weight
00112   std::sort (edges.begin(), edges.end(), compare_triplets<vtx_t,wgt_t>);
00113 
00114   // Greedy loop over sorted edges
00115   // std::cout << "After sort:" << std::endl;
00116   for (typename std::vector<triplet_t>::iterator it=edges.begin();
00117        it!=edges.end(); ++it){
00118 
00119     // std::cout << "edge (" << it->i << ", " << it->j << ", " 
00120     //           << it->val << ")" << std::endl;
00121 
00122     if ((match[it->i] == it->i) && (match[it->j] == it->j )){
00123       match[it->i] = it->j;
00124       match[it->j] = it->i;
00125       nmatch++;
00126     }
00127   }
00128   return nmatch;
00129 }
00130 }
00131 
00132 
00133 #endif