10 #ifndef _ZOLTAN2_MatcherHelper_hpp_
11 #define _ZOLTAN2_MatcherHelper_hpp_
16 #ifdef ZOLTAN2ND_HAVE_OMP
48 template <
typename LO>
63 std::vector<LO> matchedVertexForU;
66 std::vector<LO> matchedVertexForV;
68 std::vector<LO> queue;
75 LO numE,avgDegU_,k_star_,icm_,BFSInd_,numThread_,Qst_,Qend_,numMatched;
79 void delete_matched_v();
83 LO construct_layered_graph();
85 LO recursive_path_finder(LO, LO);
86 LO dfs_path_finder(LO);
88 LO augment_matching(LO);
99 Matcher(LO *_rowPtr, LO *_cols, LO _numU, LO _numV, LO _numE);
116 for(
unsigned int i=0;i<matchedVertexForU.size(); i++)
118 if(matchedVertexForU[i]!=-1)
134 std::vector<LO> &bigraphCRSCols,
135 const std::vector<LO> &vertUMatches,
136 const std::vector<LO> &vertVMatches,
137 const std::vector<LO> &bigraphVMapU,
138 const std::vector<LO> &bigraphVMapV,
139 std::vector<LO> &VC);
151 template <
typename LO>
153 :mCRS_rowPtrs(_rowPtr),mCRS_cols(_cols),numU(_numU),numV(_numV),numE(_numE)
156 avgDegU_=numE/numU+1;
157 matchedVertexForU.resize(numU);
158 matchedVertexForV.resize(numV);
160 lookahead_=
new LO[numU];
161 unmatchedU_=
new LO[numU];
163 for(LO i=0;i<numU;i++)
165 matchedVertexForU[i]=-1;
167 lookahead_[i]=mCRS_rowPtrs[i];
171 visitedV_=
new LO[numV];
173 parent_=
new LO[numV];
175 for(LO i=0;i<numV;i++)
179 matchedVertexForV[i]=-1;
190 template <
typename LO>
193 delete [] lookahead_;
194 delete [] unmatchedU_;
198 delete [] parent_; parent_ = NULL;
216 template <
typename LO>
224 t=matchedVertexForU[u];
225 matchedVertexForV[v]=u;
226 matchedVertexForU[u]=v;
245 template <
typename LO>
246 LO Matcher<LO>::dfs_path_finder(LO u)
250 for(i=lookahead_[u];i<mCRS_rowPtrs[u+1];i++)
253 assert(ind>=0 && ind<numV);
254 if(matchedVertexForV[ind]==-1)
257 if (visitedV_[ind]==0)
275 for(i=mCRS_rowPtrs[u];i<mCRS_rowPtrs[u+1];i++)
278 assert(ind>=0 && ind<numV);
281 if (visitedV_[ind]==0)
290 res=dfs_path_finder(matchedVertexForV[ind]);
298 for(i=mCRS_rowPtrs[u+1]-1;i>=mCRS_rowPtrs[u];i--)
301 assert(ind>=0 && ind<numV);
305 if (visitedV_[ind]==0)
314 res=dfs_path_finder(matchedVertexForV[ind]);
412 template <
typename LO>
413 LO Matcher<LO>::dfs_augment()
415 LO i,flag=0,flag1=0,count=0,totc=0,index=numU,cur=0;
430 if(matchedVertexForU[i]==-1)
431 unmatchedU_[cur++]=i;
440 LO ind=dfs_path_finder(u);
444 augment_matching(ind);
448 if(flag==0 || flag1==0)
455 if(matchedVertexForU[unmatchedU_[i]]==-1)
456 unmatchedU_[cur++]=unmatchedU_[i];
506 template <
typename LO>
507 LO Matcher<LO>::SGM()
513 for(j=mCRS_rowPtrs[i];j<mCRS_rowPtrs[i+1];j++)
518 if (visitedV_[ind]==0)
526 matchedVertexForU[i]=ind;
527 matchedVertexForV[ind]=i;
541 template <
typename LO>
542 LO Matcher<LO>::match_dfs()
555 template <
typename LO>
588 template <
typename LO>
590 std::vector<LO> &bigraphCRSCols,
591 const std::vector<LO> &vertSMatches,
592 const std::vector<LO> &vertTMatches,
593 const std::vector<LO> &bigraphVMapU,
594 const std::vector<LO> &bigraphVMapV,
597 LO numS = vertSMatches.size();
598 LO numT = vertTMatches.size();
604 for(LO i=0;i<numS; i++)
606 if(vertSMatches[i]==-1)
618 for (LO uID=0;uID<numS;uID++)
620 for (LO eIdx=bigraphCRSRowPtr[uID];eIdx<bigraphCRSRowPtr[uID+1];eIdx++)
622 LO vID = bigraphCRSCols[eIdx];
624 if (vertSMatches[uID]==vID)
626 bigraphCRSCols[eIdx]=-1;
638 std::queue<LO> vqueue;
640 std::copy(X.begin(), X.end(), std::inserter( L, L.begin() ) );
644 typename std::set<LO>::const_iterator iter;
645 for(iter = X.begin(); iter != X.end(); ++iter)
647 L.insert(bigraphVMapU[*iter]);
653 while(vqueue.size()!=0)
656 LO nstarts=vqueue.size();
658 for (LO i=0; i<nstarts; i++)
660 LO start = vqueue.front();
667 for (LO eIdx=bigraphCRSRowPtr[start];eIdx<bigraphCRSRowPtr[start+1];eIdx++)
669 LO newV = bigraphCRSCols[eIdx];
675 if(L.find(bigraphVMapV[newV])==L.end())
677 L.insert(bigraphVMapV[newV]);
688 LO newU = vertTMatches[start];
691 if(L.find(bigraphVMapU[newU])==L.end())
693 L.insert(bigraphVMapU[newU]);
706 for(LO uID=0;uID<numS;uID++)
709 if(L.find(bigraphVMapU[uID])==L.end())
711 VC.push_back(bigraphVMapU[uID]);
715 for(LO vID=0;vID<numT;vID++)
718 if(L.find(bigraphVMapV[vID])!=L.end())
720 VC.push_back(bigraphVMapV[vID]);
LO match()
Computes the maximum cardinality matching.
void getVCfromMatching(const std::vector< LO > &bigraphCRSRowPtr, std::vector< LO > &bigraphCRSCols, const std::vector< LO > &vertUMatches, const std::vector< LO > &vertVMatches, const std::vector< LO > &bigraphVMapU, const std::vector< LO > &bigraphVMapV, std::vector< LO > &VC)
LO getNumberOfMatchedVertices()
An implementation of the Matcher interface that operates on Epetra matrices and Graphs.
const std::vector< LO > & getVertexUMatches()
const std::vector< LO > & getVertexVMatches()
virtual ~Matcher()
Destructor.
Matcher(LO *_rowPtr, LO *_cols, LO _numU, LO _numV, LO _numE)
Constructor.