I want to find minimum vertices that connect certain vertices to each other. For example, assume list of edges is connections = [(2,0),(0,5),(2,3),(3,4),(0,4),(4,1),(5,1)]
and the graph will be like below:
Then I want to know how to connect vertices 2, 4, 5
together with minimum number of vertices. Which in this case is vertex 0
.
Therefore the output should be [0,2,4,5]
which means 4 vertices needed. I tried to implement the algorithm with brute force since there would be more complicates cases but I am not sure how to find the best answer from all possible combinations.
from itertools import combinations
verticis = [0,1,2,3,4,5]
connections = [(2,0),(0,5),(2,3),(3,4),(0,4),(4,1),(5,1)]
key = [2,4,5]
i, j = 2, len(verticis)
res1 = [com for sub in range(j) for com in combinations(verticis, sub + 1)]
res2 = [com for sub in range(i - 1) for com in combinations(verticis, sub + 1)]
res = list(set(res1) - set(res2))
This problem is in fact a well-known NP-hard combinatorial optimization problem: Steiner tree problem.
In other words:
Because of the intrinsic hardness of the problem, exact approaches for obtaining a minimum Steiner tree are all of exponential worst-case time complexity (unless P=NP). You can find various exact solution approaches to the problem in the literature. However, one simple exact approach is to model the problem instance in hand to a constraint optimization problem (COP), and solve it using a COP solving tool like MiniZinc:
In this code, the boolean decision variable
vertex[i]
indicates that whether or not the Steiner tree contains the (i-1)th vertex . In the instance you described we have three terminals. Therefore, we need to set the value of the corresponding decision variables astrue
. The objective of connecting these three nodes using minimum number of vertices is equivalent to finding a minimum Steiner tree, provided that the edge weights are all one. Hence, in the global constraintsteiner
, we set all the edge weights to one. An optimal solution to the given instance is as follows:As can be seen, only the vertex 0 need to be employed as a Steiner point (
vertex[1]
istrue
in the returned solution), and the corresponding Steiner tree has three edges (i.e., {0,2}, {0,4}, and {0,5}). For more information on the global constraintsteiner
, see this link.