質問

I'm solving a matching problem with two vectors of a class

class matching
{
public:
    int n;
    char match;
};

This is the algorithm I'm trying to implement:

int augment(vector<matching> &left, vector<matching> &right)
{
   while(there's no augmenting path)
     if(condition for matching)
        <augment>
  return "number of matching";
}

For the rough matching, if left[i] matches with right[j], then left[i].n = j, left[i].match ='M' , right[j].n = i and right[j].match = 'M' and the unmatched ones have members n = -1 and match = 'U'

While finding the augmenting paths, if one exists for another (i, j), then we change the member match of the one being unmatched from 'M' to 'U' and its n = -1 and the two matched with the augmenting path have their members match changed to 'A' while we change their members n according to their indices.

I don't know if this is the right approach to solving this, this is my first attempt on maximum matching and I've read a lot of articles and watched tutorials online and I can't get my 'code' to function appropriately.

I do not need a code, I can write my code. I just want to understand this algorithm step by step. If someone can give me an algorithm like the one I was trying above, I would appreciate it. Also, if I have been going the wrong direction since, please correct me.

役に立ちましたか?

解決

I am not sure if you are finding the augmenting paths correctly. I suggest the following approach.

  1. Find an initial matching in a greedy way. To obtain this we travel through every vertex in the left side and greedily try to match it with some free (unmatched) vertex on the right side.

  2. Try to find an augmenting path P in the graph. For this we need to do a breadth-first search starting from all the free vertices on the left side and alternating through matched and unmatched edges in the search. (i.e. the second level contains all the right side vertices adjacent to level-1 vertices, the third level contains all the left side vertices that are matched to level-2 vertices, the fourth level contains all the right side vertices adjacent to level-3 vertices etc). We stop the search when we visit a free vertex in any future level and compute the augmenting path P using the breadth-first search tree computed so far.

  3. If we can find an augmenting path P in the previous step: Change the matched and unmatched edges in P to unmatched and matched edges respectively and goto step 2.

  4. Else: The resulting matching obtained is maximum.

This algorithm requires a breadth-first search for every augumentation and so it's worst-case complexity is O(nm). Although Hopcroft-Karp algorithm can perform multiple augmentations for each breadth-first search and has a better worst-case complexity, it seems (from the Wikipedia article) that it isn't faster in practice.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top