Given a set of nodes A and a set of nodes B, where each node in A are connected to only one node in B. Now I want to select a minimum set of nodes in A that is connected to every nodes in B, while every selected node cannot be connected to each other.
I believe there is a certain way of solving this problem maybe with matching, independent set or whatever.
Although I haven't been able to prove that my algorithm is correct, I'm still putting it here.
If the nodes in A didn't have edges between them, then this would be a bipartite graph matching problem. Right now this has become a bipartite graph matching problem with the constraint the if some node in A is selected, it may disqualify some other nodes in A.
For ease of visualisation, imagine the set of A on one sides and B on the other side, similar to the visuals drawn for Bipartite matching. Now, add another set of nodes C between A and B. The condition for adding a node in C are as follows:
If there is a node in A (say a) with no edges in A, create a new node in C (say c), join a to c, and join all the corresponding nodes of a in B to c.
If there are two nodes in A (a1 and a2) which share an edge, create a new node in C (c) and create edges from a1 to c, a2 to c, and from c to all corresponding nodes in B.
Now we can use the Max-Flow algorithm to get the desired output. We should set the node capacities of each node in C to be 1 (the way to achieve this is by creating another intermediate layer D which is a copy of C, with edge capacities equal to 1 for edges between C and D)
Any help in making the proof rigorous or any counter examples would be helpful.
Related
In a group of friends, each except one friend spies exactly one other friend. Every friend has some valuables, which is a positive integer. Find a group of friends with the biggest sum of valuables such that no friend spies any other friend within this group.
Example: We have the following graph for one of the possible test cases. The value above each vertex is the positive number of valuables owned by them.
The best possible group is [A,F,D,J,H] = 92 value
Looks like we can achieve the solution by ignoring the traversal through the graph and calculating the combinations of all possible groups.
Unfortunately not able to think of a dynamic programming approach or how to get started.
Give the constraints on your graph, you will always have:
one disjoint connected sub-graph that is a tree consisting of zero-or-more simple paths all branching off the friend who is not spying on anyone; and
zero-or-more disjoint connected sub-graphs where each sub-graph contains exactly one cycle and zero-or-more simple paths branching off the cycle.
To get the best sum of valuables:
Within each branching path, there can be at most two non-selected vertices between each selected vertex. (If there were three non-selected vertices then you would get a better result if the middle vertex of those three were selected; i.e. (A)->B->C->D->(E) where () is a selected vertex will always give a better result as (A)->B->(C)->D->(E)).
Within each cycle, unless blocked by a selected adjacent vertex in a branching path, there can be at most two non-selected vertices between each selected vertex. (for similar reasons to branching paths).
If you have the connected sub-graph (similar to the bottom of your example but E spies I, rather than spying nothing):
-----------
V |
C -> D -> E -> I -> J <- H
Then you can either start with the cycle and work outwards to the branching paths or from the branching paths inwards. If you consider the cycle-outwards then:
if E is selected then D, I and J cannot be and given that the minimum separation of 1 is achieved, then C and H must be selected.
if I is selected then E and J cannot be selected and H and either C or D can be selected.
if J is selected then E, I and H cannot be selected and either C or D can be selected.
if none of the vertices on the cycle are selected then H and either C or D can be selected (which, in this case, is always a strictly worse option that selecting J as well, since it does not block selection from the branching paths).
This gives possible solutions for that connected sub-graph of:
C, E, H
C, I, H
D, I, H
C, J
D, J
You can perform a similar operation for each disjoint subgraph and, for the one disjoint sub-graph containing the friend who is not spying on anyone then that friend can either be selected or not selected and each branching simple path can be evaluated independently choosing to either skip one-or-two vertices between selected vertices and finding the optimum total.
Find the solution with the highest value for the sub-graph and move on to the next sub-graph.
I'm studying random contraction algorithm and could not understand how it works.
In the course it says that a graph G=(V,E) with n vertices, m edges. a minumum cut(A,B), are there are many vertices either on A's or B's sides. k=number of edges crossing (A,B).(call these edges F)
then it make the conclusion that an edge of F is contracted at some point, algorithm will not output(A,B).
Why is this? suppos there is a vertice C in A's side and a vertice D in B's side, so the edge(C,D) would belong to F, but even if C and D are to contract, we still have A and B, the algorithm can still return (A,B) right?
If G is a graph and (A, B) is a cut in the graph, then A and B are sets of nodes, not individual nodes in G. That is, there is no node in G called A or B. Rather, the nodes in G can be split apart into two groups, one of which we’ll call A and one of which we’ll call B. As a result, you can’t have the situation you’ve described.
Here’s another way to think about this. Suppose (A, B) is a minimum cut. You can then imagine that every node in the graph is painted red if it’s in the A side and blue if it’s in the B side. The randomized contraction algorithm will then find the cut (A, B) assuming that no red node is merged with a blue node. If nodes of opposite colors are merged, then the resulting cut won’t consist of all the red nodes on one side and all the blue nodes on the other.
I have a type of directed acyclic graph, with some constraints.
There is only one "entry" vertex
There can be multiple leaf vertices
Once a path splits, anything under that path cannot reach into the other path (this will become clearer with some examples below)
There can be any number of "split" vertices. They can be nested.
A "split" vertex can split into any number of paths. The examples below only show 2 paths for each, but it could be more.
My challenge is the following: for each "split" vertex (any vertex that has at least 2 outgoing edges), find the vertices where its paths reconnect - if such a vertex exists. The solution should be as efficient as possible.
Example A:
example a
In this example, vertex A is a "split" vertex, and its "reconnect vertex" is F.
Example B:
example b
Here, there are two split vertices: A and E. For both of them vertex G is the reconnect vertex.
Example C:
example c
Now there are three split vertices: A, D and E. The corresponding reconnect vertices are:
A -> K
D -> K
E -> J
Example D:
example d
Here we have three split vertices again: A, D and E. But this time, vertex E doesn't have a reconnect vertex because one of the paths terminates early.
Sounds like what you want is:
Connect each vertex with out-degree 0 to a single terminal vertex
Construct the dominator tree of the edge-reversed graph. The linked wikipedia article points to a couple algorithms for doing this.
The "reconnect vertex" for a split vertex is its immediate dominator in the edge-reversed graph, i.e., its parent in that dominator tree. This is called its "postdominator" in your original graph. If it's the terminal vertex that you added, then it doesn't have a reconnect vertex in your original graph.
This is the problem of identifying post-dominators in compilers and program analysis. This is often used in the context of calculating control dependences in control flow graphs. "Advanced Compiler Design and Implementation" is a good reference on these topics.
If the graph does not have cycles, then the solution (a) suggested by #matt-timmermans will work.
If the graph has cycles, then solution (a) can report spurious post-dominators. In such cases, a network-flow based approach works better. The algorithm to calculate non-termination sensitive control dependence in this paper using this approach. The basic idea is
at every split node, inject a unique token into the graph along each outgoing edge and
propagate the tokens thru the graph subject to this constraint: if node n is reachable from split node m, then tokens arriving at node m pass thru node n only if all tokens of node m have arrived at node n.
At the end, node n post-dominates node m if all tokens of node m have arrived at node n.
I'm trying to understand a Solved exercise 2, Chapter 3 - Algorithm design by tardos.
But i'm not getting the idea of the answer.
In short the question is
We are given two robots located at node a & node b. The robots need to travel to node c and d respectively. The problem is if one of the nodes gets close to each other. "Let's assume the distance is r <= 1 so that if they become close to each other by one node or less" they will have an interference problem, So they won't be able to transmit data to the base station.
The answer is quite long and it does not make any sense to me or I'm not getting its idea.
Anyway I was thinking can't we just perform DFS/BFS to find a path from node a to c, & from b to d. then we modify the DFS/BFS Algorithm so that we keep checking at every movement if the robots are getting close to each other?
Since it's required to solve this problem in polynomial time, I don't think this modification to any of the algorithm "BFS/DFS" will consume a lot of time.
The solution is "From the book"
This problem can be tricky to think about if we view things at the level of the underlying graph G: for a given configuration of the robots—that is, the current location of each one—it’s not clear what rule we should be using to decide how to move one of the robots next. So instead we apply an idea that can be very useful for situations in which we’re trying to perform this type of search. We observe that our problem looks a lot like a path-finding problem, not in the original graph G but in the space of all possible configurations.
Let us define the following (larger) graph H. The node set of H is the set of all possible configurations of the robots; that is, H consists of all possible pairs of nodes in G. We join two nodes of H by an edge if they represent configurations that could be consecutive in a schedule; that is, (u,v) and (u′,v′)will be joined by an edge in H if one of the pairs u,u′ or v,v′ are equal, and the other pair corresponds to an edge in G.
Why the need for larger graph H?
What does he mean by: The node set of H is the set of all possible configurations of the robots; that is, H consists of all possible pairs of nodes in G.
And what does he mean by: We join two nodes of H by an edge if they represent configurations that could be consecutive in a schedule; that is, (u,v) and (u′,v′) will be joined by an edge in H if one of the pairs u,u′ or v,v′ are equal, and the other pair corresponds to an edge in G.?
I do not have the book, but it seems from their answer that at each step they move one robot or the other. Assuming that, H consists of all possible pairs of nodes that are more than distance r apart. The nodes in H are adjacent if they can be reached by moving one robot or the other.
There are not enough details in your proposed algorithm to say anything about it.
Anyway I was thinking can't we just perform DFS/BFS to find a path from node a to c, & from b to d. then we modify the DFS/BFS Algorithm so that we keep checking at every movement if the robots are getting close to each other?
I don't think this would be possible. What you're proposing is to calculate the full path, and afterwards check if the given path could work. If not, how would you handle the situation so that when you rerun the algorithm, it won't find that pathological path? You could exclude that from the set of possible options, but I don't see think that'd be a good approach.
Suppose a path of length n, and now suppose that the pathology resides in the first step of the given path. Suppose now that this happens every time you recalculate the path. You would have to recalculate the path a lot of times just because the algorithm itself isn't aware of the restrictions needed to get to the right answer.
I think this is the point: the algorithm itself doesn't consider the problem's restrictions, and that is the main problem, because there's no easy way of correcting the given (wrong) solution.
What does he mean by: The node set of H is the set of all possible configurations of the robots; that is, H consists of all possible pairs of nodes in G.
What they mean by that is that each node in H represents each possible position of the two robots, which is the same as "all possible pairs of nodes in G".
E.g.: graph G has nodes A, B, C, D, E. H will have nodes AB, AC, AD, AE, BC, BD, BE, CD, CE, DE (consider AB = BA for further analysis).
Let the two robots be named r1 and r2, they start at nodes A and B (given info in the question), so the path will start in node AB in graph H. Next, the possibilities are:
r1 moves to a neighbor node from A
r2 moves to a neighbor node from B
(...repeat for each step unitl r1 and r2 each reach its destination).
All these possible positions of the two robots at the same time are the configurations the answer talks about.
And what does he mean by: We join two nodes of H by an edge if they represent configurations that could be consecutive in a schedule; that is, (u,v) and (u′,v′) will be joined by an edge in H if one of the pairs u,u′ or v,v′ are equal, and the other pair corresponds to an edge in G.?
Let's look at the possibilities from what they state here:
(u,v) and (u′,v′) will be joined by an edge in H if one of the pairs u,u′ or v,v′ are equal, and the other pair corresponds to an edge in G.
The possibilities are:
(u,v) and (u,w) / (v,w) is and edge in E. In this case r2 moves to one of the neighbors from its current node.
(u,v) and (w,v) / (u,w) is and edge in E. In this case r1 moves to one of the neighbors from its current node.
This solution was a bit tricky to me too at first. But after reading it several times and drawing some examples, when I finally bumped into your question, the way you separated each part of the problem then helped me to fully understand each part of the solution. So, a big thanks to you for this question!
Hope it's clearer now for anyone stuck with this problem!
I am stuck in a problem regarding Graph traversal based on the angle between two edges. I would like to summarize the problem as follows, given 5 vertices a,b,c,d,e and the edges (a, b), (b, c), (c, d), (d, e).
If I want to traverse the graph based on calculating the angle between two edges like for example angle((a, b), (b, c)). If my angle is greater than 10 degree I should stop at b and start the process again.
What steps do I need to consider to approach this problem having concrete programming structures.
If I understand correctly, when angle((a,b),(b,c)) returns a value of over some threshold (10, in your example), you should stop traversing the graph.
This means that effectively, this node (b) is not helping by connecting the two edges ((a,b) and (b,c)). It might be useful for some other set of edges, but that specific connection is not available.
What I suggest is swapping the role of edges and nodes. Every edge in G becomes a node in G' and every node in G becomes and edge in G' only if the value of angle() returns a value lower than your threshold.
On G' you can now run BFS, DFS or any other algorithm of your liking. When you are done, use the reverse transformation to "translate" your answer back into the original graph in question.