Home> Java> javaTutorial> body text

Java program to find the maximum independent set in a graph using complementary graphs

WBOY
Release: 2023-09-20 16:41:10
forward
520 people have browsed it

Java program to find the maximum independent set in a graph using complementary graphs

This is a Java program executed in C that uses the graph complement method to find the largest autonomous set in the graph. First, the program constructs the complement of the given input graph. It then emphasizes each vertex in the complementary graph and recursively finds the maximum free set (MIS) by counting or excluding the current vertex. The program keeps track of the estimate of the largest free set found so far and returns it as the final result. By leveraging complementary graphs, we are able to transform the problem of finding the largest autonomous set into finding the largest clique in the original graph, thus achieving an efficient solution.

usage instructions

  • Brute force cracking method

Violent method

A brute-force method for finding the largest autonomous set in a graph involves generating all possible subsets of vertices in the graph and checking whether each subset forms a free set. In a Java program implemented in C, the calculation is repeated for all possible subsets and confirms that each vertex in the subset has no adjacent vertices in the same subset. By comprehensively investigating all subsets, the program distinguishes the largest free set with the highest number of vertices that satisfy this condition. Nonetheless, due to its exponential time complexity, this approach is not suitable for extended graphs, but appears to be basically reasonable for smaller graphs.

algorithm

  • Initialize the variable maxSetSize to 0, which is used to store the evaluation value of the largest autonomous set found.

  • Generate all possible subsets of vertices in the graph. This can be done by employing a bit-masking process or by recursively emphasizing all possible vertex combinations.

  • For each subset:

  • Check whether the subset forms an autonomous set. Iterate over each vertex in the subset.

  • For each vertex v in the subset, check whether there is an adjacent vertex u also in the subset. If such an adjacent vertex is found, the loop is broken since the subsets are not independent.

  • If no neighbor vertices of any vertex are found in the subset, check maxSetSize if the current subset metric is greater than maxSetSize.

  • The value of maxSetSize will represent the estimate of the largest independent set found.

  • Optionally, if you need to get the true set of vertices in the maximum free set, keep track of the vertices compared to the subset with the maximum extreme size.

  • Return maxSetSize as the measure of the largest autonomous set. If following an actual vertex set, both the degree and the corresponding vertex set are returned.

Example

#include  #include  using namespace std; bool hasAdjacentVertices(int v, vector& subset, vector>& graph) { // Check if vertex v has any adjacent vertices within the subset for (int u : subset) { if (graph[v][u] == 1) return true; } return false; } int findLargestIndependentSet(vector>& graph) { int numVertices = graph.size(); int maxSetSize = 0; // Generate all possible subsets of vertices for (int i = 0; i < (1 << numVertices); i++) { vector subset; // Construct the current subset based on the bitmask for (int j = 0; j < numVertices; j++) { if (i & (1 << j)) subset.push_back(j); } bool isIndependent = true; // Check if the subset forms an independent set for (int v : subset) { if (hasAdjacentVertices(v, subset, graph)) { // If an adjacent vertex exists within the subset, it is not an independent set isIndependent = false; break; } } if (isIndependent && subset.size() > maxSetSize) maxSetSize = subset.size(); } return maxSetSize; } int main() { // Example adjacency matrix for a graph with 4 vertices vector> graph = { {0, 1, 1, 1}, {1, 0, 1, 0}, {1, 1, 0, 1}, {1, 0, 1, 0} }; int largestIndependentSetSize = findLargestIndependentSet(graph); cout << "The size of the largest independent set is: " << largestIndependentSetSize << endl; return 0; }
Copy after login

Output

The size of the largest independent set is: 2
Copy after login

in conclusion

This article introduces a Java program implemented in C language, which is used to find the largest free set in the chart. The method used is: brute force method. The brute force method consists of generating all possible subsets of vertices and checking whether each subset forms a free set. The algorithm and its implementation are explained, along with example code and output results. These methods provide different strategies for solving the problem of finding the largest free set in a graph.

The above is the detailed content of Java program to find the maximum independent set in a graph using complementary graphs. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!