Zoltan2  Version of the Day
Zoltan2_GreedyMWM.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef ZOLTAN2_GREEDYMWM_HPP__
51 #define ZOLTAN2_GREEDYMWM_HPP__
52 namespace Zoltan2 {
53 
55 //
56 // Greedy algorithm for maximum-weight matching.
57 // This is an 1/2-approximation, but requires a sort.
58 // We could also use the Path Growing Algorithm by
59 // Drake & Hogardy, which runs in linear time.
60 // The algorithm runs in serial; the graph must be gathered to
61 // the process.
63 
64 #include <vector>
65 #include <algorithm>
66 
67 // This struct should be local to GreedyMWM(), but compiler complains.
68 template <typename vtx_t, typename wgt_t>
69 struct GMWM_triplet{
70  vtx_t i;
71  vtx_t j;
72  wgt_t val;
73 };
74 
75 template <typename vtx_t, typename wgt_t>
78 {
79  return (a.val > b.val); // descending order
80 }
81 
82 
83 template <typename vtx_t, typename wgt_t>
84 vtx_t GreedyMWM(
85  int *idx, // Index into compressed sparse edge list;
86  // idx[i] is index into adj of first edge for vertex i
87  vtx_t *adj, // Edge list in compressed sparse format
88  wgt_t *wgt, // weights for each edge
89  vtx_t tnVtx, // number of vertices
90  vtx_t *match // output result: vtx i matches with vtx match[i]
91 )
92 {
93  typedef GMWM_triplet<vtx_t, wgt_t> triplet_t;
94  vtx_t nmatch=0;
95  std::vector<triplet_t> edges(idx[tnVtx]);
96 
97  // Make vector of triplets (edges)
98  size_t k=0;
99  for (int i=0; i<tnVtx; i++){
100  for (int jj=idx[i]; jj<idx[i+1]; jj++){
101  int j = adj[jj];
102  if (i<=j){ // We need each edge only once.
103  edges[k].i = i;
104  edges[k].j = j;
105  edges[k].val = wgt[k];
106  }
107  k++;
108  }
109  }
110 
111  // Sort edges by weight
112  std::sort (edges.begin(), edges.end(), compare_triplets<vtx_t,wgt_t>);
113 
114  // Greedy loop over sorted edges
115  // std::cout << "After sort:" << std::endl;
116  for (typename std::vector<triplet_t>::iterator it=edges.begin();
117  it!=edges.end(); ++it){
118 
119  // std::cout << "edge (" << it->i << ", " << it->j << ", "
120  // << it->val << ")" << std::endl;
121 
122  if ((match[it->i] == it->i) && (match[it->j] == it->j )){
123  match[it->i] = it->j;
124  match[it->j] = it->i;
125  nmatch++;
126  }
127  }
128  return nmatch;
129 }
130 }
131 
132 
133 #endif
static bool compare_triplets(GMWM_triplet< vtx_t, wgt_t > a, GMWM_triplet< vtx_t, wgt_t > b)
vtx_t GreedyMWM(int *idx, vtx_t *adj, wgt_t *wgt, vtx_t tnVtx, vtx_t *match)