How to understand the random contraction algorithm? - algorithm

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.

Related

Finding k-coloring (with k = 2/3) graph in linear time

Question:
Given a graph G = (V,E) a k-coloring of G is a labeling of the vertices with the colors c_1, c_2, ..., c_k such that for every edge (u,v) the color of u is different from the color of v.
A. Give a linear time algorithm that finds a 2-coloring for a tree.
B. Think about coloring a tree with two colors so that you have the maximum number of nodes colored c_1. Prove that your algorithm from part (a), with a minor modification probably, can be used to solve this problem. Be precise.
C. Show an example of a tree, T, that can have at most j nodes colored c_1 in a 2-coloring but T can have j' > j nodes colored c_1 in a 3-coloring. Try to find the smallest example of such a tree T.
D. Give a linear time dynamic programming algorithm that creates a 3-coloring for a tree such that the maximum number of nodes are colored c_1. Justify your answer.
What I have:
Parts A and B seem rather simple, but I am struggling on C and D
A. This seems simple, run DFS, but modify it so if it
encounters an uncolored node color it c1 and its children c2,
encounters a colored node color its children the opposite color
if it ever tries to "flip" the color of a child, then return that it is impossible to have k-coloring
This is just DFS, so will run at O(V+E)
Edit: Since this a tree you can call BFS on a random node, alternating colors in the BFS
B. Since this is a binary first choice (ie c1 or c2) and all choices follow from the first choice, we can simply calculate which is superior coloring the first node c1 or c2. We can do this by adding a counter to the above algorithm calculating the number of c1 nodes. Run the algorithm twice once starting with c1, then with c2, compare the number of c1 nodes between the two, and then pick the graph with more c1 nodes
C. Been fiddling with this for a while and can't find a single one, much less the smallest one possible
Edit: 1-3, 2-3, 3-4, 4-5, 4-6 described by Bing Wang works here
D. No idea. I assume you are going to have to be using a modified DFS to have it run at linear, but am extremely unsure beyond that. I can come up with ways to brute force it, but nothing that runs at linear.
Edit: Still confused about this
c: 1-3, 2-3, 3-4, 4-5,4-6. This tree can have 3 nodes in each color in bi-color. But can have 4 nodes in the same color if you color node #3/#4 to two other colors - in a tri-color. You should be easily prove no smaller answer by going over all possibilities.
D: Just traverse the tree, e.g. DFS. Each node needs to keep c_1 count of all 3 colorings. WHen you deal with 1st node, that would be (1,0,0) - fairly easy count. Once you add the nodes into the visited, try all 3 colors, each color would be compatible with 2 colors of the connecting node, from which you pick the bigger one, etc, to construct the 3 values of current node.
E=[(1,3),(2,3),(3,4),(4,5),(4,6)]
V={v:set() for e in E for v in e}
[None if V[e[0]].add(e[1]) else V[e[1]].add(e[0]) for e in E]
serialized={}
def traverse(c):
serialized[c]=V[c]
[traverse(n) for n in V[c] if n not in serialized]
traverse(E[0][0])
visited=set()
for k,v in reversed(serialized.items()):
filtered=v.intersection(visited)
visited.add(k)
m=[1+sum(max([V[child][1],V[child][2]]) for child in filtered)
,sum(max([V[child][0],V[child][2]]) for child in filtered)
,sum(max([V[child][0],V[child][1]]) for child in filtered)]
V[k]=m
print(max(V[k]))
A. Pick any vertex and color it c1. Run BFS from there. Everything at an odd distance gets c2 and at an even distance gets c1.
B. There are only two colorings, and they differ only in swapping c1 for c2. Run (A) and swap if you need to.
C. Imagine A and B are connected, and a has a number of other neighbors which are all leaves, as does B. With 2 colors you can get one more than the size of A or B a given color, but with three you can give two different colors to A & B, and color all the leaves the same color. The count of leaves depends on the j an j' inputs. If we're finding the smallest values of j and j' possible, then give each two leaves. Then you can have four c1 leaves with A c2 and B c3, as opposed to a max of 3 with only two colors.

In a DAG, how to find vertices where paths converge?

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.

How can I cast this into a graph algorithm?

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.

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.

How to traverse a Graph based on the angle between two edges

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.

Resources