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.
Related
Consider a multigraph G, where the following three reductions need to be made:
Vertices with two neighbors are removed from the graph and their neighbors joined to each other via a new edge.
Vertices with one neighbor are removed from the graph.
Duplicate edges are removed from the graph.
This is a homework question that I had on a recent assignment, where I am asked to show that these three reductions can be done in O(m+n) time. Any help to better understand how to go about doing this is greatly appreciated. Thanks!
This reduction isn't unique: consider a graph with two vertices and one edge, v-w, which has two possible reductions. I will explain how to get an arbitrary valid reduction.
You'll first want to remove duplicate edges: this can be done using a set or a hash-table to identify duplicates, in O(n+m) time. I'll assume you're storing the graph as a dictionary from vertices to their adjacency sets.
After this, you'll want to iterate over the vertices, and keep a set (or any container with O(1) membership testing) to store 'to be deleted' vertices. After this first pass over vertices, this will contain any vertices with degree 1 or 2.
Now, while your 'to be deleted' set isn't empty, you'll:
Pop a vertex v from the set.
If v has degree 0, ignore it.
If v has degree 1 and its neighbor is w, delete v from your graph and remove v from w's adjacency set. If w now has degree 1 or 2 and isn't in the 'to be deleted' set, add it to the set.
Otherwise, v has degree 2, and two distinct neighbors u, w.
If u and w are not adjacent: add an edge from u to w, remove v and its edges from your graph.
If u and w are adjacent: remove v and its edges from your graph. If u or w now have degree 1 or 2, add them to the 'to be deleted' set.
This does constant work per vertex and edge, but relies upon a certain graph representation of 'adjacency sets' where edges can be deleted in constant time. Converting to and from this representation, given adjacency lists or a list of edges, can be done in O(m+n) time.
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
We have a bipartite graph, where set A have n vertices and set B have n vertices.
Also each vertices in set A have k edges to set B and each vertices in set B have k edges to set A.
There is a special vertex s that has edges to all vertices to set A, and a special vertex t that has edges to all vertices in B.
How can I prove that there are k edge distinct paths from s to t?
The problem that I am facing is that it asks given the graph mentioned above(Minus the vertices s and t), I need to prove that if at each round I remove all edges from A to B in a way that I can’t remove more than 1 edge from same vertices, there is a way to do this removal so that A and B will become disconnected in k rounds.
Also each vertices in set A have k edges to set B and each vertices in
set B have k edges to set A.
=> There exist at least k vertices in A and there exist at least k vertices in B. (I)
Now we use:
There is a special vertex s that has edges to all vertices to set A,
and a special vertex t that has edges to all vertices in B.
(which we'll call (II)) to show there must be at least k edge disjoint path from s to t.
Consider the following removal-process:
Go from s to a vertex v_a in A.
Go from v_a to a vertex v_bin B.
Go from v_b to t.
Remove all the edges along this path (to make sure we are not reusing them later on)
Note: one such removal round corresponds to exactly a path from s to t.
Now: we can repeat this removal-process at least k times. Why?
Because after k-1 rounds, there must remain at least one vertex v_a_last in A because of (I). This vertex can be reached from s because of (II). This vertex v_a_last must have at least one adjacent vertex v_b_last in B which we have not come along yet (v_a_last has k neighbors in B but we have come across at most k-1 of them so far since we have only made k-1 removal-rounds so far). Since we haven't come along v_b_last so far, the edge from v_b_last to t must still be in the graph. Hence in round k we can go from s to v_a_last to v_b_last to t which is the k-th edge-disjoint paths from s to t.
I need to find maximum number of pairs of connected edges in a graph such that each pair is separated from every other pair by at least two edges. This could be seen as the maximum matching without the constraint of covering all edges where each component in the alternating path is with length 2.
Clarification of terms:
Connected pairs: Pairs of edges must be in the same connected component.
Connected pairs: Two paired edges do not necessarily need to share a vertex.
Each pair is separated by at least two edges: Given pairs [(u1, v1), (u2, v2)] and [(u3, v3), (u4, v4)], the minimum distance between u ∈ {u1, v1, u2, v2} and v ∈ {u3, v3, u4, v4} is no less than two?
Each pair is separated by at least two edges: Given pairs [(u1, v1), (u2, v2)] and [(u3, v3), (u4, v4)], the minimum distance between, say u1 and u2 can be anything, including zero (the same vertex)?
The straightforward way to do it would be to create a new graph G' in which you have a vertex for every pair of connected edges in G, and an edge connects two vertices in G' whenever the corresponding edge pairs in G are less than 2 edges apart. You could then look for a maximum independent set in G'.
This is far from ideal, since G' will be huge and independent set is NP-complete. If you can show that G' has some special structure, you might be able to do much better though. E.g., if it is necessarily bipartite, then König's theorem will let you find a maximum independent set in polynomial time using maximum matching. (This will still be very slow in practice, as there could be O(m^2) vertices...)
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.