unique maximum matching - algorithm

I am trying to use bipartite graph to code my program with following properties:
in each side, there is N vertices
the graph is connected
Now, I wanna add a condition in my code which check if the number of edges is bigger than M, do not allow user to more activities(in a simple sentence print something in that condition) where M is maximum number edges such that it still has a unique maximum matching.
The question is how can I find M?
Any idea will be appreciated
Thanks

if you mean to find maximum m such that there is at least one graph with n vertices and m edges with a unique maximum matching, the answer is (n + 1) * n / 2.
to show that there is at least one graph with this number of edge, consider a graph with vertices x1, x2, .., xn in one part and vertices y1, .. yn in another part. draw an edge between vertex xi and yj iff (i <= j).
to show there can be no more edges, use induction on the number of vertices. first of all we can show if every vertex in the graph is connected to at least two vertex, the graph has at least two different maximum matching. (consider one maximum matching, follow a path from a vertex whose edges alternates between matching edges and non-matching edges, make a circle and reverse all the edges.)
so we know there is one vertex with degree equal to one. remove this vertex and it's neighbor and use induction on the remaining graph.
sorry for bad English.

Related

Graph Theory : Will it stop or not?

I cannot find out how to start with this question:
A graph has n vertices and m edges.No two pairs of vertices can be connected by more than one edge.Rahul starts to play a game:
He changes the edges in following manner -
He selects one vertex, and adds an edge from that vertex towards all other vertex where the edges do not exist.
and at the same time, deletes all pre-existing edges from that vertex.
This game will stop only when their exist a direct edge between every two Vertices. You need to determine whether it is possible to finish this game, or whether this will never happen, no matter what moves he make.
Input : Initial state of graph will be given.
Output :"yes" or "no"
Can someone give a hint on how to start??
1) Order of moves doesn't matter (since you can exchange any two subsequent moves to the same result);
2) Two subsequent changes with the same vertex have zero effect;
3) You can get to final state iff. you can get there changing any vertex no more than once;
4) Any two connected vertices must be both changed or both unchanged, of any two vertices not connected exactly one should be changed.
Take a connected component in the graph. Vertices there should all change or all remain unchanged. If the component isn't fully connected, finishing the game is impossible. If there are at least three connected components, finishing the game is impossible. If there are exactly two fully connected components, all vertices in exactly one of them should be changed.
Answer: the game can be finished if and only if the graph either is already fully connected or consists of two fully connected components. (It's easy to see that when the graph consists of two fully connected components, changing a vertex effectively moves it from one component to another.)
Algorithm to check for the answer: suppose we are given a number of vertices n and then a list of edges in form (a,b) where a and b are vertex numbers from [1,n]. Let vertices be an array of records (num_edges, connected, sample), initialized with (0,k,k) (k is vertex number). Then for each edge (A,B):
increase num_edges for A and B by 1;
if A.sample equals B.sample, go to next edge;
exchange A.connected and B.connected, starting from A.connected vertex set sample to A.sample and follow connected until reaching B; got to next edge.
Finally check that all vertices starting from 1 and following connected have the same num_edges equal to (their number - 1) and all remaining vertices for similar loop. Time should be O(max(n log(n), m)), memory O(n).
A solved graph with n vertices will be a complete graph kn with ½n(n-1) edges.
Flipping the state of a vertex will mean that the vertex becomes disconnected from the graph and there are two disconnected complete sub-graphs K1 and K(n-1) which have contain 0 and ½(n-1)(n-2) edges, respectively.
Flipping the state of other vertices will disconnect each of them from the complete sub-graph containing it and connect it to all the vertices of the other complete sub-graph. So, generally, if there are x vertices that have flipped state then there will be a two complete sub-graphs Kx and K(n-x) with ½x(x-1) and ½(n-x)(n-1-x) edges respectively for a total of m = ½n(n-1) - nx +x(x-1) edges.
If we know m and n then we can solve the quadratic equation to find the number of moves x required to solve the problem:
x = ( n - SQRT( 4m + 2n - n² ) ) / 2
If x is a non-integer then the puzzle is not solvable.
If x is an integer then the puzzle may be solvable in exactly x moves but an additional check is needed to see if there is are two disconnected complete sub-graphs Kx and K(n-x).
Algorithm:
Calculate x; if it is not an integer then the graph is not solvable. [Complexity: O(1)]
Pick a random vertex:
its degree should be either (x-1) or (n-x-1); if it is not then the graph is not solvable.
generate a list of all its adjacent vertices. [Complexity: O(n)]
perform a depth-first (or breadth-first) search from that vertex. [Complexity: O(n+m)]
if the number of edges visited is ½x(x-1) or ½(n-x)(n-1-x) (corresponding to the degree of the original vertex) and no vertices are visited that were not adjacent to the original then the sub-graph is complete and you know the graph is solvable; if either condition is not true then the graph is not solvable.
To be certain you could do the same for the other sub-graph but it is unnecessary.
Examples:
The graph where n=4,m=2 with edges (1,2) and (3,4) is solvable since x = ( 4 - SQRT( 0 ) ) / 2 = 2, an integer, and there are two K2 disconnected sub-graphs.
The graph where n=4,m=3 with edges (1,2), (2,3), (3,4) has x = ( 4 - SQRT( 4 ) ) / 2 = 1, an integer, but there is only one, connected non-complete graph when there should be two disconnected K1 and K3 sub-graphs.

Algorithm for minimum vertex cover in Bipartite graph

I am trying to figure out an algorithm for finding minimum vertex cover of a bipartite graph.
I was thinking about a solution, that reduces the problem to maximum matching in bipartite graph. It's known that it can be found using max flow in networ created from the bip. graph.
Max matching M should determine min. vertex cover C, but I can't cope with choosing the vertices to set C.
Let's say bip. graph has parts X, Y and vertices that are endpoints of max matching edges are in set A, those who are not belong to B.
I would say I should choose one vertex for an edge in M to C.
Specifically the endpoint of edge e in M that is connected to vertex in set B, else if it is connected only to vertices in A it does not matter.
This idea unfortunately doesn't work generally as there can be counterexamples found to my algorithm, since vertices in A can be also connected by other edges than those who are included in M.
Any help would be appriciated.
Kőnig's theorem proof does exactly that - building a minimum vertex cover from a maximum matching in a bipartite graph.
Let's say you have G = (V, E) a bipartite graph, separated between X and Y.
As you said, first you have to find a maximum matching (which can be achieved with Dinic's algorithm for instance). Let's call M this maximum matching.
Then to construct your minimum vertex cover:
Find U the set (possibly empty) of unmatched vertices in X1, ie. not connected to any edge in M
Build Z the set or vertices either in U, or connected to U by alternating paths (paths that alternate between edges of M and edges not in M)
Then K = (X \ Z) U (Y ∩ Z) is your minimum vertex cover
The Wikipedia article has details about how you can prove K is indeed a minimum vertex cover.
1 Or Y, both are symmetrical

Minimal Vertex Cover using Meet-in-the-Middle

I was studying Meet-in-the-Middle algorithm and found the following exercise:
Given a graph of n nodes (n <= 30), find out a set with the smallest number of vertices such that each edge in the graph has at least one node inside the set.
I have no idea how to do that, only hint I got was
complexity O(3^(n/2))
can you explain the idea?
Take out an edge (u1, v1) from the graph, remove all edges that share a vertex with it. Take out another one (u2, v2), ... continue until the rest of the graph has no edges.
You end up with a number of pairs of vertices
(u1, v1), (u2, v2), ..., (uk, vk)
The rest of the vertices are:
w1, w2, ..., wm
Call the first set of vertices paired vertices, and the second set unpaired vertices. Note, 2k + m = n, there are no edges between unpaired vertices in the original graph.
A vertex cover must have either u1, v1, or both in it. There are 3 choices for each pair (uj, vj). Consider all 3^k ways to include the paired vertices to the vertex cover.
For each of those configurations, an unpaired vertex wi is to be included into the cover if and only if at least one of its neighbors is not in the cover (note that each of wi's neighbors are paired vertices so whether they are included is known).
For each of the 3^k selections of paired vertices, include the unpaired vertices according to the above criteria, then verify each edge between paired vertices has an incident vertex from the cover, if so, it is a candidate cover set. Take one of the candidate cover sets of minimum size as output.
Overall complexity of the above algorithm is O(3^(n/2)E) where E is the number of edges in the graph.

Find the maximum number of edges in the graph

There are 'n' vertices and 0 edges of an undirected graph. What can be the maximum number of edges that we can draw such that the graph remains disconnected.
I have made the solution that we can exclude one vertex and can find the maximum number of edges between n-1 vertices of undirected graph, so that the graph still remains disconnected.
which is n(n-1)/2 for n vertices and will be (n-1)(n-2)/2 for n-1 vertices.
Can there be a better solution?
You can resolve this using analysis. Take your idea and generalize it. You divide the n vertices in two groups , of size x and n-x.
Now the number of edges is a function of x, expressed by
f(x)= x(x-1)/2 + (n-x)(n-x-1)/2
f(x) = 1/2(2x^2 - 2nx +n^2 - n)
The value which maximize this function is the partition size you want. If you make calculation you find that it decrease from x=0 to x=n/2, then increase to x=n. As x = 0 or x = n means the graph is collected, you take the next greatest value which is x=1. So your intuition is optimal.
Your solution should be the best solution.
Because any new edge added must have the nth vertex at one end.
If graph can have multi edges, answer is infinity for n>=3.
If it can also contain self-loops, answer is infinity for n>=2,
If none of those holds your solution is correct.

Algorithm to select points from matrix

This seems to be a simple problem but I am stuck at this problem. The problem is that I have a matrix of size X x Y. There may be some points at (i,j). It is, however, not necessary that all locations (i,j) should have a point (i.e. there are < XY points). How should I can select the biggest subset of these points such that no points in the selected subset has same i (row number) or j (column number)
This is the maximum matching problem, which is poly-time solvable. Given an instance of your problem, construct a bipartite graph with nodes a1, …, aX, b1, …, bY, and edges aibj for all (i,j) where there's a point. Take the points corresponding to the edges in a maximum matching.
Construct graph. Points are vertices of graph. Every vertice is connected with all vertices in the same row and same column - edges of graph. Your problem is to select maximum independent set of vertices (without common edges).
Independent_set
This problem is equivalent to to maximum clique problem in complement graph, so Bron-Kerbosch algorithm could be used.
http://en.wikipedia.org/wiki/Clique_problem#Finding_maximum_cliques_in_arbitrary_graphs
http://en.wikipedia.org/wiki/Complement_graph
Bron-Kerbosch algorithm

Resources