subset vertices such that minimum weight is d - algorithm

Imagine you have a weighted undirected graph G=(V,E) that is fully connected (edge between every pair of vertices). You seek to have a graph G' where G' is a subset of G where vertices with their corresponding edges have been removed such that the edge with the minimum weight is d.
Trivially you can just remove all vertices but a few with highly weighted edges. But what if you want G' to be as big as possible ? What is the way to remove the minimal set of vertices to satisfy the weight condition ?
For instance I have graph A,B,C (A,B) = 2 (A,C) = 3 and (B,C) = 2 and my minimum d=2. I could remove both A and C to get just B. But I could also remove B to leave A and C. The second solution is the one with the minimal amount of removals.

Related

How to calculate the expected value of random graph generation

Hello this is my first question. I met a homework in algorithm and probability that I can't find a clue to calculate.
Question:
Computing Number of Triangles in a Graph: Given an undirected graph G = (V, E), a triangle in G is a clique of size 3 (formally, a set of nodes {u, v, w} is a triangle in G if (u, v), (v, w), (u, w) are all edges of G). Consider the following algorithm for approximating the number of triangles in a graph. First construct a sampled graph G' = (V, E') as follows. The vertex set of G' is same as that of G. For every e ∈ E, put e in E' with probability p (think of p as, say, 0.1). In this new sampled graph G', count the number of triangles and let T' be the number of triangles in G' (assume that you have given a black box subroutine to count the number of triangles in G' ). Then output T̃= T'/p.
Show that the expected value of T̃=T ,T is the triangle number of original graph G.
I am confusing that the edge in G or G' to form a triangle is not independent since two adjacent triangles in G might share the edge. And not the all the pair of vertices in G can form a edge in G', only those edges are in G will be present in G' with p. It's hard for me to think of the relationship of number of edges and number of triangles in G or G'.
Hope someone can give me some hints, even not the whole solution is OK.
the edge in G or G' to form a triangle is not independent since two adjacent triangles in G might share the edge
Doesn't matter. The sum of expectations is the expectation of the sum regardless of correlation, so you can reason about the triangles individually. (Higher moments, were you concerned about analyzing the estimation quality of this algorithm, would be trickier.)

Minimum Spanning Tree Graph

I have a connected graph G=(V,E) V={1,2,...,n} and a cost function c:E->R
and a second partial graph G'=(V,T) where T={ for every vertex v∈ V find the neighbor with the minimum cost and add the new edge to T}
If G' graph has at least 2 connected components with the set of vertices we consider the graph H where
iff the set of edges (from the initial graph G) is not null.We define over the edges of H a cost function.
Let's say I choose V(H)={a,e,f} and E(H)={ae,af,fe} and
E12={ab,bc,bd,ed}
E23={eg,ef} E31={fc,fd}
c'(ae)=min{c(ab),c(bc),c(bd),c(ed)}=4
c'(af)=min{c(fc),c(fd)}=9
c'(fe)=min{c(eg),c(ef)}=8
Now for every edge e ∈ E(H) we note with e' the edge (from the original graph G)
for which this minimum is attained.
So e'={bc,df,eg} because bc=4 , df=9 and eg=8 and are the min edges that connect my components.
And I have a minimum spanning tree in H relative to the cost function c' and A' is the set of edges for this tree.
So A'={ae,fe} (I deleted the edge with the maximum cost=af from my graph H to create a min spanning tree)
and I have another set of edges A'={e'|e∈A'} and
is a min spanning tree in G relative to the function cost c.
But none of my edges from A' are the same with the ones from e'.
What I'm I doing wrong?
Looks like you're implementing Boruvka's algorithm. If you look at the notation, it says there's an edge from one new node vC1 to a new node vC2 if there are a pair of nodes x ∈ C1 and y ∈ C2 that are adjacentnin the original graph G. In other words, there's an edge between two new nodes if the connected components they correspond to in G' are adjacent in G. The cost of the edge running between them is then the lowest of the costs of any of the edges running between those CC's in the original graph G.

How to update MST from the old MST if one edge is deleted

I am studying algorithms, and I have seen an exercise like this
I can overcome this problem with exponential time but. I don't know how to prove this linear time O(E+V)
I will appreciate any help.
Let G be the graph where the minimum spanning tree T is embedded; let A and B be the two trees remaining after (u,v) is removed from T.
Premise P: Select minimum weight edge (x,y) from G - (u,v) that reconnects A and B. Then T' = A + B + (x,y) is a MST of G - (u,v).
Proof of P: It's obvious that T' is a tree. Suppose it were not minimum. Then there would be a MST - call it M - of smaller weight. And either M contains (x,y), or it doesn't.
If M contains (x,y), then it must have the form A' + B' + (x,y) where A' and B' are minimum weight trees that span the same vertices as A and B. These can't have weight smaller than A and B, otherwise T would not have been an MST. So M is not smaller than T' after all, a contradiction; M can't exist.
If M does not contain (x,y), then there is some other path P from x to y in M. One or more edges of P pass from a vertex in A to another in B. Call such an edge c. Now, c has weight at least that of (x,y), else we would have picked it instead of (x,y) to form T'. Note P+(x,y) is a cycle. Consequently, M - c + (x,y) is also a spanning tree. If c were of greater weight than (x,y) then this new tree would have smaller weight than M. This contradicts the assumption that M is a MST. Again M can't exist.
Since in either case, M can't exist, T' must be a MST. QED
Algorithm
Traverse A and color all its vertices Red. Similarly label B's vertices Blue. Now traverse the edge list of G - (u,v) to find a minimum weight edge connecting a Red vertex with a Blue. The new MST is this edge plus A and B.
When you remove one of the edges then the MST breaks into two parts, lets call them a and b, so what you can do is iterate over all vertices from the part a and look for all adjacent edges, if any of the edges forms a link between the part a and part b you have found the new MST.
Pseudocode :
for(all vertices in part a){
u = current vertex;
for(all adjacent edges of u){
v = adjacent vertex of u for the current edge
if(u and v belong to different part of the MST) found new MST;
}
}
Complexity is O(V + E)
Note : You can keep a simple array to check if vertex is in part a of the MST or part b.
Also note that in order to get the O(V + E) complexity, you need to have an adjacency list representation of the graph.
Let's say you have graph G' after removing the edge. G' consists have two connected components.
Let each node in the graph have a componentID. Set the componentID for all the nodes based on which component they belong to. This can be done with a simple BFS for example on G'. This is an O(V) operation as G' only has V nodes and V-2 edges.
Once all the nodes have been flagged, iterate over all unused edges and find the one with the least weight that connects the two components (componentIDs of the two nodes will be different). This is an O(E) operation.
Thus the total runtime is O(V+E).

Merge most of the black vertices of DAG together so that it remains DAG?

I have a DAG ( Directed Acyclic Graph ) with vertices having any of the 2 colours black or white. I need to merge as many black vertices together with the constraint that the graph should remain acyclic. Hence the final DAG should have minimum no. of black vertices. What is the best algorithm for this problem?
Here is one possible strategy. It reduces your problem to a colouring problem (which you can then use established heuristics algorithm from literature to solve).
Call the DAG G = (V,E) where V is the set of vertices. Let B be the set of black vertices and W be the set of white vertices. We want to construct a new simple graph G' = (B,E'). We construct it as follow:
algorithm contruct G' input: G
Let G' be a graph with vertex set B and no edges
for any pair of vertices v and v' where v,v' in B:
Let (G'', v'') = merge (v,v',G)
#comment: here, we let G'' to be the graph resulted from merging v and v'
#also, let's assume that v and v' merge to become v''
if detect_cycle(G'',v'') = true:
add edge (v,v') into G'
output G'
algorithm detect_cycle(G,v):
do BFS in G starting at v, with the modification when reaching any vertex v':
if v is connected to v': return true
return false
Note that G' is a simple graph and not a DAG and when doing BFS on G, you cannot go against the direction of an edge in G.
Essentially, we try to build G' with the set of black vertices in G such that if two vertices v adjacent to v' in G', then merging them causes cyclic graph in G. If v is not adjacent to v' in G' then it's safe to merge them. The problem then got reduced to find the minimum number of colors required to vertex-color G'. For background on vertex colouring, check out this link: https://en.wikipedia.org/wiki/Graph_coloring#Vertex_coloring. Basically vertex coloring is about finding the minimum number of sets where in each set, you can put in pairwise-nonadjacent vertices, then assign a label (or color) to each set (every vertex in the same set get the same label). Every black vertex with the same label in G' could be merged in G.
Heuristic algorithms for graph colouring could be found here:
http://heuristicswiki.wikispaces.com/Graph+coloring
and here: http://heuristicswiki.wikispaces.com/Degree+based+ordering
I hope it helps. Let me know if you find a better solution or a bug in the above solution.
Let the graph be G = (V,E)
Topological sort the graph to get the list of vertices = L(V).
L(B) = list of black vertices extracted from L(V) with the order maintained.
Let n = no. of vertices in L(B).
Let DVA = empty array of deleted vertices of size n initialized with 0.
for i = vertices 1 to n in L(B)
if(DVA[i] == 1)
continue;
for j = vertices i+1 to n in L(B)
if(DVA[j] == 1)
continue;
if(detect_cycle(G, i, j) == 0) //merging i and j will not create cycle
Merge j to i in G;
DVA[j] = 1;
This algorithm works on the fact that topological order of black vertices do not change while merging 2 vertices (except for these 2 vertices) .
I guess this method will produce fairly good result, but I am not sure whether it will produce the optimal result of having least no. of black vertices.

How can I get the antichain elements in SPOJ-DIVREL?

Problem: http://www.spoj.com/problems/DIVREL
In question, we just need to find the maximum number of elements which are not multiples (a divisible by b form) from a set of elements given. If we just make an edge from an element to its multiple and construct a graph it will be a DAG.
Now the question just changes to finding the minimum number of chains which contain all the vertices which equals the antichain cardinality using Dilworth's theorem as it is a partially ordered set.
Minimum chains can be found using bipartite matching (How: It is minimum path cover) but now I am unable to find the antichain elements themselves?
To compute the antichain you can:
Compute the maximum bipartite matching (e.g. with a maximum flow algorithm) on a new bipartite graph D which has an edge from LHS a to RHS b if and only if a divides b.
Use the matching to compute a minimal vertex cover (e.g. with the algorithm described in the proof of Konig's theorem
The antichain is given by all vertices not in the vertex cover
There cannot be an edge between two such elements as otherwise we would have discovered an edge that is not covered by a vertex cover resulting in a contradiction.
The algorithm to find the min vertex cover is (from the link above):
Let S0 consist of all vertices unmatched by M.
For integer j ≥ 0, let S(2j+1) be the set of all vertices v such that v is adjacent via some edge in E \ M to a vertex in S(2j) and v has not been included in any
previously-defined set Sk, where k < 2j+1. If there is no such vertex,
but there remain vertices not included in any previously-defined set
Sk, arbitrarily choose one of these and let S(2j+1) consist of that
single vertex.
For integer j ≥ 1, let S(2j) be the set of all vertices u
such that u is adjacent via some edge in M to a vertex in S(2j−1). Note
that for each v in S(2j−1) there is a vertex u to which it is matched
since otherwise v would have been in S0. Therefore M sets up a
one-to-one correspondence between the vertices of S(2j−1) and the
vertices of S(2j).
The union of the odd indexed subsets is the vertex cover.

Resources