 # SEAGRP - Editorial

Author: Sergey Nagin
Tester: Mahbub
Editorialist: Jingbo Shang

Medium

### PREREQUISITES:

Matching, Gaussian Elimination

### PROBLEM:

Determine whether there is any perfect matching.

### EXPLANATION:

The problem setting is exactly same as the perfect matching. That is, find a subset of edges, there are no edges have common nodes and all nodes are covered by one edge. Since the graph is general, Blossom algorithm which can provide the maximum matching in O(VE) time is enough.

Here, I would like to introduce a simpler way to solve this problem. Of course, it is not some strange greedy algorithms which may passed because we can’t design cases for all wrong solutions (contestants are always creative :D). Anyway, let me introduce the simple solution – Tutte matrix. We can randomly choose the X*[j]. Since we only need to check whether its determinant is zero, we can module all number during Gaussian Elimination (used to calculate the determinant) by a big prime, such as 10^9 + 7. Due to the randomness, we can solve this problem with a high probability. This solution is O(V^3 log MOD), where MOD is the big prime number we chosen. O(log MOD) is caused by the calculating the inverse or elimination by subtracting.

### AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

15 Likes

Hello,

Thanks for this very interesting problem and editorial… It was a joy to attempt to solve this problem during contest time and it is even better to read editorial and learn that there was a simple solution after all, which even used a standard algorithm.

I’m very interested now in reading more about the Blossom Algorithm and how it works so I can try to adapt it to solve this problem.

Best,

Bruno

I wanted to try Hungarian algorithm in this problem, but first had no time, and then got stucked with step 5, “Cover the zero elements with the minimum number of lines it is possible to cover them with.”, but to do this I have to solve matching problem as a subproblem (recursion?)…

In TopCoder tutorial, there is also:

Find the maximum matching using only 0-weight edges (for this purpose you can use max-flow algorithm, augmenting path algorithm, etc.).

Absolutely confused Any idea someone?

1 Like

Hi,

I tried following strategy (though I never got it correct). Can anybody point out why is this algorithm incorrect? What is wrong?

Before doing anything ensure that total no. of vertex is even otherwise output NO.
Lets work on a single connected component of our graph. The same procedure can be applied to all the components.

1. Find out all the leaf nodes (vertex with degree 1)
2. Now keep on removing the leaf edge one by one. Since, each leaf node(node with degree 1) can only be matched with the node it is allready connected with. So, at each step we will remove 2 nodes from the graph. So, the no. of nodes in the graph will always remain even. (This step may give arise to more leaf nodes. They must also be removed)
3. Repeat step 2 until following conditions are met:
4. If no leaves are left then we either have exhausted the graph or all we are left with a cycle or vertices with degree 0.
5. If the graph is exhausted then we are done(current component is matchable).
6. If any vertex with degree=0 left output NO.
7. If a cycle left then find the length of the cycle, If the length is odd then output NO otherwise the current component is matchable.

Something like this pictorially : This is a very simple example. Here vertex 1 & 8 are the what I called leaf nodes. And edges {(1, 2), (7,8)} are what I called as leaf edges.
Why is this strategy incorrect?

2 Likes

If we try to find if each of the connected components in the given graph has a Hamiltonian Path or not and also check if each of the components have even number of vertices, will this suffice to solve the problem ? Is this approach correct ?

I didn’t use a prime modulus or anything like that. I just chose random integers as variables of the Tutte matrix, and did the GEM on doubles. Chances are small that there’d always be an undetectably non-zero (or zero) solution So I just try this an (insert constant here) amount of times and decide if the determinant can be non-zero.

That being said, this problem would’ve done better in a short contest. It’s hard to make worst-case test data, so wrong/slow solutions could pass, I think. Also, I solved it using Google Search algorithm, where I googled “perfect matching general graph” and got to Tutte matrix in no time. Overall, deciding if there’s a perfect matching in a graph sounds like a standard problem (and therefore solvable with hardly any effort by finding the solution), so it is a bit of a waste.

5 Likes

I think this problem tested google searching skills more than the programming knowledge. I know this was not intended but all one had to do to solve this problem was to figure out that it is a maximum matching problem in general graphs, search the same, find out about blossom algorithm and then search blossom algorithm code. Nevertheless it led me to many new algorithms and concepts. I read in norbert blum’s paper how a O(m*root(n)) complexity algorithm can find maximum matching in a general graph. If anyone solved the problem using this, please share your code.

2 Likes

what i could derive from other contestants solution that they were selecting vertex having minimum size and then marking it,can somebody expain this concept to me as editorial does not clarifies the concept clearly(atleast to me)?

Most of the accepted code used maximum matching for bipartite graph algorithm… But there may be general graph in test case so how they were passed…

I too did a lot of Google searching for this problem but couldn’t clearly follow the blossom algorithm so tried brute forcing and much to my surprise it got accepted. Initially check if the number of vertices are even. If true, then check if any vertex has degree less than one. If all vertices have degree greater than zero, then sort the vertices based on their degrees and then try to find a solution by removing edges. Try this for every possible combination until a certain set of edge removals satisfy.

1 Like

Very interesting paper :

http://web.eecs.umich.edu/~pettie/matching/Lovasz-determinants-matching-1979.pdf

On page 3 :

In fact, results of Zippel  imply that the probability of error is less than m / N.

Thus choosing random values for the matrix between 1 and 1000 * M gives you a probabilistic answer >= 99.999%. 4 Likes

I got away with a recursive brute force solution.
http://www.codechef.com/viewsolution/3173530

i can’t understand ,why is it giving wrong answer…(it runs fine on my compiler)

//www.codechef.com/JAN14/problems/SEAGRP/

#include
using namespace std;
#include<stdio.h>
#define max 100

int main()
{
int t,m,n,i,x,y;
scanf("%d",&t);
while(t–)
{
scanf("%d%d",&n,&m);
int f[max]={0},a[max]={0};
for(i=0 ; i<m ; i++)
{
scanf("%d%d",&x,&y);
f[x]++;
f[y]++;
}
for(i=1 ; i<=n ; i++)
{
a[f*]++;
}
for(i=1 ; i<=n ; i++)
{
if(a*%2 || f*==0)
break;
}
if(i==n+1)
printf("Yes
");
else
printf("No
");
}
return 0;
}

The Hungarian algorithm is for bipartite graphs only (how do you convert a matrix to a multipartite graph, anyway?). And bipartite matching can be solved in O(N^3) using network flows, which is much simpler to code.

My idea was to have 2N vertexes let say v1 and v2 for each v, and there is edge between v1* and v2[j] iff there is edge v* v[j] in original graph…

I had initially thought of an approach exactly like yours. However i later realized that the the graph remaining is guaranteed to have atleast 1 cycle and will not always have exactly 1 cycle.There are many cases in which the remaining graph will consist of multiple cycles joined together for which your algorithm fails.

1 Like

The approach will be correct however finding a Hamiltonian path in a graph is an NP-complete problem so it would have been difficult to get it accepted within the time limits.

understood thanks I’m not trying to find the actual path but only checking if graph is Hamiltonian using Dirac’s Theorem and that is not NP-complete.