How to find maximal subgraph of bipartite graph with valence constraint? - algorithm

I have a bipartite graph. I'll refer to red-nodes and black-nodes of the respective disjoint sets.
I would like to know how to find a connected induced subgraph that maximizes the number of red-nodes while ensuring that all black nodes in the subgraph have new valences less than or equal to 2. Where "induced" means that if two nodes are connected in the original graph and both exist in the subgraph then the edge between them is automatically included. Eventually I'd like to introduce non-negative edge-weights.
Can this be reduced to a standard graph algorithm? Hopefully one with known complexity and simple implementation.
It's clearly possible to grow a subgraph greedily. But is this best?

I'm sure that this problem belongs to NP-complete class, so there is no easy way to solve it. I would suggest you using constraint satisfaction approach. There are quite a few ways to formulate your problem, for example mixed-integer programming, MaxSAT or even pseudo-boolean constraints.
For the first try, I would recommend MiniZinc solver. For example, consider this example of defining and solving graph problems in MiniZinc.

Unfortunately this is NP-hard, so there are probably no polynomial-time algorithms to solve it. Here is a reduction from the NP-hard problem Independent Set, where we are given a graph G = (V, E) (with n = |V| and m = |E|) and an integer k, and the task is to determine whether it is possible to find a set of k or more vertices such that no two vertices in the set are linked by an edge:
For every vertex v_i in G, create a red vertex r_i in H.
For every edge (v_i, v_j) in G, create the following in H:
a black vertex b_ij,
n+1 red vertices t_ijk (1 <= k <= n+1),
n black vertices u_ijk (1 <= k <= n),
n edges (t_ijk, u_ijk) (1 <= k <= n)
n edges (t_ijk, u_ij{k-1}) (2 <= k <= n+1)
the three edges (r_i, b_ij), (r_j, b_ij), and (t_ij1, b_ij).
For every pair of vertices v_i, v_j, create the following:
a black vertex c_ij,
the two edges (r_i, c_ij) and (r_j, c_ij).
Set the threshold to m(n+1)+k.
Call the set of all r_i R, the set of all b_ij B, the set of all c_ij C, the set of all t_ij T, and the set of all u_ij U.
The general idea here is that we force each black vertex b_ij to choose at most 1 of the 2 red vertices r_i and r_j that correspond to the endpoints of the edge (i, j) in G. We do this by giving each of these b_ij vertices 3 outgoing edges, of which one (the one to t_ij1) is a "must-have" -- that is, any solution in which a t_ij1 vertex is not selected can be improved by selecting it, as well as the n other red vertices it connects to (via a "wiggling path" that alternates between vertices in t_ijk and vertices in u_ijk), getting rid of either r_i or r_j to restore the property that no black vertex has 3 or more neighbours in the solution if necessary, and then finally restoring connectedness by choosing vertices from C as necessary. (The c_ij vertices are "connectors": they exist only to ensure that whatever subset of R we include can be made into a single connected component.)
Suppose first that there is an IS of size k in G. We will show that there is a connected induced subgraph X with at least m(n+1)+k red nodes in H, in which every black vertex has at most 2 neighbours in X.
First, include in X the k vertices from R that correspond to the vertices in the IS (such a set must exist by assumption). Because these vertices form an IS, no vertex in B is adjacent to more than 1 of them, so for each vertex b_ij, we may safely add it, and the "wiggling path" of 2n+1 vertices beginning at t_ij1, into X as well. Each of these wiggling paths contains n+1 red vertices, and there are m such paths (one for each edge in G), so there are now m(n+1)+k red vertices in X. Finally, to ensure that X is connected, add to it every vertex c_ij such that r_i and r_j are both in X already: notice that this does not change the total number of red vertices in X.
Now suppose that there is a connected induced subgraph X with at least m(n+1)+k red nodes in H, in which every black vertex has at most 2 neighbours in X. We will show that there is an IS in G of size k.
The only red vertices in H are those in R and those in T. There are only n vertices in R, so if X does not contain all m wiggly paths, it must have at most (m-1)(n+1)+n = m(n+1)-1 red vertices, contradicting the assumption that it has at least m(n+1)+k red vertices. Thus X must contain all m wiggly paths. This leaves k other red vertices in X, which must be from R. No two of these vertices can be adjacent to the same vertex in B, since that B-vertex would then be adjacent to 3 vertices: thus, these k vertices correspond to an IS in G.
Since a YES-instance of IS implies a YES-instance to the constructed instance of your problem and vice versa, the solution to the constructed instance of your problem corresponds exactly to the solution to the IS instance; and since the construction is clearly polynomial-time, this establishes that your problem is NP-hard.

Related

How to calculate the expected value of random graph generation

Hello this is my first question. I met a homework in algorithm and probability that I can't find a clue to calculate.
Question:
Computing Number of Triangles in a Graph: Given an undirected graph G = (V, E), a triangle in G is a clique of size 3 (formally, a set of nodes {u, v, w} is a triangle in G if (u, v), (v, w), (u, w) are all edges of G). Consider the following algorithm for approximating the number of triangles in a graph. First construct a sampled graph G' = (V, E') as follows. The vertex set of G' is same as that of G. For every e ∈ E, put e in E' with probability p (think of p as, say, 0.1). In this new sampled graph G', count the number of triangles and let T' be the number of triangles in G' (assume that you have given a black box subroutine to count the number of triangles in G' ). Then output T̃= T'/p.
Show that the expected value of T̃=T ,T is the triangle number of original graph G.
I am confusing that the edge in G or G' to form a triangle is not independent since two adjacent triangles in G might share the edge. And not the all the pair of vertices in G can form a edge in G', only those edges are in G will be present in G' with p. It's hard for me to think of the relationship of number of edges and number of triangles in G or G'.
Hope someone can give me some hints, even not the whole solution is OK.
the edge in G or G' to form a triangle is not independent since two adjacent triangles in G might share the edge
Doesn't matter. The sum of expectations is the expectation of the sum regardless of correlation, so you can reason about the triangles individually. (Higher moments, were you concerned about analyzing the estimation quality of this algorithm, would be trickier.)

How to update MST from the old MST if one edge is deleted

I am studying algorithms, and I have seen an exercise like this
I can overcome this problem with exponential time but. I don't know how to prove this linear time O(E+V)
I will appreciate any help.
Let G be the graph where the minimum spanning tree T is embedded; let A and B be the two trees remaining after (u,v) is removed from T.
Premise P: Select minimum weight edge (x,y) from G - (u,v) that reconnects A and B. Then T' = A + B + (x,y) is a MST of G - (u,v).
Proof of P: It's obvious that T' is a tree. Suppose it were not minimum. Then there would be a MST - call it M - of smaller weight. And either M contains (x,y), or it doesn't.
If M contains (x,y), then it must have the form A' + B' + (x,y) where A' and B' are minimum weight trees that span the same vertices as A and B. These can't have weight smaller than A and B, otherwise T would not have been an MST. So M is not smaller than T' after all, a contradiction; M can't exist.
If M does not contain (x,y), then there is some other path P from x to y in M. One or more edges of P pass from a vertex in A to another in B. Call such an edge c. Now, c has weight at least that of (x,y), else we would have picked it instead of (x,y) to form T'. Note P+(x,y) is a cycle. Consequently, M - c + (x,y) is also a spanning tree. If c were of greater weight than (x,y) then this new tree would have smaller weight than M. This contradicts the assumption that M is a MST. Again M can't exist.
Since in either case, M can't exist, T' must be a MST. QED
Algorithm
Traverse A and color all its vertices Red. Similarly label B's vertices Blue. Now traverse the edge list of G - (u,v) to find a minimum weight edge connecting a Red vertex with a Blue. The new MST is this edge plus A and B.
When you remove one of the edges then the MST breaks into two parts, lets call them a and b, so what you can do is iterate over all vertices from the part a and look for all adjacent edges, if any of the edges forms a link between the part a and part b you have found the new MST.
Pseudocode :
for(all vertices in part a){
u = current vertex;
for(all adjacent edges of u){
v = adjacent vertex of u for the current edge
if(u and v belong to different part of the MST) found new MST;
}
}
Complexity is O(V + E)
Note : You can keep a simple array to check if vertex is in part a of the MST or part b.
Also note that in order to get the O(V + E) complexity, you need to have an adjacency list representation of the graph.
Let's say you have graph G' after removing the edge. G' consists have two connected components.
Let each node in the graph have a componentID. Set the componentID for all the nodes based on which component they belong to. This can be done with a simple BFS for example on G'. This is an O(V) operation as G' only has V nodes and V-2 edges.
Once all the nodes have been flagged, iterate over all unused edges and find the one with the least weight that connects the two components (componentIDs of the two nodes will be different). This is an O(E) operation.
Thus the total runtime is O(V+E).

Merge most of the black vertices of DAG together so that it remains DAG?

I have a DAG ( Directed Acyclic Graph ) with vertices having any of the 2 colours black or white. I need to merge as many black vertices together with the constraint that the graph should remain acyclic. Hence the final DAG should have minimum no. of black vertices. What is the best algorithm for this problem?
Here is one possible strategy. It reduces your problem to a colouring problem (which you can then use established heuristics algorithm from literature to solve).
Call the DAG G = (V,E) where V is the set of vertices. Let B be the set of black vertices and W be the set of white vertices. We want to construct a new simple graph G' = (B,E'). We construct it as follow:
algorithm contruct G' input: G
Let G' be a graph with vertex set B and no edges
for any pair of vertices v and v' where v,v' in B:
Let (G'', v'') = merge (v,v',G)
#comment: here, we let G'' to be the graph resulted from merging v and v'
#also, let's assume that v and v' merge to become v''
if detect_cycle(G'',v'') = true:
add edge (v,v') into G'
output G'
algorithm detect_cycle(G,v):
do BFS in G starting at v, with the modification when reaching any vertex v':
if v is connected to v': return true
return false
Note that G' is a simple graph and not a DAG and when doing BFS on G, you cannot go against the direction of an edge in G.
Essentially, we try to build G' with the set of black vertices in G such that if two vertices v adjacent to v' in G', then merging them causes cyclic graph in G. If v is not adjacent to v' in G' then it's safe to merge them. The problem then got reduced to find the minimum number of colors required to vertex-color G'. For background on vertex colouring, check out this link: https://en.wikipedia.org/wiki/Graph_coloring#Vertex_coloring. Basically vertex coloring is about finding the minimum number of sets where in each set, you can put in pairwise-nonadjacent vertices, then assign a label (or color) to each set (every vertex in the same set get the same label). Every black vertex with the same label in G' could be merged in G.
Heuristic algorithms for graph colouring could be found here:
http://heuristicswiki.wikispaces.com/Graph+coloring
and here: http://heuristicswiki.wikispaces.com/Degree+based+ordering
I hope it helps. Let me know if you find a better solution or a bug in the above solution.
Let the graph be G = (V,E)
Topological sort the graph to get the list of vertices = L(V).
L(B) = list of black vertices extracted from L(V) with the order maintained.
Let n = no. of vertices in L(B).
Let DVA = empty array of deleted vertices of size n initialized with 0.
for i = vertices 1 to n in L(B)
if(DVA[i] == 1)
continue;
for j = vertices i+1 to n in L(B)
if(DVA[j] == 1)
continue;
if(detect_cycle(G, i, j) == 0) //merging i and j will not create cycle
Merge j to i in G;
DVA[j] = 1;
This algorithm works on the fact that topological order of black vertices do not change while merging 2 vertices (except for these 2 vertices) .
I guess this method will produce fairly good result, but I am not sure whether it will produce the optimal result of having least no. of black vertices.

How can I get the antichain elements in SPOJ-DIVREL?

Problem: http://www.spoj.com/problems/DIVREL
In question, we just need to find the maximum number of elements which are not multiples (a divisible by b form) from a set of elements given. If we just make an edge from an element to its multiple and construct a graph it will be a DAG.
Now the question just changes to finding the minimum number of chains which contain all the vertices which equals the antichain cardinality using Dilworth's theorem as it is a partially ordered set.
Minimum chains can be found using bipartite matching (How: It is minimum path cover) but now I am unable to find the antichain elements themselves?
To compute the antichain you can:
Compute the maximum bipartite matching (e.g. with a maximum flow algorithm) on a new bipartite graph D which has an edge from LHS a to RHS b if and only if a divides b.
Use the matching to compute a minimal vertex cover (e.g. with the algorithm described in the proof of Konig's theorem
The antichain is given by all vertices not in the vertex cover
There cannot be an edge between two such elements as otherwise we would have discovered an edge that is not covered by a vertex cover resulting in a contradiction.
The algorithm to find the min vertex cover is (from the link above):
Let S0 consist of all vertices unmatched by M.
For integer j ≥ 0, let S(2j+1) be the set of all vertices v such that v is adjacent via some edge in E \ M to a vertex in S(2j) and v has not been included in any
previously-defined set Sk, where k < 2j+1. If there is no such vertex,
but there remain vertices not included in any previously-defined set
Sk, arbitrarily choose one of these and let S(2j+1) consist of that
single vertex.
For integer j ≥ 1, let S(2j) be the set of all vertices u
such that u is adjacent via some edge in M to a vertex in S(2j−1). Note
that for each v in S(2j−1) there is a vertex u to which it is matched
since otherwise v would have been in S0. Therefore M sets up a
one-to-one correspondence between the vertices of S(2j−1) and the
vertices of S(2j).
The union of the odd indexed subsets is the vertex cover.

Reducing from Vertex Cover to prove NP-complete

We define ROMAN-SUBSET as the following problem:
INPUT: Directed graph G = ( V , E ) and a positive integer k
OUTPUT: If there is a subset R of V such that | R | <= k , and
such that every directed circuit in G includes at least one vertex
from R , then the output should be "TRUE", otherwise, it should be
"FALSE".
Assuming that the Vertex Cover (VC) problem is NP-complete, I must prove that ROMAN-SUBSET is also NP-complete. From what I understand, that means taking the VC input, modifying it, and then showing that plugging it into the ROMAN-SUBSET algorithm will yield the result of the VC problem.
I'm having a really tough time coming up with the transformation. I know that the VC input is a graph G and an integer k, and the problem is whether or not there exists a subset R of V that covers every edge in G, such that |R| <= k. So clearly, the R and k are similar from ROM to VC, but my difficulty is identifying how to transform the graph so that 1 vertex in every directed cycle (for ROM) corresponds to every edge (for VC). How can I go about modifying the graph to prove that VC can be reduced to ROM?
Thanks!
Here is the construction.
Take undirected graph G = (V, E) as in VC.
Now define the directed graph G1 = (V, E1), where for every edge (u,v) in E there are two edges (u,v) and (v,u) in E1.
In other words the new graph is the same as the old one, but every undirected edge is replaced with two directed edges that form a 2-cycle.
The claim is that from ROM on G1 follows VC on G.
Indeed, suppose that the answer for ROM on G1 is FALSE. Then for every choice of a set of less than k vertices there exists a cycle not in this set. So there exists an edge whose endpoints are not in the set. But this means that for the same choice of the set of less than k vertices in G there exists an edge whose endpoints are not in the set, so VC's answer is FALSE.
Conversely, suppose that the answer for ROM on G1 is TRUE. Then there exists a subset of V containing less than k vertices, so that given any cycle there exists at least one vertex in the cycle, which is in the set. But this means that for any edge in E one of its endpoints in in the set, because an edge in E corresponds to a 2-cycle in E1. Thus the answer for VC is TRUE.

Resources