Graph Theory Algorithm - algorithm

The given problem is
Given a forest with n vertices, add edges to make it into a tree with
minimal diameter.
I tried many approaches but none of them passed system test cases.Please suggest some algorithm to solve this problem.
This is the link of the editorial ncpc.idi.ntnu.no/ncpc2015/ncpc2015slides.pdf The problem name is Adjoin the Networks. I am not able to understand the solution provided in the editorial
Update:
https://www.quora.com/What-is-the-solution-for-Dreaming-on-IOI-2013
This link provides the best explanation for the solution mentioned in the editorial

The eccentricity of a vertex v, denoted ecc(v), is defined as ecc(v):=max_u d(u,v), i.e. as the distance to a most distant vertex in the graph. A center of a graphG is any vertex v for which ecc(v)=min_v max_u d(u,v), i.e. a center is a vertex that minimizes the eccentricity.
If you merge two trees (from different connected components), T1 and T2, by putting an edge between their centers c1 and c2, you get a tree T with diam T = max(diam T1, diam T2, 1+rad(T1)+rad(T2)).
The correctness of the approach below should be evident from these properties.
Here's one idea for the algorithm, off the top of my head:
let T1, T2, ..., Tk be the trees comprising the forest.
compute a center vertex ci for each of the trees Ti.
connect components by putting edges between centers in an intelligent way.
Of course, the problem is now how to cleverly solve the last bullet. Intuitively I'd suggest you connect the treest with the largest diameters first (and then update the diameter of the new tree and compute a center of the new tree). Perhaps something like this:
while the priority queue contains more than one tree do
let T1 and T2 be the trees with the largest diameters; let c1 and c2 be their centers;
connect c1 and c2 to form a new tree T;
compute a new center c of T, compute diam T and put T back into priority queue (which can be a max-heap that uses diameter as the key).
done
Update. I'm not sure whether to join largest-diameter trees first or the other way around (i.e. smallest-diameter trees first). But it's now very easy to do a sketch of a proof (once you figure out which way to go) that this is the right way to go.
Update. The math certainly goes through if you connect largest first (as suggested in the PDF).

Related

Matching non-isomorphic graphs

I have two graphs G1 and G2, which are not isomorphic. I need to make a new graph G1' such that, with the minimum changes in G1, it will have the nodes of both G1 as well as G2. For example, let's say there is a node n1 in G1 with three connecting nodes n11, n12, n13. If now a 'corresponding' node n2 in G2 has 5 nodes n21, n22, n23, n24, n25, then n1' in G1' also needs to have five nodes n11', n12', n13', n14', n15'. The first three copied from G1 and the two extra nodes which will have value of the last of the three. The tree emanating from the extra nodes will be either entirely newly created or will comprise some extra nodes from G1 that haven't got equivalent nodes in G2 (are not 'exhausted' in some sense).
The problems are 1) finding the most suitable seed as the starting point so that the starting views are as much similar as possible 2) Building the tree from the extra nodes keeping the added node count to the minimum
Edit:
I will try to explain this further with the help of an illustration. My knowledge of graph theory is very superficial, so please excuse me if something sounds silly.
I broadly want to obtain a graph which, with the minimum number of node manipulations, can take the form of one of the two non-isomorphic source graphs.
In the example above graph G' can take two forms G or H with some amount pf node shuffling.
1) To make it G, we keep all the orange nodes at their position. The dotted nodes will 'merge' to their neighboring nodes. So B21' will have the value of A21 and will be at the same position (dissolving the corresponding edges). Likewise will happen with the pairs B31'-A31, B14'-A15 B25'-B23, A32'-A22 and A23'-A32. With this configuration the graph would resembles G completely, without any edges 'sticking out'
2) To make it isomorphic with H, A11 and A12, will take the values of A13, A32 and A32' that of A23, A23' that of A22. The dotted nodes will 'come out' of their merged positions.
The problem is to find G'. Maybe there is no ready graph operation or the solution is impossible, but any pointer to achieve this with any degree of approximation and efficiency is most welcome.
NB: The starting nodes A1 and B1 are arbitrary. First half of the problem is identifying these nodes so that the views are as much similar as possible.
This is at least as hard as the graph isomorphism problem which is currently not known to be solvable in polynomial time. As such, you should not expect to be able to find an efficient algorithm for your problem.
The correspondence is straightforward to see because if G1 and G2 were in fact isomorphic, you would have G1' = G1, so an algorithm which solves this problem could be used to solve the graph isomorphism problem.

Optimal edge coloring in bipartite graphs

I've faced with following problem: find the optimal edge coloring in a bipartite graph. I know that greedy coloring algorithm can sometimes not return the optimal number of colors. By 'greedy coloring algorithm' I mean: choose first vertex with the highest degree and color its edges on colors 1...degree, then choose the vertex with degree <= to the previous degree and color every of its incident edge on the first available number (the lowest number which is not used by its neighbour), the choose the next vertex etc.
But I've introduced one modification: the edges of first choosen vertex I color in descending order (degree...1), and edges of the next vertices as previously on 1...degree. This modification resulted in examples which I've come up I've got optimal number of colors. But I'm not pretty sure that it's always a rule. Does somebody know if this version of edge coloring algorithm is optimal, or maybe anyone is able to show any counterexample?
You can take your counterexample for the "naive" greedy algorithm and turn it into a counterexample for your "sophisticated" greedy algorithm. Simply insert dummy nodes with appropriate degree to "absorb" the backwards colorings. One can always fabricate a new node with degree n in an arbitrary part of the graph: simply insert n fresh nodes in the other part and connect them each by a single edge to the desired new node.
Since all nodes that get colored in descending order are freshly inserted, all the nodes in the original counterexample are colored in ascending order, hence get the same colors as they would have in the original "naive" greedy algorithm. Since the optimal coloring has at least as many colors as the degree of the original graph, and the freshly inserted nodes all have smaller degree than the original graph's maximal degree, the new graph does not need any more colors than the original. Therefore the coloring produced by the "sophisticated" algorithm -- which will still have more colors than necessary for the original graph -- is not optimal for the new graph.
For example, take the graph described in the comment below, which has nodes B,C,D on the left and E,F,G,H on the right. It has these edges:
B connects to E, F, and G
C connects to E, F, and G
D connects to G and H
For the moment, I will assume only the first node you touch gets colored in descending order. (For other nodes, it is not even clear what "descending order" might mean -- descending from what maximum? The degree of the node may not be high enough.)
Therefore, we insert a new node A on the left and three nodes I, J, and K on the right; the connectivity is now
A connects to I, J, and K
B connects to E, F, and G
C connects to E, F, and G
D connects to G and H
The sophisticated greedy algorithm will therefore color AI-3, AJ-2, AK-1, then proceed as the naive greedy algorithm on the remaining nodes.

Does order of exploration in DFS affect edge classification

I'm implementing DFS and edge classification for college (based on the code provided in this paper: https://courses.csail.mit.edu/6.006/fall11/rec/rec14.pdf).
I've used this graph as a test:
The letters in italic are simply the vertices' names, while the numbers inside the vertices are discovery and finalization time, respectively. Edges are classified as Back, Forward or Cross; all else are tree edges.
As you can see, this graph was visited in the following order: first s, then its neighbors (following DFS); when there were no more accessible neighbors, visitation began on t.
In order to test our algorithm, the teacher will provide a text file with each edge as a line. When performing the DFS, I simply follow the order of appearance in the file for each vertex; in this case, that would be first s and then v, not t. This in turn gives a different edge classification: t to v is considered a cross edge, t to u a back one and u to t a tree one.
Is this behavior normal? Does the order of exploration affect the final edge classification? If so, are both classifications, mine and the example's, correct? If not, is there any way I can know which vertex to explore first?
Is this behavior normal? Does the order of exploration affect the final edge classification?
Absolutely. The order in which the algorithm chooses edges for exploration decides edge classification in cases when there are any back or cross edges (i.e. when the graph is not a tree.)
If so, are both classifications, mine and the example's, correct?
Yes, both classifications are correct, as long as there is no specific order of exploration requested by the problem setter.
If not, is there any way I can know which vertex to explore first?
The problem may specify a particular order of exploration - for example, the graph on your picture appears to be traversed in reverse alphabetical order of vertex naming: z before w from s, y before w from z, and v before u from t.

What is meant by the set of all possible configuration in a given graph G

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!

Find the Sunflower subgraph induced in a graph in polynomial amount of time.

A subgraph Sn of a graph G is a sunflower graph, if consists of a Cycle Cn = {v1,v2,..,vn} of n vertices together with other n independent vertices {u1,u2,...,un} such that for each i, ui is adjacent to vi and vj, where j = i-1(mod n).
You could think of a sunflower - in the sense of the question - as a cycle of triangles. In time O(N^3) you can check each triple of points to see if it is a triangle and create a new graph whose vertices denote triangles in the original graph and where two vertices are linked if the two triangles share one or more vertices.
Then a depth first search looking for back edges should find cycles in this graph. Not all cycles are good. I think it may be enough to check that no two successive edges in the supposed cycle in the derived graph are produced by the same vertex in the original graph, and that you can check this as part of the depth first search. It may take some detailed analysis of cases to establish this, unless you can find a neat proof.

Resources