Java Program to Check if a Steiner Tree of Size k Exists for a Graph
Last Updated :
31 Dec, 2022
A Steiner tree is a tree that connects a subset of the vertices in a graph with the minimum total weight. It is often used to find the shortest path between a set of points in a graph. Here is a Java program to check if a Steiner tree of size k exists for a given graph:
Java
import java.util.ArrayList;
import java.util.List;
public class SteinerTree {
private static final int INF = Integer.MAX_VALUE;
// Function to check if a Steiner tree of size k exists
// for the given graph
public static boolean checkSteinerTree(int[][] graph,
int k)
{
int n = graph.length;
int[][] dp = new int[n][1 << n];
// Initialize the dp array with INF
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << n); j++) {
dp[i][j] = INF;
}
}
// Set the base case where the tree consists of only
// one vertex
for (int i = 0; i < n; i++) {
dp[i][1 << i] = 0;
}
// Iterate through all possible tree sizes
for (int mask = 1; mask < (1 << n); mask++) {
// Iterate through all possible root vertices
for (int root = 0; root < n; root++) {
// Check if the root vertex is present in
// the tree
if ((mask & (1 << root)) == 0) {
continue;
}
// Iterate through all possible subtrees
for (int submask = mask; submask > 0;
submask = (submask - 1) & mask) {
// Check if the current subtree is valid
// (i.e., non-empty)
if (submask == 0) {
continue;
}
// Find the minimum weight of the
// current subtree
int minWeight = INF;
for (int i = 0; i < n; i++) {
// Check if the current vertex is
// present in the subtree
if ((submask & (1 << i)) == 0) {
continue;
}
// Find the minimum weight of the
// current subtree
for (int j = 0; j < n; j++) {
// Check if the current vertex
// is present in the subtree
if ((submask & (1 << j)) == 0) {
continue;
}
// Update the minimum weight of
// the current subtree
minWeight = Math.min(
minWeight, graph[i][j]);
}
}
// Update the minimum weight of the
// current tree
dp[root][mask] = Math.min(
dp[root][mask],
dp[root][submask] + minWeight);
}
}
}
// Check if a Steiner tree of size k exists
for (int i = 0; i < n; i++) {
if (dp[i][(1 << n) - 1] <= k) {
return true;
}
}
return false;
}
public static void main(String[] args)
{ // Test with a sample graph
int[][] graph = { { 0, 1, 3, INF, INF },
{ 1, 0, 3, 6, INF },
{ 3, 3, 0, 4, 2 },
{ INF, 6, 4, 0, 5 },
{ INF, INF, 2, 5, 0 } };
int k = 8;
System.out.println(checkSteinerTree(
graph, k)); // Expected output: true
}
}
Time complexity of O((3^n) * n^2), where n is the number of vertices in the graph. This can be quite slow for large graphs.
Output:
true
Explanation:
The algorithm uses dynamic programming to find the minimum weight of a Steiner tree for each possible tree size and root vertex. It uses a two-dimensional dp array, where dp[i][j] represents the minimum weight of a Steiner tree rooted at vertex i and consisting of the vertices in the set represented by j (in binary).
First, the dp array is initialized with INF for all entries. Then, the base case where the tree consists of only one vertex is set. For each possible tree size, the algorithm iterates through all possible root vertices and checks if the root is present in the tree (by checking if the corresponding bit in the mask is set). If the root is present, the algorithm iterates through all possible subtrees and finds the minimum weight of the current subtree. Then, it updates the minimum weight of the current tree by adding the minimum weight of the current subtree to the minimum weight of the current tree rooted at the same vertex and consisting of the vertices in the subtree.
Finally, the algorithm checks if a Steiner tree of size k exists by checking if the minimum weight of any tree rooted at any vertex and consisting of all vertices is less than or equal to k. If such a tree exists, the function returns true; otherwise, it returns false.
In the sample test case, the graph is a complete graph with 5 vertices and the following edge weights:
0 1 3
1 0 1 3
3 1 0 3
4 3 3 0
5 6 4 2
2 5 0
A Steiner tree of size 8 can be obtained by taking the minimum weight edge from each subtree, as shown below:
0 1 3
1 0 1 3
3 1 0
4 3 3 0
2 5 0
The minimum weight of this Steiner tree is 8, which is equal to the given value of k, so the function returns true.
Similar Reads
Java Program to Find a Good Feedback Vertex Set in a Graph
A feedback vertex set of a graph is a set of vertices whose removal leaves a graph without cycles. The following graph becomes a Directed acyclic Graph. Examples: Input: Enter the number of vertices: 4 Enter the number of edges: 5 Enter the graph: <Start> <end> 1 2 2 3 3 4 4 1 1 3 Output
8 min read
Java Program to Check if the TreeMap is Empty
The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Approaches: Using the isEmpty() methodU
3 min read
Java Program to Find a Good Feedback Edge Set in a Graph
Feedback edge set is a set of edges where F â E of a directed graph G, whose every cycle must contain at least one edge from F. In simple words, Feedback edge set is a set of edges whose removal from the graph makes the graph directed acyclic graph. Examples: Input: Output: Feedback Edge Set: ( 3 -
4 min read
Java Program to Check Whether Undirected Graph is Connected Using DFS
Given an undirected graph, the task is to check if the given graph is connected or not using DFS. A connected graph is a graph that is connected in the sense of a topological space, i.e., there is always a path from any node to any other node in the graph. A graph that is not connected is said to be
3 min read
Java Program to Detect Cycle in a Directed Graph
Given a directed graph, check whether the graph contains a cycle. Your function should return true if the given graph contains at least one cycle; otherwise, it should return false.Example: Input:Â N = 4, E = 6 Output:Â Yes Explanation:Â The diagram clearly shows a cycle 0 -> 2 -> 0Input:Â N = 4,
7 min read
Program to Find the Arboricity of a Graph
Graph arboricity measures edge density. It is the fewest trees that can cover all graph edges. It covers all graph edges with the fewest edge-disjoint trees. Trees are acyclic-connected graphs in a forest. In arboricity, a forest covers an edge if a tree includes it. Arboricity is helpful in graph t
4 min read
Java Program to Partition a Tree from a Given Element using DFS
Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, a node may be visited twice. To avoid processing a node more than once, use a boolean visited array. DFS is a traversal method used to find th
4 min read
Java Program to Find Size of the Largest Independent Set(LIS) in a Given an N-array Tree
Basically, an N-array tree is such a tree structure in which each node can have a max of up to 'N' number of children. The largest independent set is a set of vertices in which no two vertexes are adjacent to each other. So basically in this program, we have to see that how can we find the size of s
4 min read
Java Program to Find a Clique by Using the Technique of the Most Dense Subgraph
A clique is a subset of vertices of a graph such that every two distinct vertices in the clique are adjacent. Finding a clique from a graph is an important problem in graph theory and many algorithms have been proposed to solve this problem. The most popular algorithm for finding a clique is the tec
3 min read
How to Generate a Random Undirected Graph for a Given Number of Edges in Java?
An undirected graph is graph, i.e, a set of objects (called vertices or nodes) that are connected together, where all the edges are bidirectional. An undirected graph is sometimes called an undirected network. In contrast, a graph where the edges point in a direction is called a directed graph. Undi
3 min read