Codeforces #236 Div2 - algorithm

Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
the graph contains exactly 2n + p edges;
the graph doesn't contain self-loops and multiple edges;
for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
The task is to find a p-interesting graph consisting of n vertices.
To see the problem statement click here
I can not even understand the tutorial explained here.
If anyone can point me to the theory required for the background or some obscure theorem related to this problem. I'd be very glad.

That's a somewhat muddled editorial. Let's focus on creating 0-interesting graphs first. The key fact from graph theory is the following formula.
sum_{vertices v} degree(v) = 2 #edges
In a graph where every vertex has degree 4 (4-regular graph), the left-hand side is 4n, so the number of edges is exactly 2n. Every n'-vertex subgraph of a 4-regular graph has vertices of degree at most 4, so the left-hand side is at most 4n', and the number of edges is at most 2n'. Thus, every 4-regular graph is 0-interesting. There are many ways to obtain a 4-regular graph; one is to connect vertex i to vertices i - 2, i - 1, i + 1, i + 2 modulo n.
Assuming that n >= 5, the editorial aims to prove that a graph comprised of edges (1, v) and (2, v) for all v from 3 to n and (1, 2) is "(-3)-interesting", which technically doesn't work out because each 1-vertex subgraph should have at most 2(1) - 3 = -1 edges (oops). Since the actual p of interest are nonnegative and there are no self-loops, though, this problem will resolve itself when we add the additional edges as below. For n'-vertex subgraphs with n' >= 2, we consider four cases, two of which are symmetric. The first case is that the subgraph includes neither 1 nor 2. This subgraph has no edges, and n' >= 2 implies that 0 < 2n' - 3. The second case is that the subgraph includes 1 but not 2. This subgraph can have edges from 1 to each of its other vertices, at most n' - 1 <= 2n' - 3 edges. The third case is that the subgraph includes 2 but not 1; it is symmetric to the second case. The fourth case is that the subgraph includes both 1 and 2, in which case it has at most 1 edge between 1 and 2, n' - 2 edges from 1 to other vertices, and n' - 2 edges from 2 to other vertices, for a total of at most 2n' - 3 edges.
For p-interesting graphs, the observation is that, by adding p new edges to a 0-interesting graph, the number of edges in the new graph is 2n + p, as required. The number of edges in each n'-vertex subgraph is the number of old edges plus the number of new edges. The number of old edges is at most 2n', as before. The number of new edges is at most p.

Related

How can I know all possible ways that the edges are connected if I know the toposort in a graph?

How can I know all possible ways that the edges are connected if I know the topological sort?
Here is the original problem:
Now little C has topologically sorted a simple (no heavy edges) directed acyclic graph, but accidentally lost the original. Except for topological sequences, little C only remembers that the original graph had the number of edges k, and that there is one vertex u in the graph that can reach all the other vertices. He wants to know how many simple directed acyclic graphs there are that satisfy the above requirements. Since the answer may be large, you only need to output the remainder of the answer module m.
I have just learned the topological sort. I wonder how I can use it in an upside down way? I know the final toposorted way as (1 2 3 4) and there is one vertex that connects all other vertexes, and there are 4 edges in all, but I need the number of all possible ways that edges are linked.
I think this problem has something to deal with permutation number,and the specific u has to be the first in the toposorted list.
NOTICE the max of m can be up to 200'000,so definitely you can not brute force this problem!!!
Let the topological order be u = 1, 2, …, n. Since 1 can reach all other
vertices, the topological order begins with 1. Each node v > 1, being
reachable from u, must have arcs from one or more nodes < v. These
choices are linked only by the constraint on the number of arcs.
We end up computing Count[v][m] (modulo whatever the modulus is) as
the number of reconstructions on 1, 2, …, v with exactly m arcs. The
answer is Count[n][k].
Count[1][0] = 1 if m == 0 else 0
for v > 1, Count[v][m] = sum for j = 1 to min(m, v-1) of (v-1 choose j)*Count[v-1][m-j]

Find weight of MST in a complete graph of two weight with given edges

I need to get the MST of a complete graph where all edges are defaulted to weight 3, and I'm also given edges that have weight 1.
Here is an example
5 4 (N, M)
1 5
1 4
4 2
4 3
Resulting MST = 3 -> 5 -> 1 -> 4 -> 2
Where the first row has the number of total nodes (N), the amount of 1-weight edges (M) and all of the following (M) rows contain an edge that has weight of 1.
I tried constructing a complete graph and updating weights of given edges to 1, but the spacial complexity is too big for a problem that contains up to 10^5 1-weight edges.
Use Kruskal's algorithm to construct the minimum spanning forest of the graph of only the 1-weight edges. The total weight of the minimum spanning tree of the entire graph will then be the number of edges in the minimum spanning forest (times 1) plus the cost of the edges to connect the trees in the forest (number of trees minus 1 times the weight of 3) plus the cost of one 3-weight edge each to connect the remaining nodes not in the original minimum spanning forest.

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

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.

Code on Weighted, Acyclic Graph

We have a Code on Weighted, Acyclic Graph G(V, E) with positive and negative edges. we change the weight of this graph with following code, to give a G without negative edge (G'). if V={1,2...,n} and G_ij be a weight of edge i to edge j.
Change_weight(G)
for t=1 to n
for j=1 to n
G_i=min G_ij for All K
if G_i < 0 (we have a bar on G)
G_ij = G_ij+G_i for all j
G_ki = G_ki+G_i for all k
We have two axioms:
1) the shortest path between every two vertex in G is the same as G'.
2) the length of shortest path between every two vertex in G is the same as G'.
i read one pdf that has low quality, i'm not sure the code exactly mentioned, and add the picture. in this book he say the above axioms is false, anyone could help me? i think these are true?
i think two is false as following counter example, the original graph is given in left, and after the algorithm is run, the result is in right the shortest path between 1 to 3 changed, it passed from vertex 2 but after the algorithm is run it never passed from vertex 2.
Algorithm
My reading of the PDF is:
Change_weight(G)
for i=i to n
for j=1 to n
c_i=min c_ij for all j
if c_i < 0
c_ij = c_ij-c_i for all j
c_ki = c_ki+c_i for all k
The interpretation is that for each vertex we increase its outgoing edges by c_i, and decrease the incoming edges by c_i, where c_i is chosen such that all outgoing edges become non-negative.
Claim 1
"the shortest path between every two vertex in G is the same as G'"
With my reading of the pdf, this claim is true because every path between vertices i and j is changed by the same amount (c_i-c_j) and so the relative order of paths is unchanged. (Note that the path may go via intermediate vertices, but the net effect is 0 because for each intermediate vertex k we decrease the length by c_k when entering, but increase by c_k when exiting.)
Claim 2
"the length of shortest path between every two vertex in G is the same as G'".
This cannot be true - suppose we start with an original graph which has a single edge A to B with weight -1.
In the modified graph this weight will become 0.
Therefore the length of the shortest path has changed from -1 in G to 0 in G' so the statement is false.
Example
Shown below is what would happen to your graph as you applied this algorithm to node 1, followed by node 2:
Topological sort
Note that as shown in the example, we still end up with some negative weights which is probably unintended. This is because the weights of incoming edges are reduced.
However, if we work backwards through the graph (e.g. by using a topological sort), then we will always end up with non-negative weights everywhere.
In the given example, working backwards means we first update 2, and then 1 as shown below:

count unique pair in graph (vertices repetition not allowed)

Is there any Algorithm exists for counting unique pair (pair of vertices) in undirected graph (vertices repetition not allowed). I think it could be a variation of bipartite graph but if there is any better way to find out .. please comment.
[I think Problem Belongs to Perfect Matching Algorithm]
Problem Statement:
I have an undirected graph which consists of n vertexes and m edges. I can delete edges from the graph. Now I'm interested in one question : is it possible to delete edges in the graph so that the degree of each vertex in the graph will be equal 1.. There can be multiple edges in the graph, but can not be any loops
Example: n = #vertices, m = #edges
n = 4, m = 6
1 2
1 3
1 4
2 3
2 4
3 4
Unique sequence could be (1 2, 3 4) (1 4, 2 3) (1 3, 2 4)
The set of edges that covers the entire graph without using the same vertex multiple times is called a matching or independent edge set, see wikipedia.
In that article is also mentioned that the number of distinct matchings in a graph (which is the number you are after) is called the Hosoya index, see this wikipedia article.
Algorithms to compute this number are not trivial and Stack Overflow wouldn't be the right place to try to explain them, but I at least I hope you have enough pointers to investigate further.
Here is pseudo code, it should run in O(|E|) time, i.e. linear of number of edges :
Suppose G = (V, E) is your initial graph, with E - initial set of all edges
count = 0;
while(E is not empty) {
//1. pick up any edge e = (n1, n2) from E
//2. remove e from G
E = E - e;
//3. calculate number of edges in G remaining if nodes n1 and n2 were removed
// -> these are edges making pair with e
edges_not_connected_to_e = |E| - |n1| - |n2|;
// where |n1| - degree of n1 in updated G (already without edge e)
//4. update the count
count += edges_not_connected_to_e;
}
return count;
Let me know if you need more clarifications. And probably someone could fix my Graph math notations, in case they are incorrect.

Resources