Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subset of vertices

Tags:

c++

algorithm

I have a homework problem and i don't know how to solve it. If you could give me an idea i would be very grateful.

This is the problem: "You are given a connected undirected graph which has N vertices and N edges. Each vertex has a cost. You have to find a subset of vertices so that the total cost of the vertices in the subset is minimum, and each edge is incident with at least one vertex from the subset."

Thank you in advance!

P.S: I have tought about a solution for a long time, and the only ideas i came up with are backtracking or an minimum cost matching in bipartite graph but both ideas are too slow for N=100000.

like image 566
user1907615 Avatar asked Dec 16 '12 09:12

user1907615


People also ask

What is a subset of a graph?

In the mathematical field of graph theory, an induced subgraph of a graph is another graph, formed from a subset of the vertices of the graph and all of the edges (from the original graph) connecting pairs of vertices in that subset.

What is a subset of nodes?

A subset of nodes only contains a set of nodes of the original graph, while a subgraph contains a set of nodes of the original graph as well as a set of edges between these nodes.

What are the types of vertices?

Types of verticesAn isolated vertex is a vertex with degree zero; that is, a vertex that is not an endpoint of any edge (the example image illustrates one isolated vertex). A leaf vertex (also pendant vertex) is a vertex with degree one.


1 Answers

This may be solved in linear time using dynamic programming.

A connected graph with N vertices and N edges contains exactly one cycle. Start with detecting this cycle (with the help of depth-first search).

Then remove any edge on this cycle. Two vertices incident to this edge are u and v. After this edge removal, we have a tree. Interpret it as a rooted tree with the root u.

Dynamic programming recurrence for this tree may be defined this way:

  • w0[k] = 0 (for leaf nodes)
  • w1[k] = vertex_cost (for leaf nodes)
  • w0[k] = w1[k+1] (for nodes with one descendant)
  • w1[k] = vertex_cost + min(w0[k+1], w1[k+1]) (for nodes with one descendant)
  • w0[k] = sum(w1[k+1], x1[k+1], ...) (for branch nodes)
  • w1[k] = vertex_cost + sum(min(w0[k+1], w1[k+1]), min(x0[k+1], x1[k+1]), ...)

Here k is the node depth (distance from root), w0 is cost of the sub-tree starting from node w when w is not in the "subset", w1 is cost of the sub-tree starting from node w when w is in the "subset".

For each node only two values should be calculated: w0 and w1. But for nodes that were on the cycle we need 4 values: wi,j, where i=0 if node v is not in the "subset", i=1 if node v is in the "subset", j=0 if current node is not in the "subset", j=1 if current node is in the "subset".

Optimal cost of the "subset" is determined as min(u0,1, u1,0, u1,1). To get the "subset" itself, store back-pointers along with each sub-tree cost, and use them to reconstruct the subset.

like image 81
Evgeny Kluev Avatar answered Sep 27 '22 18:09

Evgeny Kluev