How to solve the following graph game - algorithm

Consider the following game on an undirected graph G. There are two players, a red color player R and a blue color player B. Initially all edges of G are uncolored. The two players alternately color an uncolored edge of G with their color until all edges are colored. The goal of B is that in the end, the blue-colored edges form a connected spanning subgraph of G. A connected spanning subgraph of G is a connected subgraph that contains all the vertexes of graph G. The goal of R is to prevent B from achieving his goal.
Assume that R starts the game. Suppose that both players play in the smartest way. Your task is to find out whether B will win the game.
Input:
Each test case begins with a line of two integers n ( 1 <= n <= 10) and m (0 <= m <= 30), indicating the number of vertexes and edges in the graph. All vertexes are numbered from 0 to n-1.
Then m lines follow. Each line contains two integers p and q ( 0 <= p, q < n) , indicating there is an edge between vertex p and vertex q.
Output:
For each test case print a line which is either "YES" or "NO" indicating B will win the game or not.
Example:
3 4
0 1
1 2
2 0
0 2
Output: Yes
My idea:
If we can find two disjoint spanning trees of the graph, then player B wins the game. Otherwise, A wins.
'Two disjoint spanning trees' means the edge sets of the two trees are disjoint
I wonder if you can prove or disprove my idea

Your idea is correct. Find a proof here:
http://www.cadmo.ethz.ch/education/lectures/FS08/graph_algo/solution01.pdf
If you search for "connectivity game" or "maker breaker games" you should find some more interesting problems and algorithms.

So I think R should follow the following strategy:Find the node with least degree (uncolored edges) (which does not have any Blue colored Edge)
call it N
if degree of N (uncolored edges) is 1 then R wins, bye bye
Find its adjacent nodes {N1,...,Nk}
Pick up M from {N1,...,Nk} such that degree (uncolored) of M (and M does not have any blue colored edge) is the least among the set
Color the edge Connecting from M to N
Repeat this.

Related

Prove that a greedy algorithm for finding minimum spanning tree will definitely stop

This is an algorithm for finding a minimum spanning tree in a connected
UN-directed graph G=(V,E):
Initialization: B = ∅ - The group of edges that will be built by the algorithm
while |B| < |V| - 1 do:
a. choose some cut in the graph (S,V\S) which there isn't an edge e belongs to B that cross it.
b. find the lightest edge crossing that cut.
c. add it to the group B.
B = B ∪ {e}.
return T = (V,B)
The meaning of cut is described in the attached image:
Cut's meaning
the vertices s,u are in one group we can call S.
all the other vertices are in the group V\S.
so this is the meaning of (S,V\S) as a cut.
also - the edge (u,w) is a crossing edge
(u,v) is the lightest crossing edge in that specific cut.
(s,u) is not a "crossing" edge
I need to prove that the algorithm will stop eventually. That |B| = |V| - 1
at some point.
I can use the following say in the proof:
'In any point of the algorithm, there exist a minimum spanning tree T=(V,Et)
of G that contains the group of edges B that were chosen by the algorithm.'
assuming I already proved that, I need to somehow show that there's is always some cut in the graph that none of his crossing edges been added to B yet.
while |B|<|V|-1 .
but I'm not sure how to do that
Let us assume that there is no such cut and |B| < |V| - 1. As the tree is connected, this means that all the vertices are connected by some edge in B (Because if a vertex is not connected, there will be a cut in which no edge belongs to B separating that vertex and the spanning tree)
As, |V| vertices need at least |V| - 1 edges to be connected, we infer that |B| >= |V| - 1, thus contradicting our assumption.
Hence Proved.
No part of this algorithm could loop forever. Every iteration of the step 2 loop increases |B| by 1. |B| will reach |V|-1 in exactly |V|-1 iterations, at which point the loop terminates.
If you're worried about step 2a not being possible, the (V, B) subgraph of G must be disconnected, because it doesn't have enough edges to be connected. We can always put some of its connected components on one side of a cut and some on the other side. (Even if step 2a were to be impossible at some point, that doesn't really sound like a non-termination condition.)

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.

Separate areas algorithm

We are given an N x M rectangular area with K lines in it. Every line has (x0,y0) - (x1,y1), beginning and end coordinates. Are there some well known algorithms or resources to learn that can help me to write a program to find how many separate areas those lines form in the rectangular area?
If this is the original rectangular area : http://prntscr.com/6p9m2c
Then there are 4 separate areas: http://prntscr.com/6p9mo5
All segments with intersections form planar graph. You have to count thoroughly vertices and edges of this graph, then apply Euler's formula
F = E - V + 2
where
V is vertice count - number of intersections (and corners and free segment ends)
E is edge count - number of segments, formed after intersections
F is number of facets.
Your needed region count is
R = F - 1
because F takes into account outer facet.
For your example - there are 16 vertices, 10 horizontal edges and 9 vertical, so
R = 10 + 9 - 16 + 2 - 1 = 4
Note that you can either count vertices with degree 1,2 (corners and free ends) or ignore them together with one neighbour segment (simplify graph) - this doesn't influence to result.
Imagine that the NxM grid is a graph where each '.' is a vertex, and two vertex is connected by an edge if they are side by side (above, below, on the side). Now each separate area corresponds to a connected component of the graph, and you can count the number of connected components in O(N*M) using BFS or DFS algorithms.

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:

Codeforces #236 Div2

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.

Resources